DownService.java 14 KB

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