123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183 |
- package com.fdkankan.job.job;
- import cn.hutool.core.collection.CollUtil;
- import cn.hutool.core.io.FileUtil;
- import cn.hutool.core.util.StrUtil;
- import cn.hutool.http.HttpUtil;
- import com.alibaba.fastjson.JSON;
- import com.alibaba.fastjson.JSONObject;
- import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
- import com.fdkankan.fyun.config.FYunFileConfig;
- import com.fdkankan.fyun.face.FYunFileServiceInterface;
- import com.fdkankan.job.entity.SceneCopyLog;
- import com.fdkankan.job.entity.SceneMoveLog;
- import com.fdkankan.job.entity.ScenePro;
- import com.fdkankan.job.service.*;
- import com.fdkankan.model.constants.ConstantFilePath;
- import com.fdkankan.redis.util.RedisUtil;
- import com.xxl.job.core.context.XxlJobHelper;
- import com.xxl.job.core.handler.annotation.XxlJob;
- import lombok.extern.slf4j.Slf4j;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.beans.factory.annotation.Value;
- import org.springframework.stereotype.Component;
- import java.nio.charset.StandardCharsets;
- import java.util.*;
- /**
- * <p>
- * v4场景回退v3
- * </p>
- *
- * @author dengsixing
- * @since 2022/12/16
- **/
- @Slf4j
- @Component
- public class RepairVideosHandler {
- @Autowired
- private ISceneProService sceneProService;
- @Autowired
- ISceneEditInfoService sceneEditInfoService;
- @Autowired
- private FYunFileServiceInterface fYunFileService;
- @Autowired
- private FYunFileConfig fYunFileConfig;
- @Autowired
- private ISceneMoveLogService sceneMoveLogService;
- @Autowired
- private ISceneCopyLogService sceneCopyLogService;
- @XxlJob("repairVideosHandler")
- public void V4toV3Handler(){
- XxlJobHelper.log("repairVideosHandler start.....");
- Set<String> faildNumList = new HashSet<>();
- List<String> numList = null;
- String nums = XxlJobHelper.getJobParam();
- if(StrUtil.isNotEmpty(nums)){
- numList = Arrays.asList(nums.split(","));
- }
- LambdaQueryWrapper<ScenePro> queryWrapper = new LambdaQueryWrapper<>();
- queryWrapper.like(ScenePro::getVideos, "/home/").like(ScenePro::getVideos, ".xml");
- if(CollUtil.isNotEmpty(numList)){
- queryWrapper.in(ScenePro::getNum, numList);
- }
- List<ScenePro> list = sceneProService.list(queryWrapper);
- for (ScenePro pro : list) {
- try {
- String videos = pro.getVideos();
- JSONObject videosObj = JSONObject.parseObject(videos);
- String upPath = videosObj.getString("upPath");
- String upName = upPath.substring(upPath.lastIndexOf("/") + 1);
- XxlJobHelper.log("文件名:{}", upName);
- String targetUpPath = String.format(ConstantFilePath.DATA_PATH_FORMAT, pro.getNum()).concat(upName);
- String targetUpPath1 = String.format(ConstantFilePath.DATA_PATH_FORMAT, pro.getNum()).concat("Up.xml");
- String targetUpPath2 = String.format(ConstantFilePath.DATA_PATH_FORMAT, pro.getNum()).concat("Up2.xml");
- boolean found = false;
- //判断home是否被删除,如果home没被删除。直接从home中获取
- upPath = upPath.replace(fYunFileConfig.getHost(), "");
- String upPath1 = upPath.replace(upName, "").concat("Up.xml");
- String upPath2 = upPath.replace(upName, "").concat("Up2.xml");
- boolean exist = fYunFileService.fileExist(upPath);
- XxlJobHelper.log("原始资源是否存在:{}", exist);
- if (exist) {
- log.info("场景:{}在原始资源中找到", pro.getNum());
- found = true;
- log.info("upPath=" + upPath);
- log.info("targetUpPath=" + targetUpPath);
- // fYunFileService.copyFileBetweenBucket(fYunFileConfig.getBucket(), upPath, fYunFileConfig.getBucket(), targetUpPath);
- fYunFileService.uploadFile(fYunFileService.getFileContent(upPath1).getBytes(StandardCharsets.UTF_8), targetUpPath1);
- fYunFileService.uploadFile(fYunFileService.getFileContent(upPath2).getBytes(StandardCharsets.UTF_8), targetUpPath2);
- } else {
- //如果home已被删除,则找相同相机下的场景,有则取回
- if (Objects.isNull(pro.getCameraId())) {
- faildNumList.add(pro.getNum());
- log.info("场景:{}相机id为空", pro.getNum());
- }
- //先查询v3场景
- List<ScenePro> v3List = sceneProService.list(
- new LambdaQueryWrapper<ScenePro>()
- .eq(ScenePro::getCameraId, pro.getCameraId())
- .ne(ScenePro::getNum, pro.getNum())
- // .like(ScenePro::getVideos, upName)
- .isNotNull(ScenePro::getVideos)
- .orderByDesc(ScenePro::getCreateTime));
- if (CollUtil.isNotEmpty(v3List)) {
- for (ScenePro scenePro : v3List) {
- //查询是否做过场景迁移
- List<SceneMoveLog> moveList = sceneMoveLogService.list(new LambdaQueryWrapper<SceneMoveLog>().eq(SceneMoveLog::getNum, scenePro.getNum()));
- if(CollUtil.isNotEmpty(moveList)){
- continue;
- }
- List<SceneCopyLog> copyList = sceneCopyLogService.list(new LambdaQueryWrapper<SceneCopyLog>().eq(SceneCopyLog::getNewNum, scenePro.getNum()));
- if(CollUtil.isNotEmpty(copyList)){
- continue;
- }
- upPath = JSONObject.parseObject(scenePro.getVideos()).getString("upPath").replace(fYunFileConfig.getHost(), "");
- if (!fYunFileService.fileExist(upPath)) {
- continue;
- }
- upName = upPath.substring(upPath.lastIndexOf("/") + 1);
- upPath1 = upPath.replace(upName, "").concat("Up.xml");
- upPath2 = upPath.replace(upName, "").concat("Up2.xml");
- found = true;
- log.info("upPath=" + upPath);
- log.info("targetUpPath=" + targetUpPath);
- fYunFileService.uploadFile(fYunFileService.getFileContent(upPath1).getBytes(StandardCharsets.UTF_8), targetUpPath1);
- fYunFileService.uploadFile(fYunFileService.getFileContent(upPath2).getBytes(StandardCharsets.UTF_8), targetUpPath2);
- log.info("场景:{}在场景{}中找到,路径为:{}", pro.getNum(), scenePro.getNum(), upPath);
- break;
- }
- }
- }
- //更新数据库以及scene.json
- if (found) {
- videosObj.put("upPath", fYunFileConfig.getHost().concat(targetUpPath));
- pro.setVideos(videosObj.toJSONString());
- sceneProService.updateById(pro);
- String sceneJsonPath = ConstantFilePath.SCENE_PATH + String.format(ConstantFilePath.DATA_PATH_FORMAT, pro.getNum()) + "scene.json";
- if (FileUtil.exist(sceneJsonPath)) {
- String sceneJsonStr = FileUtil.readUtf8String(sceneJsonPath);
- if (StrUtil.isNotEmpty(sceneJsonStr)) {
- JSONObject sceneJsonObj = JSON.parseObject(sceneJsonStr);
- sceneJsonObj.put("videos", fYunFileConfig.getHost().concat(targetUpPath));
- FileUtil.writeUtf8String(sceneJsonObj.toJSONString(), sceneJsonPath);
- }
- }
- } else {
- //如果两个目录都没有,则记录为失败
- faildNumList.add(pro.getNum());
- }
- } catch (Exception e) {
- //如果两个目录都没有,则记录为失败
- log.error(pro.getNum() + "修复失败", e);
- faildNumList.add(pro.getNum());
- }
- }
- XxlJobHelper.log("待修复场景数:{}", list.size());
- XxlJobHelper.log("repairVideosHandler end.....");
- XxlJobHelper.log("失败场景码:" + JSON.toJSONString(faildNumList));
- }
- }
|