SceneProServiceImpl.java 37 KB

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