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. //downloads:progress:v4:SS-t-8H5pZsMnHGL
  131. @Override
  132. public DownloadProcessVo downloadProcess(String sceneNum, Integer isObj) {
  133. if (StringUtils.isEmpty(sceneNum)) {
  134. throw new BusinessException(ResultCode.MISSING_REQUIRED_PARAMETERS);
  135. }
  136. ScenePlus scenePlus = scenePlusService.getByNum(sceneNum);
  137. if( scenePlus == null){
  138. throw new BusinessException(ResultCode.SCENE_NOT_EXIST);
  139. }
  140. Integer sceneType = scenePlus.getSceneSource();
  141. log.info("downloadProcess--sceneType:{},isObj:{}",sceneType,isObj);
  142. if((sceneType == 4 || sceneType == 5) && isObj !=1){ //深时场景
  143. return SSDownloadProcess(sceneNum);
  144. }
  145. String redisKey = RedisKey.PREFIX_DOWNLOAD_PROGRESS_V4;
  146. // 获取下载进度
  147. String result = redisUtil.get(String.format(redisKey,sceneNum));
  148. log.info("redis-down-msg:{}",result);
  149. if(StringUtils.isEmpty(result)){
  150. return new DownloadProcessVo();
  151. }
  152. SceneDownLog sceneDownloadLog = sceneDownloadLogService.getByStatusAndNum(sceneNum,0,getSceneVersion(scenePlus));
  153. DownloadProcessVo downloadProcessVo = JSONObject.parseObject(result, DownloadProcessVo.class);
  154. if(sceneDownloadLog != null){
  155. switch (downloadProcessVo.getStatus()) {
  156. case DownloadStatusEnum.DOWNLOAD_SUCCESS_CODE:
  157. String url = downloadProcessVo.getUrl();
  158. if (!StringUtils.isEmpty(url)) {
  159. sceneDownloadLog.setDownUrl(url);
  160. sceneDownloadLog.setStatus(1);
  161. break;
  162. }
  163. case DownloadStatusEnum.DOWNLOAD_FAILED_CODE:
  164. sceneDownloadLog.setStatus(-1);
  165. break;
  166. }
  167. sceneDownloadLogService.updateById(sceneDownloadLog);
  168. }
  169. return downloadProcessVo;
  170. }
  171. /**
  172. * status :离线包状态是否需要重新生成 0 未生成:1 不需要 2需要 3 生成中
  173. */
  174. private DownVo SSCheckDownload(String sceneNum) {
  175. DownVo downVo = new DownVo();
  176. SSDownSceneVo vo = laserService.downOfflineSceneStatus(sceneNum);
  177. if(vo == null){
  178. throw new BusinessException(ResultCode.SS_SCENE_DOWN_ERROR);
  179. }
  180. downVo.setDownloadStatus(0);
  181. if(vo.getStatus() == 1){
  182. downVo.setDownloadStatus(3);
  183. downVo.setDownloadUrl(vo.getUrl());
  184. }
  185. if(vo.getStatus() == 2){
  186. downVo.setDownloadStatus(2);
  187. }
  188. if(vo.getStatus() == 3){
  189. downVo.setDownloadStatus(1);
  190. }
  191. return downVo;
  192. }
  193. /**
  194. * downloadStatus -1下载失败 1下载成功
  195. */
  196. private DownVo SSDownload(String sceneNum,Long userId) {
  197. DownVo downVo = new DownVo();
  198. //status :0:正在生成 1,初次生成 2,已经生成直接下载 3,重新生成
  199. SSDownSceneVo vo = laserService.downOfflineScene(sceneNum);
  200. if(vo == null){
  201. throw new BusinessException(ResultCode.SS_SCENE_DOWN_ERROR);
  202. }
  203. downVo.setDownloadStatus(1);
  204. return downVo;
  205. }
  206. public static HashMap<String,Integer> ssNumProcessNumMap = new HashMap<>();
  207. private DownloadProcessVo SSDownloadProcess(String sceneNum) {
  208. DownloadProcessVo downVo = new DownloadProcessVo();
  209. SSDownSceneVo vo = laserService.downOfflineSceneStatus(sceneNum);
  210. if(vo == null){
  211. throw new BusinessException(ResultCode.SS_SCENE_DOWN_ERROR);
  212. }
  213. downVo.setStatus(1003);
  214. if(vo.getStatus() == 0 || vo.getStatus() == 2 || vo.getStatus() == 3){ //下载中
  215. ssNumProcessNumMap.merge(sceneNum, 1, Integer::sum);
  216. Integer percent = ssNumProcessNumMap.get(sceneNum);
  217. percent = percent /2;
  218. if(percent >50){
  219. percent = 50;
  220. }
  221. downVo.setStatus(1001);
  222. downVo.setPercent(percent);
  223. }
  224. if(vo.getStatus() == 1){ //下载完成
  225. ssNumProcessNumMap.remove(sceneNum);
  226. downVo.setPercent(100);
  227. downVo.setUrl(vo.getUrl());
  228. downVo.setStatus(1002);
  229. LambdaUpdateWrapper<SceneDownLog> wrapper = new LambdaUpdateWrapper<>();
  230. wrapper.eq(SceneDownLog::getSceneNum,sceneNum);
  231. wrapper.eq(SceneDownLog::getStatus,0);
  232. wrapper.set(SceneDownLog::getDownUrl,vo.getUrl());
  233. wrapper.set(SceneDownLog::getStatus,1);
  234. sceneDownloadLogService.update(wrapper);
  235. }
  236. return downVo;
  237. }
  238. }