DownService.java 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  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.RedisKeyUtil;
  12. import com.fdkankan.ucenter.common.constants.ResultCode;
  13. import com.fdkankan.ucenter.constant.CameraConstant;
  14. import com.fdkankan.ucenter.constant.LoginConstant;
  15. import com.fdkankan.ucenter.entity.*;
  16. import com.fdkankan.ucenter.httpClient.service.LaserService;
  17. import com.fdkankan.ucenter.httpClient.vo.SSDownSceneVo;
  18. import com.fdkankan.ucenter.service.*;
  19. import com.fdkankan.ucenter.vo.response.DownVo;
  20. import com.fdkankan.ucenter.vo.response.DownloadProcessVo;
  21. import lombok.extern.slf4j.Slf4j;
  22. import org.apache.commons.lang3.StringUtils;
  23. import org.apache.ibatis.annotations.Case;
  24. import org.springframework.beans.factory.annotation.Autowired;
  25. import org.springframework.stereotype.Service;
  26. import java.util.HashMap;
  27. import java.util.Map;
  28. @Service
  29. @Slf4j
  30. public class DownService implements IDownService {
  31. @Autowired
  32. ISceneProService sceneProService;
  33. @Autowired
  34. ISceneProEditService sceneProEditService;
  35. @Autowired
  36. ISceneDownloadLogService sceneDownloadLogService;
  37. @Autowired
  38. IScenePlusService scenePlusService;
  39. @Autowired
  40. IScenePlusExtService scenePlusExtService;
  41. @Autowired
  42. RedisUtil redisUtil;
  43. @Autowired
  44. ISceneEditInfoService sceneEditInfoService;
  45. @Autowired
  46. IUserService userService;
  47. @Autowired
  48. LaserService laserService;
  49. @Autowired
  50. ICameraTypeService cameraTypeService;
  51. @Override
  52. public DownVo checkDownLoad(String sceneNum,Integer isObj) {
  53. if(StringUtils.isEmpty(sceneNum)){
  54. throw new BusinessException(LoginConstant.FAILURE_CODE_3001, LoginConstant.FAILURE_MSG_3001);
  55. }
  56. ScenePro scenePro = sceneProService.getByNum(sceneNum);
  57. ScenePlus plus = scenePlusService.getByNum(sceneNum);
  58. if(scenePro == null && plus == null){
  59. throw new BusinessException(SceneConstant.FAILURE_CODE_5005, SceneConstant.FAILURE_MSG_5005);
  60. }
  61. Long cameraId = scenePro !=null ? scenePro.getCameraId() : plus.getCameraId();
  62. CameraType cameraType = cameraTypeService.getByCameraId(cameraId);
  63. isObj = isObj == null ?0 :isObj;
  64. log.info("checkDownLoad--cameraType:{},isObj:{}",cameraType,isObj);
  65. if(cameraType != null && cameraType.getIsLaser() == 1 && isObj !=1){ //深时场景
  66. return SSCheckDownload(sceneNum);
  67. }
  68. SceneDownloadLog sceneDownloadLog;
  69. int isUp = 0;
  70. if(scenePro == null){
  71. isUp = 1;
  72. }
  73. Integer sceneVersion = getSceneVersion(scenePro, plus);
  74. sceneDownloadLog = sceneDownloadLogService.getByStatusAndNum(sceneNum,0,isUp);
  75. DownVo downVo = new DownVo();
  76. if(sceneDownloadLog != null){
  77. downVo.setDownloadStatus(1);
  78. return downVo;
  79. }
  80. sceneDownloadLog = sceneDownloadLogService.getByStatusAndNum(sceneNum,1,isUp);
  81. //3下载过,并且没有修改过
  82. if(sceneDownloadLog != null && sceneDownloadLog.getSceneVersion().intValue() == sceneVersion){
  83. downVo.setDownloadStatus(3);
  84. downVo.setDownloadUrl(sceneDownloadLog.getDownloadUrl());
  85. return downVo;
  86. }
  87. //下载过,有更改
  88. if(sceneDownloadLog != null){
  89. String redisKey = RedisKey.PREFIX_DOWNLOAD_PROGRESS;
  90. if(isUp == 1){
  91. redisKey = RedisKey.PREFIX_DOWNLOAD_PROGRESS_V4;
  92. }
  93. downVo.setDownloadStatus(2);
  94. redisUtil.del(String.format(redisKey,sceneNum)); // 清除旧的下载信息
  95. return downVo;
  96. }
  97. return downVo;
  98. }
  99. private Integer getSceneVersion(ScenePro scenePro,ScenePlus scenePlus){
  100. Integer version = 0;
  101. if(scenePro != null){
  102. SceneProEdit proEdit = sceneProEditService.getByProId(scenePro.getId());
  103. if(proEdit == null){
  104. throw new BusinessException(SceneConstant.FAILURE_CODE_5005, SceneConstant.FAILURE_MSG_5005);
  105. }
  106. version = proEdit.getVersion();
  107. }
  108. if(scenePro == null && scenePlus !=null){
  109. String redisKey = String.format(RedisKeyUtil.SCENE_VERSION,scenePlus.getNum());
  110. if(!redisUtil.hasKey(redisKey) || StringUtils.isBlank(redisUtil.get(redisKey))){
  111. SceneEditInfo sceneEditInfo = sceneEditInfoService.getByScenePlusId(scenePlus.getId());
  112. if(sceneEditInfo == null){
  113. throw new BusinessException(SceneConstant.FAILURE_CODE_5005, SceneConstant.FAILURE_MSG_5005);
  114. }
  115. version = sceneEditInfo.getVersion();
  116. }else {
  117. String redisObj = redisUtil.get(redisKey);
  118. JSONObject obj = JSONObject.parseObject(redisObj);
  119. version = obj.getInteger("version");
  120. }
  121. }
  122. return version;
  123. }
  124. @Override
  125. public DownVo down(String sceneNum,String userName,Integer isObj) {
  126. if(StringUtils.isEmpty(sceneNum) || StringUtils.isEmpty(userName)){
  127. throw new BusinessException(LoginConstant.FAILURE_CODE_3001, LoginConstant.FAILURE_MSG_3001);
  128. }
  129. ScenePro scenePro = sceneProService.getByNum(sceneNum);
  130. ScenePlus plus = scenePlusService.getByNum(sceneNum);
  131. if(scenePro == null && plus == null){
  132. throw new BusinessException(SceneConstant.FAILURE_CODE_5005, SceneConstant.FAILURE_MSG_5005);
  133. }
  134. Long cameraId = scenePro !=null ? scenePro.getCameraId() : plus.getCameraId();
  135. CameraType cameraType = cameraTypeService.getByCameraId(cameraId);
  136. isObj = isObj == null ?0 :isObj;
  137. DownVo downVo = new DownVo();
  138. User user = userService.getByUserName(userName);
  139. if(user == null){
  140. downVo.setDownloadStatus(-1);
  141. return downVo;
  142. }
  143. Integer downLoadTotal = 0;
  144. Integer downLoadNum = 0;
  145. if(!"aws".equals(NacosProperty.uploadType) && cameraType != null && cameraType.getIsLaser() == 1){
  146. downLoadTotal = user.getSsDownloadNumTotal();
  147. downLoadNum = user.getSsDownloadNum();
  148. }else {
  149. downLoadTotal = user.getDownloadNumTotal();
  150. downLoadNum = user.getDownloadNum();
  151. }
  152. if(downLoadTotal <= downLoadNum ){
  153. downVo.setDownloadStatus(-1);
  154. return downVo;
  155. }
  156. log.info("down--cameraType:{},isObj:{}",cameraType,isObj);
  157. if(cameraType != null && cameraType.getIsLaser() == 1 && isObj !=1){ //深时场景
  158. return SSDownload(sceneNum,user);
  159. }
  160. Integer sceneVersion = getSceneVersion(scenePro, plus);
  161. Map<String,String> params = new HashMap<>(2);
  162. params.put("type","local");
  163. params.put("sceneNum",sceneNum);
  164. String progressKey = String.format(RedisKey.PREFIX_DOWNLOAD_PROGRESS, sceneNum);
  165. String downloadTaskKey = RedisKey.DOWNLOAD_TASK;
  166. String sysVersion = "v3";
  167. if(scenePro == null){
  168. params.put("num",sceneNum);
  169. progressKey = String.format(RedisKey.PREFIX_DOWNLOAD_PROGRESS_V4, sceneNum);
  170. downloadTaskKey = RedisKey.SCENE_DOWNLOADS_TASK_V4;
  171. sysVersion = "v4";
  172. }
  173. //先删除之前的下载进度
  174. redisUtil.del(progressKey);
  175. //入队
  176. redisUtil.lRightPush(downloadTaskKey, JSONObject.toJSONString(params));
  177. //修改用户的下载次数
  178. LambdaUpdateWrapper<User> wrapper = new LambdaUpdateWrapper<>();
  179. wrapper.eq(User::getId,user.getId());
  180. if(!"aws".equals(NacosProperty.uploadType) && cameraType != null && cameraType.getIsLaser() == 1 ){ //深时场景
  181. wrapper.setSql("ss_download_num = ss_download_num + " + 1);
  182. }else {
  183. wrapper.setSql("download_num = download_num + " + 1);
  184. }
  185. userService.update(wrapper);
  186. SceneDownloadLog sceneDownloadLogEntity = new SceneDownloadLog();
  187. sceneDownloadLogEntity.setUserId(user.getId());
  188. sceneDownloadLogEntity.setSceneNum(sceneNum);
  189. sceneDownloadLogEntity.setSceneVersion(sceneVersion);
  190. sceneDownloadLogEntity.setStatus(0);
  191. sceneDownloadLogEntity.setSysVersion(sysVersion);
  192. sceneDownloadLogService.save(sceneDownloadLogEntity);
  193. downVo.setDownloadStatus(1);
  194. return downVo;
  195. }
  196. @Override
  197. public DownloadProcessVo downloadProcess(Long userId, String sceneNum,Integer isObj) {
  198. if (StringUtils.isEmpty(sceneNum)) {
  199. throw new BusinessException(LoginConstant.FAILURE_CODE_3001, LoginConstant.FAILURE_MSG_3001);
  200. }
  201. ScenePro scenePro = sceneProService.getByNum(sceneNum);
  202. ScenePlus plus = scenePlusService.getByNum(sceneNum);
  203. if(scenePro == null && plus == null){
  204. throw new BusinessException(SceneConstant.FAILURE_CODE_5005, SceneConstant.FAILURE_MSG_5005);
  205. }
  206. Long cameraId = scenePro !=null ? scenePro.getCameraId() : plus.getCameraId();
  207. CameraType cameraType = cameraTypeService.getByCameraId(cameraId);
  208. isObj = isObj == null ?0 :isObj;
  209. log.info("downloadProcess--cameraType:{},isObj:{}",cameraType,isObj);
  210. if(cameraType != null && cameraType.getIsLaser() == 1 && isObj !=1){ //深时场景
  211. return SSDownloadProcess(sceneNum);
  212. }
  213. String redisKey = RedisKey.PREFIX_DOWNLOAD_PROGRESS;
  214. if(scenePro == null){
  215. redisKey = RedisKey.PREFIX_DOWNLOAD_PROGRESS_V4;
  216. }
  217. // 获取下载进度
  218. String result = redisUtil.get(String.format(redisKey,sceneNum));
  219. if(StringUtils.isEmpty(result)){
  220. return new DownloadProcessVo();
  221. }
  222. int isUp = 0;
  223. if(scenePro == null){
  224. isUp = 1;
  225. }
  226. SceneDownloadLog sceneDownloadLog = sceneDownloadLogService.getByStatusAndNum(sceneNum,0,isUp);
  227. DownloadProcessVo downloadProcessVo = JSONObject.parseObject(result, DownloadProcessVo.class);
  228. if(sceneDownloadLog != null){
  229. switch (downloadProcessVo.getStatus()) {
  230. case DownloadStatusEnum.DOWNLOAD_SUCCESS_CODE:
  231. String url = downloadProcessVo.getUrl();
  232. if (!StringUtils.isEmpty(url)) {
  233. sceneDownloadLog.setDownloadUrl(url);
  234. sceneDownloadLog.setStatus(1);
  235. break;
  236. }
  237. case DownloadStatusEnum.DOWNLOAD_FAILED_CODE:
  238. sceneDownloadLog.setStatus(-1);
  239. LambdaUpdateWrapper<User> wrapper = new LambdaUpdateWrapper<>();
  240. wrapper.eq(User::getId,userId);
  241. if(!"aws".equals(NacosProperty.uploadType) && cameraType != null && cameraType.getIsLaser() == 1 ){ //深时场景
  242. wrapper.setSql("ss_download_num = ss_download_num - " + 1);
  243. }else {
  244. wrapper.setSql("download_num = download_num - " + 1);
  245. }
  246. userService.update(wrapper);
  247. break;
  248. }
  249. sceneDownloadLogService.updateById(sceneDownloadLog);
  250. }
  251. return downloadProcessVo;
  252. }
  253. /**
  254. * status :离线包状态是否需要重新生成 0 未生成:1 不需要 2需要 3 生成中
  255. */
  256. private DownVo SSCheckDownload(String sceneNum) {
  257. DownVo downVo = new DownVo();
  258. SSDownSceneVo vo = laserService.downOfflineSceneStatus(sceneNum);
  259. if(vo == null){
  260. throw new BusinessException(ResultCode.FAILURE_CODE_400003,ResultCode.FAILURE_MSG_400003);
  261. }
  262. downVo.setDownloadStatus(0);
  263. if(vo.getStatus() == 1){
  264. downVo.setDownloadStatus(3);
  265. downVo.setDownloadUrl(vo.getUrl());
  266. }
  267. if(vo.getStatus() == 2){
  268. downVo.setDownloadStatus(2);
  269. }
  270. if(vo.getStatus() == 3){
  271. downVo.setDownloadStatus(1);
  272. }
  273. return downVo;
  274. }
  275. /**
  276. * downloadStatus -1下载失败 1下载成功
  277. */
  278. private DownVo SSDownload(String sceneNum,User user) {
  279. DownVo downVo = new DownVo();
  280. //status :0:正在生成 1,初次生成 2,已经生成直接下载 3,重新生成
  281. SSDownSceneVo vo = laserService.downOfflineScene(sceneNum);
  282. if(vo == null){
  283. throw new BusinessException(ResultCode.FAILURE_CODE_400003,ResultCode.FAILURE_MSG_400003);
  284. }
  285. LambdaUpdateWrapper<User> wrapper = new LambdaUpdateWrapper<>();
  286. wrapper.eq(User::getId,user.getId());
  287. if(!"aws".equals(NacosProperty.uploadType) ){ //深时场景
  288. wrapper.setSql("ss_download_num = ss_download_num + " + 1);
  289. }else {
  290. wrapper.setSql("download_num = download_num + " + 1);
  291. }
  292. userService.update(wrapper);
  293. SceneDownloadLog sceneDownloadLogEntity = new SceneDownloadLog();
  294. sceneDownloadLogEntity.setUserId(user.getId());
  295. sceneDownloadLogEntity.setSceneNum(sceneNum);
  296. sceneDownloadLogEntity.setSceneVersion(0);
  297. sceneDownloadLogEntity.setStatus(0);
  298. sceneDownloadLogEntity.setSysVersion("ss");
  299. sceneDownloadLogService.save(sceneDownloadLogEntity);
  300. downVo.setDownloadStatus(1);
  301. return downVo;
  302. }
  303. public static HashMap<String,Integer> ssNumProcessNumMap = new HashMap<>();
  304. private DownloadProcessVo SSDownloadProcess(String sceneNum) {
  305. DownloadProcessVo downVo = new DownloadProcessVo();
  306. SSDownSceneVo vo = laserService.downOfflineSceneStatus(sceneNum);
  307. if(vo == null){
  308. throw new BusinessException(ResultCode.FAILURE_CODE_400003,ResultCode.FAILURE_MSG_400003);
  309. }
  310. downVo.setStatus(1003);
  311. if(vo.getStatus() == 0 || vo.getStatus() == 2 || vo.getStatus() == 3){ //下载中
  312. ssNumProcessNumMap.merge(sceneNum, 1, Integer::sum);
  313. Integer percent = ssNumProcessNumMap.get(sceneNum);
  314. percent = percent /2;
  315. if(percent >50){
  316. percent = 50;
  317. }
  318. downVo.setStatus(1001);
  319. downVo.setPercent(percent);
  320. }
  321. if(vo.getStatus() == 1){ //下载完成
  322. ssNumProcessNumMap.remove(sceneNum);
  323. downVo.setPercent(100);
  324. downVo.setUrl(vo.getUrl());
  325. downVo.setStatus(1002);
  326. LambdaUpdateWrapper<SceneDownloadLog> wrapper = new LambdaUpdateWrapper<>();
  327. wrapper.eq(SceneDownloadLog::getSceneNum,sceneNum);
  328. wrapper.eq(SceneDownloadLog::getStatus,0);
  329. wrapper.set(SceneDownloadLog::getDownloadUrl,vo.getUrl());
  330. wrapper.set(SceneDownloadLog::getStatus,1);
  331. sceneDownloadLogService.update(wrapper);
  332. }
  333. return downVo;
  334. }
  335. @Autowired
  336. ICameraDetailService cameraDetailService;
  337. @Autowired
  338. ISceneColdStorageService sceneColdStorageService;
  339. @Override
  340. public DownVo checkDownLoadE57(String num,String userName) {
  341. checkPerm(num,userName);
  342. DownVo downVo = new DownVo();
  343. SSDownSceneVo vo = laserService.downE57Status(num);
  344. if(vo == null){
  345. throw new BusinessException(ResultCode.FAILURE_CODE_400003,ResultCode.FAILURE_MSG_400003);
  346. }
  347. downVo.setDownloadStatus(0);
  348. if(vo.getStatus() == 1){
  349. downVo.setDownloadStatus(3);
  350. downVo.setDownloadUrl(vo.getUrl());
  351. }
  352. if(vo.getStatus() == 2){
  353. downVo.setDownloadStatus(2);
  354. }
  355. if(vo.getStatus() == 3){
  356. downVo.setDownloadStatus(1);
  357. }
  358. if(vo.getStatus() == -1){
  359. downVo.setDownloadStatus(-1);
  360. }
  361. return downVo;
  362. }
  363. private void checkPerm(String num, String userName) {
  364. User user = userService.getByUserName(userName);
  365. if(user == null){
  366. throw new BusinessException(LoginConstant.FAILURE_CODE_3015,LoginConstant.FAILURE_MSG_3015);
  367. }
  368. ScenePlus scenePlus = scenePlusService.getByNum(num);
  369. if(scenePlus == null ){
  370. throw new BusinessException(ResultCode.FAILURE_CODE_400002,ResultCode.FAILURE_MSG_400002);
  371. }
  372. if(scenePlus.getSceneStatus() != -2){
  373. throw new BusinessException(ResultCode.FAILURE_CODE_400002,ResultCode.FAILURE_MSG_400002);
  374. }
  375. if(scenePlus.getSceneSource() != 4 && scenePlus.getSceneSource() != 5){
  376. throw new BusinessException(ResultCode.FAILURE_CODE_400011,ResultCode.FAILURE_MSG_400011);
  377. }
  378. if(scenePlus.getUserId()!= null && scenePlus.getUserId().equals(user.getId())){
  379. return;
  380. }
  381. if(scenePlus.getCameraId() == null){
  382. throw new BusinessException(ResultCode.FAILURE_CODE_400010,ResultCode.FAILURE_MSG_400010);
  383. }
  384. CameraDetail cameraDetail = cameraDetailService.getByCameraId(scenePlus.getCameraId());
  385. if(cameraDetail == null){
  386. throw new BusinessException(CameraConstant.FAILURE_CODE_6029,CameraConstant.FAILURE_MSG_6029);
  387. }
  388. if(!cameraDetail.getUserId().equals(user.getId())){
  389. throw new BusinessException(ResultCode.FAILURE_CODE_400012,ResultCode.FAILURE_MSG_400012);
  390. }
  391. SceneColdStorage sceneColdStorage = sceneColdStorageService.getByNum(num);
  392. if(sceneColdStorage != null && sceneColdStorage.getState() == 1){
  393. throw new BusinessException(ResultCode.FAILURE_CODE_400013,ResultCode.FAILURE_MSG_400013);
  394. }
  395. }
  396. @Override
  397. public DownVo downE57(String num,String userName) {
  398. checkPerm(num,userName);
  399. DownVo downVo = new DownVo();
  400. //status :0:正在生成 1,初次生成 2,已经生成直接下载 3,重新生成
  401. SSDownSceneVo vo = laserService.downE57(num);
  402. if(vo == null){
  403. throw new BusinessException(ResultCode.FAILURE_CODE_400003,ResultCode.FAILURE_MSG_400003);
  404. }
  405. downVo.setDownloadStatus(1);
  406. return downVo;
  407. }
  408. public static HashMap<String,Integer> ssNumProcessNumE57Map = new HashMap<>();
  409. @Override
  410. public DownloadProcessVo downloadProcessE57(String num,String userName) {
  411. checkPerm(num,userName);
  412. DownloadProcessVo downVo = new DownloadProcessVo();
  413. SSDownSceneVo vo = laserService.downE57Status(num);
  414. if(vo == null){
  415. throw new BusinessException(ResultCode.FAILURE_CODE_400003,ResultCode.FAILURE_MSG_400003);
  416. }
  417. downVo.setStatus(1003);
  418. if(vo.getStatus() == 0 || vo.getStatus() == 2 || vo.getStatus() == 3){ //下载中
  419. ssNumProcessNumE57Map.merge(num, 1, Integer::sum);
  420. Integer percent = ssNumProcessNumE57Map.get(num);
  421. percent = percent /2;
  422. if(percent >50){
  423. percent = 50;
  424. }
  425. downVo.setStatus(1001);
  426. downVo.setPercent(percent);
  427. }
  428. if(vo.getStatus() == 1 ){ //下载完成
  429. ssNumProcessNumE57Map.remove(num);
  430. downVo.setPercent(100);
  431. downVo.setUrl(vo.getUrl());
  432. downVo.setStatus(1002);
  433. }
  434. return downVo;
  435. }
  436. }