DownService.java 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. package com.fdkankan.ucenter.service.impl;
  2. import com.alibaba.fastjson.JSONObject;
  3. import com.fdkankan.common.constant.ErrorCode;
  4. import com.fdkankan.common.constant.SceneConstant;
  5. import com.fdkankan.common.exception.BusinessException;
  6. import com.fdkankan.redis.constant.RedisKey;
  7. import com.fdkankan.redis.util.RedisUtil;
  8. import com.fdkankan.ucenter.constant.LoginConstant;
  9. import com.fdkankan.ucenter.entity.*;
  10. import com.fdkankan.ucenter.service.*;
  11. import com.fdkankan.ucenter.vo.response.DownVo;
  12. import com.fdkankan.ucenter.vo.response.DownloadProcessVo;
  13. import org.apache.commons.lang3.StringUtils;
  14. import org.springframework.beans.factory.annotation.Autowired;
  15. import org.springframework.stereotype.Service;
  16. import java.util.HashMap;
  17. import java.util.Map;
  18. @Service
  19. public class DownService implements IDownService {
  20. @Autowired
  21. ISceneProService sceneProService;
  22. @Autowired
  23. ISceneProEditService sceneProEditService;
  24. @Autowired
  25. ISceneDownloadLogService sceneDownloadLogService;
  26. @Autowired
  27. IScenePlusService scenePlusService;
  28. @Autowired
  29. RedisUtil redisUtil;
  30. @Autowired
  31. ISceneEditInfoService sceneEditInfoService;
  32. @Autowired
  33. IUserService userService;
  34. @Override
  35. public DownVo checkDownLoad(String sceneNum) {
  36. if(StringUtils.isEmpty(sceneNum)){
  37. throw new BusinessException(LoginConstant.FAILURE_CODE_3001, LoginConstant.FAILURE_MSG_3001);
  38. }
  39. ScenePro scenePro = sceneProService.getByNum(sceneNum);
  40. ScenePlus plus = scenePlusService.getByNum(sceneNum);
  41. if(scenePro == null && plus == null){
  42. throw new BusinessException(SceneConstant.FAILURE_CODE_5005, SceneConstant.FAILURE_MSG_5005);
  43. }
  44. SceneDownloadLog sceneDownloadLog;
  45. int isUp = 0;
  46. if(scenePro == null){
  47. isUp = 1;
  48. }
  49. Integer sceneVersion = getSceneVersion(scenePro, plus);
  50. sceneDownloadLog = sceneDownloadLogService.getByStatusAndNum(sceneNum,0,isUp);
  51. DownVo downVo = new DownVo();
  52. if(sceneDownloadLog != null){
  53. downVo.setDownloadStatus(1);
  54. return downVo;
  55. }
  56. sceneDownloadLog = sceneDownloadLogService.getByStatusAndNum(sceneNum,1,isUp);
  57. //3下载过,并且没有修改过
  58. if(sceneDownloadLog != null && sceneDownloadLog.getSceneVersion().intValue() == sceneVersion){
  59. downVo.setDownloadStatus(3);
  60. downVo.setDownloadUrl(sceneDownloadLog.getDownloadUrl());
  61. return downVo;
  62. }
  63. //下载过,有更改
  64. if(sceneDownloadLog != null){
  65. String redisKey = RedisKey.PREFIX_DOWNLOAD_PROGRESS;
  66. if(isUp == 1){
  67. redisKey = RedisKey.PREFIX_DOWNLOAD_PROGRESS_V4;
  68. }
  69. downVo.setDownloadStatus(2);
  70. redisUtil.del(String.format(redisKey,sceneNum)); // 清除旧的下载信息
  71. return downVo;
  72. }
  73. return downVo;
  74. }
  75. private Integer getSceneVersion(ScenePro scenePro,ScenePlus scenePlus){
  76. Integer version = 0;
  77. if(scenePro != null){
  78. SceneProEdit proEdit = sceneProEditService.getByProId(scenePro.getId());
  79. if(proEdit == null){
  80. throw new BusinessException(SceneConstant.FAILURE_CODE_5005, SceneConstant.FAILURE_MSG_5005);
  81. }
  82. version = proEdit.getVersion();
  83. }
  84. if(scenePro == null && scenePlus !=null){
  85. SceneEditInfo sceneEditInfo = sceneEditInfoService.getByScenePlusId(scenePlus.getId());
  86. if(sceneEditInfo == null){
  87. throw new BusinessException(SceneConstant.FAILURE_CODE_5005, SceneConstant.FAILURE_MSG_5005);
  88. }
  89. version = sceneEditInfo.getVersion();
  90. }
  91. return version;
  92. }
  93. @Override
  94. public DownVo down(String sceneNum,String userName) {
  95. if(StringUtils.isEmpty(sceneNum) || StringUtils.isEmpty(userName)){
  96. throw new BusinessException(LoginConstant.FAILURE_CODE_3001, LoginConstant.FAILURE_MSG_3001);
  97. }
  98. ScenePro scenePro = sceneProService.getByNum(sceneNum);
  99. ScenePlus scenePlus = scenePlusService.getByNum(sceneNum);
  100. if(scenePro == null && scenePlus == null){
  101. throw new BusinessException(SceneConstant.FAILURE_CODE_5005, SceneConstant.FAILURE_MSG_5005);
  102. }
  103. DownVo downVo = new DownVo();
  104. User user = userService.getByUserName(userName);
  105. if(user == null || user.getDownloadNumTotal() - user.getDownloadNum() <= 0){
  106. downVo.setDownloadStatus(-1);
  107. return downVo;
  108. }
  109. Integer sceneVersion = getSceneVersion(scenePro, scenePlus);
  110. Map<String,String> params = new HashMap<>(2);
  111. params.put("type","local");
  112. params.put("sceneNum",sceneNum);
  113. String redisKey = RedisKey.DOWNLOAD_TASK;
  114. String sysVersion = "v3";
  115. if(scenePro == null){
  116. params.put("num",sceneNum);
  117. redisKey = RedisKey.SCENE_DOWNLOADS_TASK_V4;
  118. sysVersion = "v4";
  119. }
  120. redisUtil.lRightPush(redisKey, JSONObject.toJSONString(params));
  121. user.setDownloadNum(user.getDownloadNum() + 1);
  122. userService.updateById(user);
  123. SceneDownloadLog sceneDownloadLogEntity = new SceneDownloadLog();
  124. sceneDownloadLogEntity.setUserId(user.getId());
  125. sceneDownloadLogEntity.setSceneNum(sceneNum);
  126. sceneDownloadLogEntity.setSceneVersion(sceneVersion);
  127. sceneDownloadLogEntity.setStatus(0);
  128. sceneDownloadLogEntity.setSysVersion(sysVersion);
  129. sceneDownloadLogService.save(sceneDownloadLogEntity);
  130. downVo.setDownloadStatus(1);
  131. return downVo;
  132. }
  133. @Override
  134. public DownloadProcessVo downloadProcess(String sceneNum) {
  135. if (StringUtils.isEmpty(sceneNum)) {
  136. throw new BusinessException(LoginConstant.FAILURE_CODE_3001, LoginConstant.FAILURE_MSG_3001);
  137. }
  138. ScenePro scenePro = sceneProService.getByNum(sceneNum);
  139. ScenePlus scenePlus = scenePlusService.getByNum(sceneNum);
  140. if(scenePro == null && scenePlus == null){
  141. throw new BusinessException(SceneConstant.FAILURE_CODE_5005, SceneConstant.FAILURE_MSG_5005);
  142. }
  143. String redisKey = RedisKey.PREFIX_DOWNLOAD_PROGRESS;
  144. if(scenePro == null){
  145. redisKey = RedisKey.PREFIX_DOWNLOAD_PROGRESS_V4;
  146. }
  147. // 获取下载进度
  148. String result = redisUtil.get(String.format(redisKey,sceneNum));
  149. if(StringUtils.isEmpty(result)){
  150. return new DownloadProcessVo();
  151. }
  152. return JSONObject.parseObject(result,DownloadProcessVo.class);
  153. }
  154. }