SceneProServiceImpl.java 59 KB

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