DownService.java 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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 lombok.extern.slf4j.Slf4j;
  19. import org.apache.commons.lang3.StringUtils;
  20. import org.apache.ibatis.annotations.Case;
  21. import org.springframework.beans.factory.annotation.Autowired;
  22. import org.springframework.stereotype.Service;
  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. IScenePlusExtService scenePlusExtService;
  38. @Autowired
  39. RedisUtil redisUtil;
  40. @Autowired
  41. ISceneEditInfoService sceneEditInfoService;
  42. @Autowired
  43. IUserService userService;
  44. @Autowired
  45. LaserService laserService;
  46. @Override
  47. public DownVo checkDownLoad(String sceneNum) {
  48. if(StringUtils.isEmpty(sceneNum)){
  49. throw new BusinessException(LoginConstant.FAILURE_CODE_3001, LoginConstant.FAILURE_MSG_3001);
  50. }
  51. ScenePro scenePro = sceneProService.getByNum(sceneNum);
  52. ScenePlus plus = scenePlusService.getByNum(sceneNum);
  53. if(scenePro == null && plus == null){
  54. throw new BusinessException(SceneConstant.FAILURE_CODE_5005, SceneConstant.FAILURE_MSG_5005);
  55. }
  56. Integer sceneType = scenePro !=null ? scenePro.getSceneSource() : plus.getSceneSource();
  57. Integer isObj = scenePro !=null ? scenePro.getIsObj() : scenePlusExtService.getByPlusId(plus.getId()).getIsObj();
  58. log.info("checkDownLoad--sceneType:{},isObj:{}",sceneType,isObj);
  59. if(sceneType == 4 && isObj !=1){ //深时场景
  60. return SSCheckDownload(sceneNum);
  61. }
  62. SceneDownloadLog sceneDownloadLog;
  63. int isUp = 0;
  64. if(scenePro == null){
  65. isUp = 1;
  66. }
  67. Integer sceneVersion = getSceneVersion(scenePro, plus);
  68. sceneDownloadLog = sceneDownloadLogService.getByStatusAndNum(sceneNum,0,isUp);
  69. DownVo downVo = new DownVo();
  70. if(sceneDownloadLog != null){
  71. downVo.setDownloadStatus(1);
  72. return downVo;
  73. }
  74. sceneDownloadLog = sceneDownloadLogService.getByStatusAndNum(sceneNum,1,isUp);
  75. //3下载过,并且没有修改过
  76. if(sceneDownloadLog != null && sceneDownloadLog.getSceneVersion().intValue() == sceneVersion){
  77. downVo.setDownloadStatus(3);
  78. downVo.setDownloadUrl(sceneDownloadLog.getDownloadUrl());
  79. return downVo;
  80. }
  81. //下载过,有更改
  82. if(sceneDownloadLog != null){
  83. String redisKey = RedisKey.PREFIX_DOWNLOAD_PROGRESS;
  84. if(isUp == 1){
  85. redisKey = RedisKey.PREFIX_DOWNLOAD_PROGRESS_V4;
  86. }
  87. downVo.setDownloadStatus(2);
  88. redisUtil.del(String.format(redisKey,sceneNum)); // 清除旧的下载信息
  89. return downVo;
  90. }
  91. return downVo;
  92. }
  93. private Integer getSceneVersion(ScenePro scenePro,ScenePlus scenePlus){
  94. Integer version = 0;
  95. if(scenePro != null){
  96. SceneProEdit proEdit = sceneProEditService.getByProId(scenePro.getId());
  97. if(proEdit == null){
  98. throw new BusinessException(SceneConstant.FAILURE_CODE_5005, SceneConstant.FAILURE_MSG_5005);
  99. }
  100. version = proEdit.getVersion();
  101. }
  102. if(scenePro == null && scenePlus !=null){
  103. SceneEditInfo sceneEditInfo = sceneEditInfoService.getByScenePlusId(scenePlus.getId());
  104. if(sceneEditInfo == null){
  105. throw new BusinessException(SceneConstant.FAILURE_CODE_5005, SceneConstant.FAILURE_MSG_5005);
  106. }
  107. version = sceneEditInfo.getVersion();
  108. }
  109. return version;
  110. }
  111. @Override
  112. public DownVo down(String sceneNum,String userName) {
  113. if(StringUtils.isEmpty(sceneNum) || StringUtils.isEmpty(userName)){
  114. throw new BusinessException(LoginConstant.FAILURE_CODE_3001, LoginConstant.FAILURE_MSG_3001);
  115. }
  116. ScenePro scenePro = sceneProService.getByNum(sceneNum);
  117. ScenePlus scenePlus = scenePlusService.getByNum(sceneNum);
  118. if(scenePro == null && scenePlus == null){
  119. throw new BusinessException(SceneConstant.FAILURE_CODE_5005, SceneConstant.FAILURE_MSG_5005);
  120. }
  121. Integer sceneType = scenePro !=null ? scenePro.getSceneSource() : scenePlus.getSceneSource();
  122. Integer isObj = scenePro !=null ? scenePro.getIsObj() : scenePlusExtService.getByPlusId(scenePlus.getId()).getIsObj();
  123. log.info("down--sceneType:{},isObj:{}",sceneType,isObj);
  124. if(sceneType == 4 && isObj !=1){ //深时场景
  125. return SSDownload(sceneNum,userName);
  126. }
  127. DownVo downVo = new DownVo();
  128. User user = userService.getByUserName(userName);
  129. if(user == null || user.getDownloadNumTotal() - user.getDownloadNum() <= 0){
  130. downVo.setDownloadStatus(-1);
  131. return downVo;
  132. }
  133. Integer sceneVersion = getSceneVersion(scenePro, scenePlus);
  134. Map<String,String> params = new HashMap<>(2);
  135. params.put("type","local");
  136. params.put("sceneNum",sceneNum);
  137. String progressKey = String.format(RedisKey.PREFIX_DOWNLOAD_PROGRESS, sceneNum);
  138. String downloadTaskKey = RedisKey.DOWNLOAD_TASK;
  139. String sysVersion = "v3";
  140. if(scenePro == null){
  141. params.put("num",sceneNum);
  142. progressKey = String.format(RedisKey.PREFIX_DOWNLOAD_PROGRESS_V4, sceneNum);
  143. downloadTaskKey = RedisKey.SCENE_DOWNLOADS_TASK_V4;
  144. sysVersion = "v4";
  145. }
  146. //先删除之前的下载进度
  147. redisUtil.del(progressKey);
  148. //入队
  149. redisUtil.lRightPush(downloadTaskKey, JSONObject.toJSONString(params));
  150. //修改用户的下载次数
  151. user.setDownloadNum(user.getDownloadNum() + 1);
  152. userService.updateById(user);
  153. SceneDownloadLog sceneDownloadLogEntity = new SceneDownloadLog();
  154. sceneDownloadLogEntity.setUserId(user.getId());
  155. sceneDownloadLogEntity.setSceneNum(sceneNum);
  156. sceneDownloadLogEntity.setSceneVersion(sceneVersion);
  157. sceneDownloadLogEntity.setStatus(0);
  158. sceneDownloadLogEntity.setSysVersion(sysVersion);
  159. sceneDownloadLogService.save(sceneDownloadLogEntity);
  160. downVo.setDownloadStatus(1);
  161. return downVo;
  162. }
  163. @Override
  164. public DownloadProcessVo downloadProcess(Long userId, String sceneNum) {
  165. if (StringUtils.isEmpty(sceneNum)) {
  166. throw new BusinessException(LoginConstant.FAILURE_CODE_3001, LoginConstant.FAILURE_MSG_3001);
  167. }
  168. ScenePro scenePro = sceneProService.getByNum(sceneNum);
  169. ScenePlus scenePlus = scenePlusService.getByNum(sceneNum);
  170. if(scenePro == null && scenePlus == null){
  171. throw new BusinessException(SceneConstant.FAILURE_CODE_5005, SceneConstant.FAILURE_MSG_5005);
  172. }
  173. Integer sceneType = scenePro !=null ? scenePro.getSceneSource() : scenePlus.getSceneSource();
  174. if(sceneType == 4){ //深时场景
  175. return SSDownloadProcess(sceneNum);
  176. }
  177. String redisKey = RedisKey.PREFIX_DOWNLOAD_PROGRESS;
  178. if(scenePro == null){
  179. redisKey = RedisKey.PREFIX_DOWNLOAD_PROGRESS_V4;
  180. }
  181. // 获取下载进度
  182. String result = redisUtil.get(String.format(redisKey,sceneNum));
  183. if(StringUtils.isEmpty(result)){
  184. return new DownloadProcessVo();
  185. }
  186. int isUp = 0;
  187. if(scenePro == null){
  188. isUp = 1;
  189. }
  190. SceneDownloadLog sceneDownloadLog = sceneDownloadLogService.getByStatusAndNum(sceneNum,0,isUp);
  191. DownloadProcessVo downloadProcessVo = JSONObject.parseObject(result, DownloadProcessVo.class);
  192. if(sceneDownloadLog != null){
  193. switch (downloadProcessVo.getStatus()) {
  194. case DownloadStatusEnum.DOWNLOAD_SUCCESS_CODE:
  195. String url = downloadProcessVo.getUrl();
  196. if (!StringUtils.isEmpty(url)) {
  197. sceneDownloadLog.setDownloadUrl(url);
  198. sceneDownloadLog.setStatus(1);
  199. break;
  200. }
  201. case DownloadStatusEnum.DOWNLOAD_FAILED_CODE:
  202. sceneDownloadLog.setStatus(-1);
  203. userService.updateDownloadNum(userId, -1);
  204. break;
  205. }
  206. sceneDownloadLogService.updateById(sceneDownloadLog);
  207. }
  208. return downloadProcessVo;
  209. }
  210. /**
  211. * status :离线包状态是否需要重新生成 0 未生成:1 不需要 2需要 3 生成中
  212. */
  213. private DownVo SSCheckDownload(String sceneNum) {
  214. DownVo downVo = new DownVo();
  215. SSDownSceneVo vo = laserService.downOfflineSceneStatus(sceneNum);
  216. if(vo == null){
  217. throw new BusinessException(ResultCode.FAILURE_CODE_400003,ResultCode.FAILURE_MSG_400003);
  218. }
  219. downVo.setDownloadStatus(0);
  220. if(vo.getStatus() == 1){
  221. downVo.setDownloadStatus(3);
  222. downVo.setDownloadUrl(vo.getUrl());
  223. }
  224. if(vo.getStatus() == 2){
  225. downVo.setDownloadStatus(2);
  226. }
  227. if(vo.getStatus() == 3){
  228. downVo.setDownloadStatus(1);
  229. }
  230. return downVo;
  231. }
  232. /**
  233. * downloadStatus -1下载失败 1下载成功
  234. */
  235. private DownVo SSDownload(String sceneNum,String userName) {
  236. DownVo downVo = new DownVo();
  237. User user = userService.getByUserName(userName);
  238. if(user == null || user.getSsDownloadNumTotal() - user.getSsDownloadNum() <= 0){
  239. downVo.setDownloadStatus(-1);
  240. return downVo;
  241. }
  242. //status :0:正在生成 1,初次生成 2,已经生成直接下载 3,重新生成
  243. SSDownSceneVo vo = laserService.downOfflineScene(sceneNum);
  244. if(vo == null){
  245. throw new BusinessException(ResultCode.FAILURE_CODE_400003,ResultCode.FAILURE_MSG_400003);
  246. }
  247. LambdaUpdateWrapper<User> wrapper = new LambdaUpdateWrapper<>();
  248. wrapper.eq(User::getId,user.getId());
  249. wrapper.set(User::getSsDownloadNum ,user.getSsDownloadNum() + 1);
  250. userService.update(wrapper);
  251. SceneDownloadLog sceneDownloadLogEntity = new SceneDownloadLog();
  252. sceneDownloadLogEntity.setUserId(user.getId());
  253. sceneDownloadLogEntity.setSceneNum(sceneNum);
  254. sceneDownloadLogEntity.setSceneVersion(0);
  255. sceneDownloadLogEntity.setStatus(0);
  256. sceneDownloadLogEntity.setSysVersion("ss");
  257. sceneDownloadLogService.save(sceneDownloadLogEntity);
  258. downVo.setDownloadStatus(1);
  259. return downVo;
  260. }
  261. public static HashMap<String,Integer> ssNumProcessNumMap = new HashMap<>();
  262. private DownloadProcessVo SSDownloadProcess(String sceneNum) {
  263. DownloadProcessVo downVo = new DownloadProcessVo();
  264. SSDownSceneVo vo = laserService.downOfflineSceneStatus(sceneNum);
  265. if(vo == null){
  266. throw new BusinessException(ResultCode.FAILURE_CODE_400003,ResultCode.FAILURE_MSG_400003);
  267. }
  268. downVo.setStatus(1003);
  269. if(vo.getStatus() == 0 || vo.getStatus() == 2 || vo.getStatus() == 3){ //下载中
  270. ssNumProcessNumMap.merge(sceneNum, 1, Integer::sum);
  271. Integer percent = ssNumProcessNumMap.get(sceneNum);
  272. percent = percent /2;
  273. if(percent >50){
  274. percent = 50;
  275. }
  276. downVo.setStatus(1001);
  277. downVo.setPercent(percent);
  278. }
  279. if(vo.getStatus() == 1){ //下载完成
  280. ssNumProcessNumMap.remove(sceneNum);
  281. downVo.setPercent(100);
  282. downVo.setUrl(vo.getUrl());
  283. downVo.setStatus(1002);
  284. LambdaUpdateWrapper<SceneDownloadLog> wrapper = new LambdaUpdateWrapper<>();
  285. wrapper.eq(SceneDownloadLog::getSceneNum,sceneNum);
  286. wrapper.eq(SceneDownloadLog::getStatus,0);
  287. wrapper.set(SceneDownloadLog::getDownloadUrl,vo.getUrl());
  288. wrapper.set(SceneDownloadLog::getStatus,1);
  289. sceneDownloadLogService.update(wrapper);
  290. }
  291. return downVo;
  292. }
  293. }