SceneProServiceImpl.java 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764
  1. package com.fdkankan.manage.service.impl;
  2. import cn.dev33.satoken.stp.StpUtil;
  3. import cn.hutool.core.bean.BeanUtil;
  4. import cn.hutool.core.io.FileUtil;
  5. import cn.hutool.json.JSONUtil;
  6. import com.alibaba.fastjson.JSONArray;
  7. import com.alibaba.fastjson.JSONObject;
  8. import com.aliyun.oss.OSS;
  9. import com.aliyun.oss.OSSClientBuilder;
  10. import com.amazonaws.services.s3.AmazonS3;
  11. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  12. import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
  13. import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
  14. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  15. import com.fdkankan.common.constant.SceneConstant;
  16. import com.fdkankan.fyun.face.FYunFileServiceInterface;
  17. import com.fdkankan.manage.common.*;
  18. import com.fdkankan.manage.exception.BusinessException;
  19. import com.fdkankan.common.util.*;
  20. import com.fdkankan.manage.httpClient.client.FdKKClient;
  21. import com.fdkankan.manage.httpClient.service.LaserService;
  22. import com.fdkankan.manage.entity.*;
  23. import com.fdkankan.manage.mapper.ISceneProMapper;
  24. import com.fdkankan.manage.mq.common.MqQueueUtil;
  25. import com.fdkankan.manage.mq.param.ScenePayStatusVo;
  26. import com.fdkankan.manage.mq.param.SceneRestStoreVo;
  27. import com.fdkankan.manage.service.*;
  28. import com.fdkankan.manage.thread.ThreadService;
  29. import com.fdkankan.manage.util.Dateutils;
  30. import com.fdkankan.manage.util.ProvinceUtils;
  31. import com.fdkankan.manage.util.SceneStatusUtil;
  32. import com.fdkankan.manage.vo.request.SceneParam;
  33. import com.fdkankan.manage.vo.response.*;
  34. import com.fdkankan.rabbitmq.util.RabbitMqProducer;
  35. import com.fdkankan.redis.util.RedisUtil;
  36. import lombok.extern.slf4j.Slf4j;
  37. import org.apache.commons.lang3.StringUtils;
  38. import org.springframework.beans.factory.annotation.Autowired;
  39. import org.springframework.beans.factory.annotation.Value;
  40. import org.springframework.stereotype.Service;
  41. import org.springframework.util.ObjectUtils;
  42. import javax.annotation.Resource;
  43. import java.io.File;
  44. import java.util.*;
  45. import java.util.concurrent.CompletableFuture;
  46. import java.util.stream.Collectors;
  47. /**
  48. * <p>
  49. * pro场景表 服务实现类
  50. * </p>
  51. *
  52. * @author
  53. * @since 2022-06-16
  54. */
  55. @Service
  56. @Slf4j
  57. public class SceneProServiceImpl extends ServiceImpl<ISceneProMapper, ScenePro> implements ISceneProService {
  58. @Autowired
  59. private ICameraService cameraService;
  60. @Autowired
  61. ICameraDetailService cameraDetailService;
  62. @Autowired
  63. ISceneService sceneService;
  64. @Autowired
  65. ISceneCopyLogService copyLogService;
  66. @Autowired
  67. IUserIncrementService userIncrementService;
  68. @Autowired
  69. IIncrementTypeService incrementTypeService;
  70. @Autowired
  71. ISceneProEditService sceneProEditService;
  72. @Autowired
  73. FYunFileServiceInterface fYunFileServiceInterface;
  74. @Autowired
  75. IScenePlusService scenePlusService;
  76. @Autowired
  77. IScene3dNumService scene3dNumService;
  78. @Autowired
  79. ISceneCopyLogService sceneCopyLogService;
  80. @Autowired
  81. IScenePlusExtService scenePlusExtService;
  82. @Autowired
  83. LaserService laserService;
  84. @Autowired
  85. FdKKClient fdKKClient;
  86. @Autowired
  87. IFolderSceneService folderSceneService;
  88. @Autowired
  89. ISceneBuildProcessLogService sceneBuildProcessLogService;
  90. @Autowired
  91. ISceneMoveLogService sceneMoveLogService;
  92. @Autowired
  93. ISceneResourceCameraService sceneResourceCameraService;
  94. @Autowired
  95. ISceneDelLogService sceneDelLogService;
  96. @Autowired
  97. RabbitMqProducer rabbitMqProducer;
  98. @Autowired
  99. RedisUtil redisUtil;
  100. @Autowired
  101. ISceneColdStorageService sceneColdStorageService;
  102. @Autowired
  103. ICommonService commonService;
  104. @Override
  105. public ScenePro getByNum(String num) {
  106. LambdaQueryWrapper<ScenePro> wrapper = new LambdaQueryWrapper<>();
  107. wrapper.eq(ScenePro::getNum,num);
  108. wrapper.eq(ScenePro::getIsUpgrade,0);
  109. List<ScenePro> list = this.list(wrapper);
  110. if(list == null || list.size() <=0){
  111. return null;
  112. }
  113. return list.get(0);
  114. }
  115. @Override
  116. public HashMap<Long, Long> getCountGroupByUserId(List<Long> userIdList,Integer isObj) {
  117. HashMap<Long,Long> map = new HashMap<>();
  118. List<GroupByCount> result = this.getBaseMapper().getCountGroupByUserId(userIdList,isObj);
  119. result.forEach(entity ->map.put(entity.getId(),entity.getCount()));
  120. return map;
  121. }
  122. @Override
  123. public HashMap<Long, Long> getCountGroupByCameraId(List<Long> cameraIds) {
  124. HashMap<Long,Long> map = new HashMap<>();
  125. List<GroupByCount> result = this.getBaseMapper().getCountGroupByCameraId(cameraIds);
  126. result.forEach(entity ->map.put(entity.getId(),entity.getCount()));
  127. return map;
  128. }
  129. @Override
  130. public void unbindCamera(Long cameraId) {
  131. LambdaUpdateWrapper<ScenePro> wrapper = new LambdaUpdateWrapper<>();
  132. wrapper.set(ScenePro::getUserId,null)
  133. .eq(ScenePro::getCameraId,cameraId);
  134. wrapper.eq(ScenePro::getIsUpgrade,0);
  135. this.update(wrapper);
  136. }
  137. @Override
  138. public List<ScenePro> getListByCameraId(Long cameraId) {
  139. LambdaQueryWrapper<ScenePro> wrapper = new LambdaQueryWrapper<>();
  140. wrapper.eq(ScenePro::getCameraId,cameraId);
  141. wrapper.eq(ScenePro::getIsUpgrade,0);
  142. return this.list(wrapper);
  143. }
  144. /**
  145. * @param payStatus -2 封存,为 1 解封
  146. */
  147. @Override
  148. public void lockOrUnLockBySpace(CameraDetail cameraDetail, Long cameraId, Integer payStatus) {
  149. ScenePayStatusVo scenePayStatusVo = new ScenePayStatusVo(cameraDetail.getCameraId());
  150. Map<String, Object> map = BeanUtil.beanToMap(scenePayStatusVo);
  151. rabbitMqProducer.sendByWorkQueue(MqQueueUtil.ucenterScenePayStatusQueue, map);
  152. }
  153. @Override
  154. public void lockOrUnLockBySpace(Long cameraId) {
  155. ScenePayStatusVo scenePayStatusVo = new ScenePayStatusVo(cameraId);
  156. Map<String, Object> map = BeanUtil.beanToMap(scenePayStatusVo);
  157. rabbitMqProducer.sendByWorkQueue(MqQueueUtil.ucenterScenePayStatusQueue, map);
  158. }
  159. @Override
  160. public void lockOrUnLockBySpace(ScenePro scenePro, ScenePlus scenePlus,Integer payStatus) {
  161. Long sceneId = scenePro == null ? scenePlus.getId() : scenePro.getId();
  162. ScenePayStatusVo scenePayStatusVo = new ScenePayStatusVo(sceneId,payStatus);
  163. Map<String, Object> map = BeanUtil.beanToMap(scenePayStatusVo);
  164. rabbitMqProducer.sendByWorkQueue(MqQueueUtil.ucenterScenePayStatusQueue, map);
  165. }
  166. /**
  167. * 从oss中获取文件,并重写,上传替换
  168. */
  169. private void updateOssStatus(String path,Integer payStatus,String updateStatus) {
  170. String localPath = String.format(OssPath.localStatusPath, path);
  171. try {
  172. if(!fYunFileServiceInterface.fileExist(path)){
  173. return;
  174. }
  175. String data = fYunFileServiceInterface.getFileContent(path);
  176. if(StringUtils.isBlank(data)){
  177. return;
  178. }
  179. JSONObject jsonObject = JSONObject.parseObject(data);
  180. jsonObject.put(updateStatus,payStatus);
  181. String json = JSONUtil.toJsonStr(jsonObject);
  182. FileUtils.writeFile(localPath,json);
  183. log.info("updateOssStatus--localPath:{},ossPath:{}",localPath,path);
  184. fYunFileServiceInterface.uploadFile(localPath,path);
  185. }catch (Exception e){
  186. e.printStackTrace();
  187. }finally {
  188. FileUtil.del(localPath);
  189. }
  190. }
  191. @Override
  192. public PageInfo pageList(SceneParam param) {
  193. if(param.getType() == 2 || param.getType() == 6){ //深时
  194. return laserService.pageList(param);
  195. }
  196. if(param.getType() == 3){ //双目lite
  197. return sceneService.pageList(param);
  198. }
  199. Page<SceneVo> page = this.getBaseMapper().pageList(new Page<>(param.getPageNum(),param.getPageSize()),param);
  200. List<SceneVo> records = page.getRecords();
  201. HashMap<String,SceneCopyLog> map = null;
  202. if(records.size() >0){
  203. List<String> numList = records.parallelStream().map(SceneVo::getNum).collect(Collectors.toList());
  204. map = copyLogService.getByNewNumList(numList);
  205. }
  206. for (SceneVo record : page.getRecords()) {
  207. record.setStatusString(SceneStatusUtil.getStatusString(record));
  208. if(map !=null ){
  209. SceneCopyLog sceneCopyLog = map.get(record.getNum());
  210. if(sceneCopyLog != null){
  211. record.setCopyTime(sceneCopyLog.getCreateTime());
  212. record.setIsCopy(true);
  213. }
  214. }
  215. if(record.getStatus() == -1){ //计算失败
  216. SceneBuildProcessLog sceneBuildProcessLog = sceneBuildProcessLogService.getByNum(record.getNum());
  217. if(sceneBuildProcessLog != null){
  218. record.setSceneBuildProcessLog(sceneBuildProcessLog);
  219. record.setBuildErrorReason(SceneBuildProcessLogEnum.getReason(sceneBuildProcessLog.getProcess()));
  220. }
  221. }
  222. //{"latitude":22.3672085,"longitude":113.595673,"altitude":9.275519,"horizontalAccuracy":65.0,"verticalAccuracy":10.0,"timestamp":1564381147.2775609}
  223. record.setAddressComponent(commonService.getAddressComponent(record.getGps()));
  224. }
  225. return PageInfo.PageInfo(page);
  226. }
  227. @Override
  228. public synchronized void move(SceneParam param) {
  229. Camera camera = cameraService.getBySnCode(param.getSnCode());
  230. if(camera == null){
  231. throw new BusinessException(ResultCode.CAMERA_SN_NOT_EXIST);
  232. }
  233. CameraDetail cameraDetail = cameraDetailService.getByCameraId(camera.getId());
  234. if(cameraDetail == null){
  235. throw new BusinessException(ResultCode.CAMERA_SN_NOT_EXIST);
  236. }
  237. ScenePro scenePro = this.getByNum(param.getNum());
  238. ScenePlus scenePlus = scenePlusService.getByNum(param.getNum());
  239. if((scenePro == null || scenePro.getCameraId() == null ) && (scenePlus== null || scenePlus.getCameraId() == null)){
  240. throw new BusinessException(SceneConstant.FAILURE_CODE_5005, SceneConstant.FAILURE_MSG_5005);
  241. }
  242. Integer status = scenePro == null ? scenePlus.getSceneStatus() : scenePro.getStatus();
  243. if(status == 0){
  244. throw new BusinessException(SceneConstant.FAILURE_CODE_5037, SceneConstant.FAILURE_MSG_5037);
  245. }
  246. Long sceneCameraId = scenePro == null ? scenePlus.getCameraId() : scenePro.getCameraId();
  247. Long space = null;
  248. space = scenePro == null ? 0 :scenePro.getSpace();
  249. String dataSource = scenePro == null ? null :scenePro.getDataSource();
  250. if(scenePlus !=null){
  251. ScenePlusExt scenePlusExt = scenePlusExtService.getByPlusId(scenePlus.getId());
  252. space = scenePlusExt.getSpace();
  253. dataSource = scenePlusExt.getDataSource();
  254. }
  255. space = space == null ? 0 :space;
  256. cameraDetailService.initSpace(cameraDetail.getCameraId());
  257. Boolean checkSpace = cameraDetailService.checkSpace(cameraDetail, space);
  258. //深时场景无限容量
  259. if(cameraDetail.getType() != 11 && cameraDetail.getType() != 10 && !checkSpace){
  260. throw new BusinessException(ResultCode.CAMERA_SPACE_ERROR);
  261. }
  262. Camera oldCamera = cameraService.getById(sceneCameraId);
  263. if(oldCamera == null){
  264. throw new BusinessException(ResultCode.CAMERA_NOT_EXIST);
  265. }
  266. CameraDetail oldCameraDetail = cameraDetailService.getByCameraId(oldCamera.getId());
  267. if(oldCameraDetail == null){
  268. throw new BusinessException(ResultCode.CAMERA_NOT_EXIST);
  269. }
  270. if(oldCameraDetail.getCameraId().equals(cameraDetail.getCameraId())){
  271. throw new BusinessException(ResultCode.CAMERA_NOT_MOVE);
  272. }
  273. if(!oldCameraDetail.getType().equals(cameraDetail.getType())){
  274. throw new BusinessException(ResultCode.CAMERA_TYPE_NOT_ERROR);
  275. }
  276. // String home = dataSource.replace("/mnt/data", "home")+"/data.fdage";
  277. // if(!fYunFileServiceInterface.fileExist(home)){
  278. // throw new BusinessException(ResultCode.MOVE_ERROR);
  279. // }
  280. HashMap<String, SceneCopyLog> byNewNumList = copyLogService.getByNewNumList(Arrays.asList(param.getNum()));
  281. if(byNewNumList.size() >0){
  282. throw new BusinessException(ResultCode.MOVE_ERROR_COPY);
  283. }
  284. if(cameraDetail.getType() == 10 || cameraDetail.getType() == 11) { //深时场景
  285. //迁移深时 dataSource
  286. File file = new File(dataSource + "_laserData");
  287. if (!file.exists()) {
  288. throw new BusinessException(ResultCode.MOVE_ERROR);
  289. }
  290. }
  291. Long oldUseSpace = oldCameraDetail.getUsedSpace() - space < 0 ? 0 : oldCameraDetail.getUsedSpace() - space;
  292. if("GB".equals(oldCameraDetail.getUnit())){
  293. oldUseSpace = 1L;
  294. }
  295. Long subSpace = oldCameraDetail.getTotalSpace() - oldUseSpace;
  296. if(scenePro!=null){
  297. LambdaUpdateWrapper<ScenePro> wrapper = new LambdaUpdateWrapper<>();
  298. wrapper.eq(ScenePro::getId,scenePro.getId());
  299. wrapper.set(ScenePro::getCameraId,camera.getId());
  300. wrapper.set(ScenePro::getUserId,cameraDetail.getUserId());
  301. this.update(wrapper);
  302. //场景迁移到另外的相机清除本身在的文件夹
  303. folderSceneService.delBySceneId(scenePro.getId());
  304. }
  305. if(scenePlus!=null){
  306. LambdaUpdateWrapper<ScenePlus> wrapper = new LambdaUpdateWrapper<>();
  307. wrapper.eq(ScenePlus::getId,scenePlus.getId());
  308. wrapper.set(ScenePlus::getCameraId,camera.getId());
  309. wrapper.set(ScenePlus::getUserId,cameraDetail.getUserId());
  310. scenePlusService.update(wrapper);
  311. folderSceneService.delBySceneId(scenePlus.getId());
  312. }
  313. //协作相机
  314. sceneResourceCameraService.setCooperationUser(cameraDetail,param.getNum());
  315. // String newDataSource = updateFdageNewDataSource(scenePro, scenePlus, oldCamera.getSnCode(), param.getSnCode(), dataSource);
  316. if(cameraDetail.getType() == 10 || cameraDetail.getType() == 11){ //深时场景
  317. //迁移深时 dataSource
  318. //FileUtil.move(new File(dataSource +"_laserData"),new File(newDataSource+"_laserData"),true);
  319. laserService.move(param.getNum(),oldCamera.getSnCode(),camera.getSnCode(),cameraDetail.getUserId(),dataSource);
  320. }
  321. cameraDetailService.initSpace(oldCameraDetail);
  322. cameraDetailService.initSpace(cameraDetail);
  323. if(oldCameraDetail.getType() != 11 && oldCameraDetail.getType() != 10 && subSpace >0){ //有剩余容量解封容量内场景
  324. this.lockOrUnLockBySpace(oldCameraDetail.getCameraId());
  325. }
  326. if(cameraDetail.getType() != 11 && cameraDetail.getType() != 10){ //有剩余容量解封容量内场景
  327. this.lockOrUnLockBySpace(cameraDetail.getCameraId());
  328. }
  329. }
  330. private JSONObject updateFdageJson(JSONObject jsonObject,String newSnCode){
  331. JSONObject cam = jsonObject.getJSONObject("cam");
  332. if(cam != null){
  333. cam.put("uuid",newSnCode.toLowerCase()); //替换相机sn uuid
  334. }
  335. JSONArray points = jsonObject.getJSONArray("points"); //修改点位中的相机sn
  336. if(points !=null){
  337. for (Object point : points) {
  338. JSONObject jobj = (JSONObject) point;
  339. jobj.put("camera",newSnCode.toLowerCase());
  340. }
  341. }
  342. jsonObject.put("creator",newSnCode.toLowerCase()); //替换相机拍摄sn
  343. return jsonObject;
  344. }
  345. /**
  346. * 兼容,之前迁移,不使用旧snCode 替换新snCode。重写文件
  347. */
  348. public String updateFdageNewDataSource(ScenePro scenePro,ScenePlus scenePlus,String oldSnCode,String newSnCode,String dataSource) {
  349. String localPathFdage = null;
  350. try {
  351. newSnCode = newSnCode.toLowerCase();
  352. String fdagePaht = dataSource.replace("/mnt/data","home") +"/data.fdage";
  353. localPathFdage = String.format(OssPath.localFdagePath,fdagePaht);
  354. String fileContent = fYunFileServiceInterface.getFileContent(fdagePaht);
  355. JSONObject jsonObject = updateFdageJson(JSONObject.parseObject(fileContent), newSnCode);
  356. FileUtils.writeFile(localPathFdage ,JSONObject.toJSONString(jsonObject));
  357. String oldFdagePaht = dataSource.replace("/mnt/data","home") ;
  358. String[] split = oldFdagePaht.split("/");
  359. String newFdagePath = split[0] +"/"+ newSnCode +"/"+ split[2] +"/"+ newSnCode +"_" + split[3].split("_")[1];
  360. String delPath = null;
  361. if(!oldFdagePaht.equals(newFdagePath)){
  362. log.info("updateFdageCopy--复制oss资源--oldFdagePaht:{},newFdagePath:{}",oldFdagePaht,newFdagePath);
  363. fYunFileServiceInterface.copyFileInBucket(oldFdagePaht,newFdagePath);
  364. delPath = oldFdagePaht;
  365. }
  366. fYunFileServiceInterface.uploadFile(localPathFdage,newFdagePath+"/data.fdage");
  367. log.info("updateFdage--localPathFdage:{},newFdagePath:{}",localPathFdage,newFdagePath);
  368. String newDataSource = newFdagePath.replace("home","/mnt/data");
  369. this.updateDataSource(scenePro,scenePlus,newDataSource);
  370. if(delPath != null){
  371. fYunFileServiceInterface.deleteFolder(oldFdagePaht);
  372. }
  373. sceneMoveLogService.saveLog(scenePro,scenePlus,oldSnCode,newSnCode,dataSource,newDataSource, fileContent);
  374. return newDataSource;
  375. }catch (Exception e){
  376. log.error("updateFdage-error:oldSnCode:{},newSnCode:{},dataSource:{}",oldSnCode,newSnCode,dataSource);
  377. log.error("updateFdage-error:",e);
  378. throw new BusinessException(ResultCode.MOVE_SCENE_ERROR);
  379. }finally {
  380. if(localPathFdage != null){
  381. FileUtil.del(localPathFdage);
  382. }
  383. if(scenePro!=null){
  384. LambdaUpdateWrapper<ScenePro> wrapper = new LambdaUpdateWrapper<>();
  385. wrapper.eq(ScenePro::getId,scenePro.getId());
  386. wrapper.set(ScenePro::getStatus,-2);
  387. this.update(wrapper);
  388. this.updateOssStatus(String.format(OssPath.v3_statusPath,scenePro.getNum()),-2,"status");
  389. }
  390. if(scenePlus!=null){
  391. LambdaUpdateWrapper<ScenePlus> wrapper = new LambdaUpdateWrapper<>();
  392. wrapper.eq(ScenePlus::getId,scenePlus.getId());
  393. wrapper.set(ScenePlus::getSceneStatus,-2);
  394. scenePlusService.update(wrapper);
  395. this.updateOssStatus(String.format(OssPath.v4_statusPath,scenePlus.getNum()),-2,"status");
  396. }
  397. }
  398. }
  399. private void updateDataSource(ScenePro scenePro,ScenePlus scenePlus, String newDataSource) {
  400. if(scenePro !=null){
  401. LambdaUpdateWrapper<ScenePro> wrapper = new LambdaUpdateWrapper<>();
  402. wrapper.eq(ScenePro::getId,scenePro.getId());
  403. wrapper.eq(ScenePro::getIsUpgrade,0);
  404. wrapper.set(ScenePro::getDataSource,newDataSource);
  405. this.update(wrapper);
  406. }
  407. if(scenePlus !=null){
  408. LambdaUpdateWrapper<ScenePlusExt> wrapperPlus = new LambdaUpdateWrapper<>();
  409. wrapperPlus.eq(ScenePlusExt::getPlusId,scenePlus.getId());
  410. wrapperPlus.set(ScenePlusExt::getDataSource,newDataSource);
  411. scenePlusExtService.update(wrapperPlus);
  412. }
  413. }
  414. @Override
  415. public void copy(String sceneNum) throws Exception {
  416. ScenePro scenePro = this.getByNum(sceneNum);
  417. ScenePlus scenePlus = scenePlusService.getByNum(sceneNum);
  418. if((scenePro == null || scenePro.getCameraId() == null ) && (scenePlus== null || scenePlus.getCameraId() == null)){
  419. throw new BusinessException(SceneConstant.FAILURE_CODE_5005, SceneConstant.FAILURE_MSG_5005);
  420. }
  421. Long cameraId = scenePro == null ? scenePlus.getCameraId() : scenePro.getCameraId();
  422. Camera camera = cameraService.getById(cameraId);
  423. if(camera == null){
  424. throw new BusinessException(CameraConstant.FAILURE_CODE_6029, CameraConstant.FAILURE_MSG_6029);
  425. }
  426. CameraDetail detailEntity = cameraDetailService.getByCameraId(cameraId);
  427. if(detailEntity == null){
  428. throw new BusinessException(CameraConstant.FAILURE_CODE_6029, CameraConstant.FAILURE_MSG_6029);
  429. }
  430. Integer sceneStatus = scenePro == null ? scenePlus.getSceneStatus() : scenePro.getStatus();
  431. if(sceneStatus != -2){
  432. throw new BusinessException(ResultCode.SCENE_IS_BUILDING);
  433. }
  434. Long needSpace = 0L;
  435. if(scenePro != null){
  436. needSpace = scenePro.getSpace();
  437. }
  438. if(scenePlus != null){
  439. ScenePlusExt scenePlusExt = scenePlusExtService.getByPlusId(scenePlus.getId());
  440. if(scenePlusExt != null && scenePlusExt.getSpace() != null){
  441. needSpace = scenePlusExt.getSpace();
  442. }
  443. }
  444. Boolean checkSpace = cameraDetailService.checkSpace(detailEntity, needSpace);
  445. if(!checkSpace){
  446. throw new BusinessException(ResultCode.CAMERA_SPACE_ERROR);
  447. }
  448. HashMap<String, Object> param = new HashMap<>();
  449. param.put("num",sceneNum);
  450. fdKKClient.copyScene(param,"m_a_n_a_g_e");
  451. }
  452. @Override
  453. public void deleteByNum(String num) {
  454. Integer sceneSource = null;
  455. Long cameraId = null;
  456. ScenePro scenePro = this.getByNum(num);
  457. if(scenePro!=null){
  458. cameraId = scenePro.getCameraId();
  459. sceneSource = scenePro.getSceneSource();
  460. this.removeById(scenePro.getId());
  461. }
  462. Scene scene = sceneService.getByNum(num);
  463. if(scene!=null){
  464. sceneService.removeById(scene.getId());
  465. }
  466. ScenePlus scenePlus = scenePlusService.getByNum(num);
  467. if(scenePlus!=null){
  468. cameraId = scenePlus.getCameraId();
  469. sceneSource = scenePlus.getSceneSource();
  470. ScenePlusExt plusExt = scenePlusExtService.getByPlusId(scenePlus.getId());
  471. if(plusExt != null){
  472. scenePlusExtService.delByPlus(scenePlus.getId());
  473. }
  474. scenePlusService.removeById(scenePlus.getId());
  475. }
  476. if(sceneSource != null && (sceneSource == 4 || sceneSource == 5)){
  477. laserService.delete(num);
  478. }
  479. if(cameraId != null){
  480. cameraDetailService.initSpace(cameraId);
  481. this.lockOrUnLockBySpace(cameraId);
  482. }
  483. sceneDelLogService.saveLog(num, StpUtil.getLoginId());
  484. }
  485. @Override
  486. public Long getKkCount(List<String> asList, String startTime) {
  487. LambdaQueryWrapper<ScenePro> wrapper = new LambdaQueryWrapper<>();
  488. wrapper.in(ScenePro::getSceneSource,asList);
  489. wrapper.eq(ScenePro::getIsUpgrade,0);
  490. wrapper.lt(ScenePro::getCreateTime,startTime);
  491. long count = this.count(wrapper);
  492. LambdaQueryWrapper<ScenePlus> wrapper2 = new LambdaQueryWrapper<>();
  493. wrapper2.in(ScenePlus::getSceneSource,asList);
  494. wrapper2.lt(ScenePlus::getCreateTime,startTime);
  495. long count1 = scenePlusService.count(wrapper2);
  496. return count + count1;
  497. }
  498. @Override
  499. public Long getSsCount(List<String> asList, String startTime) {
  500. LambdaQueryWrapper<ScenePro> wrapper = new LambdaQueryWrapper<>();
  501. wrapper.in(ScenePro::getSceneSource,asList);
  502. wrapper.eq(ScenePro::getIsUpgrade,0);
  503. wrapper.lt(ScenePro::getCreateTime,startTime);
  504. LambdaQueryWrapper<ScenePlus> wrapper2 = new LambdaQueryWrapper<>();
  505. wrapper2.in(ScenePlus::getSceneSource,asList);
  506. wrapper2.lt(ScenePlus::getCreateTime,startTime);
  507. long count = scenePlusService.count(wrapper2);
  508. return this.count(wrapper) + count;
  509. }
  510. @Override
  511. public Long getSsObjCount(List<String> asList, String startTime) {
  512. LambdaQueryWrapper<ScenePro> wrapper = new LambdaQueryWrapper<>();
  513. wrapper.in(ScenePro::getSceneSource,asList);
  514. wrapper.eq(ScenePro::getIsUpgrade,0);
  515. if(StringUtils.isNotBlank(startTime)){
  516. wrapper.lt(ScenePro::getCreateTime,startTime);
  517. }
  518. wrapper.eq(ScenePro::getIsObj,1);
  519. LambdaQueryWrapper<ScenePlus> wrapper2 = new LambdaQueryWrapper<>();
  520. wrapper2.in(ScenePlus::getSceneSource,asList);
  521. if(StringUtils.isNotBlank(startTime)){
  522. wrapper2.lt(ScenePlus::getCreateTime,startTime);
  523. }
  524. List<ScenePlus> list = scenePlusService.list(wrapper2);
  525. List<Long> plusIds = list.stream().map(ScenePlus::getId).collect(Collectors.toList());
  526. long count = 0L;
  527. if(plusIds.size() >0){
  528. LambdaQueryWrapper<ScenePlusExt> wrapper3 = new LambdaQueryWrapper<>();
  529. wrapper3.in(ScenePlusExt::getPlusId,plusIds);
  530. wrapper3.eq(ScenePlusExt::getIsObj,1);
  531. count = scenePlusExtService.count(wrapper3);
  532. }
  533. return this.count(wrapper) + count;
  534. }
  535. @Override
  536. public HashMap<String, String> getSnCodeByNumList(Set<String> numList) {
  537. HashMap<String, Long> sceneMap = new HashMap<>();
  538. HashMap<Long, String> cameraMap = new HashMap<>();
  539. HashMap<String, String> snCodeMap = new HashMap<>();
  540. LambdaQueryWrapper<ScenePro> wrapper = new LambdaQueryWrapper<>();
  541. wrapper.eq(ScenePro::getIsUpgrade,0);
  542. wrapper.in(ScenePro::getNum,numList);
  543. List<ScenePro> list = this.list(wrapper);
  544. if(list.size() >0){
  545. list.forEach(entity -> sceneMap.put(entity.getNum(),entity.getCameraId()));
  546. }
  547. LambdaQueryWrapper<ScenePlus> wrapper2 = new LambdaQueryWrapper<>();
  548. wrapper2.in(ScenePlus::getNum,numList);
  549. List<ScenePlus> list2 = scenePlusService.list(wrapper2);
  550. if(list2.size() >0){
  551. list2.forEach(entity -> sceneMap.put(entity.getNum(),entity.getCameraId()));
  552. }
  553. if(sceneMap.size() <=0){
  554. return snCodeMap;
  555. }
  556. Set<Long> cameraIds = list.stream().map(ScenePro::getCameraId).collect(Collectors.toSet());
  557. Set<Long> cameraIds2 = list2.stream().map(ScenePlus::getCameraId).collect(Collectors.toSet());
  558. cameraIds.addAll(cameraIds2);
  559. if(cameraIds.size() >0){
  560. List<Camera> cameraList = cameraService.listByIds(cameraIds);
  561. cameraList.forEach(entity -> cameraMap.put(entity.getId(),entity.getSnCode()));
  562. for (String num : numList) {
  563. Long cameraId = sceneMap.get(num);
  564. if(cameraId != null){
  565. snCodeMap.put(num,cameraMap.get(cameraId));
  566. }
  567. }
  568. }
  569. return snCodeMap;
  570. }
  571. @Override
  572. public HashMap<Long,Long > getSpaceGroupByCameraId() {
  573. List<GroupByCount> proSum = this.getBaseMapper().getProSpaceGroupByCameraId();
  574. HashMap<Long,Long > map = new HashMap<>();
  575. for (GroupByCount groupByCount : proSum) {
  576. map.merge(groupByCount.getId(), groupByCount.getCount()==null?0:groupByCount.getCount(), Long::sum);
  577. }
  578. List<GroupByCount > plusSum = this.getBaseMapper().getPlusSpaceGroupByCameraId();
  579. for (GroupByCount groupByCount : plusSum) {
  580. map.merge(groupByCount.getId(), groupByCount.getCount()==null?0:groupByCount.getCount(), Long::sum);
  581. }
  582. return map;
  583. }
  584. @Override
  585. public void rebuildScene(String num) {
  586. String dataSource = null;
  587. ScenePro scenePro = this.getByNum(num);
  588. if(scenePro!=null && scenePro.getSceneSource() != 4){
  589. throw new BusinessException(ResultCode.V3_SCENE_REBUILD);
  590. }
  591. ScenePlus scenePlus = scenePlusService.getByNum(num);
  592. if(scenePlus == null && scenePro == null){
  593. throw new BusinessException(ResultCode.SCENE_NOT_EXIST);
  594. }
  595. if(scenePro != null){
  596. dataSource = scenePro.getDataSource();
  597. }
  598. if(scenePlus != null){
  599. ScenePlusExt scenePlusExt= scenePlusExtService.getByPlusId(scenePlus.getId());
  600. if(scenePlusExt!= null){
  601. dataSource = scenePlusExt.getDataSource();
  602. }
  603. }
  604. if(StringUtils.isBlank(dataSource)){
  605. throw new BusinessException(ResultCode.SCENE_REBUILD_ERROR);
  606. }
  607. Long countByNewNum = sceneCopyLogService.getCountByNewNum(num);
  608. if(countByNewNum >0){
  609. throw new BusinessException(ResultCode.COPY_NUM_NOTREBUILD);
  610. }
  611. if(!fYunFileServiceInterface.fileExist(dataSource.replace("/mnt/data","home")+"/data.fdage")){
  612. throw new BusinessException(ResultCode.SCENE_REBUILD_ERROR);
  613. }
  614. HashMap<String,Object> paramMap = new HashMap<>();
  615. paramMap.put("num",num);
  616. try {
  617. JSONObject jsonObject = fdKKClient.rebuildScene(paramMap);
  618. Integer code = jsonObject.getInteger("code");
  619. if(code != 0){
  620. log.error("场景重算失败:{},{}",num,jsonObject);
  621. throw new BusinessException(ResultCode.SCENE_REBUILD_ERROR);
  622. }
  623. }catch (Exception e){
  624. throw new BusinessException(ResultCode.SCENE_REBUILD_ERROR);
  625. }
  626. }
  627. @Override
  628. public void restStore(String num) {
  629. if(StringUtils.isBlank(num)){
  630. throw new BusinessException(ResultCode.MISSING_REQUIRED_PARAMETERS);
  631. }
  632. String redisKey = String.format(RedisKeyUtil.restStoreKey, num);
  633. if(redisUtil.hasKey(redisKey)){
  634. throw new BusinessException(ResultCode.SCENE_STORE_ING);
  635. }
  636. SceneColdStorage sceneColdStorage = sceneColdStorageService.getByNum(num);
  637. if(sceneColdStorage == null){
  638. throw new BusinessException(ResultCode.SCENE_NOT_STORE);
  639. }
  640. String dataSource = scenePlusService.getDataSourceByNum(num);
  641. if(StringUtils.isBlank(dataSource)){
  642. throw new BusinessException(ResultCode.SCENE_NOT_EXIST);
  643. }
  644. String homePath = dataSource.replace("/mnt/data","home");
  645. SceneRestStoreVo sceneRestStoreVo = new SceneRestStoreVo(num,sceneColdStorage.getColdBucket(),sceneColdStorage.getBucket(),homePath);
  646. Map<String, Object> map = BeanUtil.beanToMap(sceneRestStoreVo);
  647. redisUtil.set(redisKey,homePath);
  648. rabbitMqProducer.sendByWorkQueue(MqQueueUtil.sceneRestoreQueue,map);
  649. //threadService.checkRestore(num,sceneColdStorage.getColdBucket(),sceneColdStorage.getBucket(),homePath);
  650. }
  651. @Override
  652. public void restStoreSuccess(String num) {
  653. ScenePro scenePro = this.getByNum(num);
  654. Date time = Dateutils.dateAddOneMonth(new Date(),-11);
  655. if(scenePro!=null ){
  656. LambdaUpdateWrapper<ScenePro> wrapper = new LambdaUpdateWrapper<>();
  657. wrapper.eq(ScenePro::getId,scenePro.getId());
  658. wrapper.set(ScenePro::getCreateTime,Dateutils.getDate(time));
  659. this.update(wrapper);
  660. }
  661. ScenePlus scenePlus = scenePlusService.getByNum(num);
  662. if(scenePlus != null ){
  663. ScenePlusExt scenePlusExt = scenePlusExtService.getByPlusId(scenePlus.getId());
  664. if(scenePlusExt != null){
  665. LambdaUpdateWrapper<ScenePlusExt> wrapper = new LambdaUpdateWrapper<>();
  666. wrapper.eq(ScenePlusExt::getId,scenePlusExt.getId());
  667. wrapper.set(ScenePlusExt::getAlgorithmTime,Dateutils.getDate(time));
  668. scenePlusExtService.update(wrapper);
  669. }
  670. }
  671. }
  672. @Override
  673. public Object getRestStoreProcess(String num) {
  674. if(StringUtils.isBlank(num)){
  675. throw new BusinessException(ResultCode.MISSING_REQUIRED_PARAMETERS);
  676. }
  677. SceneColdStorage sceneColdStorage = sceneColdStorageService.getByNum(num);
  678. if(sceneColdStorage == null){
  679. throw new BusinessException(ResultCode.SCENE_NOT_STORE);
  680. }
  681. String dataSource = scenePlusService.getDataSourceByNum(num);
  682. if(StringUtils.isBlank(dataSource)){
  683. throw new BusinessException(ResultCode.SCENE_NOT_EXIST);
  684. }
  685. String homePath = dataSource.replace("/mnt/data","home");
  686. return fYunFileServiceInterface.getRestoreFolderProcess(sceneColdStorage.getColdBucket(),homePath);
  687. }
  688. @Override
  689. public Long getSpaceSumByCameraId(Long cameraId) {
  690. return this.getBaseMapper().getSpaceSumByCameraId(cameraId);
  691. }
  692. @Override
  693. public Long getCountByCameraId(Long cameraId) {
  694. LambdaQueryWrapper<ScenePro> wrapper = new LambdaQueryWrapper<>();
  695. wrapper.eq(ScenePro::getIsUpgrade,0);
  696. wrapper.eq(ScenePro::getCameraId,cameraId);
  697. wrapper.eq(ScenePro::getStatus,-2);
  698. return this.count(wrapper);
  699. }
  700. }