飞靶同步:未更新状态字段

liweidong
qinyulong 4 weeks ago
parent 0c45ddd995
commit 4b6eb764fe
  1. 85
      blade-service/blade-desk/src/main/java/org/springblade/desk/device/service/impl/FeiBaSetServiceImpl.java

@ -26,6 +26,7 @@ import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.time.Duration;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
/**
@ -105,42 +106,56 @@ public class FeiBaSetServiceImpl extends BaseServiceImpl<FeiBaSetMapper, FeiBaSe
log.debug("同步飞靶数据: {}", result.toJSONString());
if (result != null && result.getInteger("code").equals(0)) {
List<JSONObject> jsonList = JSONArray.parseArray(result.getJSONArray("data").toJSONString(), JSONObject.class);
if (jsonList != null && jsonList.size() > 0) {
//接口返回飞靶code即为本系统code
Map<String, JSONObject> equCodeObjectMap = jsonList.stream().collect(Collectors.toMap(
e -> e.getString("code"),
e -> e));
Set<String> equCodes = equCodeObjectMap.keySet();
if (equCodes == null || equCodes.size() < 1) {
log.error("同步飞靶数据接口返回数据为空");
return;
}
LambdaQueryWrapper<FeiBaSetEntity> lambdaQueryWrapper = new LambdaQueryWrapper<>();
lambdaQueryWrapper.in(CollectionUtils.isNotEmpty(equCodes), FeiBaSetEntity::getFsCode, equCodes);
List<FeiBaSetEntity> feiBaSetEntityList = this.list(lambdaQueryWrapper);
Map<String, FeiBaSetEntity> FeiBaSetEntityMap = new HashMap<>();
if (null != feiBaSetEntityList && feiBaSetEntityList.size() > 0) {
feiBaSetEntityList.stream().collect(Collectors.toMap(
e -> e.getFsCode(),
e -> e));
}
List<FeiBaSetEntity> saves = new ArrayList<>();
for (JSONObject jsonObject : jsonList) {
FeiBaSetEntity feiBaSet = FeiBaSetEntityMap.get(jsonObject.getString("code"));
feiBaSet = Optional.ofNullable(feiBaSet).orElse(new FeiBaSetEntity());
feiBaSet.setFsCode(jsonObject.getString("code"));
feiBaSet.setFsType(jsonObject.getString("name"));
if (null == feiBaSet.getId()) {
feiBaSet.setStatus(FeiBaSetEntity.FS_STATUS_FREE);
}
saves.add(feiBaSet);
}
boolean saved = this.saveOrUpdateBatch(saves);
if (saved) {
log.debug("同步飞靶数据成功");
} else {
log.error("同步飞靶数据失败");
if (CollectionUtils.isEmpty(jsonList)) {
log.error("同步飞靶数据接口返回数据为空");
return;
}
// 接口返回飞靶code即为本系统code
Map<String, JSONObject> equCodeObjectMap = jsonList.stream()
.collect(Collectors.toMap(
e -> e.getString("code"),
Function.identity()
));
Set<String> equCodes = equCodeObjectMap.keySet();
if (CollectionUtils.isEmpty(equCodes)) {
log.error("同步飞靶数据接口返回数据code字段为空");
return;
}
// 查询数据库中已存在的记录
LambdaQueryWrapper<FeiBaSetEntity> lambdaQueryWrapper = new LambdaQueryWrapper<>();
lambdaQueryWrapper.in(FeiBaSetEntity::getFsCode, equCodes);
List<FeiBaSetEntity> feiBaSetEntityList = this.list(lambdaQueryWrapper);
Map<String, FeiBaSetEntity> feiBaSetEntityMap = feiBaSetEntityList.stream()
.collect(Collectors.toMap(
FeiBaSetEntity::getFsCode,
Function.identity()
));
// 批量更新/保存
List<FeiBaSetEntity> saves = jsonList.stream().map(jsonObject -> {
String code = jsonObject.getString("code");
FeiBaSetEntity entity = feiBaSetEntityMap.getOrDefault(code, new FeiBaSetEntity());
entity.setFsCode(code);
entity.setFsType(jsonObject.getString("name"));
// 新增记录时设置默认状态
if (entity.getId() == null) {
entity.setStatus(FeiBaSetEntity.FS_STATUS_FREE);
}
return entity;
}).collect(Collectors.toList());
boolean success = this.saveOrUpdateBatch(saves);
if (!success) {
log.error("同步飞靶数据保存失败");
} else {
log.debug("同步飞靶数据保存成功");
}
}
} catch (Exception e) {

Loading…
Cancel
Save