SceneProServiceImpl.java 59 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306
  1. package com.fdkankan.ucenter.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.SceneConstant;
  10. import com.fdkankan.common.constant.SceneKind;
  11. import com.fdkankan.common.exception.BusinessException;
  12. import com.fdkankan.common.util.*;
  13. import com.fdkankan.rabbitmq.bean.BuildSceneCallMessage;
  14. import com.fdkankan.rabbitmq.util.RabbitMqProducer;
  15. import com.fdkankan.redis.constant.RedisKey;
  16. import com.fdkankan.ucenter.common.OssPath;
  17. import com.fdkankan.ucenter.common.PageInfo;
  18. import com.fdkankan.fyun.face.FYunFileServiceInterface;
  19. import com.fdkankan.image.MatrixToImageWriterUtil;
  20. import com.fdkankan.ucenter.common.constants.ConstantFilePath;
  21. import com.fdkankan.redis.util.RedisUtil;
  22. import com.fdkankan.ucenter.common.constants.NacosProperty;
  23. import com.fdkankan.ucenter.common.utils.ShellUtil;
  24. import com.fdkankan.ucenter.constant.CameraConstant;
  25. import com.fdkankan.ucenter.constant.LoginConstant;
  26. import com.fdkankan.ucenter.entity.*;
  27. import com.fdkankan.ucenter.httpClient.service.LaserService;
  28. import com.fdkankan.ucenter.mapper.ISceneProMapper;
  29. import com.fdkankan.ucenter.mapper.ISceneUpgradeMapper;
  30. import com.fdkankan.ucenter.service.*;
  31. import com.fdkankan.ucenter.util.DateUserUtil;
  32. import com.fdkankan.ucenter.vo.ResponseScene;
  33. import com.fdkankan.ucenter.vo.request.RequestScene;
  34. import com.fdkankan.ucenter.vo.request.SceneParam;
  35. import com.fdkankan.ucenter.vo.response.GroupByCount;
  36. import com.fdkankan.ucenter.vo.response.SceneInfoVo;
  37. import com.fdkankan.ucenter.vo.response.SceneNumVo;
  38. import com.fdkankan.ucenter.vo.response.SceneVo;
  39. import java.io.File;
  40. import java.net.InetAddress;
  41. import java.net.UnknownHostException;
  42. import java.util.ArrayList;
  43. import java.util.Arrays;
  44. import java.util.Collections;
  45. import java.util.Date;
  46. import java.util.HashMap;
  47. import java.util.List;
  48. import java.util.Map;
  49. import java.util.Objects;
  50. import java.util.concurrent.CompletableFuture;
  51. import java.util.concurrent.locks.Condition;
  52. import java.util.stream.Collectors;
  53. import javax.annotation.Resource;
  54. import lombok.extern.slf4j.Slf4j;
  55. import org.apache.commons.lang3.StringUtils;
  56. import org.joda.time.DateTime;
  57. import org.springframework.beans.BeanUtils;
  58. import org.springframework.beans.factory.annotation.Autowired;
  59. import org.springframework.beans.factory.annotation.Value;
  60. import org.springframework.stereotype.Service;
  61. import org.springframework.util.ObjectUtils;
  62. /**
  63. * <p>
  64. * pro场景表 服务实现类
  65. * </p>
  66. *
  67. * @author
  68. * @since 2022-07-04
  69. */
  70. @Service
  71. @Slf4j
  72. public class SceneProServiceImpl extends ServiceImpl<ISceneProMapper, ScenePro> implements ISceneProService {
  73. @Autowired
  74. private IUserService userService;
  75. @Autowired
  76. private ISceneCooperationService sceneCooperationService;
  77. @Autowired
  78. private IScenePlusService scenePlusService;
  79. @Autowired
  80. private ISceneService sceneService;
  81. @Autowired
  82. private LaserService fdkkLaserService;
  83. @Autowired
  84. private FYunFileServiceInterface fYunFileService;
  85. @Autowired
  86. private IUserIncrementService userIncrementService;
  87. @Autowired
  88. private IIncrementTypeService incrementTypeService;
  89. @Autowired
  90. private IScenePlusExtService scenePlusExtService;
  91. @Autowired
  92. private ISceneEditInfoService sceneEditInfoService;
  93. @Autowired
  94. private ICameraDetailService cameraDetailService;
  95. @Autowired
  96. ICameraService cameraService;
  97. @Autowired
  98. private IFolderService folderService;
  99. @Autowired
  100. private IFolderSceneService folderSceneService;
  101. @Autowired
  102. private ISceneCopyLogService sceneCopyLogService;
  103. @Autowired
  104. private IScene3dNumService scene3dNumService;
  105. @Autowired
  106. private ISceneProEditService sceneProEditService;
  107. @Autowired
  108. private RedisUtil redisUtil;
  109. @Autowired
  110. private ISceneUpgradeMapper sceneUpgradeMapper;
  111. @Autowired
  112. private IInnerService innerService;
  113. @Autowired
  114. LaserService laserService;
  115. @Value("${scene.pro.url}")
  116. private String sceneProUrl;
  117. @Value("${scene.pro.new.url}")
  118. private String sceneProNewUrl;
  119. @Value("${queue.modeling.obj.modeling-pre}")
  120. private String queueObjModelingPre;
  121. @Autowired
  122. private RabbitMqProducer mqProducer;
  123. @Override
  124. public HashMap<String, SceneNumVo> findSceneNumber(String token) {
  125. User user = userService.getByUserName(JwtUtil.getUsername(token));
  126. SceneNumVo sceneNumVoKk = getSceneNumVoByType(Arrays.asList(1,2,12,13,14), user.getId());
  127. Integer oldNum = sceneService.getCountByUserId(user.getId());
  128. sceneNumVoKk.setSceneNum( oldNum + sceneNumVoKk.getSceneNum());
  129. sceneNumVoKk.setTotalNum(oldNum + sceneNumVoKk.getTotalNum());
  130. SceneNumVo sceneNumVoKj = getSceneNumVoByType(Collections.singletonList(3), user.getId());
  131. SceneNumVo sceneNumVoSS = fdkkLaserService.getLaserSceneNumByUser(token);
  132. SceneNumVo sceneNumVoSSObj = getSceneNumVoByObjType(Arrays.asList(4), user.getId());
  133. HashMap<String, SceneNumVo> hashMap = new HashMap<>();
  134. hashMap.put("kk",sceneNumVoKk);
  135. hashMap.put("kJ",sceneNumVoKj);
  136. hashMap.put("SS",sceneNumVoSS);
  137. hashMap.put("SS_OBJ",sceneNumVoSSObj);
  138. return hashMap;
  139. }
  140. private SceneNumVo getSceneNumVoByType(List<Integer> sceneSourceList, Long userId){
  141. SceneNumVo sceneNumVo = new SceneNumVo();
  142. LambdaQueryWrapper<ScenePro> queryWrapper = new LambdaQueryWrapper<>();
  143. queryWrapper.eq(ScenePro::getUserId, userId);
  144. queryWrapper.in(ScenePro::getSceneSource, sceneSourceList);
  145. queryWrapper.eq(ScenePro::getIsUpgrade, 0);
  146. Integer sceneNum = Math.toIntExact(this.count(queryWrapper));
  147. Long count = scenePlusService.getCountByUserId(userId,sceneSourceList);
  148. sceneNumVo.setCooperationSceneNum(sceneCooperationService.getCooperationSceneNum(userId,sceneSourceList));
  149. sceneNumVo.setSceneNum(sceneNum + count);
  150. sceneNumVo.setTotalNum(sceneNumVo.getSceneNum() + sceneNumVo.getCooperationSceneNum());
  151. return sceneNumVo;
  152. }
  153. private SceneNumVo getSceneNumVoByObjType(List<Integer> sceneSourceList, Long userId){
  154. SceneNumVo sceneNumVo = new SceneNumVo();
  155. LambdaQueryWrapper<ScenePro> queryWrapper = new LambdaQueryWrapper<>();
  156. queryWrapper.eq(ScenePro::getUserId, userId);
  157. queryWrapper.in(ScenePro::getSceneSource, sceneSourceList);
  158. queryWrapper.eq(ScenePro::getIsUpgrade, 0);
  159. queryWrapper.eq(ScenePro::getIsObj,1);
  160. Integer sceneNum = Math.toIntExact(this.count(queryWrapper));
  161. LambdaQueryWrapper<ScenePlusExt> wrapper = new LambdaQueryWrapper<>();
  162. wrapper.eq(ScenePlusExt::getIsObj,1);
  163. List<ScenePlusExt> list = scenePlusExtService.list(wrapper);
  164. List<Long> plusIds = list.stream().map(ScenePlusExt::getPlusId).collect(Collectors.toList());
  165. long plusCount = 0L;
  166. long cooperNum = 0L;
  167. if(plusIds.size() >0){
  168. LambdaQueryWrapper<ScenePlus> plusWr = new LambdaQueryWrapper<>();
  169. plusWr.in(ScenePlus::getId,plusIds);
  170. plusWr.in(ScenePlus::getSceneSource,sceneSourceList);
  171. List<ScenePlus> plusList = scenePlusService.list(plusWr);
  172. if(plusList.size() >0){
  173. List<ScenePlus> userList = plusList.stream().filter(entity -> userId.equals(entity.getUserId())).collect(Collectors.toList());
  174. plusCount = userList.size();
  175. List<String> numList = plusList.stream().map(ScenePlus::getNum).collect(Collectors.toList());
  176. HashMap<String, User> cooNumList = sceneCooperationService.getByNumList(numList);
  177. for (String num : cooNumList.keySet()) {
  178. if(userId.equals(cooNumList.get(num).getId())){
  179. cooperNum ++;
  180. }
  181. }
  182. }
  183. }
  184. sceneNumVo.setCooperationSceneNum(cooperNum);
  185. sceneNumVo.setSceneNum(sceneNum + plusCount);
  186. sceneNumVo.setTotalNum(sceneNumVo.getSceneNum() + sceneNumVo.getCooperationSceneNum());
  187. return sceneNumVo;
  188. }
  189. @Override
  190. public HashMap<Long,GroupByCount> findSceneNumByCameraIds(List<Long> cameraIdList) {
  191. HashMap<Long,GroupByCount> map = new HashMap<>();
  192. List<GroupByCount> result = this.getBaseMapper().findSceneProNumByCameraIds(cameraIdList);
  193. List<GroupByCount> result2 = this.getBaseMapper().findScenePlusNumByCameraIds(cameraIdList);
  194. result.forEach(entity ->map.put(entity.getId(),entity));
  195. for (GroupByCount groupByCount : result2) {
  196. if (map.get(groupByCount.getId()) != null) {
  197. Long totalCount = map.get(groupByCount.getId()).getCount() + groupByCount.getCount();
  198. String lastTime = DateUserUtil.getLastTime(map.get(groupByCount.getId()).getLastTime(), groupByCount.getLastTime());
  199. groupByCount.setCount(totalCount);
  200. groupByCount.setLastTime(lastTime);
  201. }
  202. map.put(groupByCount.getId(),groupByCount);
  203. }
  204. return map;
  205. }
  206. /**
  207. * @param payStatus -2 封存,为 1 解封
  208. */
  209. @Override
  210. public void lockOrUnLockBySpace(CameraDetail cameraDetail, Long cameraId, Integer payStatus) {
  211. LambdaQueryWrapper<ScenePro> wrapper = new LambdaQueryWrapper<>();
  212. LambdaQueryWrapper<ScenePlus> plusWr = new LambdaQueryWrapper<>();
  213. Long totalSpace = cameraDetail.getTotalSpace();
  214. UserIncrement userIncrement = userIncrementService.getByCameraId(cameraId);
  215. if(userIncrement!=null){
  216. IncrementType incrementType = incrementTypeService.getById(userIncrement.getIncrementTypeId());
  217. if(incrementType!=null){
  218. if(incrementType.getCameraCapacity() == -1){
  219. totalSpace = -1L;
  220. }else {
  221. totalSpace = incrementType.getCameraCapacity() * 1024* 1024 * 1024L;
  222. }
  223. }
  224. }
  225. log.info("lockOrUnLockBySpace---cameraId:{},payStatus:{},totalSpace:{},useSpace{}",
  226. cameraId,payStatus,totalSpace,cameraDetail.getUsedSpace());
  227. if(payStatus == 1){ //解封,判断用户权益,用户会员权益无限容量
  228. if (totalSpace != -1 && totalSpace <= cameraDetail.getUsedSpace()) {
  229. // 总容量小于已使用容量,不予解封
  230. return;
  231. }
  232. wrapper.orderByAsc(ScenePro::getCreateTime);
  233. plusWr.orderByAsc(ScenePlus::getCreateTime);
  234. wrapper.eq(ScenePro::getPayStatus,-2);
  235. plusWr.eq(ScenePlus::getPayStatus,-2);
  236. }else {
  237. if (totalSpace == -1 || totalSpace >= cameraDetail.getUsedSpace()) {
  238. // 总容量大于已使用容量,不予封存
  239. return;
  240. }
  241. wrapper.orderByDesc(ScenePro::getCreateTime);
  242. plusWr.orderByDesc(ScenePlus::getCreateTime);
  243. wrapper.eq(ScenePro::getPayStatus,1);
  244. plusWr.eq(ScenePlus::getPayStatus,1);
  245. }
  246. wrapper.eq(ScenePro::getCameraId,cameraId)
  247. .eq(ScenePro::getIsUpgrade,0);
  248. plusWr.eq(ScenePlus::getCameraId,cameraId);
  249. List<ScenePro> list = this.list(wrapper);
  250. List<ScenePlus> plusList = scenePlusService.list(plusWr);
  251. Long beyondSpace = Math.abs(cameraDetail.getUsedSpace() - totalSpace);
  252. if(totalSpace == -1){
  253. beyondSpace = Long.MAX_VALUE;
  254. }
  255. Long accumulateSpace = 0L;
  256. List<Long> lockedIds = new ArrayList<>();
  257. if(payStatus == 1){
  258. Long doSpace = getScenePlusLockedIds(lockedIds, plusList, beyondSpace, accumulateSpace);
  259. beyondSpace -= doSpace;
  260. getSceneLockedIds(lockedIds,list,beyondSpace,accumulateSpace);
  261. }else {
  262. Long doSpace = getSceneLockedIds(lockedIds, list, beyondSpace, accumulateSpace);
  263. beyondSpace -= doSpace;
  264. getScenePlusLockedIds(lockedIds,plusList,beyondSpace,accumulateSpace);
  265. }
  266. lockOrUnLockScenes(lockedIds,payStatus);
  267. }
  268. private Long getSceneLockedIds(List<Long> lockedIds ,List<ScenePro> list,Long beyondSpace,Long accumulateSpace){
  269. if (list != null && list.size() > 0){
  270. for (ScenePro scenePro : list){
  271. accumulateSpace += scenePro.getSpace();
  272. if (accumulateSpace.compareTo(beyondSpace) > 0){
  273. return accumulateSpace - scenePro.getSpace();
  274. }
  275. lockedIds.add(scenePro.getId());
  276. }
  277. }
  278. return accumulateSpace;
  279. }
  280. private Long getScenePlusLockedIds(List<Long> lockedIds , List<ScenePlus> list, Long beyondSpace, Long accumulateSpace){
  281. if (list != null && list.size() > 0){
  282. List<Long> plusIds = list.parallelStream().map(ScenePlus::getId).collect(Collectors.toList());
  283. HashMap<Long, ScenePlusExt> byPlusIds = scenePlusExtService.getByPlusIds(plusIds);
  284. for (ScenePlus scenePlus : list){
  285. ScenePlusExt scenePlusExt = byPlusIds.get(scenePlus.getId());
  286. accumulateSpace += scenePlusExt.getSpace();
  287. if (accumulateSpace.compareTo(beyondSpace) > 0){
  288. return accumulateSpace - scenePlusExt.getSpace();
  289. }
  290. lockedIds.add(scenePlus.getId());
  291. }
  292. }
  293. return accumulateSpace;
  294. }
  295. // payStatus 为 -2 封存,为 1 解封
  296. private void lockOrUnLockScenes(List<Long> lockedIds,Integer payStatus) {
  297. if (lockedIds == null || lockedIds.size() == 0){
  298. return;
  299. }
  300. LambdaUpdateWrapper<ScenePro> updateWrapper = new LambdaUpdateWrapper<>();
  301. updateWrapper.set(ScenePro::getPayStatus,payStatus)
  302. .eq(ScenePro::getIsUpgrade,0)
  303. .in(ScenePro::getId,lockedIds);
  304. this.update(updateWrapper);
  305. LambdaUpdateWrapper<ScenePlus> updatePlusWrapper = new LambdaUpdateWrapper<>();
  306. updatePlusWrapper.set(ScenePlus::getPayStatus,payStatus)
  307. .in(ScenePlus::getId,lockedIds);
  308. scenePlusService.update(updatePlusWrapper);
  309. this.updateOssStatus(lockedIds,payStatus);
  310. }
  311. /**
  312. * 修改oss status.json中 payStatus
  313. */
  314. private void updateOssStatus(List<Long> lockedIds, Integer payStatus) {
  315. LambdaQueryWrapper<ScenePro> proWr = new LambdaQueryWrapper<>();
  316. proWr.eq(ScenePro::getIsUpgrade,0);
  317. proWr.in(ScenePro::getId,lockedIds);
  318. List<ScenePro> proList = this.list(proWr);
  319. LambdaQueryWrapper<ScenePlus> pluWr = new LambdaQueryWrapper<>();
  320. pluWr.in(ScenePlus::getId,lockedIds);
  321. List<ScenePlus> plusList = scenePlusService.list(pluWr);
  322. for (ScenePro scenePro : proList) {
  323. this.updateOssStatus(String.format(OssPath.v3_statusPath,scenePro.getNum()),payStatus);
  324. }
  325. for (ScenePlus scenePlus : plusList) {
  326. this.updateOssStatus(String.format(OssPath.v4_statusPath,scenePlus.getNum()),payStatus);
  327. }
  328. }
  329. /**
  330. * 从oss中获取文件,并重写,上传替换
  331. */
  332. @Override
  333. public void updateOssStatus(String path,Integer payStatus) {
  334. try {
  335. if(!fYunFileService.fileExist(path)){
  336. return;
  337. }
  338. String data = fYunFileService.getFileContent(path);
  339. if(StringUtils.isBlank(data)){
  340. return;
  341. }
  342. JSONObject jsonObject = JSONObject.parseObject(data);
  343. jsonObject.put("payStatus",payStatus);
  344. String json = JSONUtil.toJsonStr(jsonObject);
  345. FileUtils.writeFile(OssPath.localStatusPath ,json);
  346. log.info("updateOssStatus--localPath:{},ossPath:{}",OssPath.localStatusPath,path);
  347. fYunFileService.uploadFile(OssPath.localStatusPath,path);
  348. }catch (Exception e){
  349. e.printStackTrace();
  350. }finally {
  351. FileUtil.del(OssPath.localStatusPath);
  352. }
  353. }
  354. @Override
  355. public List<ScenePro> getListByCameraId(Long cameraId) {
  356. LambdaQueryWrapper<ScenePro> wrapper = new LambdaQueryWrapper<>();
  357. wrapper.eq(ScenePro::getCameraId,cameraId)
  358. .eq(ScenePro::getIsUpgrade,0);
  359. return this.list(wrapper);
  360. }
  361. @Override
  362. public void bindOrUnCamera(List<Long> cameraIds, Long userId) {
  363. if(cameraIds.size() >0){
  364. LambdaUpdateWrapper<ScenePro> wrapper = new LambdaUpdateWrapper<>();
  365. wrapper.in(ScenePro::getCameraId,cameraIds)
  366. .eq(ScenePro::getIsUpgrade,0)
  367. .set(ScenePro::getUserId,userId);
  368. this.update(wrapper);
  369. }
  370. }
  371. @Override
  372. public List<ScenePro> getListByCameraIds(List<Long> cameraIds) {
  373. if(cameraIds.size() >0){
  374. LambdaQueryWrapper<ScenePro> wrapper = new LambdaQueryWrapper<>();
  375. wrapper.in(ScenePro::getCameraId,cameraIds)
  376. .eq(ScenePro::getIsUpgrade,0);
  377. return this.list(wrapper);
  378. }
  379. return new ArrayList<>();
  380. }
  381. @Override
  382. public List<ScenePro> getListByNums(List<String> numList) {
  383. if(numList.size() >0){
  384. LambdaQueryWrapper<ScenePro> wrapper = new LambdaQueryWrapper<>();
  385. wrapper.in(ScenePro::getNum,numList)
  386. .eq(ScenePro::getIsUpgrade,0);
  387. return this.list(wrapper);
  388. }
  389. return new ArrayList<>();
  390. }
  391. @Override
  392. public Long getCountByUserId(Long userId, List<?> resourceList) {
  393. if(resourceList.size() >0){
  394. LambdaQueryWrapper<ScenePro> wrapper = new LambdaQueryWrapper<>();
  395. wrapper.eq(ScenePro::getUserId,userId);
  396. wrapper.eq(ScenePro::getIsUpgrade,0);
  397. wrapper.in(ScenePro::getSceneSource,resourceList);
  398. return this.count(wrapper);
  399. }
  400. return 0L;
  401. }
  402. @Override
  403. public Long getCountByUserId(Long id, Integer cameraType) {
  404. return this.getBaseMapper().getCountByUserId(id,cameraType);
  405. }
  406. @Override
  407. public void deleteByIds(List<Long> sceneIds) {
  408. if(sceneIds.size() >0){
  409. LambdaQueryWrapper<ScenePro> wrapper = new LambdaQueryWrapper<>();
  410. wrapper.in(ScenePro::getId,sceneIds)
  411. .eq(ScenePro::getIsUpgrade,0);
  412. List<ScenePro> proList = this.list(wrapper);
  413. LambdaQueryWrapper<ScenePlus> plusWr = new LambdaQueryWrapper<>();
  414. plusWr.in(ScenePlus::getId,sceneIds);
  415. List<ScenePlus> plusList = scenePlusService.list(plusWr);
  416. this.deleteByList(proList,plusList);
  417. }
  418. }
  419. @Override
  420. public Page<SceneVo> pageListAndFolder(Page<Object> page, SceneParam param) {
  421. return getBaseMapper().pageListAndFolder(new Page<>(param.getPageNum(),param.getPageSize()),param);
  422. }
  423. @Override
  424. public Object getScenes(String username, RequestScene param) {
  425. User user = userService.getByUserName(username);
  426. log.info("搜索条件是:"+ param.getSearchKey());
  427. //type为12表示一键换装的请求,不查询相机数据
  428. if(StringUtils.isNotEmpty(param.getSearchKey()) && !"11".equals(param.getType())){
  429. List<Long> userIds = userService.getLikeUserName(param.getSearchKey());
  430. if(userIds.size() > 0){
  431. List<String> cooperationList = sceneCooperationService.getNumByUserIds(userIds);
  432. param.setNumList(cooperationList);
  433. }
  434. }
  435. param.setUserId(user.getId());
  436. Page<SceneVo> sceneList = sceneService.getSceneList(param);
  437. for (SceneVo record : sceneList.getRecords()) {
  438. record.setUserName(username);
  439. SceneCooperation sceneCooperationEntity = sceneCooperationService.getByNum(record.getNum());
  440. if(sceneCooperationEntity != null && sceneCooperationEntity.getUserId()!= null){
  441. User user1 = userService.getById(sceneCooperationEntity.getUserId());
  442. if(user1 != null){
  443. record.setCooperationUserId(String.valueOf(sceneCooperationEntity.getUserId()));
  444. record.setCooperationUserName(user1.getUserName());
  445. }
  446. }
  447. }
  448. return PageInfo.PageInfo(sceneList);
  449. }
  450. @Override
  451. public JSONObject newList(SceneParam param, String username) {
  452. param.setSourceList(getSceneSource(param.getSceneSource()));
  453. if(StringUtils.isNotBlank(username)){
  454. User user = userService.getByUserName(username);
  455. param.setUserId(user.getId());
  456. }
  457. Page<SceneVo> sceneVoPage = getBaseMapper().pageListAndFolder(new Page<>(param.getPageNum(),param.getPageSize()),param);
  458. List<SceneVo> folderList = sceneVoPage.getRecords().parallelStream().filter(entity -> entity.getIsFolder() == 1).collect(Collectors.toList());
  459. Long totalSceneNum = sceneVoPage.getTotal();
  460. if(folderList.size() >0){
  461. List<Long> folderIds = folderList.parallelStream().map(SceneVo::getId).collect(Collectors.toList());
  462. List<Folder> allFolderIds = folderService.getAllFolderIds(folderIds);
  463. HashMap<Long,Long> map = folderSceneService.getGroupByFolderIds(allFolderIds);
  464. for (SceneVo vo : sceneVoPage.getRecords()) {
  465. if(vo.getIsFolder() == 1 && vo.getType()!= null && vo.getType()!= 3){
  466. Long sceneNum = map.get(vo.getId()) == null ? 0L : map.get(vo.getId());
  467. vo.setSceneNum(sceneNum);
  468. vo.setCreateTimeStr(vo.getCreateTime());
  469. }else if(vo.getIsFolder() == 1 && vo.getType()!= null && vo.getType() == 3){
  470. if(param.getUserId()!= null){
  471. Integer count = sceneService.getCountByUserId(param.getUserId());
  472. vo.setSceneNum(Long.valueOf(count));
  473. }
  474. }
  475. }
  476. }
  477. //设置协作者信息
  478. if(sceneVoPage.getRecords().size() >0){
  479. List<String> numList = sceneVoPage.getRecords().parallelStream().map(SceneVo::getNum).collect(Collectors.toList());
  480. HashMap<String,User> cooMap = sceneCooperationService.getByNumList(numList);
  481. for (SceneVo vo : sceneVoPage.getRecords()) {
  482. if (StringUtils.isNotBlank(vo.getNum())) {
  483. User userVo = cooMap.get(vo.getNum());
  484. if (userVo != null) {
  485. vo.setCooperationUserId(userVo.getId().toString());
  486. vo.setCooperationUserName(userVo.getUserName());
  487. }
  488. }
  489. }
  490. }
  491. List<SceneVo> sceneList = sceneVoPage.getRecords().parallelStream().filter(entity -> entity.getIsFolder() == 0)
  492. .peek(vo->{
  493. if (ObjectUtils.isEmpty(vo.getStatus())) {
  494. return;
  495. }
  496. if (vo.getStatus().equals(500)) { //500状态为数据库中手动修改值,当场景本身异常,算法,算不了
  497. vo.setStatus(-1);
  498. }
  499. if (vo.getStatus().equals(-1)) { //计算失败,钉钉通知之后,判定是否重算,还是修改为 500
  500. vo.setStatus(0);
  501. }
  502. }).collect(Collectors.toList());
  503. if(sceneList.size() >0){
  504. folderList.addAll(sceneList);
  505. }
  506. sceneVoPage.setRecords(folderList);
  507. if(param.getFolderId() == null && param.getIsObj() == null ){
  508. List<String> sourceList = param.getSourceList();
  509. if(param.getUserId()!= null){
  510. totalSceneNum = this.getCountByUserId(param.getUserId(),sourceList);
  511. totalSceneNum += scenePlusService.getCountByUserId(param.getUserId(),sourceList);
  512. totalSceneNum += sceneService.getCountByUserId(param.getUserId());
  513. }
  514. }else if(param.getFolderId() != null ){
  515. totalSceneNum = folderSceneService.getCountByFolderAndSon(param.getFolderId());
  516. }else if(param.getIsObj() != null && param.getIsObj() ==1){
  517. if(param.getUserId()!= null){
  518. totalSceneNum = this.getCountByLaserAndIsObj(param.getUserId());
  519. }
  520. }
  521. JSONObject jsonObject = new JSONObject();
  522. jsonObject.put("pageInfo",PageInfo.PageInfo(sceneVoPage));
  523. jsonObject.put("sceneNum",totalSceneNum);
  524. return jsonObject;
  525. }
  526. private Long getCountByLaserAndIsObj(Long userId) {
  527. Long proCount = 0L;
  528. Long plusCount = 0L;
  529. LambdaQueryWrapper<ScenePro> proWr = new LambdaQueryWrapper<>();
  530. proWr.eq(ScenePro::getUserId,userId);
  531. proWr.eq(ScenePro::getSceneSource,4);
  532. proWr.eq(ScenePro::getIsObj,1);
  533. proWr.eq(ScenePro::getIsUpgrade,0);
  534. proCount = this.count(proWr);
  535. LambdaQueryWrapper<ScenePlus> plusWr = new LambdaQueryWrapper<>();
  536. plusWr.eq(ScenePlus::getUserId,userId);
  537. plusWr.eq(ScenePlus::getSceneSource,4);
  538. List<ScenePlus> list = scenePlusService.list(plusWr);
  539. if(list.size() >0){
  540. List<Long> plusIds = list.stream().map(ScenePlus::getId).collect(Collectors.toList());
  541. LambdaQueryWrapper<ScenePlusExt> extWr = new LambdaQueryWrapper<>();
  542. extWr.in(ScenePlusExt::getPlusId,plusIds);
  543. extWr.eq(ScenePlusExt::getIsObj,1);
  544. plusCount = scenePlusExtService.count(extWr);
  545. }
  546. return proCount +plusCount;
  547. }
  548. public List<String> getSceneSource(String sceneSource){
  549. List<String> sceneSourceList ;
  550. if(StringUtils.isNotBlank(sceneSource)){
  551. switch (sceneSource){
  552. case "1" : sceneSourceList = Arrays.asList("1","2","12","13","14"); break; //四维看看
  553. case "3" : sceneSourceList = Collections.singletonList("3"); break;//四维看见
  554. case "4" : sceneSourceList = Collections.singletonList("4"); break;//深时场景
  555. default:
  556. if(sceneSource.contains(",")){
  557. sceneSource = sceneSource + ",12,13,14";
  558. sceneSourceList = Arrays.asList(sceneSource.split(","));
  559. break;
  560. }
  561. sceneSourceList = Collections.singletonList(sceneSource);break;
  562. }
  563. return sceneSourceList;
  564. }
  565. return Arrays.asList("0","1","2","3","4","5","6","7","8","9","10","11","12","13","14","15");
  566. }
  567. @Override
  568. public void copyScene(String sceneNum,String userName) throws Exception {
  569. if(StringUtils.isEmpty(sceneNum)){
  570. throw new BusinessException(LoginConstant.FAILURE_CODE_3001, LoginConstant.FAILURE_MSG_3001);
  571. }
  572. ScenePro scenePro = this.getByNum(sceneNum);
  573. ScenePlus scenePlus = scenePlusService.getByNum(sceneNum);
  574. if((scenePro == null || scenePro.getCameraId() == null ) && (scenePlus== null || scenePlus.getCameraId() == null)){
  575. throw new BusinessException(SceneConstant.FAILURE_CODE_5005, SceneConstant.FAILURE_MSG_5005);
  576. }
  577. Long cameraId = scenePro == null ? scenePlus.getCameraId() : scenePro.getCameraId();
  578. Long userId = scenePro == null ? scenePlus.getUserId() :scenePro.getUserId();
  579. // 判断是否有绑定会员权益
  580. Long count = userIncrementService.getValidCountByCameraId(cameraId);
  581. if(count <= 0){
  582. throw new BusinessException(LoginConstant.FAILURE_CODE_3030, LoginConstant.FAILURE_MSG_3030);
  583. }
  584. if(userId == null){
  585. throw new BusinessException(SceneConstant.FAILURE_CODE_5013, SceneConstant.FAILURE_MSG_5013);
  586. }
  587. //只能复制自己的场景
  588. User user = userService.getByUserName(userName);
  589. if(user == null || !user.getId().equals(userId) ){
  590. throw new BusinessException(SceneConstant.FAILURE_CODE_5013, SceneConstant.FAILURE_MSG_5013);
  591. }
  592. this.copySceneNoCheck(sceneNum);
  593. }
  594. @Override
  595. public void copySceneNoCheck(String sceneNum) throws Exception {
  596. ScenePro scenePro = this.getByNum(sceneNum);
  597. ScenePlus scenePlus = scenePlusService.getByNum(sceneNum);
  598. Long cameraId = scenePro == null ? scenePlus.getCameraId() : scenePro.getCameraId();
  599. CameraDetail detailEntity = cameraDetailService.getByCameraId(cameraId);
  600. if(detailEntity == null){
  601. throw new BusinessException(CameraConstant.FAILURE_CODE_6029, CameraConstant.FAILURE_MSG_6029);
  602. }
  603. UserIncrement userIncrement = userIncrementService.getByCameraId(cameraId);
  604. if(userIncrement == null){
  605. throw new BusinessException(LoginConstant.FAILURE_CODE_3030, LoginConstant.FAILURE_MSG_3030);
  606. }
  607. IncrementType incrementType = incrementTypeService.getById(userIncrement.getIncrementTypeId());
  608. if(incrementType == null){
  609. throw new BusinessException(LoginConstant.FAILURE_CODE_3030, LoginConstant.FAILURE_MSG_3030);
  610. }
  611. if(incrementType.getCameraCapacity() != -1){
  612. Long usedSpace = detailEntity.getUsedSpace();
  613. if( scenePro != null && scenePro.getSpace() + usedSpace > incrementType.getCameraCapacity() * 1024 * 1024 * 1024L){
  614. throw new BusinessException(CameraConstant.FAILURE_CODE_6008, CameraConstant.FAILURE_MSG_6008);
  615. }
  616. if( scenePlus != null ){
  617. ScenePlusExt ext = scenePlusExtService.getByPlusId(scenePlus.getId());
  618. if(ext.getSpace() + usedSpace > incrementType.getCameraCapacity() * 1024 * 1024 * 1024L){
  619. throw new BusinessException(CameraConstant.FAILURE_CODE_6008, CameraConstant.FAILURE_MSG_6008);
  620. }
  621. }
  622. }
  623. String newNum = scene3dNumService.generateSceneNum(detailEntity.getType());
  624. String title = scenePro == null ? scenePlus.getTitle() : scenePro.getSceneName();
  625. String newTitle = title.concat("(copy)");
  626. Long sceneId = scenePro == null ? scenePlus.getId() :scenePro.getId();
  627. Long newSceneId = null;
  628. FolderScene folderScene = folderSceneService.getByType(sceneId, null);
  629. String time = DateUtil.date2String(new Date(),DateUtil.YYYYMMDDHHMMSSSSS_DATA_FORMAT);
  630. if(scenePro !=null){ //v3场景复制
  631. log.info("场景复制--V3--OldNum:{},oldTitle:{},newNum:{},newTitle:{}",
  632. scenePro.getNum(),scenePro.getSceneName(),newNum,newTitle);
  633. scenePro.setSceneName(newTitle);
  634. newSceneId = this.copyV3Scene(scenePro, newNum, detailEntity,time);
  635. }
  636. if(scenePlus != null){ //v4场景复制
  637. log.info("场景复制--V4--OldNum:{},oldTitle:{},newNum:{},newTitle:{}",
  638. scenePlus.getNum(),scenePlus.getTitle(),newNum,newTitle);
  639. scenePlus.setTitle(newTitle);
  640. newSceneId = scenePlusService.copyV4Scene(scenePlus,newNum,detailEntity,time);
  641. }
  642. log.info("场景复制--完成--sceneId:{}",newSceneId);
  643. if(newSceneId != null && folderScene!= null){
  644. folderScene.setId(null);
  645. folderScene.setSceneId(newSceneId);
  646. folderSceneService.save(folderScene);
  647. }
  648. }
  649. @Override
  650. public ScenePro getByNum(String sceneNum) {
  651. LambdaQueryWrapper<ScenePro> wrapper = new LambdaQueryWrapper<>();
  652. wrapper.eq(ScenePro::getNum,sceneNum);
  653. wrapper.eq(ScenePro::getIsUpgrade,0);
  654. List<ScenePro> list = this.list(wrapper);
  655. if(list!= null && list.size() >0){
  656. return list.get(0);
  657. }
  658. return null;
  659. }
  660. @Override
  661. public Long copyV3Scene(ScenePro oldScene,String newNum,CameraDetail cameraDetail,String time) throws Exception {
  662. SceneProEdit oldEditScene = sceneProEditService.getByProId(oldScene.getId());
  663. String oldNum = oldScene.getNum();
  664. //复制数据库数据
  665. oldScene.setId(null);
  666. oldScene.setStatus(0);
  667. oldScene.setNum(newNum);
  668. oldScene.setWebSite(oldScene.getWebSite().replace(oldNum, newNum));
  669. oldScene.setThumb(oldScene.getThumb().replace(oldNum, newNum));
  670. oldScene.setVideos(this.setVideos(oldScene.getVideos(),oldNum,newNum));
  671. oldScene.setViewCount(0);
  672. String preDataSource = oldScene.getDataSource();
  673. String newDataSource = this.setDataSource(preDataSource,time);
  674. oldScene.setDataSource(newDataSource);
  675. this.save(oldScene);
  676. if(oldScene.getSceneSource() == 4){ //深时复制
  677. laserService.copy(oldScene.getCameraId(),oldScene.getCreateTime(),newNum,0,null,
  678. oldEditScene.getSceneKey(),oldScene.getSceneName(),oldScene.getUserId(),"V3");
  679. }
  680. //更新video
  681. Map map = new HashMap();
  682. JSONObject object = new JSONObject();
  683. if(StringUtils.isNotEmpty(oldScene.getVideos())){
  684. object = JSONObject.parseObject(oldScene.getVideos());
  685. if(object.containsKey("upPath")){
  686. String upPath = object.getString("upPath");
  687. upPath = upPath.replace(oldNum,newNum);
  688. object.put("upPath",upPath);
  689. }
  690. oldScene.setVideos(object.toJSONString());
  691. log.info("更新 scene.json");
  692. map.put("videos",oldScene.getVideos());
  693. }
  694. map.put("sceneName",oldScene.getSceneName());
  695. map.put("webSite",oldScene.getWebSite());
  696. map.put("thumb",oldScene.getThumb());
  697. map.put("num",oldScene.getNum());
  698. map.put("id",oldScene.getId());
  699. map.put("dataSource",oldScene.getDataSource());
  700. //复制完成更新相机容量
  701. oldEditScene.setId(null);
  702. oldEditScene.setProId(oldScene.getId());
  703. oldEditScene.setScreencapVoiceSrc(oldEditScene.getScreencapVoiceSrc() == null ? null : oldEditScene.getScreencapVoiceSrc().replace(oldNum, newNum));
  704. oldEditScene.setScreencapVoiceSound(oldEditScene.getScreencapVoiceSound() == null ? null : oldEditScene.getScreencapVoiceSound().replace(oldNum, newNum));
  705. oldEditScene.setScreencapVoiceSoundsync(oldEditScene.getScreencapVoiceSoundsync() == null ? null : oldEditScene.getScreencapVoiceSoundsync().replace(oldNum, newNum));
  706. oldEditScene.setPlayData(oldEditScene.getPlayData() == null ? null : oldEditScene.getPlayData().replace(oldNum, newNum));
  707. oldEditScene.setScreencapThumb(oldEditScene.getScreencapThumb() == null ? null : oldEditScene.getScreencapThumb().replace(oldNum, newNum));
  708. oldEditScene.setFloorPlanPng(oldEditScene.getFloorPlanPng() == null ? null : oldEditScene.getFloorPlanPng().replace(oldNum, newNum));
  709. sceneProEditService.save(oldEditScene);
  710. this.setQrCode(oldScene.getBuildType(), oldScene.getNum());
  711. CompletableFuture.runAsync(() -> {
  712. try {
  713. log.info("开始复制场景-{}", new Date());
  714. String buildModelPath = ConstantFilePath.BUILD_MODEL_PATH;
  715. if(oldScene.getSceneSource().equals(4)){
  716. buildModelPath = ConstantFilePath.BUILD_MODEL_LASER_PATH;
  717. }
  718. this.copyFdage(preDataSource,newDataSource,buildModelPath,time);
  719. //oss复制计算结果资源
  720. downloadFromOldNumFromOss(oldNum, newNum);
  721. // 复制本地secen.json
  722. copyOldSceneLocalToNew(oldNum, newNum);
  723. FileUtils.writeJsonFile(ConstantFilePath.SCENE_PATH + "data/data" + newNum + File.separator + "scene.json", map);
  724. //上传资源到oss
  725. uploadNewSceneToOss(newNum);
  726. FileUtils.delFolder(ConstantFilePath.SCENE_PATH + "images/images" + newNum);
  727. FileUtils.delFolder(ConstantFilePath.SCENE_PATH + "data/data" + newNum);
  728. FileUtils.delFolder(ConstantFilePath.SCENE_PATH + "voice/voice" + newNum);
  729. FileUtils.delFolder(ConstantFilePath.SCENE_PATH + "video/video" + newNum);
  730. FileUtils.copyDirectiory(ConstantFilePath.SCENE_PATH +"images/images" + oldNum,ConstantFilePath.SCENE_PATH +"images/images" + newNum);
  731. FileUtils.copyDirectiory(ConstantFilePath.SCENE_PATH +"data/data" + oldNum,ConstantFilePath.SCENE_PATH +"data/data" + newNum);
  732. FileUtils.writeJsonFile(ConstantFilePath.SCENE_PATH + "data/data" + newNum + File.separator + "scene.json", map);
  733. reloadFile(ConstantFilePath.SCENE_PATH + "data/data" + newNum + "/link-scene.json",oldNum, newNum);
  734. reloadFile(ConstantFilePath.SCENE_PATH + "data/data" + newNum + "/hot.json",oldNum, newNum);
  735. FileUtils.copyDirectiory(ConstantFilePath.SCENE_PATH +"voice/voice" + oldNum,ConstantFilePath.SCENE_PATH +"voice/voice" + newNum);
  736. FileUtils.copyDirectiory(ConstantFilePath.SCENE_PATH +"video/video" + oldNum,ConstantFilePath.SCENE_PATH +"video/video" + newNum);
  737. oldScene.setStatus(-2);
  738. this.updateById(oldScene);
  739. cameraDetail.setUsedSpace(cameraDetail.getUsedSpace() + oldScene.getSpace());
  740. cameraDetailService.updateById(cameraDetail);
  741. if(oldScene.getPayStatus() == -2){
  742. this.updateOssStatus(String.format(OssPath.v3_statusPath,oldScene.getNum()),-2);
  743. }
  744. log.info("复制场景结束-{}", new Date());
  745. if(oldScene.getSceneSource() == 4){ //深时复制
  746. String laserPath = laserService.copyDataSource(preDataSource,oldScene.getDataSource());
  747. laserService.copy(oldScene.getCameraId(),oldScene.getCreateTime(),newNum,2,laserPath,
  748. oldEditScene.getSceneKey(),oldScene.getSceneName(),oldScene.getUserId(),"V3");
  749. }
  750. sceneCopyLogService.saveByNum(oldNum,newNum,oldScene.getUserId());
  751. } catch (Exception e) {
  752. this.removeById(oldScene.getId());
  753. log.error("复制场景异常", e);
  754. }
  755. });
  756. return oldScene.getId();
  757. }
  758. /**
  759. * 从旧场景下载资源
  760. * @param sceneNum
  761. * @param newNum
  762. * @throws Exception
  763. */
  764. private void downloadFromOldNumFromOss(String sceneNum, String newNum) {
  765. ShellUtil.yunDownload("images/images" + sceneNum + "/", ConstantFilePath.SCENE_PATH + "images/images" + newNum);
  766. ShellUtil.yunDownload("data/data" + sceneNum + "/", ConstantFilePath.SCENE_PATH + "data/data" + newNum);
  767. ShellUtil.yunDownload("voice/voice" + sceneNum + "/", ConstantFilePath.SCENE_PATH + "voice/voice" + newNum);
  768. ShellUtil.yunDownload("video/video" + sceneNum + "/", ConstantFilePath.SCENE_PATH + "video/video" + newNum);
  769. }
  770. /**
  771. * 从本地旧场景拷贝资源到新场景
  772. * @param sceneNum
  773. * @param newNum
  774. */
  775. private void copyOldSceneLocalToNew(String sceneNum, String newNum) throws Exception {
  776. FileUtils.copyFolderAllFiles(ConstantFilePath.SCENE_PATH + "data/data" + sceneNum + "/",
  777. ConstantFilePath.SCENE_PATH + "data/data" + newNum + "/", true);
  778. reloadFile(ConstantFilePath.SCENE_PATH + "data/data" + newNum + "/link-scene.json",sceneNum, newNum);
  779. reloadFile(ConstantFilePath.SCENE_PATH + "data/data" + newNum + "/hot.json",sceneNum, newNum);
  780. }
  781. public void copyFdage(String oldDataSource,String newDataSource,String buildModelPath,String time) throws Exception {
  782. ShellUtil.yunDownload(ConstantFilePath.OSS_PREFIX + oldDataSource.replace(buildModelPath, "")+"/", newDataSource);
  783. // 修改data.fdage
  784. String data = FileUtils.readFile(newDataSource + "/data.fdage");
  785. JSONObject jsonObject = JSONObject.parseObject(data);
  786. if(ObjectUtils.isEmpty(jsonObject)){
  787. log.error("data.fdage文件不存在");
  788. throw new BusinessException(-1,"拷贝场景出错,data.fdage文件不存在");
  789. }
  790. jsonObject.put("uuidtime",time);
  791. FileUtils.writeFile(newDataSource + "/data.fdage", jsonObject.toJSONString());
  792. ShellUtil.yunUpload(newDataSource, ConstantFilePath.OSS_PREFIX + newDataSource.replace(buildModelPath, ""));
  793. // 复制计算结果
  794. ShellUtil.yunDownload(ConstantFilePath.OSS_PREFIX + oldDataSource.concat("_results/").replace(buildModelPath, ""), newDataSource.concat("_results"));
  795. if(new File(newDataSource.concat("_results")).exists()){
  796. ShellUtil.yunUpload(newDataSource.concat("_results"), ConstantFilePath.OSS_PREFIX + newDataSource.concat("_results").replace(buildModelPath, ""));
  797. FileUtils.delAllFile(newDataSource.concat("_results"));
  798. }
  799. FileUtils.delAllFile(newDataSource);
  800. try {
  801. FileUtils.copyFolderAllFiles(oldDataSource+"/",newDataSource+"/", true);
  802. }catch (Exception e){
  803. log.error("dataSource复制失败,{}不存在",oldDataSource);
  804. }
  805. }
  806. @Override
  807. public String setDataSource(String preDataSource,String time) throws Exception {
  808. SnowflakeIdGenerator snowflakeIdGenerator = new SnowflakeIdGenerator(0,1);
  809. String[] datasource = preDataSource.split("/");
  810. datasource[4] = snowflakeIdGenerator.nextId()+"";
  811. datasource[5] = datasource[5].split("_")[0] + "_" + time;
  812. return Arrays.stream(datasource).collect(Collectors.joining("/"));
  813. }
  814. @Override
  815. public String setVideos(String videos,String oldNum,String newNum) {
  816. JSONObject object = new JSONObject();
  817. if(StringUtils.isNotEmpty(videos)){
  818. object = JSONObject.parseObject(videos);
  819. if(object.containsKey("upPath")){
  820. String upPath = object.getString("upPath");
  821. upPath = upPath.replace(oldNum,newNum);
  822. object.put("upPath",upPath);
  823. }
  824. log.info("更新 scene.json");
  825. }
  826. return object.toJSONString();
  827. }
  828. private void reloadFile(String filePath,String sceneNum, String newNum) throws Exception {
  829. // 修改link-scene.json
  830. String content = FileUtils.readFile(filePath);
  831. if (!ObjectUtils.isEmpty(content)) {
  832. content = content.replaceAll(sceneNum, newNum);
  833. FileUtils.writeFile(filePath,content);
  834. }
  835. }
  836. /**
  837. * 上传新场景资源到oss
  838. * @param newNum
  839. */
  840. private void uploadNewSceneToOss(String newNum) {
  841. ShellUtil.yunUpload("/mnt/4Dkankan/scene/images/images" + newNum, "images/images" + newNum);
  842. ShellUtil.yunUpload("/mnt/4Dkankan/scene/data/data" + newNum, "data/data" + newNum);
  843. ShellUtil.yunUpload("/mnt/4Dkankan/scene/voice/voice" + newNum, "voice/voice" + newNum);
  844. ShellUtil.yunUpload("/mnt/4Dkankan/scene/video/video" + newNum, "video/video" + newNum);
  845. }
  846. @Override
  847. public void delete(String sceneNum) {
  848. if(StringUtils.isEmpty(sceneNum)){
  849. throw new BusinessException(LoginConstant.FAILURE_CODE_3001,LoginConstant.FAILURE_MSG_3001);
  850. }
  851. String[] nums = sceneNum.split(",");
  852. List<String> numList = Arrays.asList(nums);
  853. List<ScenePro> proList = this.getListByNums(numList);
  854. List<ScenePlus> plusList = scenePlusService.getListByNums(numList);
  855. this.deleteByList(proList,plusList);
  856. }
  857. @Override
  858. public void deleteByList(List<ScenePro> proList,List<ScenePlus> plusList) {
  859. HashMap<Long ,Long >cameraMap = new HashMap<>();
  860. if(proList.size() >0){
  861. for (ScenePro scenePro : proList) {
  862. cameraMap.merge(scenePro.getCameraId(), scenePro.getSpace(), Long::sum);
  863. }
  864. List<Long> proIds = proList.parallelStream().map(ScenePro::getId).collect(Collectors.toList());
  865. this.removeByIds(proIds);
  866. }
  867. if(plusList.size() >0){
  868. List<Long> plusIds = plusList.parallelStream().map(ScenePlus::getId).collect(Collectors.toList());
  869. HashMap<Long, ScenePlusExt> plusMap = scenePlusExtService.getByPlusIds(plusIds);
  870. for (ScenePlus scenePlus : plusList) {
  871. if(scenePlus.getCameraId()!= null && plusMap.get(scenePlus.getId())!=null){
  872. cameraMap.merge(scenePlus.getCameraId(), plusMap.get(scenePlus.getId()).getSpace(), Long::sum);
  873. }
  874. }
  875. scenePlusService.removeByIds(plusIds);
  876. scenePlusExtService.removeByPlusIds(plusIds);
  877. }
  878. //恢复相机使用容量
  879. if(cameraMap.size() >0){
  880. cameraDetailService.addUsedSpace(cameraMap);
  881. }
  882. }
  883. @Override
  884. public ScenePro getByUnicode(String unicode) {
  885. LambdaQueryWrapper<ScenePro> wrapper = new LambdaQueryWrapper<>();
  886. wrapper.like(ScenePro::getDataSource,unicode);
  887. wrapper.eq(ScenePro::getIsUpgrade,0);
  888. wrapper.orderByDesc(ScenePro::getCreateTime);
  889. List<ScenePro> list = this.list(wrapper);
  890. if(list !=null && list.size() >0){
  891. return list.get(0);
  892. }
  893. return null;
  894. }
  895. @Override
  896. public SceneInfoVo getInfo(String num) {
  897. if(StringUtils.isEmpty(num)){
  898. throw new BusinessException(LoginConstant.FAILURE_CODE_3001, LoginConstant.FAILURE_MSG_3001);
  899. }
  900. ScenePro scenePro = this.getByNum(num);
  901. ScenePlus scenePlus = scenePlusService.getByNum(num);
  902. if(scenePro == null && scenePlus == null){
  903. throw new BusinessException(SceneConstant.FAILURE_CODE_5005, SceneConstant.FAILURE_MSG_5005);
  904. }
  905. if(scenePro!=null && scenePro.getStatus() != 1 && scenePro.getStatus() != -2){
  906. throw new BusinessException(SceneConstant.FAILURE_CODE_5033, SceneConstant.FAILURE_MSG_5033);
  907. }
  908. if(scenePlus!=null && scenePlus.getSceneStatus() != 1 && scenePlus.getSceneStatus() != -2){
  909. throw new BusinessException(SceneConstant.FAILURE_CODE_5033, SceneConstant.FAILURE_MSG_5033);
  910. }
  911. if(scenePro!=null && scenePro.getPayStatus() != 1){
  912. throw new BusinessException(SceneConstant.FAILURE_CODE_5034, SceneConstant.FAILURE_MSG_5034);
  913. }
  914. if(scenePlus!=null && scenePlus.getPayStatus() != 1){
  915. throw new BusinessException(SceneConstant.FAILURE_CODE_5034, SceneConstant.FAILURE_MSG_5034);
  916. }
  917. SceneInfoVo infoVo = new SceneInfoVo();
  918. if(scenePro!=null){
  919. SceneProEdit sceneProEdit = sceneProEditService.getByProId(scenePro.getId());
  920. if(sceneProEdit !=null){
  921. BeanUtils.copyProperties(sceneProEdit, infoVo);
  922. }
  923. BeanUtils.copyProperties(scenePro,infoVo);
  924. if(StringUtils.isNotEmpty(scenePro.getGps())){
  925. infoVo.setGps(JSONObject.parseObject(scenePro.getGps()).toJSONString());
  926. }
  927. infoVo.setSceneKey("");
  928. infoVo.setCreateDate(DateUserUtil.getDate(scenePro.getCreateTime()).getTime());
  929. }else {
  930. SceneEditInfo sceneEditInfo = sceneEditInfoService.getByScenePlusId(scenePlus.getId());
  931. ScenePlusExt scenePlusExt = scenePlusExtService.getByPlusId(scenePlus.getId());
  932. if(sceneEditInfo != null){
  933. BeanUtils.copyProperties(sceneEditInfo,infoVo);
  934. }
  935. if(scenePlusExt !=null){
  936. BeanUtils.copyProperties(scenePlusExt,infoVo);
  937. }
  938. BeanUtils.copyProperties(scenePlus,infoVo);
  939. infoVo.setSceneName(scenePlus.getTitle());
  940. infoVo.setStatus(scenePlus.getSceneStatus());
  941. }
  942. if(StringUtils.isNotEmpty(infoVo.getEntry())){
  943. infoVo.setEntry(JSONObject.parseObject(infoVo.getEntry()).toJSONString());
  944. }
  945. if(StringUtils.isEmpty(infoVo.getSceneKey())){
  946. infoVo.setIsPublic(0);
  947. }else {
  948. infoVo.setIsPublic(1);
  949. }
  950. //查询是否有随心装场景
  951. if(num.contains("vr-")){
  952. infoVo.setVrNum(null);
  953. infoVo.setVideosUser(null);
  954. infoVo.setBgMusicName(null);
  955. infoVo.setBgMusic(null);
  956. }
  957. infoVo.setSceneVer(innerService.getSceneNumVersion(num));
  958. infoVo.setVersion(infoVo.getSceneVer());
  959. this.updateViewCount(num);
  960. return infoVo;
  961. }
  962. @Override
  963. public void updateViewCount(String num) {
  964. String redisKey = RedisKey.SCENE_VISIT_CNT;
  965. if(!redisUtil.hHasKey(redisKey,num)){
  966. Integer count = 0;
  967. ScenePro pro = this.getByNum(num);
  968. if(pro !=null){
  969. count = pro.getViewCount() == null ? 0 : pro.getViewCount();
  970. }else {
  971. ScenePlus plus = scenePlusService.getByNum(num);
  972. if(plus !=null){
  973. ScenePlusExt scenePlusExt = scenePlusExtService.getByPlusId(plus.getId());
  974. if(scenePlusExt != null){
  975. count = scenePlusExt.getViewCount() == null ? 0 :scenePlusExt.getViewCount();
  976. }
  977. }
  978. }
  979. redisUtil.hset(redisKey,num,String.valueOf(count));
  980. }
  981. redisUtil.hincr(redisKey,num,1);
  982. }
  983. @Override
  984. public void updateDbViewCount(String num, String count) {
  985. LambdaUpdateWrapper<ScenePro> wrapper = new LambdaUpdateWrapper<>();
  986. wrapper.eq(ScenePro::getNum,num);
  987. wrapper.set(ScenePro::getViewCount,count);
  988. this.update(wrapper);
  989. ScenePlus plus = scenePlusService.getByNum(num);
  990. if(plus !=null){
  991. LambdaUpdateWrapper<ScenePlusExt> exWr = new LambdaUpdateWrapper<>();
  992. exWr.eq(ScenePlusExt::getPlusId,plus.getId())
  993. .set(ScenePlusExt::getViewCount,count);
  994. scenePlusExtService.update(exWr);
  995. }
  996. }
  997. @Override
  998. public ScenePro findByFileId(String fileId) {
  999. LambdaQueryWrapper<ScenePro> wrapper = new LambdaQueryWrapper<>();
  1000. wrapper.like(ScenePro::getDataSource,fileId);
  1001. List<ScenePro> list = this.list(wrapper);
  1002. if(list !=null && list.size() >0){
  1003. return list.get(0);
  1004. }
  1005. return null;
  1006. }
  1007. @Override
  1008. public void setQrCode(String buildType,String num) throws Exception {
  1009. String basePath = NacosProperty.getMainUrl();
  1010. if("V2".equals(buildType)){
  1011. basePath += sceneProUrl;
  1012. }
  1013. if("V3".equals(buildType)){
  1014. basePath += sceneProNewUrl;
  1015. }
  1016. MatrixToImageWriterUtil.createQRCode(basePath + num, ConstantFilePath.BASE_PATH + File.separator + "sceneQRcode/"+num+".png", false,null);
  1017. MatrixToImageWriterUtil.createQRCode(basePath + num + "&lang=en", ConstantFilePath.BASE_PATH + File.separator + "sceneQRcode/"+num+"_en.png", false,null);
  1018. }
  1019. @Override
  1020. public void generateObjFile(String num) {
  1021. LambdaQueryWrapper<ScenePro> wrapper = new LambdaQueryWrapper<>();
  1022. wrapper.eq(ScenePro::getNum,num).eq(ScenePro::getRecStatus,"A");
  1023. ScenePro sceneProEntity = this.getOne(wrapper);
  1024. if(ObjectUtils.isEmpty(sceneProEntity)){
  1025. generatePlusObjFile(num);
  1026. return;
  1027. }
  1028. if(sceneProEntity.getSceneSource() != 4){
  1029. throw new BusinessException(LoginConstant.FAILURE_CODE_3003, "只能操作激光场景");
  1030. }
  1031. // 拷贝文件
  1032. String path = sceneProEntity.getDataSource();
  1033. if (!new File(path + "/caches/reconstruction/final.bin").exists()
  1034. || !new File(path + "/caches/reconstruction/chunk.json").exists()
  1035. || !new File(path + "/caches/images").exists()
  1036. || !new File(path + "/caches/depthmap").exists()
  1037. || !new File(path + "/caches/depthmap_csc").exists()
  1038. || !new File(path + "/caches/panorama.json").exists()
  1039. || !new File(path + "/results/laserData/laser.ply").exists()) {
  1040. log.error("生成obj缺少必要文件,生成失败!");
  1041. throw new BusinessException(SceneConstant.FAILURE_CODE_5038, SceneConstant.FAILURE_MSG_5038);
  1042. }
  1043. // 获取最新的场景名称
  1044. JSONObject sceneInfo = fdkkLaserService.getSceneByNum(sceneProEntity.getNum());
  1045. LambdaUpdateWrapper<ScenePro> updateWrapper = new LambdaUpdateWrapper<>();
  1046. updateWrapper
  1047. .set(ScenePro::getStatus, 0)
  1048. .set(ScenePro::getIsObj, 1)
  1049. .set(ScenePro::getIsUpgrade, 2) // 升级中
  1050. .set(ScenePro::getSceneName, sceneInfo.getString("title"))
  1051. .eq(ScenePro::getNum, sceneProEntity.getNum());
  1052. this.update(updateWrapper);
  1053. //同步到scenePlus、scenePlus
  1054. //同步到scenePlus、scenePlus
  1055. ScenePlus scenePlus = scenePlusService.getByNum(num);
  1056. if(Objects.nonNull(scenePlus)){
  1057. sceneUpgradeMapper.deleteScenePlus(num);
  1058. sceneUpgradeMapper.deleteScenePlusExt(scenePlus.getId());
  1059. }
  1060. sceneUpgradeMapper.transferScenePlus(num);
  1061. scenePlus = scenePlusService.getByNum(num);
  1062. String sceneKind = sceneProEntity.getSceneScheme() == 3 ? SceneKind.FACE.code():SceneKind.TILES.code();
  1063. sceneUpgradeMapper.transferScenePlusExt(num, scenePlus.getId(), sceneKind);
  1064. log.info("开始发送激光场景生成obj mq消息");
  1065. // 发送MQ
  1066. BuildSceneCallMessage mqMsg = new BuildSceneCallMessage();
  1067. mqMsg.setSceneNum(sceneProEntity.getNum());
  1068. mqMsg.setAlgorithm(sceneProEntity.getAlgorithm());
  1069. mqMsg.setBuildType(sceneProEntity.getBuildType());
  1070. mqMsg.setPath(sceneProEntity.getDataSource());
  1071. mqProducer.sendByWorkQueue(queueObjModelingPre,mqMsg);
  1072. }
  1073. public void generatePlusObjFile(String num) {
  1074. ScenePlus scenePlus = scenePlusService.getByNum(num);
  1075. if(ObjectUtils.isEmpty(scenePlus)){
  1076. throw new BusinessException(SceneConstant.FAILURE_CODE_5005, SceneConstant.FAILURE_MSG_5005);
  1077. }
  1078. if(scenePlus.getSceneSource() != 4){
  1079. throw new BusinessException(LoginConstant.FAILURE_CODE_3003, "只能操作激光场景");
  1080. }
  1081. ScenePlusExt scenePlusExt = scenePlusExtService.getByPlusId(scenePlus.getId());
  1082. // 拷贝文件
  1083. String path = scenePlusExt.getDataSource();
  1084. if (!new File(path + "/caches/reconstruction/final.bin").exists()
  1085. || !new File(path + "/caches/reconstruction/chunk.json").exists()
  1086. || !new File(path + "/caches/images").exists()
  1087. || !new File(path + "/caches/depthmap").exists()
  1088. || !new File(path + "/caches/depthmap_csc").exists()
  1089. || !new File(path + "/caches/panorama.json").exists()
  1090. || !new File(path + "/results/laserData/laser.ply").exists()) {
  1091. log.error("生成obj缺少必要文件,生成失败!");
  1092. throw new BusinessException(SceneConstant.FAILURE_CODE_5038, SceneConstant.FAILURE_MSG_5038);
  1093. }
  1094. // 获取最新的场景名称
  1095. JSONObject sceneInfo = fdkkLaserService.getSceneByNum(num);
  1096. LambdaUpdateWrapper<ScenePlus> updateWrapper = new LambdaUpdateWrapper<>();
  1097. updateWrapper
  1098. .set(ScenePlus::getSceneStatus, 0)
  1099. .set(ScenePlus::getTitle, sceneInfo.getString("title"))
  1100. .eq(ScenePlus::getNum, num);
  1101. scenePlusService.update(updateWrapper);
  1102. LambdaUpdateWrapper<ScenePlusExt> plusExtUpdateWrapper = new LambdaUpdateWrapper<>();
  1103. plusExtUpdateWrapper
  1104. .set(ScenePlusExt::getIsObj, 1)
  1105. .eq(ScenePlusExt::getPlusId, scenePlus.getId());
  1106. scenePlusExtService.update(plusExtUpdateWrapper);
  1107. log.info("开始发送激光场景生成obj mq消息");
  1108. // 发送MQ
  1109. BuildSceneCallMessage mqMsg = new BuildSceneCallMessage();
  1110. mqMsg.setSceneNum(num);
  1111. mqMsg.setAlgorithm(scenePlusExt.getAlgorithm());
  1112. mqMsg.setBuildType(scenePlusExt.getBuildType());
  1113. mqMsg.setPath(scenePlusExt.getDataSource());
  1114. mqProducer.sendByWorkQueue(queueObjModelingPre,mqMsg);
  1115. }
  1116. @Override
  1117. public ResponseScene getSceneDetail(String sceneNum) {
  1118. if(StringUtils.isBlank(sceneNum)){
  1119. return null;
  1120. }
  1121. ResponseScene vo = new ResponseScene();
  1122. Scene sceneEntity = sceneService.getByNum(sceneNum);
  1123. if(sceneEntity != null){
  1124. return getResponseScene(vo, sceneEntity);
  1125. }
  1126. ScenePro sceneProEntity = this.getByNum(sceneNum);
  1127. if(sceneProEntity != null){
  1128. SceneProEdit sceneProEditEntity = sceneProEditService.getByProId(sceneProEntity.getId());
  1129. return getResponseProScene(vo, sceneProEntity, sceneProEditEntity);
  1130. }
  1131. ScenePlus scenePlus = scenePlusService.getByNum(sceneNum);
  1132. if(scenePlus != null){
  1133. ScenePlusExt ext = scenePlusExtService.getByPlusId(scenePlus.getId());
  1134. SceneEditInfo sceneEditInfo = sceneEditInfoService.getByScenePlusId(scenePlus.getId());
  1135. return getResponseProScene(vo, scenePlus, ext,sceneEditInfo);
  1136. }
  1137. return vo;
  1138. }
  1139. private ResponseScene getResponseScene(ResponseScene vo, Scene sceneEntity) {
  1140. if (sceneEntity != null){
  1141. BeanUtils.copyProperties(sceneEntity, vo);
  1142. vo.setThumbImg(sceneEntity.getThumbStatus());
  1143. vo.setCreateTime(new DateTime(sceneEntity.getCreateTime()).toString("yyyy-MM-dd HH:mm"));
  1144. vo.setSceneIndex(sceneEntity.getStyle());
  1145. vo.setHasBGM(sceneEntity.getBgMusic());
  1146. vo.setCameraType(sceneEntity.getSceneScheme());
  1147. vo.setIsPublic(StringUtils.isEmpty(sceneEntity.getSceneKey()) ? 0 : 1);
  1148. }
  1149. return vo;
  1150. }
  1151. private ResponseScene getResponseProScene(ResponseScene vo, ScenePro sceneProEntity, SceneProEdit sceneProEditEntity) {
  1152. if (sceneProEntity != null){
  1153. BeanUtils.copyProperties(sceneProEditEntity, vo);
  1154. BeanUtils.copyProperties(sceneProEntity, vo);
  1155. vo.setCreateTime(DateUserUtil.getMDate(vo.getCreateTime()));
  1156. vo.setCameraType(sceneProEntity.getSceneScheme());
  1157. vo.setThumbImg(sceneProEditEntity.getThumbStatus());
  1158. vo.setHasBGM(sceneProEditEntity.getBgMusic());
  1159. vo.setIsPublic(StringUtils.isEmpty(sceneProEditEntity.getSceneKey()) ? 0 : 1);
  1160. }
  1161. return vo;
  1162. }
  1163. private ResponseScene getResponseProScene(ResponseScene vo, ScenePlus plus, ScenePlusExt ext,SceneEditInfo editInfo) {
  1164. if (plus != null){
  1165. BeanUtils.copyProperties(plus, vo);
  1166. BeanUtils.copyProperties(ext, vo);
  1167. BeanUtils.copyProperties(editInfo, vo);
  1168. vo.setNum(plus.getNum());
  1169. vo.setSceneName(plus.getTitle());
  1170. vo.setCreateTime(DateUserUtil.getMDate(vo.getCreateTime()));
  1171. vo.setCameraType(ext.getSceneScheme());
  1172. vo.setThumbImg(1);
  1173. vo.setHasBGM(editInfo.getMusic());
  1174. vo.setIsPublic(StringUtils.isEmpty(editInfo.getScenePassword()) ? 0 : 1);
  1175. }
  1176. return vo;
  1177. }
  1178. }