DownService.java 19 KB

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