DownService.java 18 KB

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