SceneCooperationServiceImpl.java 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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.common.exception.BusinessException;
  6. import com.fdkankan.ucenter.constant.LoginConstant;
  7. import com.fdkankan.ucenter.entity.*;
  8. import com.fdkankan.ucenter.mapper.ISceneCooperationMapper;
  9. import com.fdkankan.ucenter.service.*;
  10. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  11. import com.fdkankan.ucenter.util.DateUserUtil;
  12. import com.fdkankan.ucenter.vo.request.SceneCooperationParam;
  13. import com.fdkankan.ucenter.vo.request.SceneParam;
  14. import com.google.common.collect.Lists;
  15. import org.apache.commons.lang3.StringUtils;
  16. import org.springframework.beans.factory.annotation.Autowired;
  17. import org.springframework.stereotype.Service;
  18. import org.springframework.util.CollectionUtils;
  19. import org.springframework.util.ObjectUtils;
  20. import java.util.*;
  21. import java.util.stream.Collectors;
  22. /**
  23. * <p>
  24. * 服务实现类
  25. * </p>
  26. *
  27. * @author
  28. * @since 2022-07-04
  29. */
  30. @Service
  31. public class SceneCooperationServiceImpl extends ServiceImpl<ISceneCooperationMapper, SceneCooperation> implements ISceneCooperationService {
  32. @Autowired
  33. ISceneProService sceneProService;
  34. @Autowired
  35. IScenePlusService scenePlusService;
  36. @Autowired
  37. FdkkLaserService fdkkLaserService;
  38. @Autowired
  39. ISceneResourceCooperationService sceneResourceCooperationService;
  40. @Autowired
  41. ICameraService cameraService;
  42. @Autowired
  43. ICameraDetailService cameraDetailService;
  44. @Autowired
  45. ISceneResourceService sceneResourceService;
  46. @Autowired
  47. IUserService userService;
  48. @Override
  49. public Long getCooperationSceneNum(Long userId, List<Integer> sceneSourceList) {
  50. Long cooperationSceneProNum = this.getBaseMapper().getCooperationSceneProNum(userId, sceneSourceList);
  51. Long cooperationScenePlusNum = this.getBaseMapper().getCooperationScenePlusNum(userId, sceneSourceList);
  52. return cooperationSceneProNum + cooperationScenePlusNum;
  53. }
  54. @Override
  55. public void deleteCooperationList(List<ScenePro> sceneProList,List<ScenePlus> scenePlusList) {
  56. if(CollectionUtils.isEmpty(sceneProList) && CollectionUtils.isEmpty(scenePlusList)){
  57. return;
  58. }
  59. List<String> numList = sceneProList.stream().map(ScenePro::getNum).collect(Collectors.toList());
  60. List<String> numList2 = scenePlusList.stream().map(ScenePlus::getNum).collect(Collectors.toList());
  61. numList.addAll(numList2);
  62. LambdaQueryWrapper<SceneCooperation> wrapper = new LambdaQueryWrapper<>();
  63. wrapper.in(SceneCooperation::getSceneNum,numList);
  64. List<SceneCooperation> list = this.list(wrapper);
  65. List<Long> ids = list.stream().map(SceneCooperation::getId).collect(Collectors.toList());
  66. if(ids.size() >0){
  67. this.removeByIds(ids);
  68. sceneResourceCooperationService.deleteBatchByCooperationIds(ids);
  69. }
  70. }
  71. @Override
  72. public void saveBatchByList(List<ScenePro> sceneProList, List<ScenePlus> scenePlusList, Long userId,List<Long> resourceIdList) {
  73. List<SceneCooperation> list = new ArrayList<>();
  74. for (ScenePro scenePro : sceneProList) {
  75. SceneCooperation sceneCooperationEntity = new SceneCooperation();
  76. sceneCooperationEntity.setUserId(userId);
  77. sceneCooperationEntity.setSceneNum(scenePro.getNum());
  78. sceneCooperationEntity.setRecStatus("A");
  79. sceneCooperationEntity.setCreateTime(DateUserUtil.getDate(new Date()));
  80. sceneCooperationEntity.setUpdateTime(DateUserUtil.getDate(new Date()));
  81. list.add(sceneCooperationEntity);
  82. }
  83. for (ScenePlus scenePlus : scenePlusList) {
  84. SceneCooperation sceneCooperationEntity = new SceneCooperation();
  85. sceneCooperationEntity.setUserId(userId);
  86. sceneCooperationEntity.setSceneNum(scenePlus.getNum());
  87. sceneCooperationEntity.setRecStatus("A");
  88. sceneCooperationEntity.setCreateTime(DateUserUtil.getDate(new Date()));
  89. sceneCooperationEntity.setUpdateTime(DateUserUtil.getDate(new Date()));
  90. list.add(sceneCooperationEntity);
  91. }
  92. this.saveBatch(list);
  93. if(resourceIdList.size() >0){
  94. List<SceneResourceCooperation> resourceCooperationList = new ArrayList<>();
  95. for (SceneCooperation sceneCooperation : list) {
  96. for (Long resourceId : resourceIdList) {
  97. SceneResourceCooperation sceneResourceCooperation = new SceneResourceCooperation();
  98. sceneResourceCooperation.setSceneResourceId(resourceId);
  99. sceneResourceCooperation.setSceneCooperationId(sceneCooperation.getId());
  100. sceneResourceCooperation.setRecStatus("A");
  101. sceneResourceCooperation.setCreateTime(DateUserUtil.getDate(new Date()));
  102. sceneResourceCooperation.setUpdateTime(DateUserUtil.getDate(new Date()));
  103. resourceCooperationList.add(sceneResourceCooperation);
  104. }
  105. }
  106. if(resourceCooperationList.size() >0){
  107. sceneResourceCooperationService.saveBatch(resourceCooperationList,10000);
  108. }
  109. }
  110. }
  111. @Override
  112. public JSONObject sceneResourceList(SceneCooperationParam param) {
  113. JSONObject jsonObject = new JSONObject();
  114. List<SceneResource> exclude = new ArrayList<>();
  115. SceneResource excludeEntity = new SceneResource();
  116. excludeEntity.setKeyWord("data");
  117. exclude.add(excludeEntity);
  118. jsonObject.put("exclude", exclude);
  119. if(param.getCameraId() != null){
  120. Camera cameraEntity = cameraService.getById(param.getCameraId());
  121. if(cameraEntity != null){
  122. CameraDetail cameraDetailEntity = cameraDetailService.getByCameraId(cameraEntity.getId());
  123. if(cameraDetailEntity.getCompanyId() != null && cameraDetailEntity.getCompanyId() == 1){
  124. jsonObject.put("exclude", new ArrayList<>());
  125. }
  126. }
  127. }
  128. List<ScenePro> sceneProList;
  129. List<ScenePlus> scenePlusList;
  130. //如果是场景协作,判断是V3的场景还是V4的场景,如果是v4场景,则查询v4的菜单资源
  131. if(param.getType() != null && param.getType() == 1){
  132. if(StringUtils.isEmpty(param.getSceneNum()) && CollectionUtils.isEmpty(param.getNumList())){
  133. throw new BusinessException(LoginConstant.FAILURE_CODE_3001, LoginConstant.FAILURE_MSG_3001);
  134. }
  135. List<String> numList = param.getNumList();
  136. if(numList == null){
  137. numList= new ArrayList<>();
  138. numList.add(param.getSceneNum());
  139. }
  140. sceneProList = sceneProService.getListByNums(numList);
  141. scenePlusList = scenePlusService.getListByNums(numList);
  142. if(CollectionUtils.isEmpty(sceneProList) && CollectionUtils.isEmpty(scenePlusList)){
  143. throw new BusinessException(SceneConstant.FAILURE_CODE_5005, SceneConstant.FAILURE_MSG_5005);
  144. }
  145. }else {
  146. //如果是相机协作,判断这个相机id下有没有V4的场景,如果有,则列出对应的菜单资源
  147. if(ObjectUtils.isEmpty(param.getCameraId()) && CollectionUtils.isEmpty(param.getCameraIdList())){
  148. throw new BusinessException(LoginConstant.FAILURE_CODE_3001, LoginConstant.FAILURE_MSG_3001);
  149. }
  150. List<Long> cameraIdList = param.getCameraIdList();
  151. if(cameraIdList == null){
  152. cameraIdList= new ArrayList<>();
  153. cameraIdList.add(param.getCameraId());
  154. }
  155. sceneProList = sceneProService.getListByCameraIds(cameraIdList);
  156. scenePlusList = scenePlusService.getListByCameraIds(cameraIdList);
  157. }
  158. List<String> versionList = Lists.newArrayList();
  159. if(sceneProList.size() >0){
  160. versionList.add("v3");
  161. }
  162. if(scenePlusList.size() >0){
  163. versionList.add("v4");
  164. }
  165. List<SceneResource> results = sceneResourceService.getByVersion(versionList);
  166. jsonObject.put("include", results);
  167. return jsonObject;
  168. }
  169. @Override
  170. public JSONObject cooperationSceneListNew(SceneParam param, String username) {
  171. User user = userService.getByUserName(username);
  172. LambdaQueryWrapper<SceneCooperation> wrapper = new LambdaQueryWrapper<>();
  173. wrapper.eq(SceneCooperation::getUserId,user.getId());
  174. List<SceneCooperation> list = this.list(wrapper);
  175. List<String> numList = list.parallelStream().map(SceneCooperation::getSceneNum).collect(Collectors.toList());
  176. if(numList.size() <=0){
  177. return new JSONObject();
  178. }
  179. param.setNumList(numList);
  180. param.setHasFolder(0);
  181. JSONObject jsonObject = sceneProService.newList(param, username);
  182. return jsonObject.getJSONObject("pageInfo") ;
  183. }
  184. @Override
  185. public void saveCooperation(SceneCooperationParam param, String username) {
  186. if(StringUtils.isEmpty(param.getUserName()) || StringUtils.isEmpty(param.getSceneNum()) || StringUtils.isEmpty(param.getResourceIds())){
  187. throw new BusinessException(LoginConstant.FAILURE_CODE_3001, LoginConstant.FAILURE_MSG_3001);
  188. }
  189. if(param.getUserName().equals( username)){
  190. throw new BusinessException(LoginConstant.FAILURE_CODE_3024, LoginConstant.FAILURE_MSG_3024);
  191. }
  192. User user = userService.getByUserName(param.getUserName());
  193. if(user == null){
  194. throw new BusinessException(LoginConstant.FAILURE_CODE_3021, LoginConstant.FAILURE_MSG_3021);
  195. }
  196. String[] nums = param.getSceneNum().split(",");
  197. List<String> numList = Arrays.asList(nums);
  198. List<ScenePro> proList = sceneProService.getListByNums(numList);
  199. List<ScenePlus> plusList = scenePlusService.getListByNums(numList);
  200. this.deleteCooperationList(proList,plusList);
  201. List<Long> resourceIdList = new ArrayList<>();
  202. if (StringUtils.isNotEmpty(param.getResourceIds())) {
  203. for (String rId : param.getResourceIds().split(",")) {
  204. resourceIdList.add(Long.valueOf(rId));
  205. }
  206. }
  207. this.saveBatchByList(proList,plusList,user.getId(),resourceIdList);
  208. }
  209. @Override
  210. public void deleteCooperation(SceneCooperationParam param, String username) {
  211. if(StringUtils.isEmpty(param.getSceneNum())){
  212. throw new BusinessException(LoginConstant.FAILURE_CODE_3001, LoginConstant.FAILURE_MSG_3001);
  213. }
  214. String[] nums = param.getSceneNum().split(",");
  215. List<String> numList = Arrays.asList(nums);
  216. List<ScenePro> proList = sceneProService.getListByNums(numList);
  217. List<ScenePlus> plusList = scenePlusService.getListByNums(numList);
  218. this.deleteCooperationList(proList,plusList);
  219. }
  220. @Override
  221. public List<SceneResource> getResourceByNum(String sceneNum) {
  222. if(StringUtils.isEmpty(sceneNum)){
  223. throw new BusinessException(LoginConstant.FAILURE_CODE_3001, LoginConstant.FAILURE_MSG_3001);
  224. }
  225. LambdaQueryWrapper<SceneCooperation> wrapper = new LambdaQueryWrapper<>();
  226. wrapper.eq(SceneCooperation::getSceneNum,sceneNum);
  227. List<SceneCooperation> list = this.list(wrapper);
  228. if(list == null || list.size()<=0){
  229. return new ArrayList<>();
  230. }
  231. return sceneResourceService.getByCooperationId(list.get(0).getId());
  232. }
  233. @Override
  234. public List<String> getNumByUserIds(List<Long> userIds) {
  235. LambdaQueryWrapper<SceneCooperation> wrapper = new LambdaQueryWrapper<>();
  236. wrapper.in(SceneCooperation::getUserId,userIds);
  237. Set<String> collect = this.list(wrapper).parallelStream().map(SceneCooperation::getSceneNum).collect(Collectors.toSet());
  238. return new ArrayList<>(collect);
  239. }
  240. @Override
  241. public HashMap<String, User> getByNumList(List<String> numList) {
  242. LambdaQueryWrapper<SceneCooperation> wrapper = new LambdaQueryWrapper<>();
  243. wrapper.in(SceneCooperation::getSceneNum,numList);
  244. List<SceneCooperation> list = this.list(wrapper);
  245. HashMap<String,User> cooMap = new HashMap<>();
  246. if(list.size() >0){
  247. List<Long> userIds = list.parallelStream().map(SceneCooperation::getUserId).collect(Collectors.toList());
  248. if(userIds.size() >0){
  249. HashMap<Long, User> userMap = userService.getByIds(userIds);
  250. list.forEach(entity -> cooMap.put(entity.getSceneNum(),userMap.get(entity.getUserId())));
  251. }
  252. }
  253. return cooMap;
  254. }
  255. }