DownService.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. package com.fdkankan.ucenter.service.impl;
  2. import com.alibaba.fastjson.JSONObject;
  3. import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
  4. import com.fdkankan.common.constant.ErrorCode;
  5. import com.fdkankan.common.constant.SceneConstant;
  6. import com.fdkankan.common.exception.BusinessException;
  7. import com.fdkankan.redis.constant.RedisKey;
  8. import com.fdkankan.redis.util.RedisUtil;
  9. import com.fdkankan.ucenter.common.DownloadStatusEnum;
  10. import com.fdkankan.ucenter.common.constants.ResultCode;
  11. import com.fdkankan.ucenter.constant.LoginConstant;
  12. import com.fdkankan.ucenter.entity.*;
  13. import com.fdkankan.ucenter.httpClient.service.LaserService;
  14. import com.fdkankan.ucenter.httpClient.vo.SSDownSceneVo;
  15. import com.fdkankan.ucenter.service.*;
  16. import com.fdkankan.ucenter.vo.response.DownVo;
  17. import com.fdkankan.ucenter.vo.response.DownloadProcessVo;
  18. import org.apache.commons.lang3.StringUtils;
  19. import org.apache.ibatis.annotations.Case;
  20. import org.springframework.beans.factory.annotation.Autowired;
  21. import org.springframework.stereotype.Service;
  22. import java.util.HashMap;
  23. import java.util.Map;
  24. @Service
  25. public class DownService implements IDownService {
  26. @Autowired
  27. ISceneProService sceneProService;
  28. @Autowired
  29. ISceneProEditService sceneProEditService;
  30. @Autowired
  31. ISceneDownloadLogService sceneDownloadLogService;
  32. @Autowired
  33. IScenePlusService scenePlusService;
  34. @Autowired
  35. RedisUtil redisUtil;
  36. @Autowired
  37. ISceneEditInfoService sceneEditInfoService;
  38. @Autowired
  39. IUserService userService;
  40. @Autowired
  41. LaserService laserService;
  42. @Override
  43. public DownVo checkDownLoad(String sceneNum) {
  44. if(StringUtils.isEmpty(sceneNum)){
  45. throw new BusinessException(LoginConstant.FAILURE_CODE_3001, LoginConstant.FAILURE_MSG_3001);
  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. if(sceneType == 4){ //深时场景
  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,String userName) {
  107. if(StringUtils.isEmpty(sceneNum) || StringUtils.isEmpty(userName)){
  108. throw new BusinessException(LoginConstant.FAILURE_CODE_3001, LoginConstant.FAILURE_MSG_3001);
  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. Integer sceneType = scenePro !=null ? scenePro.getSceneSource() : scenePlus.getSceneSource();
  116. if(sceneType == 4){ //深时场景
  117. return SSDownload(sceneNum,userName);
  118. }
  119. DownVo downVo = new DownVo();
  120. User user = userService.getByUserName(userName);
  121. if(user == null || user.getDownloadNumTotal() - user.getDownloadNum() <= 0){
  122. downVo.setDownloadStatus(-1);
  123. return downVo;
  124. }
  125. Integer sceneVersion = getSceneVersion(scenePro, scenePlus);
  126. Map<String,String> params = new HashMap<>(2);
  127. params.put("type","local");
  128. params.put("sceneNum",sceneNum);
  129. String progressKey = String.format(RedisKey.PREFIX_DOWNLOAD_PROGRESS, sceneNum);
  130. String downloadTaskKey = RedisKey.DOWNLOAD_TASK;
  131. String sysVersion = "v3";
  132. if(scenePro == null){
  133. params.put("num",sceneNum);
  134. progressKey = String.format(RedisKey.PREFIX_DOWNLOAD_PROGRESS_V4, sceneNum);
  135. downloadTaskKey = RedisKey.SCENE_DOWNLOADS_TASK_V4;
  136. sysVersion = "v4";
  137. }
  138. //先删除之前的下载进度
  139. redisUtil.del(progressKey);
  140. //入队
  141. redisUtil.lRightPush(downloadTaskKey, JSONObject.toJSONString(params));
  142. //修改用户的下载次数
  143. user.setDownloadNum(user.getDownloadNum() + 1);
  144. userService.updateById(user);
  145. SceneDownloadLog sceneDownloadLogEntity = new SceneDownloadLog();
  146. sceneDownloadLogEntity.setUserId(user.getId());
  147. sceneDownloadLogEntity.setSceneNum(sceneNum);
  148. sceneDownloadLogEntity.setSceneVersion(sceneVersion);
  149. sceneDownloadLogEntity.setStatus(0);
  150. sceneDownloadLogEntity.setSysVersion(sysVersion);
  151. sceneDownloadLogService.save(sceneDownloadLogEntity);
  152. downVo.setDownloadStatus(1);
  153. return downVo;
  154. }
  155. @Override
  156. public DownloadProcessVo downloadProcess(Long userId, String sceneNum) {
  157. if (StringUtils.isEmpty(sceneNum)) {
  158. throw new BusinessException(LoginConstant.FAILURE_CODE_3001, LoginConstant.FAILURE_MSG_3001);
  159. }
  160. ScenePro scenePro = sceneProService.getByNum(sceneNum);
  161. ScenePlus scenePlus = scenePlusService.getByNum(sceneNum);
  162. if(scenePro == null && scenePlus == null){
  163. throw new BusinessException(SceneConstant.FAILURE_CODE_5005, SceneConstant.FAILURE_MSG_5005);
  164. }
  165. Integer sceneType = scenePro !=null ? scenePro.getSceneSource() : scenePlus.getSceneSource();
  166. if(sceneType == 4){ //深时场景
  167. return SSDownloadProcess(sceneNum);
  168. }
  169. String redisKey = RedisKey.PREFIX_DOWNLOAD_PROGRESS;
  170. if(scenePro == null){
  171. redisKey = RedisKey.PREFIX_DOWNLOAD_PROGRESS_V4;
  172. }
  173. // 获取下载进度
  174. String result = redisUtil.get(String.format(redisKey,sceneNum));
  175. if(StringUtils.isEmpty(result)){
  176. return new DownloadProcessVo();
  177. }
  178. int isUp = 0;
  179. if(scenePro == null){
  180. isUp = 1;
  181. }
  182. SceneDownloadLog sceneDownloadLog = sceneDownloadLogService.getByStatusAndNum(sceneNum,0,isUp);
  183. DownloadProcessVo downloadProcessVo = JSONObject.parseObject(result, DownloadProcessVo.class);
  184. if(sceneDownloadLog != null){
  185. switch (downloadProcessVo.getStatus()) {
  186. case DownloadStatusEnum.DOWNLOAD_SUCCESS_CODE:
  187. String url = downloadProcessVo.getUrl();
  188. if (!StringUtils.isEmpty(url)) {
  189. sceneDownloadLog.setDownloadUrl(url);
  190. sceneDownloadLog.setStatus(1);
  191. break;
  192. }
  193. case DownloadStatusEnum.DOWNLOAD_FAILED_CODE:
  194. sceneDownloadLog.setStatus(-1);
  195. userService.updateDownloadNum(userId, -1);
  196. break;
  197. }
  198. sceneDownloadLogService.updateById(sceneDownloadLog);
  199. }
  200. return downloadProcessVo;
  201. }
  202. /**
  203. * status :离线包状态是否需要重新生成 0 未生成:1 不需要 2需要 3 生成中
  204. */
  205. private DownVo SSCheckDownload(String sceneNum) {
  206. DownVo downVo = new DownVo();
  207. SSDownSceneVo vo = laserService.downOfflineSceneStatus(sceneNum);
  208. if(vo == null){
  209. throw new BusinessException(ResultCode.FAILURE_CODE_400003,ResultCode.FAILURE_MSG_400003);
  210. }
  211. downVo.setDownloadStatus(0);
  212. if(vo.getStatus() == 1){
  213. downVo.setDownloadStatus(3);
  214. downVo.setDownloadUrl(vo.getUrl());
  215. }
  216. if(vo.getStatus() == 2){
  217. downVo.setDownloadStatus(2);
  218. }
  219. if(vo.getStatus() == 3){
  220. downVo.setDownloadStatus(1);
  221. }
  222. return downVo;
  223. }
  224. /**
  225. * downloadStatus -1下载失败 1下载成功
  226. */
  227. private DownVo SSDownload(String sceneNum,String userName) {
  228. DownVo downVo = new DownVo();
  229. User user = userService.getByUserName(userName);
  230. if(user == null || user.getSsDownloadNumTotal() - user.getSsDownloadNum() <= 0){
  231. downVo.setDownloadStatus(-1);
  232. return downVo;
  233. }
  234. //status :0:正在生成 1,初次生成 2,已经生成直接下载 3,重新生成
  235. SSDownSceneVo vo = laserService.downOfflineScene(sceneNum);
  236. if(vo == null){
  237. throw new BusinessException(ResultCode.FAILURE_CODE_400003,ResultCode.FAILURE_MSG_400003);
  238. }
  239. LambdaUpdateWrapper<User> wrapper = new LambdaUpdateWrapper<>();
  240. wrapper.eq(User::getId,user.getId());
  241. wrapper.set(User::getSsDownloadNum ,user.getSsDownloadNum() + 1);
  242. userService.update(wrapper);
  243. SceneDownloadLog sceneDownloadLogEntity = new SceneDownloadLog();
  244. sceneDownloadLogEntity.setUserId(user.getId());
  245. sceneDownloadLogEntity.setSceneNum(sceneNum);
  246. sceneDownloadLogEntity.setSceneVersion(0);
  247. sceneDownloadLogEntity.setStatus(0);
  248. sceneDownloadLogEntity.setSysVersion("ss");
  249. sceneDownloadLogService.save(sceneDownloadLogEntity);
  250. downVo.setDownloadStatus(1);
  251. return downVo;
  252. }
  253. public static HashMap<String,Integer> ssNumProcessNumMap = new HashMap<>();
  254. private DownloadProcessVo SSDownloadProcess(String sceneNum) {
  255. DownloadProcessVo downVo = new DownloadProcessVo();
  256. SSDownSceneVo vo = laserService.downOfflineSceneStatus(sceneNum);
  257. if(vo == null){
  258. throw new BusinessException(ResultCode.FAILURE_CODE_400003,ResultCode.FAILURE_MSG_400003);
  259. }
  260. downVo.setStatus(1003);
  261. if(vo.getStatus() == 0 || vo.getStatus() == 2 || vo.getStatus() == 3){ //下载中
  262. ssNumProcessNumMap.merge(sceneNum, 1, Integer::sum);
  263. Integer percent = ssNumProcessNumMap.get(sceneNum);
  264. if(percent >100){
  265. percent = 99;
  266. }
  267. downVo.setStatus(1001);
  268. downVo.setPercent(percent);
  269. }
  270. if(vo.getStatus() == 1){ //下载完成
  271. ssNumProcessNumMap.remove(sceneNum);
  272. downVo.setPercent(100);
  273. downVo.setUrl(vo.getUrl());
  274. downVo.setStatus(1002);
  275. LambdaUpdateWrapper<SceneDownloadLog> wrapper = new LambdaUpdateWrapper<>();
  276. wrapper.eq(SceneDownloadLog::getSceneNum,sceneNum);
  277. wrapper.eq(SceneDownloadLog::getStatus,0);
  278. wrapper.set(SceneDownloadLog::getDownloadUrl,vo.getUrl());
  279. wrapper.set(SceneDownloadLog::getStatus,1);
  280. sceneDownloadLogService.update(wrapper);
  281. }
  282. return downVo;
  283. }
  284. }