SceneProServiceImpl.java 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  1. package com.fdkankan.manage.service.impl;
  2. import cn.hutool.core.io.FileUtil;
  3. import cn.hutool.json.JSONUtil;
  4. import com.alibaba.fastjson.JSONObject;
  5. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  6. import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
  7. import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
  8. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  9. import com.fdkankan.common.constant.ConstantFilePath;
  10. import com.fdkankan.common.constant.SceneConstant;
  11. import com.fdkankan.common.constant.UploadFilePath;
  12. import com.fdkankan.manage.common.OssPath;
  13. import com.fdkankan.manage.common.ResultCode;
  14. import com.fdkankan.manage.exception.BusinessException;
  15. import com.fdkankan.common.response.PageInfo;
  16. import com.fdkankan.common.util.*;
  17. import com.fdkankan.manage.httpClient.client.FdKKClient;
  18. import com.fdkankan.manage.httpClient.service.LaserService;
  19. import com.fdkankan.manage.util.CreateObjUtilSelf;
  20. import com.fdkankan.manage.util.MangerUploadToOssUtil;
  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 org.apache.commons.lang3.StringUtils;
  32. import org.springframework.beans.factory.annotation.Autowired;
  33. import org.springframework.beans.factory.annotation.Value;
  34. import org.springframework.stereotype.Service;
  35. import org.springframework.util.ObjectUtils;
  36. import javax.annotation.Resource;
  37. import java.io.File;
  38. import java.util.*;
  39. import java.util.concurrent.CompletableFuture;
  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. ISceneProEditService sceneProEditService;
  64. @Autowired
  65. private MangerUploadToOssUtil mangeUploadToOssUtil;
  66. @Autowired
  67. IScenePlusService scenePlusService;
  68. @Autowired
  69. IScene3dNumService scene3dNumService;
  70. @Autowired
  71. ISceneCopyLogService sceneCopyLogService;
  72. @Autowired
  73. IScenePlusExtService scenePlusExtService;
  74. @Autowired
  75. LaserService laserService;
  76. @Resource
  77. FdKKClient fdKKClient;
  78. @Override
  79. public ScenePro getByNum(String num) {
  80. LambdaQueryWrapper<ScenePro> wrapper = new LambdaQueryWrapper<>();
  81. wrapper.eq(ScenePro::getNum,num);
  82. wrapper.eq(ScenePro::getIsUpgrade,0);
  83. List<ScenePro> list = this.list(wrapper);
  84. if(list == null || list.size() <=0){
  85. return null;
  86. }
  87. return list.get(0);
  88. }
  89. @Override
  90. public HashMap<Long, Long> getCountGroupByUserId(List<Long> userIdList) {
  91. HashMap<Long,Long> map = new HashMap<>();
  92. List<GroupByCount> result = this.getBaseMapper().getCountGroupByUserId(userIdList);
  93. result.forEach(entity ->map.put(entity.getId(),entity.getCount()));
  94. return map;
  95. }
  96. @Override
  97. public HashMap<Long, Long> getCountGroupByCameraId(List<Long> cameraIds) {
  98. HashMap<Long,Long> map = new HashMap<>();
  99. List<GroupByCount> result = this.getBaseMapper().getCountGroupByCameraId(cameraIds);
  100. result.forEach(entity ->map.put(entity.getId(),entity.getCount()));
  101. return map;
  102. }
  103. @Override
  104. public void unbindCamera(Long cameraId) {
  105. LambdaUpdateWrapper<ScenePro> wrapper = new LambdaUpdateWrapper<>();
  106. wrapper.set(ScenePro::getUserId,null)
  107. .eq(ScenePro::getCameraId,cameraId);
  108. wrapper.eq(ScenePro::getIsUpgrade,0);
  109. this.update(wrapper);
  110. }
  111. @Override
  112. public List<ScenePro> getListByCameraId(Long cameraId) {
  113. LambdaQueryWrapper<ScenePro> wrapper = new LambdaQueryWrapper<>();
  114. wrapper.eq(ScenePro::getCameraId,cameraId);
  115. wrapper.eq(ScenePro::getIsUpgrade,0);
  116. return this.list(wrapper);
  117. }
  118. /**
  119. * @param payStatus -2 封存,为 1 解封
  120. */
  121. @Override
  122. public void lockOrUnLockBySpace(CameraDetail cameraDetail, Long cameraId, Integer payStatus) {
  123. LambdaQueryWrapper<ScenePro> wrapper = new LambdaQueryWrapper<>();
  124. LambdaQueryWrapper<ScenePlus> plusWr = new LambdaQueryWrapper<>();
  125. Long count = 0L;
  126. if(payStatus == 1){ //解封,判断用户权益,用户会员权益无限容量
  127. count = userIncrementService.getValidCountByCameraId(cameraId);
  128. wrapper.orderByAsc(ScenePro::getCreateTime);
  129. plusWr.orderByAsc(ScenePlus::getCreateTime);
  130. wrapper.eq(ScenePro::getPayStatus,-2);
  131. plusWr.eq(ScenePlus::getPayStatus,-2);
  132. }else {
  133. wrapper.orderByDesc(ScenePro::getCreateTime);
  134. plusWr.orderByDesc(ScenePlus::getCreateTime);
  135. wrapper.eq(ScenePro::getPayStatus,1);
  136. plusWr.eq(ScenePlus::getPayStatus,1);
  137. }
  138. wrapper.eq(ScenePro::getCameraId,cameraId)
  139. .eq(ScenePro::getIsUpgrade,0);
  140. plusWr.eq(ScenePlus::getCameraId,cameraId);
  141. List<ScenePro> list = this.list(wrapper);
  142. List<ScenePlus> plusList = scenePlusService.list(plusWr);
  143. Long beyondSpace = Math.abs(cameraDetail.getUsedSpace() - cameraDetail.getTotalSpace());
  144. Long accumulateSpace = 0L;
  145. List<Long> lockedIds = new ArrayList<>();
  146. if(payStatus == 1){
  147. getScenePlusLockedIds(lockedIds,plusList,count,beyondSpace,accumulateSpace);
  148. getSceneLockedIds(lockedIds,list,count,beyondSpace,accumulateSpace);
  149. }else {
  150. getSceneLockedIds(lockedIds,list,count,beyondSpace,accumulateSpace);
  151. getScenePlusLockedIds(lockedIds,plusList,count,beyondSpace,accumulateSpace);
  152. }
  153. lockOrUnLockScenes(lockedIds,payStatus);
  154. }
  155. private void getSceneLockedIds(List<Long> lockedIds ,List<ScenePro> list,Long count,Long beyondSpace,Long accumulateSpace){
  156. if (list != null && list.size() > 0){
  157. for (ScenePro scenePro : list){
  158. accumulateSpace += scenePro.getSpace();
  159. if (count ==0 && accumulateSpace.compareTo(beyondSpace) > 0){
  160. break;
  161. }
  162. lockedIds.add(scenePro.getId());
  163. }
  164. }
  165. }
  166. private void getScenePlusLockedIds(List<Long> lockedIds ,List<ScenePlus> list,Long count,Long beyondSpace,Long accumulateSpace){
  167. if (list != null && list.size() > 0){
  168. List<Long> plusIds = list.parallelStream().map(ScenePlus::getId).collect(Collectors.toList());
  169. HashMap<Long, ScenePlusExt> byPlusIds = scenePlusExtService.getByPlusIds(plusIds);
  170. for (ScenePlus scenePlus : list){
  171. ScenePlusExt scenePlusExt = byPlusIds.get(scenePlus.getId());
  172. accumulateSpace += scenePlusExt.getSpace();
  173. if (count ==0 && accumulateSpace.compareTo(beyondSpace) > 0){
  174. break;
  175. }
  176. lockedIds.add(scenePlus.getId());
  177. }
  178. }
  179. }
  180. // payStatus 为 -2 封存,为 1 解封
  181. private void lockOrUnLockScenes(List<Long> lockedIds,Integer payStatus) {
  182. if (lockedIds == null || lockedIds.size() == 0){
  183. return;
  184. }
  185. LambdaUpdateWrapper<ScenePro> updateWrapper = new LambdaUpdateWrapper<>();
  186. updateWrapper.set(ScenePro::getPayStatus,payStatus)
  187. .eq(ScenePro::getIsUpgrade,0)
  188. .in(ScenePro::getId,lockedIds);
  189. this.update(updateWrapper);
  190. LambdaUpdateWrapper<ScenePlus> updatePlusWrapper = new LambdaUpdateWrapper<>();
  191. updatePlusWrapper.set(ScenePlus::getPayStatus,payStatus)
  192. .in(ScenePlus::getId,lockedIds);
  193. scenePlusService.update(updatePlusWrapper);
  194. this.updateOssStatus(lockedIds,payStatus);
  195. }
  196. /**
  197. * 修改oss status.json中 payStatus
  198. */
  199. private void updateOssStatus(List<Long> lockedIds, Integer payStatus) {
  200. LambdaQueryWrapper<ScenePro> proWr = new LambdaQueryWrapper<>();
  201. proWr.eq(ScenePro::getIsUpgrade,0);
  202. proWr.in(ScenePro::getId,lockedIds);
  203. List<ScenePro> proList = this.list(proWr);
  204. LambdaQueryWrapper<ScenePlus> pluWr = new LambdaQueryWrapper<>();
  205. pluWr.in(ScenePlus::getId,lockedIds);
  206. List<ScenePlus> plusList = scenePlusService.list(pluWr);
  207. for (ScenePro scenePro : proList) {
  208. this.updateOssStatus(String.format(OssPath.v3_statusPath,scenePro.getNum()),payStatus);
  209. }
  210. for (ScenePlus scenePlus : plusList) {
  211. this.updateOssStatus(String.format(OssPath.v4_statusPath,scenePlus.getNum()),payStatus);
  212. }
  213. }
  214. /**
  215. * 从oss中获取文件,并重写,上传替换
  216. */
  217. private void updateOssStatus(String path,Integer payStatus) {
  218. try {
  219. if(!mangeUploadToOssUtil.existKey(path)){
  220. return;
  221. }
  222. String data = mangeUploadToOssUtil.getObjectContent("4dkankan",path);
  223. if(StringUtils.isBlank(data)){
  224. return;
  225. }
  226. JSONObject jsonObject = JSONObject.parseObject(data);
  227. jsonObject.put("payStatus",payStatus);
  228. String json = JSONUtil.toJsonStr(jsonObject);
  229. FileUtils.writeFile(OssPath.localStatusPath ,json);
  230. log.info("updateOssStatus--localPath:{},ossPath:{}",OssPath.localStatusPath,path);
  231. mangeUploadToOssUtil.upload(OssPath.localStatusPath,path);
  232. }catch (Exception e){
  233. e.printStackTrace();
  234. }finally {
  235. FileUtil.del(OssPath.localStatusPath);
  236. }
  237. }
  238. @Override
  239. public PageInfo pageList(SceneParam param) {
  240. if(param.getType() == 2){ //深时
  241. if(param.getCompanyId()!= null){ //客户场景
  242. List<CameraDetail> cameraDetails = cameraDetailService.getListByCompanyId(param.getCompanyId());
  243. if(cameraDetails.size() >0){
  244. Set<Long> cameraIds = cameraDetails.stream()
  245. .filter(entity -> entity.getGoodsId() == 10).map(CameraDetail::getCameraId).collect(Collectors.toSet());
  246. List<Camera> cameraList = cameraService.listByIds(cameraIds);
  247. List<String> snCodes = cameraList.stream().map(Camera::getSnCode).collect(Collectors.toList());
  248. param.setSnCodes(snCodes);
  249. }
  250. }
  251. return laserService.pageList(param);
  252. }
  253. if(param.getType() == 3){ //双目lite
  254. return sceneService.pageList(param);
  255. }
  256. Page<SceneVo> page = this.getBaseMapper().pageList(new Page<>(param.getPageNum(),param.getPageSize()),param);
  257. List<SceneVo> records = page.getRecords();
  258. HashMap<String,SceneCopyLog> map = null;
  259. if(records.size() >0){
  260. List<String> numList = records.parallelStream().map(SceneVo::getNum).collect(Collectors.toList());
  261. map = copyLogService.getByNewNumList(numList);
  262. }
  263. for (SceneVo record : page.getRecords()) {
  264. record.setStatusString(SceneStatusUtil.getStatusString(record));
  265. if(map !=null ){
  266. SceneCopyLog sceneCopyLog = map.get(record.getNum());
  267. if(sceneCopyLog != null){
  268. record.setCopyTime(sceneCopyLog.getCreateTime());
  269. record.setIsCopy(true);
  270. }
  271. }
  272. }
  273. return PageInfo.PageInfo(page);
  274. }
  275. @Override
  276. public void move(SceneParam param) {
  277. Camera camera = cameraService.getBySnCode(param.getSnCode());
  278. if(camera == null){
  279. throw new BusinessException(ResultCode.CAMERA_SN_NOT_EXIST);
  280. }
  281. CameraDetail cameraDetail = cameraDetailService.getByCameraId(camera.getId());
  282. if(cameraDetail == null){
  283. throw new BusinessException(ResultCode.CAMERA_SN_NOT_EXIST);
  284. }
  285. ScenePro scenePro = this.getByNum(param.getNum());
  286. ScenePlus scenePlus = scenePlusService.getByNum(param.getNum());
  287. if((scenePro == null || scenePro.getCameraId() == null ) && (scenePlus== null || scenePlus.getCameraId() == null)){
  288. throw new BusinessException(SceneConstant.FAILURE_CODE_5005, SceneConstant.FAILURE_MSG_5005);
  289. }
  290. Integer status = scenePro == null ? scenePlus.getSceneStatus() : scenePro.getStatus();
  291. if(status == 0){
  292. throw new BusinessException(SceneConstant.FAILURE_CODE_5037, SceneConstant.FAILURE_MSG_5037);
  293. }
  294. Long cameraId = scenePro == null ? scenePlus.getCameraId() : scenePro.getCameraId();
  295. Long space = scenePro == null ? 0 :scenePro.getSpace();
  296. if(scenePlus !=null){
  297. ScenePlusExt scenePlusExt = scenePlusExtService.getByPlusId(scenePlus.getId());
  298. space = scenePlusExt.getSpace();
  299. }
  300. space = space == null ? 0 :space;
  301. Long newUseSpace = space + cameraDetail.getUsedSpace();
  302. Long count = userIncrementService.getValidCountByCameraId(cameraId);
  303. if(count<=0 && newUseSpace > cameraDetail.getTotalSpace()){
  304. throw new BusinessException(ResultCode.CAMERA_SPACE_ERROR);
  305. }
  306. Camera oldCamera = cameraService.getById(cameraId);
  307. if(oldCamera == null){
  308. throw new BusinessException(ResultCode.CAMERA_NOT_EXIST);
  309. }
  310. CameraDetail oldCameraDetail = cameraDetailService.getByCameraId(oldCamera.getId());
  311. if(oldCameraDetail == null){
  312. throw new BusinessException(ResultCode.CAMERA_NOT_EXIST);
  313. }
  314. if(!oldCameraDetail.getType().equals(cameraDetail.getType())){
  315. throw new BusinessException(ResultCode.CAMERA_TYPE_NOT_ERROR);
  316. }
  317. Long oldUseSpace = oldCameraDetail.getUsedSpace() - space < 0 ? 0 : oldCameraDetail.getUsedSpace() - space;
  318. oldCameraDetail.setUsedSpace(oldUseSpace);
  319. Long subSpace = oldCameraDetail.getTotalSpace() - oldUseSpace;
  320. if(subSpace >0){ //有剩余容量解封容量内场景
  321. this.lockOrUnLockBySpace(oldCameraDetail,oldCameraDetail.getCameraId(),1);
  322. }
  323. cameraDetailService.updateById(oldCameraDetail);
  324. cameraDetail.setUsedSpace(newUseSpace);
  325. cameraDetailService.updateById(cameraDetail);
  326. if(scenePro!=null){
  327. LambdaUpdateWrapper<ScenePro> wrapper = new LambdaUpdateWrapper<>();
  328. wrapper.eq(ScenePro::getId,scenePro.getId());
  329. wrapper.set(ScenePro::getCameraId,camera.getId());
  330. wrapper.set(ScenePro::getUserId,cameraDetail.getUserId());
  331. this.update(wrapper);
  332. }
  333. if(scenePlus!=null){
  334. LambdaUpdateWrapper<ScenePlus> wrapper = new LambdaUpdateWrapper<>();
  335. wrapper.eq(ScenePlus::getId,scenePlus.getId());
  336. wrapper.set(ScenePlus::getCameraId,camera.getId());
  337. wrapper.set(ScenePlus::getUserId,cameraDetail.getUserId());
  338. scenePlusService.update(wrapper);
  339. }
  340. if(cameraDetail.getType() == 10){ //深时场景
  341. laserService.move(param.getNum(),oldCamera.getSnCode(),camera.getSnCode());
  342. }
  343. }
  344. @Override
  345. public void copy(String sceneNum) throws Exception {
  346. ScenePro scenePro = this.getByNum(sceneNum);
  347. ScenePlus scenePlus = scenePlusService.getByNum(sceneNum);
  348. if((scenePro == null || scenePro.getCameraId() == null ) && (scenePlus== null || scenePlus.getCameraId() == null)){
  349. throw new BusinessException(SceneConstant.FAILURE_CODE_5005, SceneConstant.FAILURE_MSG_5005);
  350. }
  351. Long cameraId = scenePro == null ? scenePlus.getCameraId() : scenePro.getCameraId();
  352. Camera camera = cameraService.getById(cameraId);
  353. if(camera == null){
  354. throw new BusinessException(CameraConstant.FAILURE_CODE_6029, CameraConstant.FAILURE_MSG_6029);
  355. }
  356. CameraDetail detailEntity = cameraDetailService.getByCameraId(cameraId);
  357. if(detailEntity == null){
  358. throw new BusinessException(CameraConstant.FAILURE_CODE_6029, CameraConstant.FAILURE_MSG_6029);
  359. }
  360. HashMap<String, Object> param = new HashMap<>();
  361. param.put("num",sceneNum);
  362. fdKKClient.copyScene(param,"m_a_n_a_g_e");
  363. }
  364. @Override
  365. public void deleteByNum(String num) {
  366. ScenePro scenePro = this.getByNum(num);
  367. if(scenePro!=null){
  368. this.removeById(scenePro.getId());
  369. }
  370. Scene scene = sceneService.getByNum(num);
  371. if(scene!=null){
  372. sceneService.removeById(scene.getId());
  373. }
  374. ScenePlus scenePlus = scenePlusService.getByNum(num);
  375. if(scenePlus!=null){
  376. scenePlusService.removeById(scenePlus.getId());
  377. }
  378. laserService.delete(num);
  379. }
  380. @Override
  381. public Long getKkCount(List<String> asList, String startTime) {
  382. LambdaQueryWrapper<ScenePro> wrapper = new LambdaQueryWrapper<>();
  383. wrapper.in(ScenePro::getSceneSource,asList);
  384. wrapper.eq(ScenePro::getIsUpgrade,0);
  385. wrapper.lt(ScenePro::getCreateTime,startTime);
  386. long count = this.count(wrapper);
  387. LambdaQueryWrapper<ScenePlus> wrapper2 = new LambdaQueryWrapper<>();
  388. wrapper2.in(ScenePlus::getSceneSource,asList);
  389. wrapper2.lt(ScenePlus::getCreateTime,startTime);
  390. long count1 = scenePlusService.count(wrapper2);
  391. return count + count1;
  392. }
  393. @Override
  394. public Long getSsCount(List<String> asList, String startTime) {
  395. LambdaQueryWrapper<ScenePro> wrapper = new LambdaQueryWrapper<>();
  396. wrapper.in(ScenePro::getSceneSource,asList);
  397. wrapper.eq(ScenePro::getIsUpgrade,0);
  398. wrapper.lt(ScenePro::getCreateTime,startTime);
  399. LambdaQueryWrapper<ScenePlus> wrapper2 = new LambdaQueryWrapper<>();
  400. wrapper2.in(ScenePlus::getSceneSource,asList);
  401. wrapper2.lt(ScenePlus::getCreateTime,startTime);
  402. long count = scenePlusService.count(wrapper2);
  403. return this.count(wrapper) + count;
  404. }
  405. @Override
  406. public Long getSsObjCount(List<String> asList, String startTime) {
  407. LambdaQueryWrapper<ScenePro> wrapper = new LambdaQueryWrapper<>();
  408. wrapper.in(ScenePro::getSceneSource,asList);
  409. wrapper.eq(ScenePro::getIsUpgrade,0);
  410. wrapper.lt(ScenePro::getCreateTime,startTime);
  411. wrapper.eq(ScenePro::getIsObj,1);
  412. LambdaQueryWrapper<ScenePlus> wrapper2 = new LambdaQueryWrapper<>();
  413. wrapper2.in(ScenePlus::getSceneSource,asList);
  414. wrapper2.lt(ScenePlus::getCreateTime,startTime);
  415. List<ScenePlus> list = scenePlusService.list(wrapper2);
  416. List<Long> plusIds = list.stream().map(ScenePlus::getId).collect(Collectors.toList());
  417. LambdaQueryWrapper<ScenePlusExt> wrapper3 = new LambdaQueryWrapper<>();
  418. wrapper3.in(ScenePlusExt::getPlusId,plusIds);
  419. wrapper3.eq(ScenePlusExt::getIsObj,1);
  420. long count = scenePlusExtService.count(wrapper3);
  421. return this.count(wrapper) + count;
  422. }
  423. }