SceneProServiceImpl.java 36 KB

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