DownService.java 14 KB

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