CameraDetailServiceImpl.java 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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.entity.*;
  10. import com.fdkankan.manage.exception.BusinessException;
  11. import com.fdkankan.manage.httpClient.service.LaserService;
  12. import com.fdkankan.manage.mapper.ICameraDetailMapper;
  13. import com.fdkankan.manage.service.*;
  14. import com.fdkankan.manage.vo.request.SceneParam;
  15. import com.fdkankan.manage.vo.response.GroupByCount;
  16. import org.springframework.beans.factory.annotation.Autowired;
  17. import org.springframework.stereotype.Service;
  18. import java.util.*;
  19. import java.util.stream.Collectors;
  20. /**
  21. * <p>
  22. * 相机子表 服务实现类
  23. * </p>
  24. *
  25. * @author
  26. * @since 2022-06-16
  27. */
  28. @Service
  29. public class CameraDetailServiceImpl extends ServiceImpl<ICameraDetailMapper, CameraDetail> implements ICameraDetailService {
  30. @Autowired
  31. ICameraService cameraService;
  32. @Autowired
  33. ISceneCooperationService sceneCooperationService;
  34. @Autowired
  35. ISceneProService sceneProService;
  36. @Autowired
  37. ISceneService sceneService;
  38. @Autowired
  39. IScenePlusService scenePlusService;
  40. @Autowired
  41. LaserService fdkkLaserService;
  42. @Autowired
  43. IUserService userService;
  44. @Autowired
  45. LaserService laserService;
  46. @Autowired
  47. IUserIncrementService userIncrementService;
  48. @Autowired
  49. IIncrementTypeService incrementTypeService;
  50. @Override
  51. public void unbindCamera(Long cameraId) {
  52. CameraDetail cameraDetail = this.getByCameraId(cameraId);
  53. if(cameraDetail == null){
  54. throw new BusinessException(ResultCode.CAMERA_NOT_EXIST);
  55. }
  56. User user = userService.getById(cameraDetail.getUserId());
  57. if(user == null){
  58. throw new BusinessException(ResultCode.USER_NOT_EXIST);
  59. }
  60. String snCode = null;
  61. String cooperationUserName = null;
  62. if(cameraDetail.getType() == 10 || cameraDetail.getType() == 11){
  63. Camera cameraEntity = cameraService.getById(cameraDetail.getCameraId());
  64. snCode = cameraEntity.getSnCode();
  65. cooperationUserName = user.getUserName();
  66. fdkkLaserService.disableCooperation(snCode,cooperationUserName); //通知深时删除协作场景
  67. fdkkLaserService.toBind(snCode); //通知深时删除协作场景
  68. }
  69. sceneCooperationService.deleteCooperation(cameraId); //删除协作场景关系
  70. LambdaUpdateWrapper<CameraDetail> wrapper = new LambdaUpdateWrapper<>();
  71. wrapper.eq(CameraDetail::getCameraId,cameraId);
  72. wrapper.set(CameraDetail::getUserId,null);
  73. wrapper.set(CameraDetail::getCooperationUser,null);
  74. //恢复10G基本容量
  75. this.update(wrapper);
  76. if(!"local".equals(CacheUtil.uploadType) && cameraDetail.getType() !=10){
  77. sceneProService.lockOrUnLockBySpace(cameraDetail,cameraId,-2); //封存场景
  78. }
  79. }
  80. @Override
  81. public CameraDetail getByCameraId(Long cameraId) {
  82. LambdaQueryWrapper<CameraDetail> wrapper = new LambdaQueryWrapper<>();
  83. wrapper.eq(CameraDetail::getCameraId,cameraId);
  84. List<CameraDetail> list = this.list(wrapper);
  85. if(list == null || list.size() <=0){
  86. return null;
  87. }
  88. return list.get(0);
  89. }
  90. @Override
  91. public List<CameraDetail> getByCameraIds(List<Long> cameraIds) {
  92. LambdaQueryWrapper<CameraDetail> wrapper = new LambdaQueryWrapper<>();
  93. wrapper.in(CameraDetail::getCameraId,cameraIds);
  94. return this.list(wrapper);
  95. }
  96. @Override
  97. public void deleteByCameraId(Long cameraId) {
  98. LambdaQueryWrapper<CameraDetail> wrapper = new LambdaQueryWrapper<>();
  99. wrapper.eq(CameraDetail::getCameraId,cameraId);
  100. this.remove(wrapper);
  101. }
  102. @Override
  103. public HashMap<Long, Long> getCountGroupByUserId(List<Long> userIdList) {
  104. HashMap<Long,Long> map = new HashMap<>();
  105. List<GroupByCount> result = this.getBaseMapper().getCountGroupByUserId(userIdList);
  106. result.forEach(entity ->map.put(entity.getId(),entity.getCount()));
  107. return map;
  108. }
  109. @Override
  110. public HashMap<Long, Long> getCountGroupByCompanyId() {
  111. List<GroupByCount> result = this.getBaseMapper().getCountGroupByCompanyId();
  112. HashMap<Long,Long> map = new HashMap<>();
  113. result.forEach(entity ->map.put(entity.getId(),entity.getCount()));
  114. return map;
  115. }
  116. @Override
  117. public HashMap<Long, Long> getSceneCountGroupByCameraId() {
  118. LambdaQueryWrapper<CameraDetail> wrapper = new LambdaQueryWrapper<>();
  119. wrapper.isNotNull(CameraDetail::getCompanyId);
  120. List<CameraDetail> list = this.list(wrapper);
  121. Set<Long> cameraIds = list.parallelStream().map(CameraDetail::getCameraId).collect(Collectors.toSet());
  122. HashMap<Long, Long> resultMap = new HashMap<>();
  123. if(cameraIds.size() >0){
  124. HashMap<Long, Long> sceneProMap = sceneProService.getCountGroupByCameraId(new ArrayList<>(cameraIds));
  125. HashMap<Long, Long> sceneMap = sceneService.getCountGroupByCameraId(new ArrayList<>(cameraIds));
  126. HashMap<Long, Long> scenePlusMap = scenePlusService.getCountGroupByCameraId(new ArrayList<>(cameraIds));
  127. HashMap<Long,Camera> cameraHashMap = new HashMap<>();
  128. List<Camera> cameraList = cameraService.listByIds(cameraIds);
  129. cameraList.forEach(entity -> cameraHashMap.put(entity.getId(),entity));
  130. HashMap<Long,List<String>> companySnCodeMap = new HashMap<>();
  131. for (CameraDetail cameraDetail : list) {
  132. Long sceneProCount = sceneProMap.get(cameraDetail.getCameraId()) == null ? 0L : sceneProMap.get(cameraDetail.getCameraId());
  133. Long sceneCount = sceneMap.get(cameraDetail.getCameraId()) == null ? 0L : sceneMap.get(cameraDetail.getCameraId());
  134. Long scenePlusCount = scenePlusMap.get(cameraDetail.getCameraId()) == null ? 0L : scenePlusMap.get(cameraDetail.getCameraId());
  135. Long count = sceneProCount + sceneCount + scenePlusCount;
  136. List<String> snCodeList = companySnCodeMap.computeIfAbsent(cameraDetail.getCompanyId(), k -> new ArrayList<>());
  137. Camera camera = cameraHashMap.get(cameraDetail.getCameraId());
  138. if(camera != null){
  139. snCodeList.add(camera.getSnCode());
  140. }
  141. resultMap.merge(cameraDetail.getCompanyId(), count, Long::sum);
  142. }
  143. for (Long companyId : companySnCodeMap.keySet()) {
  144. List<String> snCodeList = companySnCodeMap.get(companyId);
  145. if(snCodeList == null || snCodeList.size() <=0){
  146. continue;
  147. }
  148. SceneParam param = new SceneParam();
  149. param.setSnCodes(snCodeList);
  150. PageInfo pageInfo = laserService.pageList(param);
  151. resultMap.merge(companyId, pageInfo.getTotal(), Long::sum);
  152. }
  153. }
  154. return resultMap;
  155. }
  156. @Override
  157. public Long getCountByCompanyId(Long companyId) {
  158. LambdaQueryWrapper<CameraDetail> wrapper = new LambdaQueryWrapper<>();
  159. wrapper.eq(CameraDetail::getCompanyId,companyId);
  160. return this.count(wrapper);
  161. }
  162. @Override
  163. public List<CameraDetail> getListByCompanyId(Integer companyId) {
  164. LambdaQueryWrapper<CameraDetail> wrapper = new LambdaQueryWrapper<>();
  165. wrapper.eq(CameraDetail::getCompanyId,companyId);
  166. return this.list(wrapper);
  167. }
  168. @Override
  169. public List<CameraDetail> getByUserName(String userName) {
  170. LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
  171. wrapper.like(User::getUserName,userName);
  172. List<User> list = userService.list(wrapper);
  173. if(list.size() >0){
  174. List<Long> userIds = list.stream().map(User::getId).collect(Collectors.toList());
  175. if(userIds.size() >0){
  176. LambdaQueryWrapper<CameraDetail> dtW = new LambdaQueryWrapper<>();
  177. dtW.in(CameraDetail::getUserId,userIds);
  178. return this.list(dtW);
  179. }
  180. }
  181. return null;
  182. }
  183. @Override
  184. public void delAgentId(Integer agentId) {
  185. LambdaUpdateWrapper<CameraDetail> wrapper = new LambdaUpdateWrapper<>();
  186. wrapper.eq(CameraDetail::getAgentId,agentId);
  187. wrapper.set(CameraDetail::getAgentId,null);
  188. this.update(wrapper);
  189. }
  190. @Override
  191. public List<CameraDetail> getByUserId(Long userId) {
  192. LambdaQueryWrapper<CameraDetail> wrapper = new LambdaQueryWrapper<>();
  193. wrapper.eq(CameraDetail::getUserId,userId);
  194. return this.list(wrapper);
  195. }
  196. @Override
  197. public void addUsedSpace(Long cameraId,Long space) {
  198. if(space == null){
  199. return;
  200. }
  201. CameraDetail cameraDetail = this.getByCameraId(cameraId);
  202. long usedSpace = cameraDetail.getUsedSpace() - space ;
  203. cameraDetail.setUsedSpace(usedSpace < 0 ? 0L :usedSpace);
  204. //解封封存场景
  205. if(cameraDetail.getType() != 10){
  206. sceneProService.lockOrUnLockBySpace(cameraDetail,cameraDetail.getCameraId(),1);
  207. }
  208. this.updateById(cameraDetail);
  209. }
  210. @Override
  211. public Long getTotalSpaceByCameraId(Long cameraId) {
  212. CameraDetail cameraDetail = this.getByCameraId(cameraId);
  213. if(cameraDetail == null){
  214. return 0L;
  215. }
  216. return getTotalSpaceByCameraId(cameraDetail);
  217. }
  218. @Override
  219. public Long getTotalSpaceByCameraId(CameraDetail cameraDetail) {
  220. UserIncrement userIncrement = userIncrementService.getByCameraId(cameraDetail.getCameraId());
  221. if(userIncrement != null && userIncrement.getIsExpired() == 0){
  222. IncrementType incrementType = incrementTypeService.getById(userIncrement.getIncrementTypeId());
  223. return getTotalSpace(cameraDetail,incrementType);
  224. }
  225. return getTotalSpace(cameraDetail,null);
  226. }
  227. @Override
  228. public Boolean checkSpace(Long cameraId, Long space) {
  229. CameraDetail cameraDetail = this.getByCameraId(cameraId);
  230. if(cameraDetail == null){
  231. return false;
  232. }
  233. return checkSpace(cameraDetail,space);
  234. }
  235. @Override
  236. public Boolean checkSpace(CameraDetail detailEntity, Long space) {
  237. if(detailEntity.getType() == 10 || detailEntity.getType() == 11){
  238. return true;
  239. }
  240. UserIncrement userIncrement = userIncrementService.getByCameraId(detailEntity.getCameraId());
  241. if(userIncrement == null || userIncrement.getIsExpired() == 1){
  242. return checkSpace(detailEntity,null,space);
  243. }
  244. IncrementType incrementType = incrementTypeService.getById(userIncrement.getIncrementTypeId());
  245. return checkSpace(detailEntity,incrementType,space);
  246. }
  247. @Override
  248. public Boolean checkSpace(CameraDetail detailEntity, IncrementType incrementType, Long space) {
  249. Long totalSpace = 0L;
  250. if("SP".equals(detailEntity.getUnit())){
  251. if(incrementType!=null && incrementType.getCameraSpace() == -1){
  252. return true;
  253. }
  254. totalSpace = incrementType != null ?incrementType.getCameraSpace() : detailEntity.getTotalSpace();
  255. return detailEntity.getUsedSpace() + 1 <= totalSpace;
  256. }
  257. if("GB".equals(detailEntity.getUnit())){
  258. if(incrementType!=null && incrementType.getCameraCapacity() == -1){
  259. return true;
  260. }
  261. totalSpace = incrementType != null ?incrementType.getCameraCapacity() * 1024 * 1024 * 1024L: detailEntity.getTotalSpace();
  262. return detailEntity.getUsedSpace() + space <= totalSpace ;
  263. }
  264. return false;
  265. }
  266. public Long getTotalSpace(CameraDetail detailEntity, IncrementType incrementType) {
  267. if("SP".equals(detailEntity.getUnit())){
  268. if(incrementType!=null && incrementType.getCameraSpace() == -1){
  269. return -1L;
  270. }
  271. return incrementType != null ?incrementType.getCameraSpace() : detailEntity.getTotalSpace();
  272. }
  273. if("GB".equals(detailEntity.getUnit())){
  274. if(incrementType!=null && incrementType.getCameraCapacity() == -1){
  275. return -1L;
  276. }
  277. return incrementType != null ?incrementType.getCameraCapacity() * 1024 * 1024 * 1024L: detailEntity.getTotalSpace();
  278. }
  279. return 0L;
  280. }
  281. @Override
  282. public void initSpace(Long cameraId) {
  283. CameraDetail cameraDetail = this.getByCameraId(cameraId);
  284. if(cameraDetail != null){
  285. initSpace(cameraDetail);
  286. }
  287. }
  288. @Override
  289. public void initSpace(CameraDetail cameraDetail) {
  290. LambdaUpdateWrapper<CameraDetail> wrapper = new LambdaUpdateWrapper<>();
  291. wrapper.eq(CameraDetail::getId,cameraDetail.getId());
  292. if("GB".equals(cameraDetail.getUnit())){
  293. Long proSpace = sceneProService.getSpaceSumByCameraId(cameraDetail.getCameraId());
  294. Long plusSpace = scenePlusService.getSpaceSumByCameraId(cameraDetail.getCameraId());
  295. wrapper.set(CameraDetail::getUsedSpace,(plusSpace == null ?0L:plusSpace) + (proSpace == null ?0L:proSpace));
  296. }
  297. if("SP".equals(cameraDetail.getUnit())){
  298. Long proSpace = sceneProService.getCountByCameraId(cameraDetail.getCameraId());
  299. Long plusSpace = scenePlusService.getCountByCameraId(cameraDetail.getCameraId());
  300. wrapper.set(CameraDetail::getUsedSpace,(plusSpace == null ?0L:plusSpace )+( proSpace == null ?0L:proSpace));
  301. }
  302. this.update(wrapper);
  303. }
  304. }