DownService.java 19 KB

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