SceneProServiceImpl.java 41 KB

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