CameraDetailServiceImpl.java 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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. LambdaQueryWrapper<CameraDetail> wrapper = new LambdaQueryWrapper<>();
  97. wrapper.in(CameraDetail::getCameraId,cameraIds);
  98. return this.list(wrapper);
  99. }
  100. @Override
  101. public void deleteByCameraId(Long cameraId) {
  102. LambdaQueryWrapper<CameraDetail> wrapper = new LambdaQueryWrapper<>();
  103. wrapper.eq(CameraDetail::getCameraId,cameraId);
  104. this.remove(wrapper);
  105. }
  106. @Override
  107. public HashMap<Long, Long> getCountGroupByUserId(List<Long> userIdList) {
  108. HashMap<Long,Long> map = new HashMap<>();
  109. List<GroupByCount> result = this.getBaseMapper().getCountGroupByUserId(userIdList);
  110. result.forEach(entity ->map.put(entity.getId(),entity.getCount()));
  111. return map;
  112. }
  113. @Override
  114. public HashMap<Long, Long> getCountGroupByCompanyId() {
  115. List<GroupByCount> result = this.getBaseMapper().getCountGroupByCompanyId();
  116. HashMap<Long,Long> map = new HashMap<>();
  117. result.forEach(entity ->map.put(entity.getId(),entity.getCount()));
  118. return map;
  119. }
  120. @Override
  121. public HashMap<Long, Long> getSceneCountGroupByCameraId() {
  122. LambdaQueryWrapper<CameraDetail> wrapper = new LambdaQueryWrapper<>();
  123. wrapper.isNotNull(CameraDetail::getCompanyId);
  124. List<CameraDetail> list = this.list(wrapper);
  125. Set<Long> cameraIds = list.parallelStream().map(CameraDetail::getCameraId).collect(Collectors.toSet());
  126. HashMap<Long, Long> resultMap = new HashMap<>();
  127. if(cameraIds.size() >0){
  128. HashMap<Long, Long> sceneProMap = sceneProService.getCountGroupByCameraId(new ArrayList<>(cameraIds));
  129. HashMap<Long, Long> sceneMap = sceneService.getCountGroupByCameraId(new ArrayList<>(cameraIds));
  130. HashMap<Long, Long> scenePlusMap = scenePlusService.getCountGroupByCameraId(new ArrayList<>(cameraIds));
  131. HashMap<Long,Camera> cameraHashMap = new HashMap<>();
  132. List<Camera> cameraList = cameraService.listByIds(cameraIds);
  133. cameraList.forEach(entity -> cameraHashMap.put(entity.getId(),entity));
  134. HashMap<Long,List<String>> companySnCodeMap = new HashMap<>();
  135. for (CameraDetail cameraDetail : list) {
  136. Long sceneProCount = sceneProMap.get(cameraDetail.getCameraId()) == null ? 0L : sceneProMap.get(cameraDetail.getCameraId());
  137. Long sceneCount = sceneMap.get(cameraDetail.getCameraId()) == null ? 0L : sceneMap.get(cameraDetail.getCameraId());
  138. Long scenePlusCount = scenePlusMap.get(cameraDetail.getCameraId()) == null ? 0L : scenePlusMap.get(cameraDetail.getCameraId());
  139. Long count = sceneProCount + sceneCount + scenePlusCount;
  140. List<String> snCodeList = companySnCodeMap.computeIfAbsent(cameraDetail.getCompanyId(), k -> new ArrayList<>());
  141. Camera camera = cameraHashMap.get(cameraDetail.getCameraId());
  142. if(camera != null){
  143. snCodeList.add(camera.getSnCode());
  144. }
  145. resultMap.merge(cameraDetail.getCompanyId(), count, Long::sum);
  146. }
  147. for (Long companyId : companySnCodeMap.keySet()) {
  148. List<String> snCodeList = companySnCodeMap.get(companyId);
  149. if(snCodeList == null || snCodeList.size() <=0){
  150. continue;
  151. }
  152. SceneParam param = new SceneParam();
  153. param.setSnCodes(snCodeList);
  154. PageInfo pageInfo = laserService.pageList(param);
  155. resultMap.merge(companyId, pageInfo.getTotal(), Long::sum);
  156. }
  157. }
  158. return resultMap;
  159. }
  160. @Override
  161. public Long getCountByCompanyId(Long companyId) {
  162. LambdaQueryWrapper<CameraDetail> wrapper = new LambdaQueryWrapper<>();
  163. wrapper.eq(CameraDetail::getCompanyId,companyId);
  164. return this.count(wrapper);
  165. }
  166. @Override
  167. public List<CameraDetail> getListByCompanyId(Integer companyId) {
  168. LambdaQueryWrapper<CameraDetail> wrapper = new LambdaQueryWrapper<>();
  169. wrapper.eq(CameraDetail::getCompanyId,companyId);
  170. return this.list(wrapper);
  171. }
  172. @Override
  173. public List<CameraDetail> getByUserName(String userName) {
  174. LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
  175. wrapper.like(User::getUserName,userName);
  176. List<User> list = userService.list(wrapper);
  177. if(list.size() >0){
  178. List<Long> userIds = list.stream().map(User::getId).collect(Collectors.toList());
  179. if(userIds.size() >0){
  180. LambdaQueryWrapper<CameraDetail> dtW = new LambdaQueryWrapper<>();
  181. dtW.in(CameraDetail::getUserId,userIds);
  182. return this.list(dtW);
  183. }
  184. }
  185. return null;
  186. }
  187. @Override
  188. public void delAgentId(Integer agentId) {
  189. LambdaUpdateWrapper<CameraDetail> wrapper = new LambdaUpdateWrapper<>();
  190. wrapper.eq(CameraDetail::getAgentId,agentId);
  191. wrapper.set(CameraDetail::getAgentId,null);
  192. this.update(wrapper);
  193. }
  194. @Override
  195. public List<CameraDetail> getByUserId(Long userId) {
  196. LambdaQueryWrapper<CameraDetail> wrapper = new LambdaQueryWrapper<>();
  197. wrapper.eq(CameraDetail::getUserId,userId);
  198. return this.list(wrapper);
  199. }
  200. @Override
  201. public void addUsedSpace(Long cameraId,Long space) {
  202. if(space == null){
  203. return;
  204. }
  205. CameraDetail cameraDetail = this.getByCameraId(cameraId);
  206. long usedSpace = cameraDetail.getUsedSpace() - space ;
  207. cameraDetail.setUsedSpace(usedSpace < 0 ? 0L :usedSpace);
  208. //解封封存场景
  209. if(cameraDetail.getType() != 10 && cameraDetail.getType() !=11){
  210. sceneProService.lockOrUnLockBySpace(cameraDetail);
  211. }
  212. this.updateById(cameraDetail);
  213. }
  214. @Override
  215. public Long getTotalSpaceByCameraId(Long cameraId) {
  216. CameraDetail cameraDetail = this.getByCameraId(cameraId);
  217. if(cameraDetail == null){
  218. return 0L;
  219. }
  220. return getTotalSpaceByCameraId(cameraDetail);
  221. }
  222. @Override
  223. public Long getTotalSpaceByCameraId(CameraDetail cameraDetail) {
  224. UserIncrement userIncrement = userIncrementService.getByCameraId(cameraDetail.getCameraId());
  225. if(userIncrement != null && userIncrement.getIsExpired() == 0){
  226. IncrementType incrementType = incrementTypeService.getById(userIncrement.getIncrementTypeId());
  227. return getTotalSpace(cameraDetail,incrementType);
  228. }
  229. return getTotalSpace(cameraDetail,null);
  230. }
  231. @Override
  232. public Boolean checkSpace(Long cameraId, Long space) {
  233. CameraDetail cameraDetail = this.getByCameraId(cameraId);
  234. if(cameraDetail == null){
  235. return false;
  236. }
  237. return checkSpace(cameraDetail,space);
  238. }
  239. @Override
  240. public Boolean checkSpace(CameraDetail detailEntity, Long space) {
  241. if(!"aws".equals(CacheUtil.uploadType) && (detailEntity.getType() == 10 || detailEntity.getType() == 11)){
  242. return true;
  243. }
  244. UserIncrement userIncrement = userIncrementService.getByCameraId(detailEntity.getCameraId());
  245. if(userIncrement == null || userIncrement.getIsExpired() == 1){
  246. return checkSpace(detailEntity,null,space);
  247. }
  248. IncrementType incrementType = incrementTypeService.getById(userIncrement.getIncrementTypeId());
  249. return checkSpace(detailEntity,incrementType,space);
  250. }
  251. @Override
  252. public Boolean checkSpace(CameraDetail detailEntity, IncrementType incrementType, Long space) {
  253. Long totalSpace = 0L;
  254. if("SP".equals(detailEntity.getUnit())){
  255. if(incrementType!=null && incrementType.getCameraSpace() == -1){
  256. return true;
  257. }
  258. totalSpace = incrementType != null ?incrementType.getCameraSpace() : detailEntity.getTotalSpace();
  259. return detailEntity.getUsedSpace() + 1 <= totalSpace;
  260. }
  261. if("GB".equals(detailEntity.getUnit())){
  262. if(incrementType!=null && incrementType.getCameraCapacity() == -1){
  263. return true;
  264. }
  265. totalSpace = incrementType != null ?incrementType.getCameraCapacity() * 1024 * 1024 * 1024L: detailEntity.getTotalSpace();
  266. return detailEntity.getUsedSpace() + space <= totalSpace ;
  267. }
  268. return false;
  269. }
  270. public Long getTotalSpace(CameraDetail detailEntity, IncrementType incrementType) {
  271. if("SP".equals(detailEntity.getUnit())){
  272. if(incrementType!=null && incrementType.getCameraSpace() == -1){
  273. return -1L;
  274. }
  275. return incrementType != null ?incrementType.getCameraSpace() : detailEntity.getTotalSpace();
  276. }
  277. if("GB".equals(detailEntity.getUnit())){
  278. if(incrementType!=null && incrementType.getCameraCapacity() == -1){
  279. return -1L;
  280. }
  281. return incrementType != null ?incrementType.getCameraCapacity() * 1024 * 1024 * 1024L: detailEntity.getTotalSpace();
  282. }
  283. return 0L;
  284. }
  285. @Override
  286. public void initSpace(Long cameraId) {
  287. CameraDetail cameraDetail = this.getByCameraId(cameraId);
  288. if(cameraDetail != null){
  289. initSpace(cameraDetail);
  290. }
  291. }
  292. @Override
  293. public void initSpace(CameraDetail cameraDetail) {
  294. LambdaUpdateWrapper<CameraDetail> wrapper = new LambdaUpdateWrapper<>();
  295. wrapper.eq(CameraDetail::getId,cameraDetail.getId());
  296. if("GB".equals(cameraDetail.getUnit())){
  297. Long proSpace = sceneProService.getSpaceSumByCameraId(cameraDetail.getCameraId());
  298. Long plusSpace = scenePlusService.getSpaceSumByCameraId(cameraDetail.getCameraId());
  299. wrapper.set(CameraDetail::getUsedSpace,(plusSpace == null ?0L:plusSpace) + (proSpace == null ?0L:proSpace));
  300. }
  301. if("SP".equals(cameraDetail.getUnit())){
  302. Long proSpace = sceneProService.getCountByCameraId(cameraDetail.getCameraId());
  303. Long plusSpace = scenePlusService.getCountByCameraId(cameraDetail.getCameraId());
  304. wrapper.set(CameraDetail::getUsedSpace,(plusSpace == null ?0L:plusSpace )+( proSpace == null ?0L:proSpace));
  305. }
  306. this.update(wrapper);
  307. }
  308. }