SceneProServiceImpl.java 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851
  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. @Override
  166. public void lockOrUnLockBySpace(ScenePro scenePro, ScenePlus scenePlus,Integer payStatus) {
  167. Long sceneId = scenePro == null ? scenePlus.getId() : scenePro.getId();
  168. ScenePayStatusVo scenePayStatusVo = new ScenePayStatusVo(sceneId,payStatus);
  169. Map<String, Object> map = BeanUtil.beanToMap(scenePayStatusVo);
  170. rabbitMqProducer.sendByWorkQueue(MqQueueUtil.ucenterScenePayStatusQueue, map);
  171. }
  172. /**
  173. * 从oss中获取文件,并重写,上传替换
  174. */
  175. private void updateOssStatus(String path,Integer payStatus,String updateStatus) {
  176. String localPath = String.format(OssPath.localStatusPath, path);
  177. try {
  178. if(!fYunFileServiceInterface.fileExist(path)){
  179. return;
  180. }
  181. String data = fYunFileServiceInterface.getFileContent(path);
  182. if(StringUtils.isBlank(data)){
  183. return;
  184. }
  185. JSONObject jsonObject = JSONObject.parseObject(data);
  186. jsonObject.put(updateStatus,payStatus);
  187. String json = JSONUtil.toJsonStr(jsonObject);
  188. FileUtils.writeFile(localPath,json);
  189. log.info("updateOssStatus--localPath:{},ossPath:{}",localPath,path);
  190. fYunFileServiceInterface.uploadFile(localPath,path);
  191. }catch (Exception e){
  192. e.printStackTrace();
  193. }finally {
  194. FileUtil.del(localPath);
  195. }
  196. }
  197. @Override
  198. public PageInfo pageList(SceneParam param) {
  199. if(param.getType() == 2 || param.getType() == 6){ //深时
  200. return laserService.pageList(param);
  201. }
  202. if(param.getType() == 3){ //双目lite
  203. return sceneService.pageList(param);
  204. }
  205. //优化查询,去掉left join
  206. //snCode 查询,userName 查询,companyId查询
  207. //snCode回显,userName回显
  208. if(StringUtils.isNotBlank(param.getSnCode())){
  209. List<Camera> cameraList = cameraService.getLikeBySnCode(param.getSnCode());
  210. List<Long> cameraIds = cameraList.stream().map(Camera::getId).collect(Collectors.toList());
  211. param.setCameraIds(cameraIds);
  212. }
  213. if(StringUtils.isNotBlank(param.getUserName())){
  214. List<User> userList = userService.getLikeByUserName(param.getUserName());
  215. List<Long> userIds = userList.stream().map(User::getId).collect(Collectors.toList());
  216. param.setUserIds(userIds);
  217. }
  218. if(param.getCompanyId() != null){
  219. List<CameraDetail> details = cameraDetailService.getListByCompanyId(param.getCompanyId());
  220. List<Long> cameraIds = details.stream().map(CameraDetail::getCameraId).collect(Collectors.toList());
  221. if(param.getCameraIds() == null || param.getCameraIds().size() <=0 ){
  222. param.setCameraIds(cameraIds);
  223. }else {
  224. List<Long> intersection = cameraIds.stream().filter(param.getCameraIds()::contains).collect(Collectors.toList());
  225. param.setCameraIds(intersection);
  226. }
  227. }
  228. Page<SceneVo> page = this.getBaseMapper().pageList(new Page<>(param.getPageNum(),param.getPageSize()),param);
  229. List<SceneVo> records = page.getRecords();
  230. HashMap<String,SceneCopyLog> map = null;
  231. HashMap<String,SceneColdStorage> coldStorageMap = null;
  232. HashMap<Long, User> userMap = null;
  233. HashMap<Long, Camera> cameraMap = null;
  234. if(!records.isEmpty()){
  235. List<String> numList = records.parallelStream().map(SceneVo::getNum).collect(Collectors.toList());
  236. map = copyLogService.getByNewNumList(numList);
  237. coldStorageMap = sceneColdStorageService.getByNumList(numList);
  238. List<Long> userIds = records.parallelStream().map(SceneVo::getUserId).collect(Collectors.toList());
  239. userMap = userService.getByIds(userIds);
  240. List<Long> cameraIds = records.parallelStream().map(SceneVo::getCameraId).collect(Collectors.toList());
  241. cameraMap = cameraService.getByIds(cameraIds);
  242. }
  243. for (SceneVo record : page.getRecords()) {
  244. record.setStatusString(SceneStatusUtil.getStatusString(record));
  245. if(userMap != null){
  246. User user = userMap.get(record.getUserId());
  247. if(user != null){
  248. record.setUserName(user.getUserName());
  249. }
  250. }
  251. if(cameraMap != null){
  252. Camera camera = cameraMap.get(record.getUserId());
  253. if(camera != null){
  254. record.setSnCode(camera.getSnCode());
  255. }
  256. }
  257. if(map !=null ){
  258. SceneCopyLog sceneCopyLog = map.get(record.getNum());
  259. if(sceneCopyLog != null){
  260. record.setCopyTime(sceneCopyLog.getCreateTime());
  261. record.setIsCopy(true);
  262. }
  263. }
  264. if(coldStorageMap != null){
  265. SceneColdStorage sceneColdStorage = coldStorageMap.get(record.getNum());
  266. if(sceneColdStorage != null){
  267. record.setIsColdStorage(true);
  268. }
  269. }
  270. if(record.getStatus() == -1){ //计算失败
  271. SceneBuildProcessLog sceneBuildProcessLog = sceneBuildProcessLogService.getByNum(record.getNum());
  272. if(sceneBuildProcessLog != null){
  273. record.setSceneBuildProcessLog(sceneBuildProcessLog);
  274. record.setBuildErrorReason(SceneBuildProcessLogEnum.getReason(sceneBuildProcessLog.getProcess()));
  275. }
  276. }
  277. //{"latitude":22.3672085,"longitude":113.595673,"altitude":9.275519,"horizontalAccuracy":65.0,"verticalAccuracy":10.0,"timestamp":1564381147.2775609}
  278. record.setAddressComponent(commonService.getAddressComponent(record.getGps()));
  279. }
  280. return PageInfo.PageInfo(page);
  281. }
  282. @Override
  283. public synchronized void move(SceneParam param) {
  284. Camera camera = cameraService.getBySnCode(param.getSnCode());
  285. if(camera == null){
  286. throw new BusinessException(ResultCode.CAMERA_SN_NOT_EXIST);
  287. }
  288. CameraDetail cameraDetail = cameraDetailService.getByCameraId(camera.getId());
  289. if(cameraDetail == null){
  290. throw new BusinessException(ResultCode.CAMERA_SN_NOT_EXIST);
  291. }
  292. ScenePro scenePro = this.getByNum(param.getNum());
  293. ScenePlus scenePlus = scenePlusService.getByNum(param.getNum());
  294. if((scenePro == null || scenePro.getCameraId() == null ) && (scenePlus== null || scenePlus.getCameraId() == null)){
  295. throw new BusinessException(SceneConstant.FAILURE_CODE_5005, SceneConstant.FAILURE_MSG_5005);
  296. }
  297. Integer status = scenePro == null ? scenePlus.getSceneStatus() : scenePro.getStatus();
  298. if(status == 0){
  299. throw new BusinessException(SceneConstant.FAILURE_CODE_5037, SceneConstant.FAILURE_MSG_5037);
  300. }
  301. Long sceneCameraId = scenePro == null ? scenePlus.getCameraId() : scenePro.getCameraId();
  302. Long space = scenePro == null ? 0 :scenePro.getSpace();
  303. String dataSource = scenePro == null ? null :scenePro.getDataSource();
  304. if(scenePlus !=null){
  305. ScenePlusExt scenePlusExt = scenePlusExtService.getByPlusId(scenePlus.getId());
  306. space = scenePlusExt.getSpace();
  307. dataSource = scenePlusExt.getDataSource();
  308. }
  309. space = space == null ? 0 :space;
  310. cameraDetailService.initSpace(cameraDetail.getCameraId());
  311. Boolean checkSpace = cameraDetailService.checkSpace(cameraDetail, space);
  312. //深时场景无限容量
  313. if(cameraDetail.getType() != 11 && cameraDetail.getType() != 10 && !checkSpace){
  314. throw new BusinessException(ResultCode.CAMERA_SPACE_ERROR);
  315. }
  316. Camera oldCamera = cameraService.getById(sceneCameraId);
  317. if(oldCamera == null){
  318. throw new BusinessException(ResultCode.CAMERA_NOT_EXIST);
  319. }
  320. CameraDetail oldCameraDetail = cameraDetailService.getByCameraId(oldCamera.getId());
  321. if(oldCameraDetail == null){
  322. throw new BusinessException(ResultCode.CAMERA_NOT_EXIST);
  323. }
  324. if(oldCameraDetail.getCameraId().equals(cameraDetail.getCameraId())){
  325. throw new BusinessException(ResultCode.CAMERA_NOT_MOVE);
  326. }
  327. if(!oldCameraDetail.getType().equals(cameraDetail.getType())){
  328. throw new BusinessException(ResultCode.CAMERA_TYPE_NOT_ERROR);
  329. }
  330. // String home = dataSource.replace("/mnt/data", "home")+"/data.fdage";
  331. // if(!fYunFileServiceInterface.fileExist(home)){
  332. // throw new BusinessException(ResultCode.MOVE_ERROR);
  333. // }
  334. // HashMap<String, SceneCopyLog> byNewNumList = copyLogService.getByNewNumList(Arrays.asList(param.getNum()));
  335. // if(byNewNumList.size() >0){
  336. // throw new BusinessException(ResultCode.MOVE_ERROR_COPY);
  337. // }
  338. if((cameraDetail.getType() == 10 || cameraDetail.getType() == 11) && status ==-2) { //深时场景
  339. //迁移深时 dataSource
  340. File file = new File(dataSource + "_laserData");
  341. if (!file.exists()) {
  342. log.info("迁移失败--dataSource:{},sceneNum:{},toSnCode:{}",dataSource + "_laserData",param.getNum(),param.getSnCode());
  343. throw new BusinessException(ResultCode.MOVE_ERROR);
  344. }
  345. }
  346. Long oldUseSpace = oldCameraDetail.getUsedSpace() - space < 0 ? 0 : oldCameraDetail.getUsedSpace() - space;
  347. if("GB".equals(oldCameraDetail.getUnit())){
  348. oldUseSpace = 1L;
  349. }
  350. Long subSpace = oldCameraDetail.getTotalSpace() - oldUseSpace;
  351. if(scenePro!=null){
  352. LambdaUpdateWrapper<ScenePro> wrapper = new LambdaUpdateWrapper<>();
  353. wrapper.eq(ScenePro::getId,scenePro.getId());
  354. wrapper.set(ScenePro::getCameraId,camera.getId());
  355. wrapper.set(ScenePro::getUserId,cameraDetail.getUserId());
  356. // wrapper.set(ScenePro::getStatus,0);
  357. // this.updateOssStatus(String.format(OssPath.v3_statusPath,scenePro.getNum()),0,"status");
  358. this.update(wrapper);
  359. //场景迁移到另外的相机清除本身在的文件夹
  360. folderSceneService.delBySceneId(scenePro.getId());
  361. }
  362. if(scenePlus!=null){
  363. LambdaUpdateWrapper<ScenePlus> wrapper = new LambdaUpdateWrapper<>();
  364. wrapper.eq(ScenePlus::getId,scenePlus.getId());
  365. wrapper.set(ScenePlus::getCameraId,camera.getId());
  366. wrapper.set(ScenePlus::getUserId,cameraDetail.getUserId());
  367. //wrapper.set(ScenePlus::getSceneStatus,0);
  368. //this.updateOssStatus(String.format(OssPath.v4_statusPath,scenePlus.getNum()),0,"status");
  369. scenePlusService.update(wrapper);
  370. folderSceneService.delBySceneId(scenePlus.getId());
  371. }
  372. //协作相机
  373. sceneResourceCameraService.setCooperationUser(cameraDetail,param.getNum());
  374. //String newDataSource = updateFdageNewDataSource(scenePro, scenePlus, oldCamera.getSnCode(), param.getSnCode(), dataSource);
  375. if(cameraDetail.getType() == 10 || cameraDetail.getType() == 11){ //深时场景
  376. //迁移深时 dataSource
  377. //FileUtil.move(new File(dataSource +"_laserData"),new File(newDataSource+"_laserData"),true);
  378. laserService.move(param.getNum(),oldCamera.getSnCode(),camera.getSnCode(),cameraDetail.getUserId(),dataSource);
  379. }
  380. cameraDetailService.initSpace(oldCameraDetail);
  381. cameraDetailService.initSpace(cameraDetail);
  382. if(oldCameraDetail.getType() != 11 && oldCameraDetail.getType() != 10 && subSpace >0){ //有剩余容量解封容量内场景
  383. this.lockOrUnLockBySpace(oldCameraDetail.getCameraId());
  384. }
  385. if(cameraDetail.getType() != 11 && cameraDetail.getType() != 10){ //有剩余容量解封容量内场景
  386. this.lockOrUnLockBySpace(cameraDetail.getCameraId());
  387. }
  388. sceneMoveLogService.saveLog(scenePro,scenePlus,camera.getSnCode(),oldCamera.getSnCode(),dataSource,dataSource, null);
  389. }
  390. private JSONObject updateFdageJson(JSONObject jsonObject,String newSnCode){
  391. JSONObject cam = jsonObject.getJSONObject("cam");
  392. if(cam != null){
  393. cam.put("uuid",newSnCode.toLowerCase()); //替换相机sn uuid
  394. }
  395. JSONArray points = jsonObject.getJSONArray("points"); //修改点位中的相机sn
  396. if(points !=null){
  397. for (Object point : points) {
  398. JSONObject jobj = (JSONObject) point;
  399. jobj.put("camera",newSnCode.toLowerCase());
  400. }
  401. }
  402. jsonObject.put("creator",newSnCode.toLowerCase()); //替换相机拍摄sn
  403. return jsonObject;
  404. }
  405. /**
  406. * 兼容,之前迁移,不使用旧snCode 替换新snCode。重写文件
  407. */
  408. public String updateFdageNewDataSource(ScenePro scenePro,ScenePlus scenePlus,String oldSnCode,String newSnCode,String dataSource) {
  409. String localPathFdage = null;
  410. try {
  411. newSnCode = newSnCode.toLowerCase();
  412. String fdagePaht = dataSource.replace("/mnt/data","home") +"/data.fdage";
  413. if(!fYunFileServiceInterface.fileExist(fdagePaht)){
  414. return dataSource;
  415. }
  416. localPathFdage = String.format(OssPath.localFdagePath,fdagePaht);
  417. String fileContent = fYunFileServiceInterface.getFileContent(fdagePaht);
  418. JSONObject jsonObject = updateFdageJson(JSONObject.parseObject(fileContent), newSnCode);
  419. FileUtils.writeFile(localPathFdage ,JSONObject.toJSONString(jsonObject));
  420. String oldFdagePaht = dataSource.replace("/mnt/data","home") ;
  421. String[] split = oldFdagePaht.split("/");
  422. String newFdagePath = split[0] +"/"+ newSnCode +"/"+ split[2] +"/"+ newSnCode +"_" + split[3].split("_")[1];
  423. String delPath = null;
  424. if(!oldFdagePaht.equals(newFdagePath)){
  425. log.info("updateFdageCopy--复制oss资源--oldFdagePaht:{},newFdagePath:{}",oldFdagePaht,newFdagePath);
  426. fYunFileServiceInterface.copyFileInBucket(oldFdagePaht,newFdagePath);
  427. delPath = oldFdagePaht;
  428. }
  429. fYunFileServiceInterface.uploadFile(localPathFdage,newFdagePath+"/data.fdage");
  430. log.info("updateFdage--localPathFdage:{},newFdagePath:{}",localPathFdage,newFdagePath);
  431. String newDataSource = newFdagePath.replace("home","/mnt/data");
  432. this.updateDataSource(scenePro,scenePlus,newDataSource);
  433. if(delPath != null){
  434. fYunFileServiceInterface.deleteFolder(oldFdagePaht);
  435. }
  436. sceneMoveLogService.saveLog(scenePro,scenePlus,oldSnCode,newSnCode,dataSource,newDataSource, fileContent);
  437. return newDataSource;
  438. }catch (Exception e){
  439. log.error("updateFdage-error:oldSnCode:{},newSnCode:{},dataSource:{}",oldSnCode,newSnCode,dataSource);
  440. log.error("updateFdage-error:",e);
  441. throw new BusinessException(ResultCode.MOVE_SCENE_ERROR);
  442. }finally {
  443. if(localPathFdage != null){
  444. FileUtil.del(localPathFdage);
  445. }
  446. if(scenePro!=null){
  447. LambdaUpdateWrapper<ScenePro> wrapper = new LambdaUpdateWrapper<>();
  448. wrapper.eq(ScenePro::getId,scenePro.getId());
  449. wrapper.set(ScenePro::getStatus,-2);
  450. this.update(wrapper);
  451. this.updateOssStatus(String.format(OssPath.v3_statusPath,scenePro.getNum()),-2,"status");
  452. }
  453. if(scenePlus!=null){
  454. LambdaUpdateWrapper<ScenePlus> wrapper = new LambdaUpdateWrapper<>();
  455. wrapper.eq(ScenePlus::getId,scenePlus.getId());
  456. wrapper.set(ScenePlus::getSceneStatus,-2);
  457. scenePlusService.update(wrapper);
  458. this.updateOssStatus(String.format(OssPath.v4_statusPath,scenePlus.getNum()),-2,"status");
  459. }
  460. }
  461. }
  462. private void updateDataSource(ScenePro scenePro,ScenePlus scenePlus, String newDataSource) {
  463. if(scenePro !=null){
  464. LambdaUpdateWrapper<ScenePro> wrapper = new LambdaUpdateWrapper<>();
  465. wrapper.eq(ScenePro::getId,scenePro.getId());
  466. wrapper.eq(ScenePro::getIsUpgrade,0);
  467. wrapper.set(ScenePro::getDataSource,newDataSource);
  468. this.update(wrapper);
  469. }
  470. if(scenePlus !=null){
  471. LambdaUpdateWrapper<ScenePlusExt> wrapperPlus = new LambdaUpdateWrapper<>();
  472. wrapperPlus.eq(ScenePlusExt::getPlusId,scenePlus.getId());
  473. wrapperPlus.set(ScenePlusExt::getDataSource,newDataSource);
  474. scenePlusExtService.update(wrapperPlus);
  475. }
  476. }
  477. @Override
  478. public void copy(String sceneNum) throws Exception {
  479. ScenePro scenePro = this.getByNum(sceneNum);
  480. ScenePlus scenePlus = scenePlusService.getByNum(sceneNum);
  481. if((scenePro == null || scenePro.getCameraId() == null ) && (scenePlus== null || scenePlus.getCameraId() == null)){
  482. throw new BusinessException(SceneConstant.FAILURE_CODE_5005, SceneConstant.FAILURE_MSG_5005);
  483. }
  484. Long cameraId = scenePro == null ? scenePlus.getCameraId() : scenePro.getCameraId();
  485. Camera camera = cameraService.getById(cameraId);
  486. if(camera == null){
  487. throw new BusinessException(CameraConstant.FAILURE_CODE_6029, CameraConstant.FAILURE_MSG_6029);
  488. }
  489. CameraDetail detailEntity = cameraDetailService.getByCameraId(cameraId);
  490. if(detailEntity == null){
  491. throw new BusinessException(CameraConstant.FAILURE_CODE_6029, CameraConstant.FAILURE_MSG_6029);
  492. }
  493. Integer sceneStatus = scenePro == null ? scenePlus.getSceneStatus() : scenePro.getStatus();
  494. if(sceneStatus != -2){
  495. throw new BusinessException(ResultCode.SCENE_IS_BUILDING);
  496. }
  497. Long needSpace = 0L;
  498. if(scenePro != null){
  499. needSpace = scenePro.getSpace();
  500. }
  501. if(scenePlus != null){
  502. ScenePlusExt scenePlusExt = scenePlusExtService.getByPlusId(scenePlus.getId());
  503. if(scenePlusExt != null && scenePlusExt.getSpace() != null){
  504. needSpace = scenePlusExt.getSpace();
  505. }
  506. }
  507. Boolean checkSpace = cameraDetailService.checkSpace(detailEntity, needSpace);
  508. if(!checkSpace){
  509. throw new BusinessException(ResultCode.CAMERA_SPACE_ERROR);
  510. }
  511. HashMap<String, Object> param = new HashMap<>();
  512. param.put("num",sceneNum);
  513. fdKKClient.copyScene(param,"m_a_n_a_g_e");
  514. }
  515. @Override
  516. public void deleteByNum(String num) {
  517. Integer sceneSource = null;
  518. Long cameraId = null;
  519. ScenePro scenePro = this.getByNum(num);
  520. if(scenePro!=null){
  521. cameraId = scenePro.getCameraId();
  522. sceneSource = scenePro.getSceneSource();
  523. this.removeById(scenePro.getId());
  524. }
  525. Scene scene = sceneService.getByNum(num);
  526. if(scene!=null){
  527. sceneService.removeById(scene.getId());
  528. }
  529. ScenePlus scenePlus = scenePlusService.getByNum(num);
  530. if(scenePlus!=null){
  531. cameraId = scenePlus.getCameraId();
  532. sceneSource = scenePlus.getSceneSource();
  533. ScenePlusExt plusExt = scenePlusExtService.getByPlusId(scenePlus.getId());
  534. if(plusExt != null){
  535. scenePlusExtService.delByPlus(scenePlus.getId());
  536. }
  537. scenePlusService.removeById(scenePlus.getId());
  538. }
  539. if(sceneSource != null && (sceneSource == 4 || sceneSource == 5)){
  540. laserService.delete(num);
  541. }
  542. if(cameraId != null){
  543. cameraDetailService.initSpace(cameraId);
  544. this.lockOrUnLockBySpace(cameraId);
  545. }
  546. sceneDelLogService.saveLog(num, StpUtil.getLoginId());
  547. }
  548. @Override
  549. public Long getKkCount(List<String> asList, String startTime) {
  550. LambdaQueryWrapper<ScenePro> wrapper = new LambdaQueryWrapper<>();
  551. wrapper.in(ScenePro::getSceneSource,asList);
  552. wrapper.eq(ScenePro::getIsUpgrade,0);
  553. wrapper.lt(ScenePro::getCreateTime,startTime);
  554. long count = this.count(wrapper);
  555. LambdaQueryWrapper<ScenePlus> wrapper2 = new LambdaQueryWrapper<>();
  556. wrapper2.in(ScenePlus::getSceneSource,asList);
  557. wrapper2.lt(ScenePlus::getCreateTime,startTime);
  558. long count1 = scenePlusService.count(wrapper2);
  559. return count + count1;
  560. }
  561. @Override
  562. public Long getSsCount(List<String> asList, String startTime) {
  563. LambdaQueryWrapper<ScenePro> wrapper = new LambdaQueryWrapper<>();
  564. wrapper.in(ScenePro::getSceneSource,asList);
  565. wrapper.eq(ScenePro::getIsUpgrade,0);
  566. wrapper.lt(ScenePro::getCreateTime,startTime);
  567. LambdaQueryWrapper<ScenePlus> wrapper2 = new LambdaQueryWrapper<>();
  568. wrapper2.in(ScenePlus::getSceneSource,asList);
  569. wrapper2.lt(ScenePlus::getCreateTime,startTime);
  570. long count = scenePlusService.count(wrapper2);
  571. return this.count(wrapper) + count;
  572. }
  573. @Override
  574. public Long getSsObjCount(List<String> asList, String startTime) {
  575. LambdaQueryWrapper<ScenePro> wrapper = new LambdaQueryWrapper<>();
  576. wrapper.in(ScenePro::getSceneSource,asList);
  577. wrapper.eq(ScenePro::getIsUpgrade,0);
  578. if(StringUtils.isNotBlank(startTime)){
  579. wrapper.lt(ScenePro::getCreateTime,startTime);
  580. }
  581. wrapper.eq(ScenePro::getIsObj,1);
  582. LambdaQueryWrapper<ScenePlus> wrapper2 = new LambdaQueryWrapper<>();
  583. wrapper2.in(ScenePlus::getSceneSource,asList);
  584. if(StringUtils.isNotBlank(startTime)){
  585. wrapper2.lt(ScenePlus::getCreateTime,startTime);
  586. }
  587. List<ScenePlus> list = scenePlusService.list(wrapper2);
  588. List<Long> plusIds = list.stream().map(ScenePlus::getId).collect(Collectors.toList());
  589. long count = 0L;
  590. if(plusIds.size() >0){
  591. LambdaQueryWrapper<ScenePlusExt> wrapper3 = new LambdaQueryWrapper<>();
  592. wrapper3.in(ScenePlusExt::getPlusId,plusIds);
  593. wrapper3.eq(ScenePlusExt::getIsObj,1);
  594. count = scenePlusExtService.count(wrapper3);
  595. }
  596. return this.count(wrapper) + count;
  597. }
  598. @Override
  599. public HashMap<String, String> getSnCodeByNumList(Set<String> numList) {
  600. HashMap<String, Long> sceneMap = new HashMap<>();
  601. HashMap<Long, String> cameraMap = new HashMap<>();
  602. HashMap<String, String> snCodeMap = new HashMap<>();
  603. LambdaQueryWrapper<ScenePro> wrapper = new LambdaQueryWrapper<>();
  604. wrapper.eq(ScenePro::getIsUpgrade,0);
  605. wrapper.in(ScenePro::getNum,numList);
  606. List<ScenePro> list = this.list(wrapper);
  607. if(list.size() >0){
  608. list.forEach(entity -> sceneMap.put(entity.getNum(),entity.getCameraId()));
  609. }
  610. LambdaQueryWrapper<ScenePlus> wrapper2 = new LambdaQueryWrapper<>();
  611. wrapper2.in(ScenePlus::getNum,numList);
  612. List<ScenePlus> list2 = scenePlusService.list(wrapper2);
  613. if(list2.size() >0){
  614. list2.forEach(entity -> sceneMap.put(entity.getNum(),entity.getCameraId()));
  615. }
  616. if(sceneMap.size() <=0){
  617. return snCodeMap;
  618. }
  619. Set<Long> cameraIds = list.stream().map(ScenePro::getCameraId).collect(Collectors.toSet());
  620. Set<Long> cameraIds2 = list2.stream().map(ScenePlus::getCameraId).collect(Collectors.toSet());
  621. cameraIds.addAll(cameraIds2);
  622. if(cameraIds.size() >0){
  623. List<Camera> cameraList = cameraService.listByIds(cameraIds);
  624. cameraList.forEach(entity -> cameraMap.put(entity.getId(),entity.getSnCode()));
  625. for (String num : numList) {
  626. Long cameraId = sceneMap.get(num);
  627. if(cameraId != null){
  628. snCodeMap.put(num,cameraMap.get(cameraId));
  629. }
  630. }
  631. List<CameraDetail> cameraIds1 = cameraDetailService.getByCameraIds(cameraList.stream().map(Camera::getId).collect(Collectors.toList()));
  632. for (CameraDetail cameraDetail : cameraIds1) {
  633. snCodeMap.put(cameraMap.get(cameraDetail.getCameraId()), CameraUtils.getCameraTypeStr(cameraDetail.getType()));
  634. }
  635. }
  636. return snCodeMap;
  637. }
  638. @Override
  639. public HashMap<Long,Long > getSpaceGroupByCameraId() {
  640. List<GroupByCount> proSum = this.getBaseMapper().getProSpaceGroupByCameraId();
  641. HashMap<Long,Long > map = new HashMap<>();
  642. for (GroupByCount groupByCount : proSum) {
  643. map.merge(groupByCount.getId(), groupByCount.getCount()==null?0:groupByCount.getCount(), Long::sum);
  644. }
  645. List<GroupByCount > plusSum = this.getBaseMapper().getPlusSpaceGroupByCameraId();
  646. for (GroupByCount groupByCount : plusSum) {
  647. map.merge(groupByCount.getId(), groupByCount.getCount()==null?0:groupByCount.getCount(), Long::sum);
  648. }
  649. return map;
  650. }
  651. @Override
  652. public void rebuildScene(String num) {
  653. String redisKey2 = String.format(RedisKeyUtil.numShootKey, num);
  654. redisUtil.del(redisKey2);
  655. String dataSource = null;
  656. ScenePro scenePro = this.getByNum(num);
  657. if(scenePro!=null && scenePro.getSceneSource() != 4){
  658. throw new BusinessException(ResultCode.V3_SCENE_REBUILD);
  659. }
  660. ScenePlus scenePlus = scenePlusService.getByNum(num);
  661. if(scenePlus == null && scenePro == null){
  662. throw new BusinessException(ResultCode.SCENE_NOT_EXIST);
  663. }
  664. if(scenePro != null){
  665. dataSource = scenePro.getDataSource();
  666. }
  667. if(scenePlus != null){
  668. ScenePlusExt scenePlusExt= scenePlusExtService.getByPlusId(scenePlus.getId());
  669. if(scenePlusExt!= null){
  670. dataSource = scenePlusExt.getDataSource();
  671. }
  672. }
  673. if(StringUtils.isBlank(dataSource)){
  674. throw new BusinessException(ResultCode.SCENE_REBUILD_ERROR);
  675. }
  676. Long countByNewNum = sceneCopyLogService.getCountByNewNum(num);
  677. if(countByNewNum >0){
  678. throw new BusinessException(ResultCode.COPY_NUM_NOTREBUILD);
  679. }
  680. SceneColdStorage coldStorage = sceneColdStorageService.getByNum(num);
  681. if(coldStorage != null){
  682. throw new BusinessException(ResultCode.SCENE_REBUILD_ERROR2);
  683. }
  684. if(!fYunFileServiceInterface.fileExist(dataSource.replace("/mnt/data","home")+"/data.fdage")){
  685. throw new BusinessException(ResultCode.SCENE_REBUILD_ERROR);
  686. }
  687. HashMap<String,Object> paramMap = new HashMap<>();
  688. paramMap.put("num",num);
  689. try {
  690. JSONObject jsonObject = fdKKClient.rebuildScene(paramMap);
  691. Integer code = jsonObject.getInteger("code");
  692. if(code != 0){
  693. log.error("场景重算失败:{},{}",num,jsonObject);
  694. throw new BusinessException(ResultCode.SCENE_REBUILD_ERROR);
  695. }
  696. }catch (Exception e){
  697. throw new BusinessException(ResultCode.SCENE_REBUILD_ERROR);
  698. }
  699. }
  700. @Override
  701. public void restStore(String num) {
  702. if(StringUtils.isBlank(num)){
  703. throw new BusinessException(ResultCode.MISSING_REQUIRED_PARAMETERS);
  704. }
  705. String redisKey = String.format(RedisKeyUtil.restStoreKey, num);
  706. if(redisUtil.hasKey(redisKey)){
  707. throw new BusinessException(ResultCode.SCENE_STORE_ING);
  708. }
  709. SceneColdStorage sceneColdStorage = sceneColdStorageService.getByNum(num);
  710. if(sceneColdStorage == null){
  711. throw new BusinessException(ResultCode.SCENE_NOT_STORE);
  712. }
  713. String dataSource = scenePlusService.getDataSourceByNum(num);
  714. if(StringUtils.isBlank(dataSource)){
  715. throw new BusinessException(ResultCode.SCENE_NOT_EXIST);
  716. }
  717. String homePath = dataSource.replace("/mnt/data","home");
  718. SceneRestStoreVo sceneRestStoreVo = new SceneRestStoreVo(num,sceneColdStorage.getColdBucket(),sceneColdStorage.getBucket(),homePath);
  719. Map<String, Object> map = BeanUtil.beanToMap(sceneRestStoreVo);
  720. redisUtil.set(redisKey,homePath);
  721. rabbitMqProducer.sendByWorkQueue(MqQueueUtil.sceneRestoreQueue,map);
  722. //threadService.checkRestore(num,sceneColdStorage.getColdBucket(),sceneColdStorage.getBucket(),homePath);
  723. }
  724. @Override
  725. public void restStoreSuccess(String num) {
  726. Integer sceneResource = null;
  727. ScenePro scenePro = this.getByNum(num);
  728. Date time = Dateutils.dateAddOneMonth(new Date(),-11);
  729. String date = Dateutils.getDate(time);
  730. if(scenePro!=null ){
  731. sceneResource = scenePro.getSceneSource();
  732. LambdaUpdateWrapper<ScenePro> wrapper = new LambdaUpdateWrapper<>();
  733. wrapper.eq(ScenePro::getId,scenePro.getId());
  734. wrapper.set(ScenePro::getCreateTime,date);
  735. this.update(wrapper);
  736. }
  737. ScenePlus scenePlus = scenePlusService.getByNum(num);
  738. if(scenePlus != null ){
  739. sceneResource = scenePlus.getSceneSource();
  740. ScenePlusExt scenePlusExt = scenePlusExtService.getByPlusId(scenePlus.getId());
  741. if(scenePlusExt != null){
  742. LambdaUpdateWrapper<ScenePlusExt> wrapper = new LambdaUpdateWrapper<>();
  743. wrapper.eq(ScenePlusExt::getId,scenePlusExt.getId());
  744. wrapper.set(ScenePlusExt::getAlgorithmTime,date);
  745. scenePlusExtService.update(wrapper);
  746. }
  747. }
  748. //深时场景通知修改计算完成时间
  749. if(sceneResource != null && (sceneResource == 4 || sceneResource == 5)){
  750. HashMap<String,String> map = new HashMap<>();
  751. map.put("sceneCode",num);
  752. map.put("algorithmTime",date);
  753. rabbitMqProducer.sendByWorkQueue(MqQueueUtil.laserUnfreezeScene,map);
  754. }
  755. }
  756. @Override
  757. public Object getRestStoreProcess(String num) {
  758. if(StringUtils.isBlank(num)){
  759. throw new BusinessException(ResultCode.MISSING_REQUIRED_PARAMETERS);
  760. }
  761. SceneColdStorage sceneColdStorage = sceneColdStorageService.getByNum(num);
  762. if(sceneColdStorage == null){
  763. throw new BusinessException(ResultCode.SCENE_NOT_STORE);
  764. }
  765. String dataSource = scenePlusService.getDataSourceByNum(num);
  766. if(StringUtils.isBlank(dataSource)){
  767. throw new BusinessException(ResultCode.SCENE_NOT_EXIST);
  768. }
  769. String homePath = dataSource.replace("/mnt/data","home");
  770. return fYunFileServiceInterface.getRestoreFolderProcess(sceneColdStorage.getColdBucket(),homePath);
  771. }
  772. @Override
  773. public Long getSpaceSumByCameraId(Long cameraId) {
  774. return this.getBaseMapper().getSpaceSumByCameraId(cameraId);
  775. }
  776. @Override
  777. public Long getCountByCameraId(Long cameraId) {
  778. LambdaQueryWrapper<ScenePro> wrapper = new LambdaQueryWrapper<>();
  779. wrapper.eq(ScenePro::getIsUpgrade,0);
  780. wrapper.eq(ScenePro::getCameraId,cameraId);
  781. wrapper.eq(ScenePro::getStatus,-2);
  782. return this.count(wrapper);
  783. }
  784. }