|
@@ -10,10 +10,7 @@ import com.sunwin.robot2ips.beans.vo.RecordTaskPoint;
|
|
|
import com.sunwin.robot2ips.common.RedisKey;
|
|
|
import com.sunwin.robot2ips.dao.UploadMapper;
|
|
|
import com.sunwin.robot2ips.service.CheckDataService;
|
|
|
-import com.sunwin.robot2ips.util.DateUtil;
|
|
|
-import com.sunwin.robot2ips.util.ExcelUtil;
|
|
|
-import com.sunwin.robot2ips.util.HttpUtils;
|
|
|
-import com.sunwin.robot2ips.util.RedisUtils;
|
|
|
+import com.sunwin.robot2ips.util.*;
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
@@ -63,69 +60,102 @@ public class CheckDataServiceImpl implements CheckDataService {
|
|
|
return;
|
|
|
}
|
|
|
Map<Integer, List<RobotDTO>> robotMap = robots.stream().collect(Collectors.groupingBy(RobotDTO::getId));
|
|
|
- //从redis中获取最新一次上传id (若为首次上传 查询当前最近的30条记录上传)
|
|
|
+ //从redis中获取最新一次上传id (若为首次上传 查询当前最近记录上传)
|
|
|
Long lastDataId = (Long) redisUtils.get(RedisKey.UPLOAD_IPS_LAST_ID);
|
|
|
- logger.info(" last data id ={} ", lastDataId);
|
|
|
-
|
|
|
- //根据lastDataId查询基于上次上传后的30条巡检数据
|
|
|
- List<RecordTaskPoint> checkDataList = uploadMapper.queryDataAfterId(lastDataId,count);
|
|
|
+ //根据lastDataId查询基于上次上传后巡检数据
|
|
|
+ List<RecordTaskPoint> checkDataList = uploadMapper.queryDataAfterId(lastDataId, count);
|
|
|
if (CollectionUtils.isEmpty(checkDataList)) {
|
|
|
- logger.info("no new data ");
|
|
|
return;
|
|
|
}
|
|
|
- //获取当前时间并更新redis最近上传的最新id(若上传失败则回滚!)
|
|
|
- redisUtils.set(RedisKey.UPLOAD_IPS_LAST_ID, Long.valueOf(checkDataList.get(0).getResultId()));
|
|
|
- //将巡检数据按照robotId分组
|
|
|
- Map<Integer, List<RecordTaskPoint>> robotCheckData = checkDataList.stream().collect(Collectors.groupingBy(RecordTaskPoint::getRobotId));
|
|
|
+ Long maxId =null;
|
|
|
+ //lastDataId为空即为首次上传,倒叙查询最新的巡检数据取最大的一条数据id
|
|
|
+ if (lastDataId == null) {
|
|
|
+ maxId = Long.parseLong(checkDataList.get(0).getResultId());
|
|
|
+ } else {
|
|
|
+ //不为空则顺序查询,最后一条为最大的数据id
|
|
|
+ maxId = Long.valueOf(checkDataList.get(checkDataList.size() - 1).getResultId());
|
|
|
+ }
|
|
|
+ //组装数据同步请求对象
|
|
|
+ List<RecordData> uploadResult = getRecordData(robotMap, checkDataList);
|
|
|
+ //若任务执行后 改变机器人信息 会导致无同步数据
|
|
|
+ if (CollectionUtils.isEmpty(uploadResult)) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ //上传成功计数器
|
|
|
+ int successCount = 0;
|
|
|
+ //根据机器人拆分组装后遍历上传
|
|
|
+ for (RecordData recordData : uploadResult) {
|
|
|
+ //HttpPost
|
|
|
+ String resp = HttpUtils.HttpPostWithJson(url, JSONObject.toJSONString(recordData));
|
|
|
+ //上传成功
|
|
|
+ if (!StringUtils.isEmpty(resp)) {
|
|
|
+ IPSResp ipsResp = JSONObject.parseObject(resp, IPSResp.class);
|
|
|
+ if (ipsResp.getIsSuccessed().equals("1")) {
|
|
|
+ successCount++;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ //未全部成功则判定全部失败 下次重新上传
|
|
|
+ if (uploadResult.size() != successCount) {
|
|
|
+ redisUtils.set(RedisKey.UPLOAD_IPS_LAST_ID, lastDataId);
|
|
|
+ } else {
|
|
|
+ //全部成功则更新redis上传的最新id
|
|
|
+ redisUtils.set(RedisKey.UPLOAD_IPS_LAST_ID, maxId);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 封装请求对象
|
|
|
+ *
|
|
|
+ * @param robotMap
|
|
|
+ * @param checkDataList
|
|
|
+ * @return java.util.List<com.sunwin.robot2ips.beans.vo.RecordData>
|
|
|
+ * @author machao
|
|
|
+ * @date 2022/6/16 15:25
|
|
|
+ **/
|
|
|
+ private List<RecordData> getRecordData(Map<Integer, List<RobotDTO>> robotMap, List<RecordTaskPoint> checkDataList) {
|
|
|
+ //将巡检数据按照taskId分组
|
|
|
+ Map<String, List<RecordTaskPoint>> robotCheckData = checkDataList.stream().collect(Collectors.groupingBy(RecordTaskPoint::getTaskId));
|
|
|
//初始化上传集合
|
|
|
List<RecordData> uploadResult = new ArrayList<>();
|
|
|
- //根据机器人拆分巡检数据
|
|
|
- for (Integer key : robotCheckData.keySet()) {
|
|
|
- List<RobotDTO> robot = robotMap.get(key);
|
|
|
+ //根据任务拆分巡检数据
|
|
|
+ for (String taskId : robotCheckData.keySet()) {
|
|
|
+ //获取taskId对应的巡检数据进行组装
|
|
|
+ List<RecordTaskPoint> checkDataArray = robotCheckData.get(taskId);
|
|
|
+ //获取任务执行机器人id
|
|
|
+ Integer robotId = checkDataArray.get(0).getRobotId();
|
|
|
+ List<RobotDTO> robot = robotMap.get(robotId);
|
|
|
if (CollectionUtils.isEmpty(robot)) {
|
|
|
continue;
|
|
|
}
|
|
|
RobotDTO robotDTO = robot.get(0);
|
|
|
+ //初始化数据同步请求对象
|
|
|
RecordData recordData = new RecordData();
|
|
|
recordData.setRobotCode(robotDTO.getId().toString());
|
|
|
recordData.setRobotName(robotDTO.getRobotName());
|
|
|
- //获取机器人对应的巡检数据进行组装
|
|
|
- List<RecordTaskPoint> checkDataArray = robotCheckData.get(key);
|
|
|
- //初始化集合
|
|
|
+ recordData.setTaskId(taskId);
|
|
|
+ //初始化实例集合
|
|
|
List<RecordTaskPoint> recordTaskPoints = new ArrayList<>();
|
|
|
//遍历设置时间
|
|
|
for (RecordTaskPoint recordTaskPoint : checkDataArray) {
|
|
|
+ //巡检照片转base64
|
|
|
+ String base64 = FtpUtils.addrToBase64(recordTaskPoint.getImage());
|
|
|
+ recordTaskPoint.setImage(base64);
|
|
|
//取巡检数据的巡检时间的年月日作为任务日期
|
|
|
String yyyyMMdd = DateUtil.parseToString(recordTaskPoint.getUpdateTime(), DateUtil.DateFormat_yyyy_MM_dd);
|
|
|
Date start = DateUtil.strToDate(yyyyMMdd + " 00:00:00");
|
|
|
Date end = DateUtil.strToDate(yyyyMMdd + " 23:59:59");
|
|
|
//任务时间写死识别日期的00:00-23:59
|
|
|
- recordTaskPoint.setRecordPlanStartDate(start);
|
|
|
- recordTaskPoint.setRecordCompleteDate(end);
|
|
|
+ //recordTaskPoint.setRecordPlanStartDate(start);
|
|
|
+ //recordTaskPoint.setRecordCompleteDate(end);
|
|
|
recordTaskPoint.setRecordPlanCompleteDate(end);
|
|
|
- recordTaskPoint.setRecordStartDate(start);
|
|
|
+ //recordTaskPoint.setRecordStartDate(start);
|
|
|
recordTaskPoints.add(recordTaskPoint);
|
|
|
}
|
|
|
recordData.setRecordTaskPoints(recordTaskPoints);
|
|
|
uploadResult.add(recordData);
|
|
|
}
|
|
|
- //初始化失败的最小id 集合
|
|
|
- ArrayList<Long> minIds = new ArrayList<>();
|
|
|
- //根据机器人拆分组装后遍历上传
|
|
|
- for (RecordData recordData : uploadResult) {
|
|
|
- //HttpPost
|
|
|
- String resp = HttpUtils.HttpPostWithJson(url, JSONObject.toJSONString(recordData));
|
|
|
- //上传成功
|
|
|
- if (!StringUtils.isEmpty(resp)) {
|
|
|
- IPSResp ipsResp = JSONObject.parseObject(resp, IPSResp.class);
|
|
|
- if (ipsResp.getIsSuccessed().equals("1")) {
|
|
|
- continue;
|
|
|
- }
|
|
|
- }
|
|
|
- //失败即全部重新上传
|
|
|
- redisUtils.set(RedisKey.UPLOAD_IPS_LAST_ID, lastDataId);
|
|
|
- }
|
|
|
-
|
|
|
+ return uploadResult;
|
|
|
}
|
|
|
|
|
|
|