SceneCooperationServiceImpl.java 14 KB

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