SceneCooperationServiceImpl.java 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640
  1. package com.fdkankan.ucenter.service.impl;
  2. import com.alibaba.fastjson.JSONObject;
  3. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  4. import com.fdkankan.common.constant.SceneConstant;
  5. import com.fdkankan.redis.util.RedisUtil;
  6. import com.fdkankan.ucenter.common.PageInfo;
  7. import com.fdkankan.ucenter.common.RedisKeyUtil;
  8. import com.fdkankan.ucenter.common.SceneSourceUtil;
  9. import com.fdkankan.ucenter.common.constants.NacosProperty;
  10. import com.fdkankan.ucenter.common.constants.ResultCode;
  11. import com.fdkankan.ucenter.constant.LoginConstant;
  12. import com.fdkankan.ucenter.entity.*;
  13. import com.fdkankan.ucenter.exception.BusinessException;
  14. import com.fdkankan.ucenter.httpClient.service.LaserService;
  15. import com.fdkankan.ucenter.mapper.ISceneCooperationMapper;
  16. import com.fdkankan.ucenter.service.*;
  17. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  18. import com.fdkankan.ucenter.util.DateUserUtil;
  19. import com.fdkankan.ucenter.vo.request.SceneCooperationParam;
  20. import com.fdkankan.ucenter.vo.request.SceneParam;
  21. import com.google.common.collect.Lists;
  22. import com.sun.org.apache.bcel.internal.generic.RET;
  23. import org.apache.commons.lang3.StringUtils;
  24. import org.opencv.face.Face;
  25. import org.springframework.beans.factory.annotation.Autowired;
  26. import org.springframework.stereotype.Service;
  27. import org.springframework.util.CollectionUtils;
  28. import org.springframework.util.ObjectUtils;
  29. import java.util.*;
  30. import java.util.stream.Collectors;
  31. /**
  32. * <p>
  33. * 服务实现类
  34. * </p>
  35. *
  36. * @author
  37. * @since 2022-07-04
  38. */
  39. @Service
  40. public class SceneCooperationServiceImpl extends ServiceImpl<ISceneCooperationMapper, SceneCooperation> implements ISceneCooperationService {
  41. @Autowired
  42. ISceneProService sceneProService;
  43. @Autowired
  44. ISceneService sceneService;
  45. @Autowired
  46. IScenePlusService scenePlusService;
  47. @Autowired
  48. IScenePlusExtService scenePlusExtService;
  49. @Autowired
  50. LaserService fdkkLaserService;
  51. @Autowired
  52. ISceneResourceCooperationService sceneResourceCooperationService;
  53. @Autowired
  54. ICameraService cameraService;
  55. @Autowired
  56. ICameraDetailService cameraDetailService;
  57. @Autowired
  58. ISceneResourceService sceneResourceService;
  59. @Autowired
  60. IUserService userService;
  61. @Autowired
  62. RedisUtil redisUtil;
  63. @Autowired
  64. IMailTemplateService mailTemplateService;
  65. @Autowired
  66. IProductOrderService productOrderService;
  67. @Autowired
  68. IProductCooperationService productCooperationService;
  69. @Autowired
  70. ISceneCooperationCountService sceneCooperationCountService;
  71. @Override
  72. public Long getCooperationSceneNum(Long userId, List<Integer> sceneSourceList) {
  73. Long cooperationSceneProNum = this.getBaseMapper().getCooperationSceneProNum(userId, sceneSourceList);
  74. Long cooperationScenePlusNum = this.getBaseMapper().getCooperationScenePlusNum(userId, sceneSourceList);
  75. return cooperationSceneProNum + cooperationScenePlusNum;
  76. }
  77. @Override
  78. public void deleteCooperationList(List<ScenePro> sceneProList,List<ScenePlus> scenePlusList,List<Long> userIds) {
  79. if(CollectionUtils.isEmpty(sceneProList) && CollectionUtils.isEmpty(scenePlusList)){
  80. return;
  81. }
  82. List<String> numList = sceneProList.stream().map(ScenePro::getNum).collect(Collectors.toList());
  83. List<String> numList2 = scenePlusList.stream().map(ScenePlus::getNum).collect(Collectors.toList());
  84. numList.addAll(numList2);
  85. this.deleteCooperationList(numList,userIds,null);
  86. }
  87. @Override
  88. public void deleteCooperationList(List<String> numList,List<Long> userIds,String sceneType) {
  89. if(CollectionUtils.isEmpty(numList) ){
  90. return;
  91. }
  92. LambdaQueryWrapper<SceneCooperation> wrapper = new LambdaQueryWrapper<>();
  93. wrapper.in(SceneCooperation::getSceneNum,numList);
  94. if(sceneType != null){
  95. wrapper.eq(SceneCooperation::getSceneType,sceneType);
  96. }
  97. if(userIds != null && !userIds.isEmpty()){
  98. wrapper.in(SceneCooperation::getUserId,userIds);
  99. }
  100. List<SceneCooperation> list = this.list(wrapper);
  101. List<Long> ids = list.stream().map(SceneCooperation::getId).collect(Collectors.toList());
  102. if(ids.size() >0){
  103. this.removeByIds(ids);
  104. sceneResourceCooperationService.deleteBatchByCooperationIds(ids);
  105. }
  106. for (String num : numList) {
  107. redisUtil.hdel(RedisKeyUtil.SCENE_COOPERATION_NUM_USERID,num);
  108. }
  109. }
  110. @Override
  111. public List<SceneCooperation> saveBatchByList(List<String> numList, List<Long> userIds,String type,String sceneType) {
  112. List<SceneCooperation> list = new ArrayList<>();
  113. List<String> delList = new ArrayList<>();
  114. HashMap<String, List<User>> byNumList = this.getByNumList(numList,sceneType);
  115. for (Long userId : userIds) {
  116. for (String num : numList) {
  117. List<User> users = byNumList.get(num);
  118. if(users != null && !users.isEmpty()){
  119. List<Long> collect1 = users.stream().map(User::getId).collect(Collectors.toList());
  120. if("scene".equals(type) && numList.size() == 1){
  121. for (Long l : collect1) {
  122. if(!userIds.contains(l)){
  123. delList.add(num + "," +l);
  124. }
  125. }
  126. }
  127. if(collect1.contains(userId)){
  128. continue;
  129. }
  130. }
  131. SceneCooperation sceneCooperationEntity = new SceneCooperation();
  132. sceneCooperationEntity.setUserId(userId);
  133. sceneCooperationEntity.setSceneNum(num);
  134. sceneCooperationEntity.setSceneType(sceneType);
  135. sceneCooperationEntity.setRecStatus("A");
  136. sceneCooperationEntity.setCreateTime(DateUserUtil.getDate(new Date()));
  137. sceneCooperationEntity.setUpdateTime(DateUserUtil.getDate(new Date()));
  138. list.add(sceneCooperationEntity);
  139. }
  140. }
  141. if(!list.isEmpty()){
  142. for (SceneCooperation sceneCooperation : list) {
  143. redisUtil.hset(RedisKeyUtil.SCENE_COOPERATION_NUM_USERID, sceneCooperation.getSceneNum(), sceneCooperation.getUserId() + "");
  144. }
  145. this.saveBatch(list);
  146. }
  147. for (String num : delList) {
  148. String[] split = num.split(",");
  149. delCooperation(split[0],Long.valueOf(split[1]));
  150. }
  151. return list;
  152. }
  153. private void delCooperation(String num ,Long userId){
  154. redisUtil.hdel(RedisKeyUtil.SCENE_COOPERATION_NUM_USERID,num,userId.toString());
  155. LambdaQueryWrapper<SceneCooperation> wrapper = new LambdaQueryWrapper<>();
  156. wrapper.eq(SceneCooperation::getSceneNum,num);
  157. wrapper.eq(SceneCooperation::getUserId,userId);
  158. this.remove(wrapper);
  159. }
  160. @Override
  161. public JSONObject sceneResourceList(SceneCooperationParam param) {
  162. JSONObject jsonObject = new JSONObject();
  163. List<SceneResource> exclude = new ArrayList<>();
  164. SceneResource excludeEntity = new SceneResource();
  165. excludeEntity.setKeyWord("data");
  166. exclude.add(excludeEntity);
  167. jsonObject.put("exclude", exclude);
  168. if(param.getCameraId() != null){
  169. Camera cameraEntity = cameraService.getById(param.getCameraId());
  170. if(cameraEntity != null){
  171. CameraDetail cameraDetailEntity = cameraDetailService.getByCameraId(cameraEntity.getId());
  172. if(cameraDetailEntity.getCompanyId() != null && cameraDetailEntity.getCompanyId() == 1){
  173. jsonObject.put("exclude", new ArrayList<>());
  174. }
  175. }
  176. }
  177. List<ScenePro> sceneProList;
  178. List<ScenePlus> scenePlusList;
  179. //如果是场景协作,判断是V3的场景还是V4的场景,如果是v4场景,则查询v4的菜单资源
  180. if(param.getType() != null && param.getType() == 1){
  181. if(StringUtils.isEmpty(param.getSceneNum()) && CollectionUtils.isEmpty(param.getNumList())){
  182. throw new BusinessException(LoginConstant.FAILURE_CODE_3001, LoginConstant.FAILURE_MSG_3001);
  183. }
  184. List<String> numList = param.getNumList();
  185. if(numList == null){
  186. numList= new ArrayList<>();
  187. numList.add(param.getSceneNum());
  188. }
  189. sceneProList = sceneProService.getListByNums(numList);
  190. scenePlusList = scenePlusService.getListByNums(numList);
  191. if(CollectionUtils.isEmpty(sceneProList) && CollectionUtils.isEmpty(scenePlusList)){
  192. throw new BusinessException(SceneConstant.FAILURE_CODE_5005, SceneConstant.FAILURE_MSG_5005);
  193. }
  194. }else {
  195. //如果是相机协作,判断这个相机id下有没有V4的场景,如果有,则列出对应的菜单资源
  196. if(ObjectUtils.isEmpty(param.getCameraId()) && CollectionUtils.isEmpty(param.getCameraIdList())){
  197. throw new BusinessException(LoginConstant.FAILURE_CODE_3001, LoginConstant.FAILURE_MSG_3001);
  198. }
  199. List<Long> cameraIdList = param.getCameraIdList();
  200. if(cameraIdList == null){
  201. cameraIdList= new ArrayList<>();
  202. cameraIdList.add(param.getCameraId());
  203. }
  204. sceneProList = sceneProService.getListByCameraIds(cameraIdList);
  205. scenePlusList = scenePlusService.getListByCameraIds(cameraIdList);
  206. }
  207. List<String> versionList = Lists.newArrayList();
  208. if(sceneProList.size() >0){
  209. versionList.add("v3");
  210. }
  211. if(scenePlusList.size() >0){
  212. versionList.add("v4");
  213. }
  214. if(versionList.size() <=0){
  215. versionList.add("v3");
  216. }
  217. List<SceneResource> results = sceneResourceService.getByVersion(versionList);
  218. jsonObject.put("include", results);
  219. return jsonObject;
  220. }
  221. @Override
  222. public JSONObject cooperationSceneListNew(SceneParam param, String username) {
  223. User user = userService.getByUserName(username);
  224. LambdaQueryWrapper<SceneCooperation> wrapper = new LambdaQueryWrapper<>();
  225. wrapper.eq(SceneCooperation::getUserId,user.getId());
  226. wrapper.eq(SceneCooperation::getSceneType,"mesh");
  227. List<SceneCooperation> list = this.list(wrapper);
  228. List<String> numList = list.parallelStream().map(SceneCooperation::getSceneNum).collect(Collectors.toList());
  229. if(numList.size() <=0){
  230. JSONObject jsonObject = new JSONObject();
  231. jsonObject.put("list", new ArrayList<>());
  232. jsonObject.put("total",0);
  233. return jsonObject;
  234. }
  235. param.setNumList(numList);
  236. param.setHasFolder(0);
  237. JSONObject jsonObject = sceneProService.newList(param, username);
  238. return jsonObject.getJSONObject("pageInfo") ;
  239. }
  240. @Override
  241. public void saveCooperation(SceneCooperationParam param, String loginUserName) {
  242. if(StringUtils.isEmpty(param.getUserName()) || StringUtils.isEmpty(param.getSceneNum())){
  243. throw new BusinessException(LoginConstant.FAILURE_CODE_3001, LoginConstant.FAILURE_MSG_3001);
  244. }
  245. if(param.getUserName().equals( loginUserName)){
  246. throw new BusinessException(LoginConstant.FAILURE_CODE_3024, LoginConstant.FAILURE_MSG_3024);
  247. }
  248. User loginUser = userService.getByUserName(loginUserName);
  249. User user = userService.getByUserName(param.getUserName());
  250. if(user == null){
  251. throw new BusinessException(LoginConstant.FAILURE_CODE_3021, LoginConstant.FAILURE_MSG_3021);
  252. }
  253. String[] nums = param.getSceneNum().split(",");
  254. List<String> numList = Arrays.asList(nums);
  255. List<ScenePro> proList = sceneProService.getListByNums(numList);
  256. List<ScenePlus> plusList = scenePlusService.getListByNums(numList);
  257. //this.deleteCooperationList(proList,plusList,Arrays.asList(user.getId()));
  258. saveCooperationCommon(loginUser,param.getLang(),Arrays.asList(user),proList,plusList,null,"scene","mesh");
  259. }
  260. @Override
  261. public ProductOrder saveBatchCooperation(SceneCooperationParam param, String loginUserName) {
  262. if( StringUtils.isEmpty(param.getSceneNum())){
  263. throw new BusinessException(LoginConstant.FAILURE_CODE_3001, LoginConstant.FAILURE_MSG_3001);
  264. }
  265. String[] nums = param.getSceneNum().split(",");
  266. List<String> numList = Arrays.asList(nums);
  267. if(param.getUserNameList() == null || param.getUserNameList().isEmpty()){
  268. this.deleteCooperationList(numList,null,param.getSceneType());
  269. laserService.saveBatchCooperation(numList,new ArrayList<>(),param.getUserNameList(),"scene","update");
  270. return null;
  271. }
  272. if(param.getUserNameList().contains( loginUserName)){
  273. throw new BusinessException(LoginConstant.FAILURE_CODE_3024, LoginConstant.FAILURE_MSG_3024);
  274. }
  275. if(param.getUserNameList().size() != new HashSet<>(param.getUserNameList()).size()){
  276. throw new BusinessException(ResultCode.COO_LIMIT_ERROR2);
  277. }
  278. if(param.getUserNameList().size() >5){
  279. throw new BusinessException(ResultCode.COO_LIMIT_ERROR);
  280. }
  281. User loginUser = userService.getByUserName(loginUserName);
  282. if(numList.size() >1){
  283. HashMap<String, List<User>> byNumList = this.getByNumList(numList, param.getSceneType());
  284. for (String num : numList) {
  285. if(byNumList.get(num) != null && !byNumList.isEmpty()) {
  286. if (byNumList.get(num).size() + param.getUserNameList().size() > 5) {
  287. throw new BusinessException(ResultCode.COO_LIMIT_ERROR);
  288. }
  289. List<User> users = byNumList.get(num);
  290. Set<String> collect = users.stream().map(User::getUserName).collect(Collectors.toSet());
  291. if(collect.containsAll(param.getUserNameList())){
  292. throw new BusinessException(ResultCode.COO_LIMIT_ERROR2);
  293. }
  294. }
  295. }
  296. }
  297. List<User> users = new ArrayList<>();
  298. for (String userName : param.getUserNameList()) {
  299. User user = userService.getByUserName(userName);
  300. if(user == null){
  301. throw new BusinessException(LoginConstant.FAILURE_CODE_3021, LoginConstant.FAILURE_MSG_3021);
  302. }
  303. users.add(user);
  304. }
  305. ProductOrder productOrder = checkNeedPay(numList, users, loginUser, param.getPayType(), param.getTimeZone(),null,param.getLang(),param.getSceneType());
  306. if(productOrder == null){
  307. successAddCooperation(numList,users,param.getLang(),loginUser,param.getSceneType());
  308. }
  309. return productOrder;
  310. }
  311. @Override
  312. public ProductOrder saveCamera(SceneCooperationParam param, String loginUserName) {
  313. if(param.getCameraId() == null || StringUtils.isEmpty(param.getUserName())){
  314. throw new BusinessException(LoginConstant.FAILURE_CODE_3001, LoginConstant.FAILURE_MSG_3001);
  315. }
  316. if(param.getUserName().equals( loginUserName)){
  317. throw new BusinessException(LoginConstant.FAILURE_CODE_3025, LoginConstant.FAILURE_MSG_3025);
  318. }
  319. User user = userService.getByUserName(param.getUserName());
  320. if(user == null){
  321. throw new BusinessException(LoginConstant.FAILURE_CODE_3015,LoginConstant.FAILURE_MSG_3015);
  322. }
  323. User loginUser = userService.getByUserName(loginUserName);
  324. Camera camera = cameraService.getById(param.getCameraId());
  325. CameraDetail cameraDetail = cameraDetailService.getByCameraId(param.getCameraId());
  326. if(camera == null || cameraDetail == null || cameraDetail.getUserId() == null){
  327. throw new BusinessException(ResultCode.CAMERA_NOT_EXIT);
  328. }
  329. if(!loginUser.getId().equals(cameraDetail.getUserId())){
  330. throw new BusinessException(ResultCode.NOT_PER);
  331. }
  332. if(cameraDetail.getCooperationUser() != null){
  333. throw new BusinessException(ResultCode.COO_ERROR);
  334. }
  335. List<ScenePro> v3List = sceneProService.getListByCameraId(param.getCameraId());
  336. List<ScenePlus> v4List = scenePlusService.getListByCameraId(param.getCameraId());
  337. List<String> v3NumList = v3List.stream().map(ScenePro::getNum).collect(Collectors.toList());
  338. List<String> v4NumList = v4List.stream().map(ScenePlus::getNum).collect(Collectors.toList());
  339. List<String> allList = new ArrayList<>();
  340. allList.addAll(v3NumList);
  341. allList.addAll(v4NumList);
  342. ProductOrder productOrder = checkNeedPay(allList, Arrays.asList(user), loginUser, param.getPayType(), param.getTimeZone(),param.getCameraId(),param.getLang(),null);
  343. if(productOrder == null){
  344. successAddCooperation(v3List,v4List,Arrays.asList(user),param.getLang(),loginUser,Arrays.asList(camera),null);
  345. cameraDetailService.updateCooperationByIds(Arrays.asList(param.getCameraId()),user.getId());
  346. }
  347. return productOrder;
  348. }
  349. private void successAddCooperation(List<String> numList, List<User> users, String lang, User loginUser,String sceneType){
  350. List<ScenePro> proList = sceneProService.getListByNums(numList);
  351. List<ScenePlus> plusList = scenePlusService.getListByNums(numList);
  352. List<Long> userIds = users.stream().map(User::getId).collect(Collectors.toList());
  353. //this.deleteCooperationList(proList,plusList,userIds);
  354. saveCooperationCommon(loginUser,lang,users,proList,plusList,null,"scene",sceneType);
  355. }
  356. @Override
  357. public void successAddCooperation(List<String> numList,List<Long> userIds,Long loginUserId,Long cameraId,String lang,String sceneType,List<ProductCooperation> needPay ){
  358. //this.deleteCooperationList(numList,userIds);
  359. List<ScenePro> proList = sceneProService.getListByNums(numList);
  360. List<ScenePlus> plusList = scenePlusService.getListByNums(numList);
  361. List<User> users = userService.listByIds(userIds);
  362. User user = userService.getById(loginUserId);
  363. if(cameraId != null ){
  364. Camera camera = cameraService.getById(cameraId);
  365. saveCooperationCommon(user,lang,users,proList,plusList,Arrays.asList(camera),"camera",sceneType);
  366. cameraDetailService.updateCooperationByIds(Arrays.asList(cameraId),userIds.get(0));
  367. }else {
  368. saveCooperationCommon(user,lang,users,proList,plusList,null,"scene",sceneType);
  369. }
  370. sceneCooperationCountService.saveCount(needPay,userIds.size());
  371. }
  372. private void successAddCooperation(List<ScenePro> v3List,List<ScenePlus> v4List,List<User> users,String lang,User loginUser,List<Camera>cameraList,String sceneType){
  373. List<Long> userIds = users.stream().map(User::getId).collect(Collectors.toList());
  374. //this.deleteCooperationList(v3List,v4List,userIds);
  375. saveCooperationCommon(loginUser,lang,users,v3List,v4List,cameraList,"camera",sceneType);
  376. }
  377. private ProductOrder checkNeedPay(List<String> numList, List<User> users,User loginUser,Integer payType,Integer timeZone,Long cameraId,String lang,String sceneType) {
  378. HashMap<String,String> needNumListMesh = new HashMap<>();
  379. HashMap<String,String> needNumListLaser =new HashMap<>();
  380. if(StringUtils.isBlank(sceneType)){
  381. needNumListMesh = getTotalCount(numList,users,"mesh",cameraId);
  382. needNumListLaser = getTotalCount(numList,users,"laser",cameraId);
  383. }else {
  384. needNumListMesh = getTotalCount(numList,users,sceneType,cameraId);
  385. }
  386. if(needNumListMesh.size() + needNumListLaser.size()<=0){
  387. return null;
  388. }
  389. ProductOrder productOrder = productOrderService.createOrder(needNumListMesh.size() + needNumListLaser.size(), "cooperation", loginUser, payType, timeZone, cameraId, lang, sceneType);
  390. productCooperationService.add(productOrder,users,numList,needNumListMesh,needNumListLaser,sceneType);
  391. return productOrder;
  392. }
  393. private HashMap<String,String> getTotalCount(List<String> numList, List<User> users,String sceneType,Long cameraId){
  394. List<String> forList = new ArrayList<>(numList);
  395. List<SceneCooperationCount> freeCountList = sceneCooperationCountService.getByNumList(numList,sceneType);
  396. HashMap<String,Integer> freeMap = new HashMap<>();
  397. freeCountList.forEach(e -> freeMap.put(e.getNum(),e.getCount()));
  398. HashMap<String, List<User>> map = this.getByNumList(numList,sceneType);
  399. HashMap<String,String> needNumList = new HashMap<>();
  400. if(cameraId != null && "mesh".equals(sceneType)){
  401. CameraDetail cameraDetail = cameraDetailService.getByCameraId(cameraId);
  402. if(cameraDetail == null || cameraDetail.getType() == null){
  403. throw new BusinessException(ResultCode.CAMERA_NOT_EXIT);
  404. }
  405. if(cameraDetail.getType() == 10 || cameraDetail.getType() == 11){
  406. List<ScenePro> listByNums = sceneProService.getListByNums(numList);
  407. List<String> newList = listByNums.stream().filter(e->e.getIsObj() == 1).map(ScenePro::getNum).collect(Collectors.toList());
  408. List<ScenePlus> listByNums1 = scenePlusService.getListByNums(numList);
  409. List<Long> ids = listByNums1.stream().map(ScenePlus::getId).collect(Collectors.toList());
  410. HashMap<Long, ScenePlusExt> byPlusIds = scenePlusExtService.getByPlusIds(ids);
  411. List<String> newList2 = listByNums1.stream().filter(e -> byPlusIds.get(e.getId()) != null && byPlusIds.get(e.getId()).getIsObj() == 1).map(ScenePlus::getNum).collect(Collectors.toList());
  412. forList = newList;
  413. forList.addAll(newList2);
  414. }
  415. }
  416. for (String num : forList) {
  417. Integer freeCount = freeMap.get(num) == null ? 1 :freeMap.get(num);
  418. List<User> dbUserList = map.get(num);
  419. if(dbUserList != null && !dbUserList.isEmpty()){
  420. List<Long> dbUserIds = dbUserList.stream().map(User::getId).collect(Collectors.toList());
  421. HashMap<Long,User> userHashMap = new HashMap<>();
  422. for (User user : users) {
  423. if(dbUserIds.contains(user.getId())){
  424. userHashMap.put(user.getId(),user);
  425. }
  426. }
  427. Integer totalSize = dbUserList.size() - userHashMap.size();
  428. if(numList.size() >1 || cameraId != null){
  429. freeCount = (totalSize > freeCount ? totalSize : (freeCount - totalSize));
  430. }else {
  431. freeCount = (totalSize > freeCount ? totalSize : freeCount) ;
  432. }
  433. }
  434. for (int i = 0 ; i< users.size() - freeCount;i++){
  435. needNumList.put(users.get(i).getId()+","+num,num);
  436. }
  437. }
  438. return needNumList;
  439. }
  440. @Autowired
  441. LaserService laserService;
  442. private void saveCooperationCommon(User LoginUser,String lang,List<User> userList,List<ScenePro> proList, List<ScenePlus> plusList,List<Camera >cameraList,String type,String sceneType){
  443. List<Long> userIds = userList.stream().map(User::getId).collect(Collectors.toList());
  444. Set<String> numList = new HashSet<>();
  445. List<String> v3List = proList.stream().map(ScenePro::getNum).collect(Collectors.toList());
  446. List<String> v4List = plusList.stream().map(ScenePlus::getNum).collect(Collectors.toList());
  447. numList.addAll(v3List);
  448. numList.addAll(v4List);
  449. List<ScenePro> collect1 = proList.stream().filter(e -> SceneSourceUtil.getLaserList().contains(e.getSceneSource())).collect(Collectors.toList());
  450. List<ScenePlus> collect2 = plusList.stream().filter(e -> SceneSourceUtil.getLaserList().contains(e.getSceneSource())).collect(Collectors.toList());
  451. Set<String> numList1 = collect1.stream().map(ScenePro::getNum).collect(Collectors.toSet());
  452. Set<String> numList2 = collect2.stream().map(ScenePlus::getNum).collect(Collectors.toSet());
  453. numList1.addAll(numList2);
  454. if(StringUtils.isBlank(sceneType) && !collect1.isEmpty() && !collect2.isEmpty()){
  455. List<String> newList = collect1.stream().filter(e->e.getIsObj() == 1).map(ScenePro::getNum).collect(Collectors.toList());
  456. List<Long> ids = collect2.stream().map(ScenePlus::getId).collect(Collectors.toList());
  457. HashMap<Long, ScenePlusExt> byPlusIds = scenePlusExtService.getByPlusIds(ids);
  458. List<String> newList2 = collect2.stream().filter(e -> byPlusIds.get(e.getId()) != null && byPlusIds.get(e.getId()).getIsObj() == 1).map(ScenePlus::getNum).collect(Collectors.toList());
  459. numList = new HashSet<>();
  460. numList.addAll(newList2);
  461. numList.addAll(newList);
  462. }
  463. List<SceneCooperation> addList =new ArrayList<>();
  464. if(!numList.isEmpty()){
  465. addList = this.saveBatchByList(new ArrayList<>(numList), userIds, type,"mesh");
  466. }
  467. if(!numList1.isEmpty()){
  468. addList = this.saveBatchByList(new ArrayList<>(numList1), userIds, type,"laser");
  469. }
  470. List<Long> collect3 = addList.stream().map(SceneCooperation::getUserId).collect(Collectors.toList());
  471. for (User user : userList) {
  472. if("aws".equals(NacosProperty.uploadType)){
  473. if("camera".equals(type) && cameraList != null){
  474. HashMap<Long, Camera> cameraMap = new HashMap<>();
  475. cameraList.forEach(e -> cameraMap.put(e.getId(),e));
  476. mailTemplateService.sendCameraCooperation(cameraMap,user.getUserName(),lang);
  477. }else {
  478. if(collect3.contains(user.getId())){
  479. mailTemplateService.sendSceneCooperation(proList,plusList,user.getUserName(),lang);
  480. }
  481. }
  482. }
  483. }
  484. List<String> collect = userList.stream().map(User::getUserName).collect(Collectors.toList());
  485. List<String> snCodeList = new ArrayList<>();
  486. if(cameraList !=null ){
  487. snCodeList = cameraList.stream().map(Camera::getSnCode).collect(Collectors.toList());
  488. }
  489. if(StringUtils.isNotBlank(sceneType) && "mesh".equals(sceneType)){
  490. return;
  491. }
  492. String operatingMode = (StringUtils.isBlank(sceneType) || numList1.size() > 1) ? "add" :"update";
  493. laserService.saveBatchCooperation(new ArrayList<>(numList1),snCodeList,collect,type,operatingMode);
  494. }
  495. @Override
  496. public void deleteCooperation(SceneCooperationParam param, String username) {
  497. if(StringUtils.isEmpty(param.getSceneNum())){
  498. throw new BusinessException(LoginConstant.FAILURE_CODE_3001, LoginConstant.FAILURE_MSG_3001);
  499. }
  500. String[] nums = param.getSceneNum().split(",");
  501. List<String> numList = Arrays.asList(nums);
  502. List<ScenePro> proList = sceneProService.getListByNums(numList);
  503. List<ScenePlus> plusList = scenePlusService.getListByNums(numList);
  504. this.deleteCooperationList(proList,plusList,null);
  505. }
  506. @Override
  507. public List<SceneResource> getResourceByNum(String sceneNum) {
  508. if(StringUtils.isEmpty(sceneNum)){
  509. throw new BusinessException(LoginConstant.FAILURE_CODE_3001, LoginConstant.FAILURE_MSG_3001);
  510. }
  511. LambdaQueryWrapper<SceneCooperation> wrapper = new LambdaQueryWrapper<>();
  512. wrapper.eq(SceneCooperation::getSceneNum,sceneNum);
  513. List<SceneCooperation> list = this.list(wrapper);
  514. if(list == null || list.size()<=0){
  515. return new ArrayList<>();
  516. }
  517. return sceneResourceService.getByCooperationId(list.get(0).getId());
  518. }
  519. @Override
  520. public List<String> getNumByUserIds(List<Long> userIds) {
  521. LambdaQueryWrapper<SceneCooperation> wrapper = new LambdaQueryWrapper<>();
  522. wrapper.in(SceneCooperation::getUserId,userIds);
  523. Set<String> collect = this.list(wrapper).parallelStream().map(SceneCooperation::getSceneNum).collect(Collectors.toSet());
  524. return new ArrayList<>(collect);
  525. }
  526. @Override
  527. public HashMap<String, List<User>> getByNumList(List<String> numList,String sceneType) {
  528. if(numList == null || numList.isEmpty()){
  529. return new HashMap<>();
  530. }
  531. LambdaQueryWrapper<SceneCooperation> wrapper = new LambdaQueryWrapper<>();
  532. wrapper.in(SceneCooperation::getSceneNum,numList);
  533. if(StringUtils.isNotBlank(sceneType)){
  534. wrapper.eq(SceneCooperation::getSceneType,sceneType);
  535. }
  536. List<SceneCooperation> list = this.list(wrapper);
  537. HashMap<String,List<User>> cooMap = new HashMap<>();
  538. if(list.size() >0){
  539. List<Long> userIds = list.parallelStream().map(SceneCooperation::getUserId).collect(Collectors.toList());
  540. if(userIds.size() >0){
  541. HashMap<Long, User> userMap = userService.getByIds(userIds);
  542. for (SceneCooperation entity : list) {
  543. User user = userMap.get(entity.getUserId());
  544. cooMap.computeIfAbsent(entity.getSceneNum(), k -> new ArrayList<>());
  545. if(!cooMap.get(entity.getSceneNum()).contains(user)){
  546. cooMap.get(entity.getSceneNum()).add(user);
  547. }
  548. }
  549. }
  550. }
  551. return cooMap;
  552. }
  553. @Override
  554. public List<SceneCooperation> getByNum(String num,String sceneType) {
  555. LambdaQueryWrapper<SceneCooperation> wrapper = new LambdaQueryWrapper<>();
  556. wrapper.eq(SceneCooperation::getSceneNum,num);
  557. wrapper.eq(SceneCooperation::getSceneType,sceneType);
  558. List<SceneCooperation> list = this.list(wrapper);
  559. return list;
  560. }
  561. @Override
  562. public Object cooperationSceneList(SceneParam param, String username) {
  563. return null;
  564. }
  565. }