异步新增和更新:
@Override
public int saveBroadcastStatusInfo(String bureauCode, String stationCode, List<BroadcastInfoDTO> broadcastInfoDTOList) {
// 转为 deviceId - this map集合
Map<String, BroadcastStatus> deviceIdThisMap = broadcastInfoDTOList.stream().map(o -> {
BroadcastStatus target = new BroadcastStatus();
// BeanUtils.copyProperties(o, target);
target.setDeviceId(o.getDeviceID());
target.setDeviceName(o.getDeviceName());
target.setDeviceClass(o.getDeviceClass());
target.setOnlineStatus(o.getStatus().getOnlineStatus());
target.setFaultState(o.getStatus().getFaultState());
target.setFaultDescribe(o.getStatus().getFaultDescribe());
target.setOcupyState(o.getStatus().getOcupyState());
target.setOcupyDescribe(o.getStatus().getOcupyDescribe());
target.setTemp(o.getStatus().getTemp());
target.setBureauCode(bureauCode);
target.setStationCode(stationCode);
target.setCreateTime(new Date());
target.setModifyTime(new Date());
return target;
}).collect(Collectors.toMap(BroadcastStatus::getDeviceId, BroadcastStatus -> BroadcastStatus));
// 获取当前车站所有数据集合
LambdaQueryWrapper<BroadcastStatus> queryWrapper = new LambdaQueryWrapper<BroadcastStatus>().eq(BroadcastStatus::getBureauCode, bureauCode).eq(BroadcastStatus::getStationCode, stationCode);
List<BroadcastStatus> dbAllList = broadcastStatusMapper.selectList(queryWrapper);
// 数据库当前设备id集合
Set<String> dbDeviceIds = dbAllList.stream().map(BroadcastStatus::getDeviceId).collect(Collectors.toSet());
// 异步更新
CompletableFuture<Integer> updateFuture = CompletableFuture.supplyAsync(() -> {
List<BroadcastStatus> upList = deviceIdThisMap.keySet().stream().filter(dbDeviceIds::contains).map(deviceIdThisMap::get).collect(Collectors.toList());
int size = 0;
if (!CollectionUtils.isEmpty(upList)) {
size = broadcastStatusMapper.batchUpdate(bureauCode, stationCode, upList);
}
return size;
}, executor);
// 异步插入数据
CompletableFuture<Integer> insertFuture = CompletableFuture.supplyAsync(() -> {
List<BroadcastStatus> insertList = deviceIdThisMap.keySet().stream().filter(deviceId -> !dbDeviceIds.contains(deviceId)).map(deviceIdThisMap::get).collect(Collectors.toList());
int size = 0;
if (!CollectionUtils.isEmpty(insertList)) {
size = broadcastStatusMapper.batchInsert(insertList);
}
return size;
}, executor);
// //TODO 广播状态插入历史记录表
// List<BroadcastStatus> collect = deviceIdThisMap.keySet().stream().map(deviceIdThisMap::get).collect(Collectors.toList());
// if(!CollectionUtils.isEmpty(collect)) {
// broadcastStatusHistoryMapper.batchInsertHistory(collect);
// }
try {
Integer updateSize = updateFuture.get();
Integer insertSize = insertFuture.get();
return insertSize + updateSize;
} catch (InterruptedException | ExecutionException e) {
log.error("设备状态信息-广播状态信息保存错误:", e);
}
return 0;
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 记一次.NET内存居高不下排查解决与启示