DownService.java 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. package com.fdkankan.fusion.service.impl;
  2. import cn.dev33.satoken.stp.StpUtil;
  3. import com.alibaba.fastjson.JSONObject;
  4. import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
  5. import com.fdkankan.fusion.common.ResultCode;
  6. import com.fdkankan.fusion.common.enums.DownloadStatusEnum;
  7. import com.fdkankan.fusion.entity.*;
  8. import com.fdkankan.fusion.exception.BusinessException;
  9. import com.fdkankan.fusion.httpClient.LaserService;
  10. import com.fdkankan.fusion.httpClient.response.SSDownSceneVo;
  11. import com.fdkankan.fusion.response.DownVo;
  12. import com.fdkankan.fusion.response.DownloadProcessVo;
  13. import com.fdkankan.fusion.service.*;
  14. import com.fdkankan.redis.constant.RedisKey;
  15. import com.fdkankan.redis.util.RedisUtil;
  16. import lombok.extern.slf4j.Slf4j;
  17. import org.apache.commons.lang3.StringUtils;
  18. import org.springframework.beans.factory.annotation.Autowired;
  19. import org.springframework.stereotype.Service;
  20. import java.util.HashMap;
  21. import java.util.Map;
  22. @Service
  23. @Slf4j
  24. public class DownService implements IDownService {
  25. @Autowired
  26. ISceneDownLogService sceneDownloadLogService;
  27. @Autowired
  28. IScenePlusService scenePlusService;
  29. @Autowired
  30. ISceneEditInfoService sceneEditInfoService;
  31. @Autowired
  32. RedisUtil redisUtil;
  33. @Autowired
  34. LaserService laserService;
  35. @Autowired
  36. ITmDepartmentService tmDepartmentService;
  37. @Autowired
  38. ICameraService cameraService;
  39. private Integer getSceneVersion( ScenePlus scenePlus) {
  40. Integer version = 0;
  41. if(scenePlus !=null){
  42. SceneEditInfo sceneEditInfo = sceneEditInfoService.getByScenePlusId(scenePlus.getId());
  43. if(sceneEditInfo == null){
  44. throw new BusinessException(ResultCode.SCENE_NOT_EXIST);
  45. }
  46. version = sceneEditInfo.getVersion();
  47. }
  48. return version;
  49. }
  50. @Override
  51. public DownVo checkDownLoad(String sceneNum, Integer isObj) {
  52. if(StringUtils.isEmpty(sceneNum)){
  53. throw new BusinessException(ResultCode.MISSING_REQUIRED_PARAMETERS);
  54. }
  55. ScenePlus plus = scenePlusService.getByNum(sceneNum);
  56. if(plus == null){
  57. throw new BusinessException(ResultCode.SCENE_NOT_EXIST);
  58. }
  59. Integer sceneType = plus.getSceneSource();
  60. log.info("checkDownLoad--sceneType:{},isObj:{}",sceneType,isObj);
  61. if((sceneType == 4 || sceneType == 5) && isObj !=1){ //深时场景
  62. return SSCheckDownload(sceneNum);
  63. }
  64. SceneDownLog sceneDownloadLog;
  65. Integer sceneVersion = getSceneVersion(plus);
  66. sceneDownloadLog = sceneDownloadLogService.getByStatusAndNum(sceneNum,0,sceneVersion);
  67. DownVo downVo = new DownVo();
  68. if(sceneDownloadLog != null){
  69. downVo.setDownloadStatus(1);
  70. return downVo;
  71. }
  72. sceneDownloadLog = sceneDownloadLogService.getByStatusAndNum(sceneNum,1,sceneVersion);
  73. //下载过,有更改
  74. if(sceneDownloadLog == null){
  75. String redisKey = RedisKey.PREFIX_DOWNLOAD_PROGRESS;
  76. downVo.setDownloadStatus(2);
  77. redisUtil.del(String.format(redisKey,sceneNum)); // 清除旧的下载信息
  78. return downVo;
  79. }
  80. //3下载过,并且没有修改过
  81. downVo.setDownloadStatus(3);
  82. downVo.setDownloadUrl(sceneDownloadLog.getDownUrl());
  83. return downVo;
  84. }
  85. @Override
  86. public DownVo down(String sceneNum,Integer isObj) {
  87. if(StringUtils.isEmpty(sceneNum) ){
  88. throw new BusinessException(ResultCode.MISSING_REQUIRED_PARAMETERS);
  89. }
  90. ScenePlus scenePlus = scenePlusService.getByNum(sceneNum);
  91. if( scenePlus == null){
  92. throw new BusinessException(ResultCode.SCENE_NOT_EXIST);
  93. }
  94. Long userId = scenePlus.getUserId();
  95. Integer sceneType = scenePlus.getSceneSource();
  96. Integer sceneVersion = getSceneVersion( scenePlus);
  97. log.info("down--sceneType:{},isObj:{}",sceneType,isObj);
  98. saveLog(scenePlus,sceneVersion);
  99. if((sceneType == 4 || sceneType == 5) && isObj !=1){ //深时场景
  100. return SSDownload(sceneNum,userId);
  101. }
  102. DownVo downVo = new DownVo();
  103. String redisKey = RedisKey.SCENE_DOWNLOADS_TASK_V4;
  104. Map<String,String> params = new HashMap<>(2);
  105. params.put("type","local");
  106. params.put("num",sceneNum);
  107. redisUtil.lRightPush(redisKey, JSONObject.toJSONString(params));
  108. downVo.setDownloadStatus(1);
  109. return downVo;
  110. }
  111. private void saveLog(ScenePlus scenePlus,Integer sceneVersion){
  112. String userName = (String) StpUtil.getExtra("userName");
  113. String nickName = (String) StpUtil.getExtra("nickName");
  114. String deptId = tmDepartmentService.getDeptId();
  115. TmDepartment tmDepartment = tmDepartmentService.getById(deptId);
  116. Camera camera = cameraService.getByCameraId(scenePlus.getCameraId());
  117. SceneDownLog sceneDownloadLogEntity = new SceneDownLog();
  118. sceneDownloadLogEntity.setSceneNum(scenePlus.getNum());
  119. sceneDownloadLogEntity.setSceneTitle(scenePlus.getTitle());
  120. sceneDownloadLogEntity.setUserName(userName);
  121. sceneDownloadLogEntity.setNickName(nickName);
  122. sceneDownloadLogEntity.setSnCode(camera.getSnCode());
  123. sceneDownloadLogEntity.setDeptName(tmDepartment.getName());
  124. sceneDownloadLogEntity.setDeptLevel(tmDepartment.getDeptType());
  125. sceneDownloadLogEntity.setDeptId(deptId);
  126. sceneDownloadLogEntity.setStatus(0);
  127. sceneDownloadLogEntity.setVersion(sceneVersion);
  128. sceneDownloadLogService.save(sceneDownloadLogEntity);
  129. }
  130. @Override
  131. public DownloadProcessVo downloadProcess(String sceneNum, Integer isObj) {
  132. if (StringUtils.isEmpty(sceneNum)) {
  133. throw new BusinessException(ResultCode.MISSING_REQUIRED_PARAMETERS);
  134. }
  135. ScenePlus scenePlus = scenePlusService.getByNum(sceneNum);
  136. if( scenePlus == null){
  137. throw new BusinessException(ResultCode.SCENE_NOT_EXIST);
  138. }
  139. Integer sceneType = scenePlus.getSceneSource();
  140. log.info("downloadProcess--sceneType:{},isObj:{}",sceneType,isObj);
  141. if((sceneType == 4 || sceneType == 5) && isObj !=1){ //深时场景
  142. return SSDownloadProcess(sceneNum);
  143. }
  144. String redisKey = RedisKey.PREFIX_DOWNLOAD_PROGRESS_V4;
  145. // 获取下载进度
  146. String result = redisUtil.get(String.format(redisKey,sceneNum));
  147. if(StringUtils.isEmpty(result)){
  148. return new DownloadProcessVo();
  149. }
  150. SceneDownLog sceneDownloadLog = sceneDownloadLogService.getByStatusAndNum(sceneNum,0,getSceneVersion(scenePlus));
  151. DownloadProcessVo downloadProcessVo = JSONObject.parseObject(result, DownloadProcessVo.class);
  152. if(sceneDownloadLog != null){
  153. switch (downloadProcessVo.getStatus()) {
  154. case DownloadStatusEnum.DOWNLOAD_SUCCESS_CODE:
  155. String url = downloadProcessVo.getUrl();
  156. if (!StringUtils.isEmpty(url)) {
  157. sceneDownloadLog.setDownUrl(url);
  158. sceneDownloadLog.setStatus(1);
  159. break;
  160. }
  161. case DownloadStatusEnum.DOWNLOAD_FAILED_CODE:
  162. sceneDownloadLog.setStatus(-1);
  163. break;
  164. }
  165. sceneDownloadLogService.updateById(sceneDownloadLog);
  166. }
  167. return downloadProcessVo;
  168. }
  169. /**
  170. * status :离线包状态是否需要重新生成 0 未生成:1 不需要 2需要 3 生成中
  171. */
  172. private DownVo SSCheckDownload(String sceneNum) {
  173. DownVo downVo = new DownVo();
  174. SSDownSceneVo vo = laserService.downOfflineSceneStatus(sceneNum);
  175. if(vo == null){
  176. throw new BusinessException(ResultCode.SS_SCENE_DOWN_ERROR);
  177. }
  178. downVo.setDownloadStatus(0);
  179. if(vo.getStatus() == 1){
  180. downVo.setDownloadStatus(3);
  181. downVo.setDownloadUrl(vo.getUrl());
  182. }
  183. if(vo.getStatus() == 2){
  184. downVo.setDownloadStatus(2);
  185. }
  186. if(vo.getStatus() == 3){
  187. downVo.setDownloadStatus(1);
  188. }
  189. return downVo;
  190. }
  191. /**
  192. * downloadStatus -1下载失败 1下载成功
  193. */
  194. private DownVo SSDownload(String sceneNum,Long userId) {
  195. DownVo downVo = new DownVo();
  196. //status :0:正在生成 1,初次生成 2,已经生成直接下载 3,重新生成
  197. SSDownSceneVo vo = laserService.downOfflineScene(sceneNum);
  198. if(vo == null){
  199. throw new BusinessException(ResultCode.SS_SCENE_DOWN_ERROR);
  200. }
  201. downVo.setDownloadStatus(1);
  202. return downVo;
  203. }
  204. public static HashMap<String,Integer> ssNumProcessNumMap = new HashMap<>();
  205. private DownloadProcessVo SSDownloadProcess(String sceneNum) {
  206. DownloadProcessVo downVo = new DownloadProcessVo();
  207. SSDownSceneVo vo = laserService.downOfflineSceneStatus(sceneNum);
  208. if(vo == null){
  209. throw new BusinessException(ResultCode.SS_SCENE_DOWN_ERROR);
  210. }
  211. downVo.setStatus(1003);
  212. if(vo.getStatus() == 0 || vo.getStatus() == 2 || vo.getStatus() == 3){ //下载中
  213. ssNumProcessNumMap.merge(sceneNum, 1, Integer::sum);
  214. Integer percent = ssNumProcessNumMap.get(sceneNum);
  215. percent = percent /2;
  216. if(percent >50){
  217. percent = 50;
  218. }
  219. downVo.setStatus(1001);
  220. downVo.setPercent(percent);
  221. }
  222. if(vo.getStatus() == 1){ //下载完成
  223. ssNumProcessNumMap.remove(sceneNum);
  224. downVo.setPercent(100);
  225. downVo.setUrl(vo.getUrl());
  226. downVo.setStatus(1002);
  227. LambdaUpdateWrapper<SceneDownLog> wrapper = new LambdaUpdateWrapper<>();
  228. wrapper.eq(SceneDownLog::getSceneNum,sceneNum);
  229. wrapper.eq(SceneDownLog::getStatus,0);
  230. wrapper.set(SceneDownLog::getDownUrl,vo.getUrl());
  231. wrapper.set(SceneDownLog::getStatus,1);
  232. sceneDownloadLogService.update(wrapper);
  233. }
  234. return downVo;
  235. }
  236. }