SceneProServiceImpl.java 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697
  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.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.OssPath;
  14. import com.fdkankan.manage.common.ResultCode;
  15. import com.fdkankan.manage.exception.BusinessException;
  16. import com.fdkankan.manage.common.PageInfo;
  17. import com.fdkankan.common.util.*;
  18. import com.fdkankan.manage.httpClient.client.FdKKClient;
  19. import com.fdkankan.manage.httpClient.service.LaserService;
  20. import com.fdkankan.manage.common.CameraConstant;
  21. import com.fdkankan.manage.entity.*;
  22. import com.fdkankan.manage.mapper.ISceneProMapper;
  23. import com.fdkankan.manage.service.*;
  24. import com.fdkankan.manage.util.SceneStatusUtil;
  25. import com.fdkankan.manage.vo.request.SceneParam;
  26. import com.fdkankan.manage.vo.response.CameraDataVo;
  27. import com.fdkankan.manage.vo.response.GroupByCount;
  28. import com.fdkankan.manage.vo.response.SceneVo;
  29. import lombok.extern.slf4j.Slf4j;
  30. import org.apache.commons.lang3.StringUtils;
  31. import org.springframework.beans.factory.annotation.Autowired;
  32. import org.springframework.beans.factory.annotation.Value;
  33. import org.springframework.stereotype.Service;
  34. import org.springframework.util.ObjectUtils;
  35. import javax.annotation.Resource;
  36. import java.io.File;
  37. import java.util.*;
  38. import java.util.concurrent.CompletableFuture;
  39. import java.util.stream.Collectors;
  40. /**
  41. * <p>
  42. * pro场景表 服务实现类
  43. * </p>
  44. *
  45. * @author
  46. * @since 2022-06-16
  47. */
  48. @Service
  49. @Slf4j
  50. public class SceneProServiceImpl extends ServiceImpl<ISceneProMapper, ScenePro> implements ISceneProService {
  51. @Autowired
  52. private ICameraService cameraService;
  53. @Autowired
  54. ICameraDetailService cameraDetailService;
  55. @Autowired
  56. ISceneService sceneService;
  57. @Autowired
  58. ISceneCopyLogService copyLogService;
  59. @Autowired
  60. IUserIncrementService userIncrementService;
  61. @Autowired
  62. IIncrementTypeService incrementTypeService;
  63. @Autowired
  64. ISceneProEditService sceneProEditService;
  65. @Autowired
  66. FYunFileServiceInterface fYunFileServiceInterface;
  67. @Autowired
  68. IScenePlusService scenePlusService;
  69. @Autowired
  70. IScene3dNumService scene3dNumService;
  71. @Autowired
  72. ISceneCopyLogService sceneCopyLogService;
  73. @Autowired
  74. IScenePlusExtService scenePlusExtService;
  75. @Autowired
  76. LaserService laserService;
  77. @Autowired
  78. FdKKClient fdKKClient;
  79. @Autowired
  80. IFolderSceneService folderSceneService;
  81. @Autowired
  82. IJyUserService jyUserService;
  83. @Autowired
  84. IJySceneUserAuthService jySceneUserAuthService;
  85. @Autowired
  86. ISysUserService sysUserService;
  87. @Autowired
  88. IJySceneAuthService jySceneAuthService;
  89. @Autowired
  90. IJyUserPlatformService jyUserPlatformService;
  91. @Autowired
  92. IUserService userService;
  93. @Override
  94. public ScenePro getByNum(String num) {
  95. LambdaQueryWrapper<ScenePro> wrapper = new LambdaQueryWrapper<>();
  96. wrapper.eq(ScenePro::getNum,num);
  97. wrapper.eq(ScenePro::getIsUpgrade,0);
  98. List<ScenePro> list = this.list(wrapper);
  99. if(list == null || list.size() <=0){
  100. return null;
  101. }
  102. return list.get(0);
  103. }
  104. @Override
  105. public HashMap<Long, Long> getCountGroupByUserId(List<Long> userIdList,Integer isObj) {
  106. HashMap<Long,Long> map = new HashMap<>();
  107. List<GroupByCount> result = this.getBaseMapper().getCountGroupByUserId(userIdList,isObj);
  108. result.forEach(entity ->map.put(entity.getId(),entity.getCount()));
  109. return map;
  110. }
  111. @Override
  112. public HashMap<Long, Long> getCountGroupByCameraId(List<Long> cameraIds) {
  113. HashMap<Long,Long> map = new HashMap<>();
  114. List<GroupByCount> result = this.getBaseMapper().getCountGroupByCameraId(cameraIds);
  115. result.forEach(entity ->map.put(entity.getId(),entity.getCount()));
  116. return map;
  117. }
  118. @Override
  119. public void unbindCamera(Long cameraId) {
  120. LambdaUpdateWrapper<ScenePro> wrapper = new LambdaUpdateWrapper<>();
  121. wrapper.set(ScenePro::getUserId,null)
  122. .eq(ScenePro::getCameraId,cameraId);
  123. wrapper.eq(ScenePro::getIsUpgrade,0);
  124. this.update(wrapper);
  125. }
  126. @Override
  127. public List<ScenePro> getListByCameraId(Long cameraId) {
  128. LambdaQueryWrapper<ScenePro> wrapper = new LambdaQueryWrapper<>();
  129. wrapper.eq(ScenePro::getCameraId,cameraId);
  130. wrapper.eq(ScenePro::getIsUpgrade,0);
  131. return this.list(wrapper);
  132. }
  133. /**
  134. * @param payStatus -2 封存,为 1 解封
  135. */
  136. @Override
  137. public void lockOrUnLockBySpace(CameraDetail cameraDetail, Long cameraId, Integer payStatus) {
  138. LambdaQueryWrapper<ScenePro> wrapper = new LambdaQueryWrapper<>();
  139. LambdaQueryWrapper<ScenePlus> plusWr = new LambdaQueryWrapper<>();
  140. Long totalSpace = cameraDetail.getTotalSpace();
  141. UserIncrement userIncrement = userIncrementService.getByCameraId(cameraId);
  142. if(userIncrement!=null){
  143. IncrementType incrementType = incrementTypeService.getById(userIncrement.getIncrementTypeId());
  144. if(incrementType!=null){
  145. if(incrementType.getCameraCapacity() == -1){
  146. totalSpace = -1L;
  147. }else {
  148. totalSpace = incrementType.getCameraCapacity() * 1024 * 1024L;
  149. }
  150. }
  151. }
  152. log.info("lockOrUnLockBySpace---cameraId:{},payStatus:{},totalSpace:{},useSpace{}",
  153. cameraId,payStatus,totalSpace,cameraDetail.getUsedSpace());
  154. if(payStatus == 1){ //解封,判断用户权益,用户会员权益无限容量
  155. wrapper.orderByAsc(ScenePro::getCreateTime);
  156. plusWr.orderByAsc(ScenePlus::getCreateTime);
  157. wrapper.eq(ScenePro::getPayStatus,-2);
  158. plusWr.eq(ScenePlus::getPayStatus,-2);
  159. }else {
  160. if (totalSpace >= cameraDetail.getUsedSpace()) {
  161. // 总容量大于已使用容量,不予封存
  162. return;
  163. }
  164. wrapper.orderByDesc(ScenePro::getCreateTime);
  165. plusWr.orderByDesc(ScenePlus::getCreateTime);
  166. wrapper.eq(ScenePro::getPayStatus,1);
  167. plusWr.eq(ScenePlus::getPayStatus,1);
  168. }
  169. wrapper.eq(ScenePro::getCameraId,cameraId)
  170. .eq(ScenePro::getIsUpgrade,0);
  171. plusWr.eq(ScenePlus::getCameraId,cameraId);
  172. List<ScenePro> list = this.list(wrapper);
  173. List<ScenePlus> plusList = scenePlusService.list(plusWr);
  174. Long beyondSpace = Math.abs(cameraDetail.getUsedSpace() - totalSpace);
  175. Long accumulateSpace = 0L;
  176. List<Long> lockedIds = new ArrayList<>();
  177. if(payStatus == 1){
  178. Long doSpace = getScenePlusLockedIds(lockedIds, plusList, totalSpace, beyondSpace, accumulateSpace);
  179. beyondSpace -= doSpace;
  180. getSceneLockedIds(lockedIds,list,totalSpace,beyondSpace,accumulateSpace);
  181. }else {
  182. Long doSpace = getSceneLockedIds(lockedIds, list, totalSpace, beyondSpace, accumulateSpace);
  183. beyondSpace -= doSpace;
  184. getScenePlusLockedIds(lockedIds,plusList,totalSpace,beyondSpace,accumulateSpace);
  185. }
  186. lockOrUnLockScenes(lockedIds,payStatus);
  187. }
  188. private Long getSceneLockedIds(List<Long> lockedIds ,List<ScenePro> list,Long count,Long beyondSpace,Long accumulateSpace){
  189. if (list != null && list.size() > 0){
  190. for (ScenePro scenePro : list){
  191. accumulateSpace += scenePro.getSpace();
  192. if (count ==-1 && accumulateSpace.compareTo(beyondSpace) > 0){
  193. return accumulateSpace - scenePro.getSpace();
  194. }
  195. lockedIds.add(scenePro.getId());
  196. }
  197. }
  198. return accumulateSpace;
  199. }
  200. private Long getScenePlusLockedIds(List<Long> lockedIds , List<ScenePlus> list, Long count, Long beyondSpace, Long accumulateSpace){
  201. if (list != null && list.size() > 0){
  202. List<Long> plusIds = list.parallelStream().map(ScenePlus::getId).collect(Collectors.toList());
  203. HashMap<Long, ScenePlusExt> byPlusIds = scenePlusExtService.getByPlusIds(plusIds);
  204. for (ScenePlus scenePlus : list){
  205. ScenePlusExt scenePlusExt = byPlusIds.get(scenePlus.getId());
  206. accumulateSpace += scenePlusExt.getSpace();
  207. if (count ==-1 && accumulateSpace.compareTo(beyondSpace) > 0){
  208. return accumulateSpace - scenePlusExt.getSpace();
  209. }
  210. lockedIds.add(scenePlus.getId());
  211. }
  212. }
  213. return accumulateSpace;
  214. }
  215. // payStatus 为 -2 封存,为 1 解封
  216. private void lockOrUnLockScenes(List<Long> lockedIds,Integer payStatus) {
  217. if (lockedIds == null || lockedIds.size() == 0){
  218. return;
  219. }
  220. LambdaUpdateWrapper<ScenePro> updateWrapper = new LambdaUpdateWrapper<>();
  221. updateWrapper.set(ScenePro::getPayStatus,payStatus)
  222. .eq(ScenePro::getIsUpgrade,0)
  223. .in(ScenePro::getId,lockedIds);
  224. this.update(updateWrapper);
  225. LambdaUpdateWrapper<ScenePlus> updatePlusWrapper = new LambdaUpdateWrapper<>();
  226. updatePlusWrapper.set(ScenePlus::getPayStatus,payStatus)
  227. .in(ScenePlus::getId,lockedIds);
  228. scenePlusService.update(updatePlusWrapper);
  229. this.updateOssStatus(lockedIds,payStatus);
  230. }
  231. /**
  232. * 修改oss status.json中 payStatus
  233. */
  234. private void updateOssStatus(List<Long> lockedIds, Integer payStatus) {
  235. LambdaQueryWrapper<ScenePro> proWr = new LambdaQueryWrapper<>();
  236. proWr.eq(ScenePro::getIsUpgrade,0);
  237. proWr.in(ScenePro::getId,lockedIds);
  238. List<ScenePro> proList = this.list(proWr);
  239. LambdaQueryWrapper<ScenePlus> pluWr = new LambdaQueryWrapper<>();
  240. pluWr.in(ScenePlus::getId,lockedIds);
  241. List<ScenePlus> plusList = scenePlusService.list(pluWr);
  242. for (ScenePro scenePro : proList) {
  243. this.updateOssStatus(String.format(OssPath.v3_statusPath,scenePro.getNum()),payStatus);
  244. }
  245. for (ScenePlus scenePlus : plusList) {
  246. this.updateOssStatus(String.format(OssPath.v4_statusPath,scenePlus.getNum()),payStatus);
  247. }
  248. }
  249. /**
  250. * 从oss中获取文件,并重写,上传替换
  251. */
  252. private void updateOssStatus(String path,Integer payStatus) {
  253. try {
  254. if(!fYunFileServiceInterface.fileExist(path)){
  255. return;
  256. }
  257. String data = fYunFileServiceInterface.getFileContent("4dkankan",path);
  258. if(StringUtils.isBlank(data)){
  259. return;
  260. }
  261. JSONObject jsonObject = JSONObject.parseObject(data);
  262. jsonObject.put("payStatus",payStatus);
  263. String json = JSONUtil.toJsonStr(jsonObject);
  264. FileUtils.writeFile(OssPath.localStatusPath ,json);
  265. log.info("updateOssStatus--localPath:{},ossPath:{}",OssPath.localStatusPath,path);
  266. fYunFileServiceInterface.uploadFile(OssPath.localStatusPath,path);
  267. }catch (Exception e){
  268. e.printStackTrace();
  269. }finally {
  270. FileUtil.del(OssPath.localStatusPath);
  271. }
  272. }
  273. @Override
  274. public PageInfo pageList(SceneParam param) {
  275. // if(param.getType() == 2 || param.getType() == 6){ //深时
  276. // return laserService.pageList(param);
  277. // }
  278. if(param.getType() == 3){ //双目lite
  279. return sceneService.pageList(param);
  280. }
  281. SysUser byId = sysUserService.getById(Long.valueOf(StpUtil.getLoginId().toString()));
  282. JyUser jyUser = jyUserService.getBySysId(byId.getId());
  283. if(byId.getRoleId() != 1L && byId.getRoleId() != 45L && byId.getRoleId() != 48L){
  284. if(jyUser == null){
  285. throw new BusinessException(ResultCode.MISSING_REQUIRED_PARAMETERS);
  286. }
  287. param.setUserId(jyUser.getUserId());
  288. List<JySceneUserAuth> list = jySceneUserAuthService.getByJyUserId(jyUser.getId());
  289. if(list.size() >0){
  290. List<String> numList = list.stream().filter(entity -> !(entity.getLookAuth()==0 && entity.getEditAuth() ==0)).map(JySceneUserAuth::getNum).collect(Collectors.toList());
  291. param.setAuthNumList(numList);
  292. }
  293. }
  294. if(byId.getRoleId() == 45L || byId.getRoleId() == 48L){
  295. List<Long> userIds = new ArrayList<>();
  296. //获取本平台所有用户
  297. if(byId.getRoleId() == 48L){
  298. Integer loginPlatformId = jyUserPlatformService.getLoginPlatformId();
  299. if(loginPlatformId != null) {
  300. List<Integer> jyUserIds = jyUserPlatformService.getUserIdsByFaltform(loginPlatformId);
  301. List<JyUser> jyUsers = jyUserService.listByIds(jyUserIds);
  302. userIds = jyUsers.stream().map(JyUser::getUserId).collect(Collectors.toList());
  303. }
  304. }
  305. if(byId.getRoleId() == 45L){
  306. //获取江门平台用户
  307. List<JyUser> jyUsers = jyUserService.getListByJm();
  308. userIds = jyUsers.stream().map(JyUser::getUserId).collect(Collectors.toList());
  309. }
  310. if(userIds.isEmpty()){
  311. return PageInfo.PageInfo(new Page<>(param.getPageNum(),param.getPageSize()));
  312. }
  313. param.setUserIds(userIds);
  314. }
  315. Page<SceneVo> page = null;
  316. Set<Long> userIds = null;
  317. HashMap<Long,JyUser> userMap = new HashMap<>();
  318. if(param.getType() == 2 || param.getType() == 6) { //深时点云,深光点云
  319. page = laserService.pageList(param);
  320. }else {
  321. page = this.getBaseMapper().pageList(new Page<>(param.getPageNum(),param.getPageSize()),param);
  322. }
  323. userIds = page.getRecords().stream().map(SceneVo::getUserId).collect(Collectors.toSet());
  324. userMap = jyUserService.getMapByUserIds(userIds);
  325. List<SceneVo> records = page.getRecords();
  326. List<String> numList = records.parallelStream().map(SceneVo::getNum).collect(Collectors.toList());
  327. HashMap<String,SceneCopyLog> map = copyLogService.getByNewNumList(numList);
  328. for (SceneVo record : page.getRecords()) {
  329. record.setStatusString(SceneStatusUtil.getStatusString(record));
  330. if(userMap.get(record.getUserId()) != null){
  331. record.setUserName(userMap.get(record.getUserId()).getRyNo());
  332. record.setNickName(userMap.get(record.getUserId()).getRyNickName());
  333. }
  334. if(map !=null ){
  335. SceneCopyLog sceneCopyLog = map.get(record.getNum());
  336. if(sceneCopyLog != null){
  337. record.setCopyTime(sceneCopyLog.getCreateTime());
  338. record.setIsCopy(true);
  339. }
  340. }
  341. if( byId.getRoleId() == 1L || byId.getRoleId() == 45L || byId.getRoleId() == 48L || jyUser.getUserId().equals(record.getUserId())){
  342. record.setIsAuth(true);
  343. record.setEditAuth(1);
  344. record.setIsAuthOther(true);
  345. }else {
  346. JySceneAuth jySceneAuth = jySceneAuthService.getByNum(record.getNum());
  347. if(jySceneAuth != null){
  348. JySceneUserAuth jySceneUserAuth = jySceneUserAuthService.getByNumAndJyUserId(jyUser.getId(), record.getNum(),jySceneAuth.getAuthType());
  349. if(jySceneUserAuth != null && (jySceneUserAuth.getCanAuthLook() == 1 || jySceneUserAuth.getCanEditLook() ==1)){
  350. record.setIsAuth(true);
  351. }
  352. if(jySceneUserAuth != null){
  353. record.setEditAuth(jySceneUserAuth.getEditAuth());
  354. record.setEditEndTime(jySceneUserAuth.getEditEndTime());
  355. }
  356. }
  357. }
  358. }
  359. return PageInfo.PageInfo(page);
  360. }
  361. @Override
  362. public Object sceneCount() {
  363. SysUser byId = sysUserService.getById(Long.valueOf(StpUtil.getLoginId().toString()));
  364. JyUser jyUser = jyUserService.getBySysId(byId.getId());
  365. LambdaQueryWrapper<ScenePlus> wrapper = new LambdaQueryWrapper<>();
  366. if(byId.getRoleId() != 1L && byId.getRoleId() !=45L && byId.getRoleId() != 48){
  367. if(jyUser == null){
  368. throw new BusinessException(ResultCode.MISSING_REQUIRED_PARAMETERS);
  369. }
  370. wrapper.eq(ScenePlus::getUserId,jyUser.getUserId());
  371. List<JySceneUserAuth> list = jySceneUserAuthService.getByJyUserId(jyUser.getId());
  372. if(list.size() >0){
  373. return 1;
  374. }
  375. }
  376. List<Long> userIds = new ArrayList<>();
  377. if(byId.getRoleId() == 45L || byId.getRoleId() == 48L){
  378. //获取本平台所有用户
  379. if(byId.getRoleId() == 48L){
  380. Integer loginPlatformId = jyUserPlatformService.getLoginPlatformId();
  381. if(loginPlatformId != null){
  382. List<Integer> jyUserIds = jyUserPlatformService.getUserIdsByFaltform(loginPlatformId);
  383. List<JyUser> jyUsers = jyUserService.listByIds(jyUserIds);
  384. userIds = jyUsers.stream().map(JyUser::getUserId).collect(Collectors.toList());
  385. }
  386. }
  387. if(byId.getRoleId() == 45L){
  388. //获取江门平台用户
  389. List<JyUser> jyUsers = jyUserService.getListByJm();
  390. userIds = jyUsers.stream().map(JyUser::getUserId).collect(Collectors.toList());
  391. }
  392. if(userIds.isEmpty()){
  393. return 0;
  394. }
  395. wrapper.in(ScenePlus::getUserId,userIds);
  396. }
  397. return scenePlusService.count(wrapper);
  398. }
  399. @Override
  400. public void move(SceneParam param) {
  401. Camera camera = cameraService.getBySnCode(param.getSnCode());
  402. if(camera == null){
  403. throw new BusinessException(ResultCode.CAMERA_SN_NOT_EXIST);
  404. }
  405. CameraDetail cameraDetail = cameraDetailService.getByCameraId(camera.getId());
  406. if(cameraDetail == null){
  407. throw new BusinessException(ResultCode.CAMERA_SN_NOT_EXIST);
  408. }
  409. ScenePro scenePro = this.getByNum(param.getNum());
  410. ScenePlus scenePlus = scenePlusService.getByNum(param.getNum());
  411. if((scenePro == null || scenePro.getCameraId() == null ) && (scenePlus== null || scenePlus.getCameraId() == null)){
  412. throw new BusinessException(SceneConstant.FAILURE_CODE_5005, SceneConstant.FAILURE_MSG_5005);
  413. }
  414. Integer status = scenePro == null ? scenePlus.getSceneStatus() : scenePro.getStatus();
  415. if(status == 0){
  416. throw new BusinessException(SceneConstant.FAILURE_CODE_5037, SceneConstant.FAILURE_MSG_5037);
  417. }
  418. Long sceneCameraId = scenePro == null ? scenePlus.getCameraId() : scenePro.getCameraId();
  419. Long space = scenePro == null ? 0 :scenePro.getSpace();
  420. if(scenePlus !=null){
  421. ScenePlusExt scenePlusExt = scenePlusExtService.getByPlusId(scenePlus.getId());
  422. space = scenePlusExt.getSpace();
  423. }
  424. space = space == null ? 0 :space;
  425. Long newUseSpace = space + cameraDetail.getUsedSpace();
  426. Long totalSpace = cameraDetail.getTotalSpace();
  427. UserIncrement userIncrement = userIncrementService.getByCameraId(camera.getId());
  428. if(userIncrement!=null){
  429. IncrementType incrementType = incrementTypeService.getById(userIncrement.getIncrementTypeId());
  430. if(incrementType!=null){
  431. if(incrementType.getCameraCapacity() == -1){
  432. totalSpace = -1L;
  433. }else {
  434. totalSpace = incrementType.getCameraCapacity() * 1024 * 1024L;
  435. }
  436. }
  437. }
  438. //深时场景无限容量
  439. if(cameraDetail.getType() != 10 && totalSpace !=-1 && newUseSpace > totalSpace){
  440. throw new BusinessException(ResultCode.CAMERA_SPACE_ERROR);
  441. }
  442. Camera oldCamera = cameraService.getById(sceneCameraId);
  443. if(oldCamera == null){
  444. throw new BusinessException(ResultCode.CAMERA_NOT_EXIST);
  445. }
  446. CameraDetail oldCameraDetail = cameraDetailService.getByCameraId(oldCamera.getId());
  447. if(oldCameraDetail == null){
  448. throw new BusinessException(ResultCode.CAMERA_NOT_EXIST);
  449. }
  450. if(oldCameraDetail.getCameraId().equals(cameraDetail.getCameraId())){
  451. throw new BusinessException(ResultCode.CAMERA_NOT_MOVE);
  452. }
  453. if(!oldCameraDetail.getType().equals(cameraDetail.getType())){
  454. throw new BusinessException(ResultCode.CAMERA_TYPE_NOT_ERROR);
  455. }
  456. Long oldUseSpace = oldCameraDetail.getUsedSpace() - space < 0 ? 0 : oldCameraDetail.getUsedSpace() - space;
  457. oldCameraDetail.setUsedSpace(oldUseSpace);
  458. Long subSpace = oldCameraDetail.getTotalSpace() - oldUseSpace;
  459. if(cameraDetail.getType() != 10 && subSpace >0){ //有剩余容量解封容量内场景
  460. this.lockOrUnLockBySpace(oldCameraDetail,oldCameraDetail.getCameraId(),1);
  461. }
  462. cameraDetailService.updateById(oldCameraDetail);
  463. cameraDetail.setUsedSpace(newUseSpace);
  464. cameraDetailService.updateById(cameraDetail);
  465. if(scenePro!=null){
  466. LambdaUpdateWrapper<ScenePro> wrapper = new LambdaUpdateWrapper<>();
  467. wrapper.eq(ScenePro::getId,scenePro.getId());
  468. wrapper.set(ScenePro::getCameraId,camera.getId());
  469. wrapper.set(ScenePro::getUserId,cameraDetail.getUserId());
  470. this.update(wrapper);
  471. //场景迁移到另外的相机清除本身在的文件夹
  472. folderSceneService.delBySceneId(scenePro.getId());
  473. }
  474. if(scenePlus!=null){
  475. LambdaUpdateWrapper<ScenePlus> wrapper = new LambdaUpdateWrapper<>();
  476. wrapper.eq(ScenePlus::getId,scenePlus.getId());
  477. wrapper.set(ScenePlus::getCameraId,camera.getId());
  478. wrapper.set(ScenePlus::getUserId,cameraDetail.getUserId());
  479. scenePlusService.update(wrapper);
  480. folderSceneService.delBySceneId(scenePlus.getId());
  481. }
  482. if(cameraDetail.getType() == 10 || cameraDetail.getType() ==11){ //深时场景
  483. laserService.move(param.getNum(),oldCamera.getSnCode(),camera.getSnCode());
  484. }
  485. }
  486. @Override
  487. public void copy(String sceneNum) throws Exception {
  488. ScenePro scenePro = this.getByNum(sceneNum);
  489. ScenePlus scenePlus = scenePlusService.getByNum(sceneNum);
  490. if((scenePro == null || scenePro.getCameraId() == null ) && (scenePlus== null || scenePlus.getCameraId() == null)){
  491. throw new BusinessException(SceneConstant.FAILURE_CODE_5005, SceneConstant.FAILURE_MSG_5005);
  492. }
  493. // Long cameraId = scenePro == null ? scenePlus.getCameraId() : scenePro.getCameraId();
  494. // Camera camera = cameraService.getById(cameraId);
  495. // if(camera == null){
  496. // throw new BusinessException(CameraConstant.FAILURE_CODE_6029, CameraConstant.FAILURE_MSG_6029);
  497. // }
  498. // CameraDetail detailEntity = cameraDetailService.getByCameraId(cameraId);
  499. // if(detailEntity == null){
  500. // throw new BusinessException(CameraConstant.FAILURE_CODE_6029, CameraConstant.FAILURE_MSG_6029);
  501. // }
  502. // if(detailEntity.getType() == 10 || detailEntity.getType() == 11){
  503. // throw new BusinessException(ResultCode.SS_NO_COPY);
  504. // }
  505. HashMap<String, Object> param = new HashMap<>();
  506. param.put("num",sceneNum);
  507. JSONObject jsonObject = fdKKClient.copyScene(param, "m_a_n_a_g_e");
  508. Integer code = jsonObject.getInteger("code");
  509. if(code != 0){
  510. throw new BusinessException(jsonObject.getInteger("code"),jsonObject.getString("message"));
  511. }
  512. }
  513. @Override
  514. public void deleteByNum(String num) {
  515. Integer sceneSource = null;
  516. Long cameraId = null;
  517. Long space = null;
  518. ScenePro scenePro = this.getByNum(num);
  519. if(scenePro!=null){
  520. cameraId = scenePro.getCameraId();
  521. sceneSource = scenePro.getSceneSource();
  522. space = scenePro.getSpace();
  523. this.removeById(scenePro.getId());
  524. }
  525. Scene scene = sceneService.getByNum(num);
  526. if(scene!=null){
  527. sceneService.removeById(scene.getId());
  528. }
  529. ScenePlus scenePlus = scenePlusService.getByNum(num);
  530. if(scenePlus!=null){
  531. cameraId = scenePlus.getCameraId();
  532. sceneSource = scenePlus.getSceneSource();
  533. ScenePlusExt plusExt = scenePlusExtService.getByPlusId(scenePlus.getId());
  534. if(plusExt != null){
  535. space = plusExt.getSpace();
  536. scenePlusExtService.delByPlus(scenePlus.getId());
  537. }
  538. scenePlusService.removeById(scenePlus.getId());
  539. scenePlusExtService.delByPlus(scenePlus.getId());
  540. }
  541. if(sceneSource != null && (sceneSource == 4 || sceneSource == 5)){
  542. laserService.delete(num);
  543. }
  544. // if(cameraId != null){
  545. // cameraDetailService.addUsedSpace(cameraId,space);
  546. // }
  547. }
  548. @Override
  549. public Long getKkCount(List<String> asList, String startTime) {
  550. LambdaQueryWrapper<ScenePro> wrapper = new LambdaQueryWrapper<>();
  551. wrapper.in(ScenePro::getSceneSource,asList);
  552. wrapper.eq(ScenePro::getIsUpgrade,0);
  553. wrapper.lt(ScenePro::getCreateTime,startTime);
  554. long count = this.count(wrapper);
  555. LambdaQueryWrapper<ScenePlus> wrapper2 = new LambdaQueryWrapper<>();
  556. wrapper2.in(ScenePlus::getSceneSource,asList);
  557. wrapper2.lt(ScenePlus::getCreateTime,startTime);
  558. long count1 = scenePlusService.count(wrapper2);
  559. return count + count1;
  560. }
  561. @Override
  562. public Long getSsCount(List<String> asList, String startTime) {
  563. LambdaQueryWrapper<ScenePro> wrapper = new LambdaQueryWrapper<>();
  564. wrapper.in(ScenePro::getSceneSource,asList);
  565. wrapper.eq(ScenePro::getIsUpgrade,0);
  566. wrapper.lt(ScenePro::getCreateTime,startTime);
  567. LambdaQueryWrapper<ScenePlus> wrapper2 = new LambdaQueryWrapper<>();
  568. wrapper2.in(ScenePlus::getSceneSource,asList);
  569. wrapper2.lt(ScenePlus::getCreateTime,startTime);
  570. long count = scenePlusService.count(wrapper2);
  571. return this.count(wrapper) + count;
  572. }
  573. @Override
  574. public Long getSsObjCount(List<String> asList, String startTime) {
  575. LambdaQueryWrapper<ScenePro> wrapper = new LambdaQueryWrapper<>();
  576. wrapper.in(ScenePro::getSceneSource,asList);
  577. wrapper.eq(ScenePro::getIsUpgrade,0);
  578. if(StringUtils.isNotBlank(startTime)){
  579. wrapper.lt(ScenePro::getCreateTime,startTime);
  580. }
  581. wrapper.eq(ScenePro::getIsObj,1);
  582. LambdaQueryWrapper<ScenePlus> wrapper2 = new LambdaQueryWrapper<>();
  583. wrapper2.in(ScenePlus::getSceneSource,asList);
  584. if(StringUtils.isNotBlank(startTime)){
  585. wrapper2.lt(ScenePlus::getCreateTime,startTime);
  586. }
  587. List<ScenePlus> list = scenePlusService.list(wrapper2);
  588. List<Long> plusIds = list.stream().map(ScenePlus::getId).collect(Collectors.toList());
  589. long count = 0L;
  590. if(plusIds.size() >0){
  591. LambdaQueryWrapper<ScenePlusExt> wrapper3 = new LambdaQueryWrapper<>();
  592. wrapper3.in(ScenePlusExt::getPlusId,plusIds);
  593. wrapper3.eq(ScenePlusExt::getIsObj,1);
  594. count = scenePlusExtService.count(wrapper3);
  595. }
  596. return this.count(wrapper) + count;
  597. }
  598. @Override
  599. public HashMap<String, String> getSnCodeByNumList(Set<String> numList) {
  600. HashMap<String, Long> sceneMap = new HashMap<>();
  601. HashMap<Long, String> cameraMap = new HashMap<>();
  602. HashMap<String, String> snCodeMap = new HashMap<>();
  603. LambdaQueryWrapper<ScenePro> wrapper = new LambdaQueryWrapper<>();
  604. wrapper.eq(ScenePro::getIsUpgrade,0);
  605. wrapper.in(ScenePro::getNum,numList);
  606. List<ScenePro> list = this.list(wrapper);
  607. if(list.size() >0){
  608. list.forEach(entity -> sceneMap.put(entity.getNum(),entity.getCameraId()));
  609. }
  610. LambdaQueryWrapper<ScenePlus> wrapper2 = new LambdaQueryWrapper<>();
  611. wrapper2.in(ScenePlus::getNum,numList);
  612. List<ScenePlus> list2 = scenePlusService.list(wrapper2);
  613. if(list2.size() >0){
  614. list2.forEach(entity -> sceneMap.put(entity.getNum(),entity.getCameraId()));
  615. }
  616. if(sceneMap.size() <=0){
  617. return snCodeMap;
  618. }
  619. Set<Long> cameraIds = list.stream().map(ScenePro::getCameraId).collect(Collectors.toSet());
  620. Set<Long> cameraIds2 = list2.stream().map(ScenePlus::getCameraId).collect(Collectors.toSet());
  621. cameraIds.addAll(cameraIds2);
  622. if(cameraIds.size() >0){
  623. List<Camera> cameraList = cameraService.listByIds(cameraIds);
  624. cameraList.forEach(entity -> cameraMap.put(entity.getId(),entity.getSnCode()));
  625. for (String num : numList) {
  626. Long cameraId = sceneMap.get(num);
  627. if(cameraId != null){
  628. snCodeMap.put(num,cameraMap.get(cameraId));
  629. }
  630. }
  631. }
  632. return snCodeMap;
  633. }
  634. }