SceneProServiceImpl.java 40 KB

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