SceneProServiceImpl.java 31 KB

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