DownService.java 14 KB

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