RepairVideosHandler.java 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. package com.fdkankan.job.job;
  2. import cn.hutool.core.collection.CollUtil;
  3. import cn.hutool.core.io.FileUtil;
  4. import cn.hutool.core.util.StrUtil;
  5. import cn.hutool.http.HttpUtil;
  6. import com.alibaba.fastjson.JSON;
  7. import com.alibaba.fastjson.JSONObject;
  8. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  9. import com.fdkankan.fyun.config.FYunFileConfig;
  10. import com.fdkankan.fyun.face.FYunFileServiceInterface;
  11. import com.fdkankan.job.entity.SceneCopyLog;
  12. import com.fdkankan.job.entity.SceneMoveLog;
  13. import com.fdkankan.job.entity.ScenePro;
  14. import com.fdkankan.job.service.*;
  15. import com.fdkankan.model.constants.ConstantFilePath;
  16. import com.fdkankan.redis.util.RedisUtil;
  17. import com.xxl.job.core.context.XxlJobHelper;
  18. import com.xxl.job.core.handler.annotation.XxlJob;
  19. import lombok.extern.slf4j.Slf4j;
  20. import org.springframework.beans.factory.annotation.Autowired;
  21. import org.springframework.beans.factory.annotation.Value;
  22. import org.springframework.stereotype.Component;
  23. import java.nio.charset.StandardCharsets;
  24. import java.util.*;
  25. /**
  26. * <p>
  27. * v4场景回退v3
  28. * </p>
  29. *
  30. * @author dengsixing
  31. * @since 2022/12/16
  32. **/
  33. @Slf4j
  34. @Component
  35. public class RepairVideosHandler {
  36. @Autowired
  37. private ISceneProService sceneProService;
  38. @Autowired
  39. ISceneEditInfoService sceneEditInfoService;
  40. @Autowired
  41. private FYunFileServiceInterface fYunFileService;
  42. @Autowired
  43. private FYunFileConfig fYunFileConfig;
  44. @Autowired
  45. private ISceneMoveLogService sceneMoveLogService;
  46. @Autowired
  47. private ISceneCopyLogService sceneCopyLogService;
  48. @XxlJob("repairVideosHandler")
  49. public void V4toV3Handler(){
  50. XxlJobHelper.log("repairVideosHandler start.....");
  51. Set<String> faildNumList = new HashSet<>();
  52. List<String> numList = null;
  53. String nums = XxlJobHelper.getJobParam();
  54. if(StrUtil.isNotEmpty(nums)){
  55. numList = Arrays.asList(nums.split(","));
  56. }
  57. LambdaQueryWrapper<ScenePro> queryWrapper = new LambdaQueryWrapper<>();
  58. queryWrapper.like(ScenePro::getVideos, "/home/").like(ScenePro::getVideos, ".xml");
  59. if(CollUtil.isNotEmpty(numList)){
  60. queryWrapper.in(ScenePro::getNum, numList);
  61. }
  62. List<ScenePro> list = sceneProService.list(queryWrapper);
  63. for (ScenePro pro : list) {
  64. try {
  65. String videos = pro.getVideos();
  66. JSONObject videosObj = JSONObject.parseObject(videos);
  67. String upPath = videosObj.getString("upPath");
  68. String upName = upPath.substring(upPath.lastIndexOf("/") + 1);
  69. XxlJobHelper.log("文件名:{}", upName);
  70. String targetUpPath = String.format(ConstantFilePath.DATA_PATH_FORMAT, pro.getNum()).concat(upName);
  71. String targetUpPath1 = String.format(ConstantFilePath.DATA_PATH_FORMAT, pro.getNum()).concat("Up.xml");
  72. String targetUpPath2 = String.format(ConstantFilePath.DATA_PATH_FORMAT, pro.getNum()).concat("Up2.xml");
  73. boolean found = false;
  74. //判断home是否被删除,如果home没被删除。直接从home中获取
  75. upPath = upPath.replace(fYunFileConfig.getHost(), "");
  76. String upPath1 = upPath.replace(upName, "").concat("Up.xml");
  77. String upPath2 = upPath.replace(upName, "").concat("Up2.xml");
  78. boolean exist = fYunFileService.fileExist(upPath);
  79. XxlJobHelper.log("原始资源是否存在:{}", exist);
  80. if (exist) {
  81. log.info("场景:{}在原始资源中找到", pro.getNum());
  82. found = true;
  83. log.info("upPath=" + upPath);
  84. log.info("targetUpPath=" + targetUpPath);
  85. // fYunFileService.copyFileBetweenBucket(fYunFileConfig.getBucket(), upPath, fYunFileConfig.getBucket(), targetUpPath);
  86. fYunFileService.uploadFile(fYunFileService.getFileContent(upPath1).getBytes(StandardCharsets.UTF_8), targetUpPath1);
  87. fYunFileService.uploadFile(fYunFileService.getFileContent(upPath2).getBytes(StandardCharsets.UTF_8), targetUpPath2);
  88. } else {
  89. //如果home已被删除,则找相同相机下的场景,有则取回
  90. if (Objects.isNull(pro.getCameraId())) {
  91. faildNumList.add(pro.getNum());
  92. log.info("场景:{}相机id为空", pro.getNum());
  93. }
  94. //先查询v3场景
  95. List<ScenePro> v3List = sceneProService.list(
  96. new LambdaQueryWrapper<ScenePro>()
  97. .eq(ScenePro::getCameraId, pro.getCameraId())
  98. .ne(ScenePro::getNum, pro.getNum())
  99. // .like(ScenePro::getVideos, upName)
  100. .isNotNull(ScenePro::getVideos)
  101. .orderByDesc(ScenePro::getCreateTime));
  102. if (CollUtil.isNotEmpty(v3List)) {
  103. for (ScenePro scenePro : v3List) {
  104. //查询是否做过场景迁移
  105. List<SceneMoveLog> moveList = sceneMoveLogService.list(new LambdaQueryWrapper<SceneMoveLog>().eq(SceneMoveLog::getNum, scenePro.getNum()));
  106. if(CollUtil.isNotEmpty(moveList)){
  107. continue;
  108. }
  109. List<SceneCopyLog> copyList = sceneCopyLogService.list(new LambdaQueryWrapper<SceneCopyLog>().eq(SceneCopyLog::getNewNum, scenePro.getNum()));
  110. if(CollUtil.isNotEmpty(copyList)){
  111. continue;
  112. }
  113. upPath = JSONObject.parseObject(scenePro.getVideos()).getString("upPath").replace(fYunFileConfig.getHost(), "");
  114. if (!fYunFileService.fileExist(upPath)) {
  115. continue;
  116. }
  117. upName = upPath.substring(upPath.lastIndexOf("/") + 1);
  118. upPath1 = upPath.replace(upName, "").concat("Up.xml");
  119. upPath2 = upPath.replace(upName, "").concat("Up2.xml");
  120. found = true;
  121. log.info("upPath=" + upPath);
  122. log.info("targetUpPath=" + targetUpPath);
  123. fYunFileService.uploadFile(fYunFileService.getFileContent(upPath1).getBytes(StandardCharsets.UTF_8), targetUpPath1);
  124. fYunFileService.uploadFile(fYunFileService.getFileContent(upPath2).getBytes(StandardCharsets.UTF_8), targetUpPath2);
  125. log.info("场景:{}在场景{}中找到,路径为:{}", pro.getNum(), scenePro.getNum(), upPath);
  126. break;
  127. }
  128. }
  129. }
  130. //更新数据库以及scene.json
  131. if (found) {
  132. videosObj.put("upPath", fYunFileConfig.getHost().concat(targetUpPath));
  133. pro.setVideos(videosObj.toJSONString());
  134. sceneProService.updateById(pro);
  135. String sceneJsonPath = ConstantFilePath.SCENE_PATH + String.format(ConstantFilePath.DATA_PATH_FORMAT, pro.getNum()) + "scene.json";
  136. if (FileUtil.exist(sceneJsonPath)) {
  137. String sceneJsonStr = FileUtil.readUtf8String(sceneJsonPath);
  138. if (StrUtil.isNotEmpty(sceneJsonStr)) {
  139. JSONObject sceneJsonObj = JSON.parseObject(sceneJsonStr);
  140. sceneJsonObj.put("videos", fYunFileConfig.getHost().concat(targetUpPath));
  141. FileUtil.writeUtf8String(sceneJsonObj.toJSONString(), sceneJsonPath);
  142. }
  143. }
  144. } else {
  145. //如果两个目录都没有,则记录为失败
  146. faildNumList.add(pro.getNum());
  147. }
  148. } catch (Exception e) {
  149. //如果两个目录都没有,则记录为失败
  150. log.error(pro.getNum() + "修复失败", e);
  151. faildNumList.add(pro.getNum());
  152. }
  153. }
  154. XxlJobHelper.log("待修复场景数:{}", list.size());
  155. XxlJobHelper.log("repairVideosHandler end.....");
  156. XxlJobHelper.log("失败场景码:" + JSON.toJSONString(faildNumList));
  157. }
  158. }