DownService.java 11 KB

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