CameraDetailServiceImpl.java 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  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. @Autowired
  51. IFolderSceneService folderSceneService;
  52. @Override
  53. public void unbindCamera(Long cameraId) {
  54. CameraDetail cameraDetail = this.getByCameraId(cameraId);
  55. if(cameraDetail == null){
  56. throw new BusinessException(ResultCode.CAMERA_NOT_EXIST);
  57. }
  58. User user = userService.getById(cameraDetail.getUserId());
  59. if(user == null){
  60. throw new BusinessException(ResultCode.USER_NOT_EXIST);
  61. }
  62. String snCode = null;
  63. String cooperationUserName = null;
  64. if(cameraDetail.getType() == 10 || cameraDetail.getType() == 11){
  65. Camera cameraEntity = cameraService.getById(cameraDetail.getCameraId());
  66. snCode = cameraEntity.getSnCode();
  67. cooperationUserName = user.getUserName();
  68. fdkkLaserService.disableCooperation(snCode,cooperationUserName); //通知深时删除协作场景
  69. fdkkLaserService.toBind(snCode); //通知深时删除协作场景
  70. }
  71. sceneCooperationService.deleteCooperation(cameraId); //删除协作场景关系
  72. LambdaUpdateWrapper<CameraDetail> wrapper = new LambdaUpdateWrapper<>();
  73. wrapper.eq(CameraDetail::getCameraId,cameraId);
  74. wrapper.set(CameraDetail::getUserId,null);
  75. wrapper.set(CameraDetail::getCooperationUser,null);
  76. //恢复10G基本容量
  77. this.update(wrapper);
  78. if(!"local".equals(CacheUtil.uploadType) && cameraDetail.getType() !=10 && cameraDetail.getType() !=11){
  79. sceneProService.lockOrUnLockBySpace(cameraDetail); //封存场景
  80. }
  81. //解绑删除相机。文件夹与场景绑定关系
  82. folderSceneService.delByCameraId(cameraId);
  83. }
  84. @Override
  85. public CameraDetail getByCameraId(Long cameraId) {
  86. LambdaQueryWrapper<CameraDetail> wrapper = new LambdaQueryWrapper<>();
  87. wrapper.eq(CameraDetail::getCameraId,cameraId);
  88. List<CameraDetail> list = this.list(wrapper);
  89. if(list == null || list.size() <=0){
  90. return null;
  91. }
  92. return list.get(0);
  93. }
  94. @Override
  95. public List<CameraDetail> getByCameraIds(List<Long> cameraIds) {
  96. if(cameraIds.isEmpty()){
  97. return new ArrayList<>();
  98. }
  99. LambdaQueryWrapper<CameraDetail> wrapper = new LambdaQueryWrapper<>();
  100. wrapper.in(CameraDetail::getCameraId,cameraIds);
  101. return this.list(wrapper);
  102. }
  103. @Override
  104. public void deleteByCameraId(Long cameraId) {
  105. LambdaQueryWrapper<CameraDetail> wrapper = new LambdaQueryWrapper<>();
  106. wrapper.eq(CameraDetail::getCameraId,cameraId);
  107. this.remove(wrapper);
  108. }
  109. @Override
  110. public HashMap<Long, Long> getCountGroupByUserId(List<Long> userIdList) {
  111. HashMap<Long,Long> map = new HashMap<>();
  112. List<GroupByCount> result = this.getBaseMapper().getCountGroupByUserId(userIdList);
  113. result.forEach(entity ->map.put(entity.getId(),entity.getCount()));
  114. return map;
  115. }
  116. @Override
  117. public HashMap<Long, Long> getCountGroupByCompanyId() {
  118. List<GroupByCount> result = this.getBaseMapper().getCountGroupByCompanyId();
  119. HashMap<Long,Long> map = new HashMap<>();
  120. result.forEach(entity ->map.put(entity.getId(),entity.getCount()));
  121. return map;
  122. }
  123. @Override
  124. public HashMap<Long, Long> getSceneCountGroupByCameraId() {
  125. LambdaQueryWrapper<CameraDetail> wrapper = new LambdaQueryWrapper<>();
  126. wrapper.isNotNull(CameraDetail::getCompanyId);
  127. List<CameraDetail> list = this.list(wrapper);
  128. Set<Long> cameraIds = list.parallelStream().map(CameraDetail::getCameraId).collect(Collectors.toSet());
  129. HashMap<Long, Long> resultMap = new HashMap<>();
  130. if(cameraIds.size() >0){
  131. HashMap<Long, Long> sceneProMap = sceneProService.getCountGroupByCameraId(new ArrayList<>(cameraIds));
  132. HashMap<Long, Long> sceneMap = sceneService.getCountGroupByCameraId(new ArrayList<>(cameraIds));
  133. HashMap<Long, Long> scenePlusMap = scenePlusService.getCountGroupByCameraId(new ArrayList<>(cameraIds));
  134. HashMap<Long,Camera> cameraHashMap = new HashMap<>();
  135. List<Camera> cameraList = cameraService.listByIds(cameraIds);
  136. cameraList.forEach(entity -> cameraHashMap.put(entity.getId(),entity));
  137. HashMap<Long,List<String>> companySnCodeMap = new HashMap<>();
  138. for (CameraDetail cameraDetail : list) {
  139. Long sceneProCount = sceneProMap.get(cameraDetail.getCameraId()) == null ? 0L : sceneProMap.get(cameraDetail.getCameraId());
  140. Long sceneCount = sceneMap.get(cameraDetail.getCameraId()) == null ? 0L : sceneMap.get(cameraDetail.getCameraId());
  141. Long scenePlusCount = scenePlusMap.get(cameraDetail.getCameraId()) == null ? 0L : scenePlusMap.get(cameraDetail.getCameraId());
  142. Long count = sceneProCount + sceneCount + scenePlusCount;
  143. List<String> snCodeList = companySnCodeMap.computeIfAbsent(cameraDetail.getCompanyId(), k -> new ArrayList<>());
  144. Camera camera = cameraHashMap.get(cameraDetail.getCameraId());
  145. if(camera != null){
  146. snCodeList.add(camera.getSnCode());
  147. }
  148. resultMap.merge(cameraDetail.getCompanyId(), count, Long::sum);
  149. }
  150. for (Long companyId : companySnCodeMap.keySet()) {
  151. List<String> snCodeList = companySnCodeMap.get(companyId);
  152. if(snCodeList == null || snCodeList.size() <=0){
  153. continue;
  154. }
  155. SceneParam param = new SceneParam();
  156. param.setSnCodes(snCodeList);
  157. PageInfo pageInfo = laserService.pageList(param);
  158. resultMap.merge(companyId, pageInfo.getTotal(), Long::sum);
  159. }
  160. }
  161. return resultMap;
  162. }
  163. @Override
  164. public Long getCountByCompanyId(Long companyId) {
  165. LambdaQueryWrapper<CameraDetail> wrapper = new LambdaQueryWrapper<>();
  166. wrapper.eq(CameraDetail::getCompanyId,companyId);
  167. return this.count(wrapper);
  168. }
  169. @Override
  170. public List<CameraDetail> getListByCompanyId(Integer companyId) {
  171. LambdaQueryWrapper<CameraDetail> wrapper = new LambdaQueryWrapper<>();
  172. wrapper.eq(CameraDetail::getCompanyId,companyId);
  173. return this.list(wrapper);
  174. }
  175. @Override
  176. public List<CameraDetail> getByUserName(String userName) {
  177. LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
  178. wrapper.like(User::getUserName,userName);
  179. List<User> list = userService.list(wrapper);
  180. if(list.size() >0){
  181. List<Long> userIds = list.stream().map(User::getId).collect(Collectors.toList());
  182. if(userIds.size() >0){
  183. LambdaQueryWrapper<CameraDetail> dtW = new LambdaQueryWrapper<>();
  184. dtW.in(CameraDetail::getUserId,userIds);
  185. return this.list(dtW);
  186. }
  187. }
  188. return null;
  189. }
  190. @Override
  191. public void delAgentId(Integer agentId) {
  192. LambdaUpdateWrapper<CameraDetail> wrapper = new LambdaUpdateWrapper<>();
  193. wrapper.eq(CameraDetail::getAgentId,agentId);
  194. wrapper.set(CameraDetail::getAgentId,null);
  195. this.update(wrapper);
  196. }
  197. @Override
  198. public List<CameraDetail> getByUserId(Long userId) {
  199. LambdaQueryWrapper<CameraDetail> wrapper = new LambdaQueryWrapper<>();
  200. wrapper.eq(CameraDetail::getUserId,userId);
  201. return this.list(wrapper);
  202. }
  203. @Override
  204. public void addUsedSpace(Long cameraId,Long space) {
  205. if(space == null){
  206. return;
  207. }
  208. CameraDetail cameraDetail = this.getByCameraId(cameraId);
  209. long usedSpace = cameraDetail.getUsedSpace() - space ;
  210. cameraDetail.setUsedSpace(usedSpace < 0 ? 0L :usedSpace);
  211. //解封封存场景
  212. if(cameraDetail.getType() != 10 && cameraDetail.getType() !=11){
  213. sceneProService.lockOrUnLockBySpace(cameraDetail);
  214. }
  215. this.updateById(cameraDetail);
  216. }
  217. @Override
  218. public Long getTotalSpaceByCameraId(Long cameraId) {
  219. CameraDetail cameraDetail = this.getByCameraId(cameraId);
  220. if(cameraDetail == null){
  221. return 0L;
  222. }
  223. return getTotalSpaceByCameraId(cameraDetail);
  224. }
  225. @Override
  226. public Long getTotalSpaceByCameraId(CameraDetail cameraDetail) {
  227. UserIncrement userIncrement = userIncrementService.getByCameraId(cameraDetail.getCameraId());
  228. if(userIncrement != null && userIncrement.getIsExpired() == 0){
  229. IncrementType incrementType = incrementTypeService.getById(userIncrement.getIncrementTypeId());
  230. return getTotalSpace(cameraDetail,incrementType);
  231. }
  232. return getTotalSpace(cameraDetail,null);
  233. }
  234. @Override
  235. public Boolean checkSpace(Long cameraId, Long space) {
  236. CameraDetail cameraDetail = this.getByCameraId(cameraId);
  237. if(cameraDetail == null){
  238. return false;
  239. }
  240. return checkSpace(cameraDetail,space);
  241. }
  242. @Override
  243. public Boolean checkSpace(CameraDetail detailEntity, Long space) {
  244. if(!"aws".equals(CacheUtil.uploadType) && (detailEntity.getType() == 10 || detailEntity.getType() == 11)){
  245. return true;
  246. }
  247. UserIncrement userIncrement = userIncrementService.getByCameraId(detailEntity.getCameraId());
  248. if(userIncrement == null || userIncrement.getIsExpired() == 1){
  249. return checkSpace(detailEntity,null,space);
  250. }
  251. IncrementType incrementType = incrementTypeService.getById(userIncrement.getIncrementTypeId());
  252. return checkSpace(detailEntity,incrementType,space);
  253. }
  254. @Override
  255. public Boolean checkSpace(CameraDetail detailEntity, IncrementType incrementType, Long space) {
  256. Long totalSpace = 0L;
  257. if("SP".equals(detailEntity.getUnit())){
  258. if(incrementType!=null && incrementType.getCameraSpace() == -1){
  259. return true;
  260. }
  261. totalSpace = incrementType != null ?incrementType.getCameraSpace() : detailEntity.getTotalSpace();
  262. Long proCount = sceneProService.getCountByCameraId(detailEntity.getCameraId());
  263. Long plusCount = scenePlusService.getCountByCameraId(detailEntity.getCameraId());
  264. return proCount + plusCount + 1 <= totalSpace;
  265. }
  266. if("GB".equals(detailEntity.getUnit())){
  267. if(incrementType!=null && incrementType.getCameraCapacity() == -1){
  268. return true;
  269. }
  270. totalSpace = incrementType != null ?incrementType.getCameraCapacity() * 1024 * 1024 * 1024L: detailEntity.getTotalSpace();
  271. return detailEntity.getUsedSpace() + space <= totalSpace ;
  272. }
  273. return false;
  274. }
  275. public Long getTotalSpace(CameraDetail detailEntity, IncrementType incrementType) {
  276. if("SP".equals(detailEntity.getUnit())){
  277. if(incrementType!=null && incrementType.getCameraSpace() == -1){
  278. return -1L;
  279. }
  280. return incrementType != null ?incrementType.getCameraSpace() : detailEntity.getTotalSpace();
  281. }
  282. if("GB".equals(detailEntity.getUnit())){
  283. if(incrementType!=null && incrementType.getCameraCapacity() == -1){
  284. return -1L;
  285. }
  286. return incrementType != null ?incrementType.getCameraCapacity() * 1024 * 1024 * 1024L: detailEntity.getTotalSpace();
  287. }
  288. return 0L;
  289. }
  290. @Override
  291. public void initSpace(Long cameraId) {
  292. CameraDetail cameraDetail = this.getByCameraId(cameraId);
  293. if(cameraDetail != null){
  294. initSpace(cameraDetail);
  295. }
  296. }
  297. @Override
  298. public void initSpace(CameraDetail cameraDetail) {
  299. LambdaUpdateWrapper<CameraDetail> wrapper = new LambdaUpdateWrapper<>();
  300. wrapper.eq(CameraDetail::getId,cameraDetail.getId());
  301. if("GB".equals(cameraDetail.getUnit())){
  302. Long proSpace = sceneProService.getSpaceSumByCameraId(cameraDetail.getCameraId());
  303. Long plusSpace = scenePlusService.getSpaceSumByCameraId(cameraDetail.getCameraId());
  304. wrapper.set(CameraDetail::getUsedSpace,(plusSpace == null ?0L:plusSpace) + (proSpace == null ?0L:proSpace));
  305. }
  306. if("SP".equals(cameraDetail.getUnit())){
  307. Long proSpace = sceneProService.getCountByCameraId(cameraDetail.getCameraId());
  308. Long plusSpace = scenePlusService.getCountByCameraId(cameraDetail.getCameraId());
  309. wrapper.set(CameraDetail::getUsedSpace,(plusSpace == null ?0L:plusSpace )+( proSpace == null ?0L:proSpace));
  310. }
  311. this.update(wrapper);
  312. }
  313. @Override
  314. public void updateUseSpace(Long cameraId, Long spase) {
  315. LambdaUpdateWrapper<CameraDetail> wrapper = new LambdaUpdateWrapper<>();
  316. wrapper.eq(CameraDetail::getCameraId,cameraId);
  317. wrapper.setSql("used_space = used_space + " + spase);
  318. this.update(wrapper);
  319. }
  320. }