CameraDetailServiceImpl.java 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. package com.fdkankan.manage.service.impl;
  2. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  3. import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
  4. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  5. import com.fdkankan.common.constant.Constant;
  6. import com.fdkankan.manage.common.CacheUtil;
  7. import com.fdkankan.manage.common.PageInfo;
  8. import com.fdkankan.manage.common.ResultCode;
  9. import com.fdkankan.manage.exception.BusinessException;
  10. import com.fdkankan.manage.entity.Camera;
  11. import com.fdkankan.manage.entity.CameraDetail;
  12. import com.fdkankan.manage.entity.User;
  13. import com.fdkankan.manage.httpClient.service.LaserService;
  14. import com.fdkankan.manage.mapper.ICameraDetailMapper;
  15. import com.fdkankan.manage.service.*;
  16. import com.fdkankan.manage.vo.request.SceneParam;
  17. import com.fdkankan.manage.vo.response.GroupByCount;
  18. import org.springframework.beans.factory.annotation.Autowired;
  19. import org.springframework.stereotype.Service;
  20. import java.util.ArrayList;
  21. import java.util.HashMap;
  22. import java.util.List;
  23. import java.util.Set;
  24. import java.util.stream.Collectors;
  25. /**
  26. * <p>
  27. * 相机子表 服务实现类
  28. * </p>
  29. *
  30. * @author
  31. * @since 2022-06-16
  32. */
  33. @Service
  34. public class CameraDetailServiceImpl extends ServiceImpl<ICameraDetailMapper, CameraDetail> implements ICameraDetailService {
  35. @Autowired
  36. ICameraService cameraService;
  37. @Autowired
  38. ISceneCooperationService sceneCooperationService;
  39. @Autowired
  40. ISceneProService sceneProService;
  41. @Autowired
  42. ISceneService sceneService;
  43. @Autowired
  44. IScenePlusService scenePlusService;
  45. @Autowired
  46. LaserService fdkkLaserService;
  47. @Autowired
  48. IUserService userService;
  49. @Autowired
  50. LaserService laserService;
  51. @Override
  52. public void unbindCamera(Long cameraId) {
  53. CameraDetail cameraDetail = this.getByCameraId(cameraId);
  54. if(cameraDetail == null){
  55. throw new BusinessException(ResultCode.CAMERA_NOT_EXIST);
  56. }
  57. User user = userService.getById(cameraDetail.getUserId());
  58. if(user == null){
  59. throw new BusinessException(ResultCode.USER_NOT_EXIST);
  60. }
  61. String snCode = null;
  62. String cooperationUserName = null;
  63. if(cameraDetail.getType() == 10 || cameraDetail.getType() == 11){
  64. Camera cameraEntity = cameraService.getById(cameraDetail.getCameraId());
  65. snCode = cameraEntity.getSnCode();
  66. cooperationUserName = user.getUserName();
  67. fdkkLaserService.disableCooperation(snCode,cooperationUserName); //通知深时删除协作场景
  68. fdkkLaserService.toBind(snCode); //通知深时删除协作场景
  69. }
  70. sceneCooperationService.deleteCooperation(cameraId); //删除协作场景关系
  71. LambdaUpdateWrapper<CameraDetail> wrapper = new LambdaUpdateWrapper<>();
  72. wrapper.eq(CameraDetail::getCameraId,cameraId);
  73. wrapper.set(CameraDetail::getUserId,null);
  74. wrapper.set(CameraDetail::getCooperationUser,null);
  75. //恢复10G基本容量
  76. wrapper.set(CameraDetail::getTotalSpace,Long.parseLong(Constant.EXPANSION_SPACE_VALUE_1G ) * 10L);
  77. this.update(wrapper);
  78. if(!"local".equals(CacheUtil.uploadType) && cameraDetail.getType() !=10){
  79. sceneProService.lockOrUnLockBySpace(cameraDetail,cameraId,-2); //封存场景
  80. }
  81. }
  82. @Override
  83. public CameraDetail getByCameraId(Long cameraId) {
  84. LambdaQueryWrapper<CameraDetail> wrapper = new LambdaQueryWrapper<>();
  85. wrapper.eq(CameraDetail::getCameraId,cameraId);
  86. List<CameraDetail> list = this.list(wrapper);
  87. if(list == null || list.size() <=0){
  88. return null;
  89. }
  90. return list.get(0);
  91. }
  92. @Override
  93. public List<CameraDetail> getByCameraIds(List<Long> cameraIds) {
  94. LambdaQueryWrapper<CameraDetail> wrapper = new LambdaQueryWrapper<>();
  95. wrapper.in(CameraDetail::getCameraId,cameraIds);
  96. return this.list(wrapper);
  97. }
  98. @Override
  99. public void deleteByCameraId(Long cameraId) {
  100. LambdaQueryWrapper<CameraDetail> wrapper = new LambdaQueryWrapper<>();
  101. wrapper.eq(CameraDetail::getCameraId,cameraId);
  102. this.remove(wrapper);
  103. }
  104. @Override
  105. public HashMap<Long, Long> getCountGroupByUserId(List<Long> userIdList) {
  106. HashMap<Long,Long> map = new HashMap<>();
  107. List<GroupByCount> result = this.getBaseMapper().getCountGroupByUserId(userIdList);
  108. result.forEach(entity ->map.put(entity.getId(),entity.getCount()));
  109. return map;
  110. }
  111. @Override
  112. public HashMap<Long, Long> getCountGroupByCompanyId() {
  113. List<GroupByCount> result = this.getBaseMapper().getCountGroupByCompanyId();
  114. HashMap<Long,Long> map = new HashMap<>();
  115. result.forEach(entity ->map.put(entity.getId(),entity.getCount()));
  116. return map;
  117. }
  118. @Override
  119. public HashMap<Long, Long> getSceneCountGroupByCameraId() {
  120. LambdaQueryWrapper<CameraDetail> wrapper = new LambdaQueryWrapper<>();
  121. wrapper.isNotNull(CameraDetail::getCompanyId);
  122. List<CameraDetail> list = this.list(wrapper);
  123. Set<Long> cameraIds = list.parallelStream().map(CameraDetail::getCameraId).collect(Collectors.toSet());
  124. HashMap<Long, Long> resultMap = new HashMap<>();
  125. if(cameraIds.size() >0){
  126. HashMap<Long, Long> sceneProMap = sceneProService.getCountGroupByCameraId(new ArrayList<>(cameraIds));
  127. HashMap<Long, Long> sceneMap = sceneService.getCountGroupByCameraId(new ArrayList<>(cameraIds));
  128. HashMap<Long, Long> scenePlusMap = scenePlusService.getCountGroupByCameraId(new ArrayList<>(cameraIds));
  129. HashMap<Long,Camera> cameraHashMap = new HashMap<>();
  130. List<Camera> cameraList = cameraService.listByIds(cameraIds);
  131. cameraList.forEach(entity -> cameraHashMap.put(entity.getId(),entity));
  132. HashMap<Long,List<String>> companySnCodeMap = new HashMap<>();
  133. for (CameraDetail cameraDetail : list) {
  134. Long sceneProCount = sceneProMap.get(cameraDetail.getCameraId()) == null ? 0L : sceneProMap.get(cameraDetail.getCameraId());
  135. Long sceneCount = sceneMap.get(cameraDetail.getCameraId()) == null ? 0L : sceneMap.get(cameraDetail.getCameraId());
  136. Long scenePlusCount = scenePlusMap.get(cameraDetail.getCameraId()) == null ? 0L : scenePlusMap.get(cameraDetail.getCameraId());
  137. Long count = sceneProCount + sceneCount + scenePlusCount;
  138. List<String> snCodeList = companySnCodeMap.computeIfAbsent(cameraDetail.getCompanyId(), k -> new ArrayList<>());
  139. Camera camera = cameraHashMap.get(cameraDetail.getCameraId());
  140. if(camera != null){
  141. snCodeList.add(camera.getSnCode());
  142. }
  143. resultMap.merge(cameraDetail.getCompanyId(), count, Long::sum);
  144. }
  145. for (Long companyId : companySnCodeMap.keySet()) {
  146. List<String> snCodeList = companySnCodeMap.get(companyId);
  147. if(snCodeList == null || snCodeList.size() <=0){
  148. continue;
  149. }
  150. SceneParam param = new SceneParam();
  151. param.setSnCodes(snCodeList);
  152. PageInfo pageInfo = laserService.pageList(param);
  153. resultMap.merge(companyId, pageInfo.getTotal(), Long::sum);
  154. }
  155. }
  156. return resultMap;
  157. }
  158. @Override
  159. public Long getCountByCompanyId(Long companyId) {
  160. LambdaQueryWrapper<CameraDetail> wrapper = new LambdaQueryWrapper<>();
  161. wrapper.eq(CameraDetail::getCompanyId,companyId);
  162. return this.count(wrapper);
  163. }
  164. @Override
  165. public List<CameraDetail> getListByCompanyId(Integer companyId) {
  166. LambdaQueryWrapper<CameraDetail> wrapper = new LambdaQueryWrapper<>();
  167. wrapper.eq(CameraDetail::getCompanyId,companyId);
  168. return this.list(wrapper);
  169. }
  170. @Override
  171. public List<CameraDetail> getByUserName(String userName) {
  172. LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
  173. wrapper.like(User::getUserName,userName);
  174. List<User> list = userService.list(wrapper);
  175. if(list.size() >0){
  176. List<Long> userIds = list.stream().map(User::getId).collect(Collectors.toList());
  177. if(userIds.size() >0){
  178. LambdaQueryWrapper<CameraDetail> dtW = new LambdaQueryWrapper<>();
  179. dtW.in(CameraDetail::getUserId,userIds);
  180. return this.list(dtW);
  181. }
  182. }
  183. return null;
  184. }
  185. @Override
  186. public void delAgentId(Integer agentId) {
  187. LambdaUpdateWrapper<CameraDetail> wrapper = new LambdaUpdateWrapper<>();
  188. wrapper.eq(CameraDetail::getAgentId,agentId);
  189. wrapper.set(CameraDetail::getAgentId,null);
  190. this.update(wrapper);
  191. }
  192. @Override
  193. public List<CameraDetail> getByUserId(Long userId) {
  194. LambdaQueryWrapper<CameraDetail> wrapper = new LambdaQueryWrapper<>();
  195. wrapper.eq(CameraDetail::getUserId,userId);
  196. return this.list(wrapper);
  197. }
  198. @Override
  199. public void addUsedSpace(Long cameraId,Long space) {
  200. if(space == null){
  201. return;
  202. }
  203. CameraDetail cameraDetail = this.getByCameraId(cameraId);
  204. long usedSpace = cameraDetail.getUsedSpace() - space ;
  205. cameraDetail.setUsedSpace(usedSpace < 0 ? 0L :usedSpace);
  206. //解封封存场景
  207. if(cameraDetail.getType() != 10){
  208. sceneProService.lockOrUnLockBySpace(cameraDetail,cameraDetail.getCameraId(),1);
  209. }
  210. this.updateById(cameraDetail);
  211. }
  212. }