DownService.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. package com.fdkankan.manage.service.impl;
  2. import com.alibaba.fastjson.JSONObject;
  3. import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
  4. import com.fdkankan.manage.common.ResultCode;
  5. import com.fdkankan.common.constant.SceneConstant;
  6. import com.fdkankan.manage.common.DownloadStatusEnum;
  7. import com.fdkankan.manage.exception.BusinessException;
  8. import com.fdkankan.manage.entity.*;
  9. import com.fdkankan.manage.httpClient.service.LaserService;
  10. import com.fdkankan.manage.service.*;
  11. import com.fdkankan.manage.vo.response.DownVo;
  12. import com.fdkankan.manage.vo.response.DownloadProcessVo;
  13. import com.fdkankan.manage.vo.response.SSDownSceneVo;
  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.ArrayList;
  21. import java.util.Arrays;
  22. import java.util.HashMap;
  23. import java.util.Map;
  24. @Service
  25. @Slf4j
  26. public class DownService implements IDownService {
  27. @Autowired
  28. ISceneProService sceneProService;
  29. @Autowired
  30. ISceneProEditService sceneProEditService;
  31. @Autowired
  32. ISceneDownloadLogService sceneDownloadLogService;
  33. @Autowired
  34. IScenePlusService scenePlusService;
  35. @Autowired
  36. ISceneEditInfoService sceneEditInfoService;
  37. @Autowired
  38. RedisUtil redisUtil;
  39. @Autowired
  40. LaserService laserService;
  41. @Override
  42. public DownVo checkDownLoad(String sceneNum,Integer isObj) {
  43. if(StringUtils.isEmpty(sceneNum)){
  44. throw new BusinessException(ResultCode.MISSING_REQUIRED_PARAMETERS.code(),ResultCode.MISSING_REQUIRED_PARAMETERS.message());
  45. }
  46. ScenePro scenePro = sceneProService.getByNum(sceneNum);
  47. ScenePlus plus = scenePlusService.getByNum(sceneNum);
  48. if(scenePro == null && plus == null){
  49. throw new BusinessException(SceneConstant.FAILURE_CODE_5005, SceneConstant.FAILURE_MSG_5005);
  50. }
  51. Integer sceneType = scenePro !=null ? scenePro.getSceneSource() : plus.getSceneSource();
  52. log.info("checkDownLoad--sceneType:{},isObj:{}",sceneType,isObj);
  53. if((sceneType == 4 || sceneType == 5) && isObj !=1){ //深时场景
  54. return SSCheckDownload(sceneNum);
  55. }
  56. SceneDownloadLog sceneDownloadLog;
  57. int isUp = 0;
  58. if(scenePro == null){
  59. isUp = 1;
  60. }
  61. Integer sceneVersion = getSceneVersion(scenePro, plus);
  62. sceneDownloadLog = sceneDownloadLogService.getByStatusAndNum(sceneNum,0,isUp);
  63. DownVo downVo = new DownVo();
  64. if(sceneDownloadLog != null){
  65. downVo.setDownloadStatus(1);
  66. return downVo;
  67. }
  68. sceneDownloadLog = sceneDownloadLogService.getByStatusAndNum(sceneNum,1,isUp);
  69. //3下载过,并且没有修改过
  70. if(sceneDownloadLog != null && sceneDownloadLog.getSceneVersion().intValue() == sceneVersion){
  71. downVo.setDownloadStatus(3);
  72. downVo.setDownloadUrl(sceneDownloadLog.getDownloadUrl());
  73. return downVo;
  74. }
  75. //下载过,有更改
  76. if(sceneDownloadLog != null){
  77. String redisKey = RedisKey.PREFIX_DOWNLOAD_PROGRESS;
  78. if(isUp == 1){
  79. redisKey = RedisKey.PREFIX_DOWNLOAD_PROGRESS_V4;
  80. }
  81. downVo.setDownloadStatus(2);
  82. redisUtil.del(String.format(redisKey,sceneNum)); // 清除旧的下载信息
  83. return downVo;
  84. }
  85. return downVo;
  86. }
  87. private Integer getSceneVersion(ScenePro scenePro, ScenePlus scenePlus) {
  88. Integer version = 0;
  89. if(scenePro != null){
  90. SceneProEdit proEdit = sceneProEditService.getByProId(scenePro.getId());
  91. if(proEdit == null){
  92. throw new BusinessException(SceneConstant.FAILURE_CODE_5005, SceneConstant.FAILURE_MSG_5005);
  93. }
  94. version = proEdit.getVersion();
  95. }
  96. if(scenePro == null && scenePlus !=null){
  97. SceneEditInfo sceneEditInfo = sceneEditInfoService.getByScenePlusId(scenePlus.getId());
  98. if(sceneEditInfo == null){
  99. throw new BusinessException(SceneConstant.FAILURE_CODE_5005, SceneConstant.FAILURE_MSG_5005);
  100. }
  101. version = sceneEditInfo.getVersion();
  102. }
  103. return version;
  104. }
  105. @Override
  106. public DownVo down(String sceneNum,Integer isObj) {
  107. if(StringUtils.isEmpty(sceneNum) ){
  108. throw new BusinessException(ResultCode.MISSING_REQUIRED_PARAMETERS.code(),ResultCode.MISSING_REQUIRED_PARAMETERS.message());
  109. }
  110. ScenePro scenePro = sceneProService.getByNum(sceneNum);
  111. ScenePlus scenePlus = scenePlusService.getByNum(sceneNum);
  112. if(scenePro == null && scenePlus == null){
  113. throw new BusinessException(SceneConstant.FAILURE_CODE_5005, SceneConstant.FAILURE_MSG_5005);
  114. }
  115. Long userId = scenePro == null ? scenePlus.getUserId() : scenePro.getUserId();
  116. Integer sceneType = scenePro !=null ? scenePro.getSceneSource() : scenePlus.getSceneSource();
  117. log.info("down--sceneType:{},isObj:{}",sceneType,isObj);
  118. if((sceneType == 4 || sceneType == 5) && isObj !=1){ //深时场景
  119. return SSDownload(sceneNum,userId);
  120. }
  121. DownVo downVo = new DownVo();
  122. Integer sceneVersion = getSceneVersion(scenePro, scenePlus);
  123. Map<String,String> params = new HashMap<>(2);
  124. params.put("type","local");
  125. params.put("sceneNum",sceneNum);
  126. String redisKey = RedisKey.DOWNLOAD_TASK;
  127. String sysVersion = "v3";
  128. if(scenePro == null){
  129. params.put("num",sceneNum);
  130. redisKey = RedisKey.SCENE_DOWNLOADS_TASK_V4;
  131. sysVersion = "v4";
  132. }
  133. redisUtil.lRightPush(redisKey, JSONObject.toJSONString(params));
  134. SceneDownloadLog sceneDownloadLogEntity = new SceneDownloadLog();
  135. sceneDownloadLogEntity.setUserId(userId);
  136. sceneDownloadLogEntity.setSceneNum(sceneNum);
  137. sceneDownloadLogEntity.setSceneVersion(sceneVersion);
  138. sceneDownloadLogEntity.setStatus(0);
  139. sceneDownloadLogEntity.setSysVersion(sysVersion);
  140. sceneDownloadLogService.save(sceneDownloadLogEntity);
  141. downVo.setDownloadStatus(1);
  142. return downVo;
  143. }
  144. @Override
  145. public DownloadProcessVo downloadProcess(String sceneNum,Integer isObj) {
  146. if (StringUtils.isEmpty(sceneNum)) {
  147. throw new BusinessException(ResultCode.MISSING_REQUIRED_PARAMETERS.code(),ResultCode.MISSING_REQUIRED_PARAMETERS.message());
  148. }
  149. ScenePro scenePro = sceneProService.getByNum(sceneNum);
  150. ScenePlus scenePlus = scenePlusService.getByNum(sceneNum);
  151. if(scenePro == null && scenePlus == null){
  152. throw new BusinessException(SceneConstant.FAILURE_CODE_5005, SceneConstant.FAILURE_MSG_5005);
  153. }
  154. Integer sceneType = scenePro !=null ? scenePro.getSceneSource() : scenePlus.getSceneSource();
  155. log.info("downloadProcess--sceneType:{},isObj:{}",sceneType,isObj);
  156. if((sceneType == 4 || sceneType == 5) && isObj !=1){ //深时场景
  157. return SSDownloadProcess(sceneNum);
  158. }
  159. String redisKey = RedisKey.PREFIX_DOWNLOAD_PROGRESS;
  160. if(scenePro == null){
  161. redisKey = RedisKey.PREFIX_DOWNLOAD_PROGRESS_V4;
  162. }
  163. // 获取下载进度
  164. String result = redisUtil.get(String.format(redisKey,sceneNum));
  165. if(StringUtils.isEmpty(result)){
  166. return new DownloadProcessVo();
  167. }
  168. int isUp = 0;
  169. if(scenePro == null){
  170. isUp = 1;
  171. }
  172. SceneDownloadLog sceneDownloadLog = sceneDownloadLogService.getByStatusAndNum(sceneNum,0,isUp);
  173. DownloadProcessVo downloadProcessVo = JSONObject.parseObject(result, DownloadProcessVo.class);
  174. if(sceneDownloadLog != null){
  175. switch (downloadProcessVo.getStatus()) {
  176. case DownloadStatusEnum.DOWNLOAD_SUCCESS_CODE:
  177. String url = downloadProcessVo.getUrl();
  178. if (!StringUtils.isEmpty(url)) {
  179. sceneDownloadLog.setDownloadUrl(url);
  180. sceneDownloadLog.setStatus(1);
  181. break;
  182. }
  183. case DownloadStatusEnum.DOWNLOAD_FAILED_CODE:
  184. sceneDownloadLog.setStatus(-1);
  185. break;
  186. }
  187. sceneDownloadLogService.updateById(sceneDownloadLog);
  188. }
  189. return downloadProcessVo;
  190. }
  191. /**
  192. * status :离线包状态是否需要重新生成 0 未生成:1 不需要 2需要 3 生成中
  193. */
  194. private DownVo SSCheckDownload(String sceneNum) {
  195. DownVo downVo = new DownVo();
  196. SSDownSceneVo vo = laserService.downOfflineSceneStatus(sceneNum);
  197. if(vo == null){
  198. throw new BusinessException(ResultCode.SS_SCENE_DOWN_ERROR);
  199. }
  200. downVo.setDownloadStatus(0);
  201. if(vo.getStatus() == 1){
  202. downVo.setDownloadStatus(3);
  203. downVo.setDownloadUrl(vo.getUrl());
  204. }
  205. if(vo.getStatus() == 2){
  206. downVo.setDownloadStatus(2);
  207. }
  208. if(vo.getStatus() == 3){
  209. downVo.setDownloadStatus(1);
  210. }
  211. return downVo;
  212. }
  213. /**
  214. * downloadStatus -1下载失败 1下载成功
  215. */
  216. private DownVo SSDownload(String sceneNum,Long userId) {
  217. DownVo downVo = new DownVo();
  218. //status :0:正在生成 1,初次生成 2,已经生成直接下载 3,重新生成
  219. SSDownSceneVo vo = laserService.downOfflineScene(sceneNum);
  220. if(vo == null){
  221. throw new BusinessException(ResultCode.SS_SCENE_DOWN_ERROR);
  222. }
  223. SceneDownloadLog sceneDownloadLogEntity = new SceneDownloadLog();
  224. sceneDownloadLogEntity.setUserId(userId);
  225. sceneDownloadLogEntity.setSceneNum(sceneNum);
  226. sceneDownloadLogEntity.setSceneVersion(0);
  227. sceneDownloadLogEntity.setStatus(0);
  228. sceneDownloadLogEntity.setSysVersion("ss");
  229. sceneDownloadLogService.save(sceneDownloadLogEntity);
  230. downVo.setDownloadStatus(1);
  231. return downVo;
  232. }
  233. public static HashMap<String,Integer> ssNumProcessNumMap = new HashMap<>();
  234. private DownloadProcessVo SSDownloadProcess(String sceneNum) {
  235. DownloadProcessVo downVo = new DownloadProcessVo();
  236. SSDownSceneVo vo = laserService.downOfflineSceneStatus(sceneNum);
  237. if(vo == null){
  238. throw new BusinessException(ResultCode.SS_SCENE_DOWN_ERROR);
  239. }
  240. downVo.setStatus(1003);
  241. if(vo.getStatus() == 0 || vo.getStatus() == 2 || vo.getStatus() == 3){ //下载中
  242. ssNumProcessNumMap.merge(sceneNum, 1, Integer::sum);
  243. Integer percent = ssNumProcessNumMap.get(sceneNum);
  244. percent = percent /2;
  245. if(percent >50){
  246. percent = 50;
  247. }
  248. downVo.setStatus(1001);
  249. downVo.setPercent(percent);
  250. }
  251. if(vo.getStatus() == 1){ //下载完成
  252. ssNumProcessNumMap.remove(sceneNum);
  253. downVo.setPercent(100);
  254. downVo.setUrl(vo.getUrl());
  255. downVo.setStatus(1002);
  256. LambdaUpdateWrapper<SceneDownloadLog> wrapper = new LambdaUpdateWrapper<>();
  257. wrapper.eq(SceneDownloadLog::getSceneNum,sceneNum);
  258. wrapper.eq(SceneDownloadLog::getStatus,0);
  259. wrapper.set(SceneDownloadLog::getDownloadUrl,vo.getUrl());
  260. wrapper.set(SceneDownloadLog::getStatus,1);
  261. sceneDownloadLogService.update(wrapper);
  262. }
  263. return downVo;
  264. }
  265. }