DownService.java 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  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 plus = scenePlusService.getByNum(sceneNum);
  194. if(scenePro == null && plus == null){
  195. throw new BusinessException(SceneConstant.FAILURE_CODE_5005, SceneConstant.FAILURE_MSG_5005);
  196. }
  197. Long cameraId = scenePro !=null ? scenePro.getCameraId() : plus.getCameraId();
  198. CameraType cameraType = cameraTypeService.getByCameraId(cameraId);
  199. isObj = isObj == null ?0 :isObj;
  200. log.info("downloadProcess--cameraType:{},isObj:{}",cameraType.getCameraType(),isObj);
  201. if(cameraType.getIsLaser() == 1 && 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. LambdaUpdateWrapper<User> wrapper = new LambdaUpdateWrapper<>();
  231. wrapper.eq(User::getId,userId);
  232. if(!"aws".equals(NacosProperty.uploadType) && cameraType.getIsLaser() == 1 ){ //深时场景
  233. wrapper.setSql("ss_download_num = ss_download_num - " + 1);
  234. }else {
  235. wrapper.setSql("download_num = download_num - " + 1);
  236. }
  237. userService.update(wrapper);
  238. break;
  239. }
  240. sceneDownloadLogService.updateById(sceneDownloadLog);
  241. }
  242. return downloadProcessVo;
  243. }
  244. /**
  245. * status :离线包状态是否需要重新生成 0 未生成:1 不需要 2需要 3 生成中
  246. */
  247. private DownVo SSCheckDownload(String sceneNum) {
  248. DownVo downVo = new DownVo();
  249. SSDownSceneVo vo = laserService.downOfflineSceneStatus(sceneNum);
  250. if(vo == null){
  251. throw new BusinessException(ResultCode.FAILURE_CODE_400003,ResultCode.FAILURE_MSG_400003);
  252. }
  253. downVo.setDownloadStatus(0);
  254. if(vo.getStatus() == 1){
  255. downVo.setDownloadStatus(3);
  256. downVo.setDownloadUrl(vo.getUrl());
  257. }
  258. if(vo.getStatus() == 2){
  259. downVo.setDownloadStatus(2);
  260. }
  261. if(vo.getStatus() == 3){
  262. downVo.setDownloadStatus(1);
  263. }
  264. return downVo;
  265. }
  266. /**
  267. * downloadStatus -1下载失败 1下载成功
  268. */
  269. private DownVo SSDownload(String sceneNum,String userName) {
  270. DownVo downVo = new DownVo();
  271. User user = userService.getByUserName(userName);
  272. if(user == null || user.getSsDownloadNumTotal() - user.getSsDownloadNum() <= 0){
  273. downVo.setDownloadStatus(-1);
  274. return downVo;
  275. }
  276. //status :0:正在生成 1,初次生成 2,已经生成直接下载 3,重新生成
  277. SSDownSceneVo vo = laserService.downOfflineScene(sceneNum);
  278. if(vo == null){
  279. throw new BusinessException(ResultCode.FAILURE_CODE_400003,ResultCode.FAILURE_MSG_400003);
  280. }
  281. LambdaUpdateWrapper<User> wrapper = new LambdaUpdateWrapper<>();
  282. wrapper.eq(User::getId,user.getId());
  283. if(!"aws".equals(NacosProperty.uploadType) ){ //深时场景
  284. wrapper.setSql("ss_download_num = ss_download_num + " + 1);
  285. }else {
  286. wrapper.setSql("download_num = download_num + " + 1);
  287. }
  288. userService.update(wrapper);
  289. SceneDownloadLog sceneDownloadLogEntity = new SceneDownloadLog();
  290. sceneDownloadLogEntity.setUserId(user.getId());
  291. sceneDownloadLogEntity.setSceneNum(sceneNum);
  292. sceneDownloadLogEntity.setSceneVersion(0);
  293. sceneDownloadLogEntity.setStatus(0);
  294. sceneDownloadLogEntity.setSysVersion("ss");
  295. sceneDownloadLogService.save(sceneDownloadLogEntity);
  296. downVo.setDownloadStatus(1);
  297. return downVo;
  298. }
  299. public static HashMap<String,Integer> ssNumProcessNumMap = new HashMap<>();
  300. private DownloadProcessVo SSDownloadProcess(String sceneNum) {
  301. DownloadProcessVo downVo = new DownloadProcessVo();
  302. SSDownSceneVo vo = laserService.downOfflineSceneStatus(sceneNum);
  303. if(vo == null){
  304. throw new BusinessException(ResultCode.FAILURE_CODE_400003,ResultCode.FAILURE_MSG_400003);
  305. }
  306. downVo.setStatus(1003);
  307. if(vo.getStatus() == 0 || vo.getStatus() == 2 || vo.getStatus() == 3){ //下载中
  308. ssNumProcessNumMap.merge(sceneNum, 1, Integer::sum);
  309. Integer percent = ssNumProcessNumMap.get(sceneNum);
  310. percent = percent /2;
  311. if(percent >50){
  312. percent = 50;
  313. }
  314. downVo.setStatus(1001);
  315. downVo.setPercent(percent);
  316. }
  317. if(vo.getStatus() == 1){ //下载完成
  318. ssNumProcessNumMap.remove(sceneNum);
  319. downVo.setPercent(100);
  320. downVo.setUrl(vo.getUrl());
  321. downVo.setStatus(1002);
  322. LambdaUpdateWrapper<SceneDownloadLog> wrapper = new LambdaUpdateWrapper<>();
  323. wrapper.eq(SceneDownloadLog::getSceneNum,sceneNum);
  324. wrapper.eq(SceneDownloadLog::getStatus,0);
  325. wrapper.set(SceneDownloadLog::getDownloadUrl,vo.getUrl());
  326. wrapper.set(SceneDownloadLog::getStatus,1);
  327. sceneDownloadLogService.update(wrapper);
  328. }
  329. return downVo;
  330. }
  331. }