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

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

Loading…
Cancel
Save