SceneProServiceImpl.java 33 KB

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