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

liweidong
qinyulong 1 month 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.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;
e -> e.getString("code"), }
e -> e));
Set<String> equCodes = equCodeObjectMap.keySet(); // 接口返回飞靶code即为本系统code
if (equCodes == null || equCodes.size() < 1) { Map<String, JSONObject> equCodeObjectMap = jsonList.stream()
log.error("同步飞靶数据接口返回数据为空"); .collect(Collectors.toMap(
return; e -> e.getString("code"),
} Function.identity()
LambdaQueryWrapper<FeiBaSetEntity> lambdaQueryWrapper = new LambdaQueryWrapper<>(); ));
lambdaQueryWrapper.in(CollectionUtils.isNotEmpty(equCodes), FeiBaSetEntity::getFsCode, equCodes);
List<FeiBaSetEntity> feiBaSetEntityList = this.list(lambdaQueryWrapper); Set<String> equCodes = equCodeObjectMap.keySet();
Map<String, FeiBaSetEntity> FeiBaSetEntityMap = new HashMap<>(); if (CollectionUtils.isEmpty(equCodes)) {
if (null != feiBaSetEntityList && feiBaSetEntityList.size() > 0) { log.error("同步飞靶数据接口返回数据code字段为空");
feiBaSetEntityList.stream().collect(Collectors.toMap( return;
e -> e.getFsCode(), }
e -> e));
} // 查询数据库中已存在的记录
List<FeiBaSetEntity> saves = new ArrayList<>(); LambdaQueryWrapper<FeiBaSetEntity> lambdaQueryWrapper = new LambdaQueryWrapper<>();
for (JSONObject jsonObject : jsonList) { lambdaQueryWrapper.in(FeiBaSetEntity::getFsCode, equCodes);
FeiBaSetEntity feiBaSet = FeiBaSetEntityMap.get(jsonObject.getString("code")); List<FeiBaSetEntity> feiBaSetEntityList = this.list(lambdaQueryWrapper);
feiBaSet = Optional.ofNullable(feiBaSet).orElse(new FeiBaSetEntity());
feiBaSet.setFsCode(jsonObject.getString("code")); Map<String, FeiBaSetEntity> feiBaSetEntityMap = feiBaSetEntityList.stream()
feiBaSet.setFsType(jsonObject.getString("name")); .collect(Collectors.toMap(
if (null == feiBaSet.getId()) { FeiBaSetEntity::getFsCode,
feiBaSet.setStatus(FeiBaSetEntity.FS_STATUS_FREE); Function.identity()
} ));
saves.add(feiBaSet);
} // 批量更新/保存
boolean saved = this.saveOrUpdateBatch(saves); List<FeiBaSetEntity> saves = jsonList.stream().map(jsonObject -> {
if (saved) { String code = jsonObject.getString("code");
log.debug("同步飞靶数据成功"); FeiBaSetEntity entity = feiBaSetEntityMap.getOrDefault(code, new FeiBaSetEntity());
} else {
log.error("同步飞靶数据失败"); 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) { } catch (Exception e) {

Loading…
Cancel
Save