CameraServiceImpl.java 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. package com.fdkankan.ucenter.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.plugins.pagination.Page;
  5. import com.fdkankan.common.exception.BusinessException;
  6. import com.fdkankan.db.response.PageInfo;
  7. import com.fdkankan.common.util.DateUtil;
  8. import com.fdkankan.common.util.FileSizeUtil;
  9. import com.fdkankan.ucenter.entity.*;
  10. import com.fdkankan.ucenter.mapper.ICameraMapper;
  11. import com.fdkankan.ucenter.service.*;
  12. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  13. import com.fdkankan.ucenter.util.DateUserUtil;
  14. import com.fdkankan.ucenter.vo.request.CameraParam;
  15. import com.fdkankan.ucenter.vo.response.CameraAppVo;
  16. import com.fdkankan.ucenter.vo.response.CameraSpaceVo;
  17. import com.fdkankan.ucenter.vo.response.CameraVo;
  18. import com.fdkankan.ucenter.vo.response.GroupByCount;
  19. import org.apache.commons.lang3.StringUtils;
  20. import org.joda.time.DateTime;
  21. import org.joda.time.Days;
  22. import org.springframework.beans.factory.annotation.Autowired;
  23. import org.springframework.stereotype.Service;
  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 CameraServiceImpl extends ServiceImpl<ICameraMapper, Camera> implements ICameraService {
  36. @Autowired
  37. IUserService userService;
  38. @Autowired
  39. FdkkLaserService fdkkLaserService;
  40. @Autowired
  41. ISceneProService sceneProService;
  42. @Autowired
  43. ICameraSpaceService cameraSpaceService;
  44. @Autowired
  45. IUserIncrementService userIncrementService;
  46. @Autowired
  47. ISceneService sceneService;
  48. @Autowired
  49. ISceneResourceCameraService sceneResourceCameraService;
  50. @Autowired
  51. ICameraDetailService cameraDetailService;
  52. @Autowired
  53. IScenePlusService scenePlusService;
  54. @Autowired
  55. ISceneCooperationService sceneCooperationService;
  56. @Override
  57. public Camera getBySnCode(String snCode) {
  58. LambdaQueryWrapper<Camera> wrapper = new LambdaQueryWrapper<>();
  59. wrapper.eq(Camera::getSnCode,snCode);
  60. List<Camera> list = this.list(wrapper);
  61. if(list !=null && list.size() > 0){
  62. return list.get(0);
  63. }
  64. return null;
  65. }
  66. @Override
  67. public List<Camera> getCameraLikeSnCode(String snCode) {
  68. LambdaQueryWrapper<Camera> wrapper = new LambdaQueryWrapper<>();
  69. wrapper.like(Camera::getSnCode,snCode);
  70. return this.list(wrapper);
  71. }
  72. @Override
  73. public HashMap<Long, Camera> getByIds(List<Long> cameraIds) {
  74. LambdaQueryWrapper<Camera> wrapper = new LambdaQueryWrapper<>();
  75. wrapper.in(Camera::getId,cameraIds);
  76. List<Camera> list = this.list(wrapper);
  77. HashMap<Long, Camera> map = new HashMap<>();
  78. list.forEach(entity -> map.put(entity.getId(),entity));
  79. return map;
  80. }
  81. @Override
  82. public PageInfo pageList(CameraParam param) {
  83. User user = userService.getByUserName(param.getUserName());
  84. param.setUserId(user.getId());
  85. Page<CameraVo> page = this.getBaseMapper().pageList(new Page<>(param.getPageNum(),param.getPageSize()),param);
  86. List<Long> cameraIdList = page.getRecords().parallelStream().map(CameraVo::getId).collect(Collectors.toList());
  87. HashMap<Long,GroupByCount> sceneNumMap = new HashMap<>(); //场景数量,最后拍摄时间
  88. HashMap<Long, CameraSpaceVo> cameraSpaceMap = new HashMap<>(); //到期时间
  89. HashMap<Long, UserIncrement> incrementMap = new HashMap<>(); //到期时间,是否过期
  90. if(cameraIdList.size() >0){
  91. sceneNumMap = sceneProService.findSceneNumByCameraIds(cameraIdList);
  92. cameraSpaceMap = cameraSpaceService.getVoListByCameraIds(cameraIdList);
  93. incrementMap = userIncrementService.findByCameraIds(cameraIdList);
  94. }
  95. for (CameraVo responseCamera : page.getRecords()) {
  96. responseCamera.setCameraType(param.getCameraType());
  97. if (responseCamera.getType() != 0){
  98. responseCamera.setUsedSpaceStr(FileSizeUtil.formatFileSize(Long.parseLong(responseCamera.getUsedSpace())));
  99. responseCamera.setTotalSpaceStr(FileSizeUtil.formatFileSize(Long.parseLong(responseCamera.getTotalSpace())));
  100. String usedSpace = String.valueOf(FileSizeUtil.formetFileSize(Long.parseLong(responseCamera.getUsedSpace()), FileSizeUtil.SIZETYPE_GB));
  101. String totalSpace = String.valueOf(FileSizeUtil.formetFileSize(Long.parseLong(responseCamera.getTotalSpace()), FileSizeUtil.SIZETYPE_GB));
  102. responseCamera.setUsedSpace(usedSpace);
  103. responseCamera.setTotalSpace(totalSpace);
  104. }
  105. if(cameraSpaceMap.get(responseCamera.getId()) !=null){
  106. responseCamera.setSpace((long)FileSizeUtil.formetFileSize(cameraSpaceMap.get(responseCamera.getId()).getSpace().longValue(), FileSizeUtil.SIZETYPE_GB));
  107. responseCamera.setSpaceStr(FileSizeUtil.formatFileSize(cameraSpaceMap.get(responseCamera.getId()).getSpace().longValue()));
  108. responseCamera.setSpaceEndStr(DateUtil.date2String(cameraSpaceMap.get(responseCamera.getId()).getSpaceEndTime(), DateUtil.YYYY_MM_DD_DATE_FORMAT));
  109. if(Days.daysBetween(new DateTime(), new DateTime(cameraSpaceMap.get(responseCamera.getId()).getSpaceEndTime())).getDays() < 7){
  110. responseCamera.setIsExpire(true);
  111. }
  112. }
  113. if(incrementMap.get(responseCamera.getId()) != null){
  114. responseCamera.setUserIncrementId(incrementMap.get(responseCamera.getId()).getId());
  115. responseCamera.setSpaceEndStr(DateUserUtil.getDayTime(incrementMap.get(responseCamera.getId()).getIncrementEndTime()));
  116. String dayTime = incrementMap.get(responseCamera.getId()).getIncrementEndTime();
  117. Integer surplusDate = Days.daysBetween(new DateTime(), new DateTime( DateUserUtil.getDate(dayTime))).getDays();
  118. if(surplusDate < 31){
  119. responseCamera.setIsExpire(true);
  120. responseCamera.setSurplusDate(surplusDate);
  121. }else {
  122. responseCamera.setIsExpire(false);
  123. }
  124. }
  125. if(responseCamera.getType() == 10){
  126. Map<String, String> params = new HashMap<>();
  127. params.put("snCode", responseCamera.getSnCode());
  128. responseCamera.setSceneNum(fdkkLaserService.getLaserSceneNum(params));
  129. }else{
  130. Integer sceneNum = sceneNumMap.get(responseCamera.getId()) == null ? 0 : Math.toIntExact(sceneNumMap.get(responseCamera.getId()).getCount());
  131. String lastTime = sceneNumMap.get(responseCamera.getId()) == null ? "" : sceneNumMap.get(responseCamera.getId()).getLastTime();
  132. responseCamera.setSceneNum(sceneNum);
  133. responseCamera.setLastTime(lastTime);
  134. }
  135. }
  136. return PageInfo.PageInfo(page);
  137. }
  138. @Override
  139. public HashMap<String, Object> deadline(String username) {
  140. //7天内过期的集合
  141. List<CameraSpaceVo> cameraSpaceList = new ArrayList<>();
  142. //所有期限的集合
  143. List<CameraSpaceVo> cameraSpaceAll = new ArrayList<>();
  144. User user = userService.getByUserName(username);
  145. CameraParam param = new CameraParam();
  146. param.setPageNum(1);
  147. param.setPageSize(100);
  148. param.setUserId(user.getId());
  149. Page<CameraVo> page = this.getBaseMapper().pageList(new Page<>(param.getPageNum(),param.getPageSize()),param);
  150. List<Long> cameraIdList = page.getRecords().parallelStream().map(CameraVo::getId).collect(Collectors.toList());
  151. if(cameraIdList.size() >0){
  152. HashMap<Long, CameraSpaceVo> cameraSpaceMap = cameraSpaceService.getVoListByCameraIds(cameraIdList);
  153. for (CameraVo responseCamera : page.getRecords()) {
  154. CameraSpaceVo cameraSpace = cameraSpaceMap.get(responseCamera.getId());
  155. if(cameraSpace != null){
  156. DateTime dateTime = new DateTime(cameraSpace.getSpaceEndTime());
  157. cameraSpace.setSnCode(responseCamera.getSnCode());
  158. cameraSpace.setUsedSpace(Double.parseDouble(responseCamera.getUsedSpace()));
  159. cameraSpace.setUsedSpaceStr(responseCamera.getUsedSpaceStr());
  160. cameraSpace.setTotalSpace(Double.parseDouble(responseCamera.getTotalSpace()));
  161. cameraSpace.setTotalSpaceStr(responseCamera.getTotalSpaceStr());
  162. Long space = cameraSpace.getSpace();
  163. cameraSpace.setSpace((long)FileSizeUtil.formetFileSize(space, FileSizeUtil.SIZETYPE_GB));
  164. cameraSpace.setSpaceStr(FileSizeUtil.formatFileSize(space));
  165. cameraSpace.setSpaceEndStr(DateUtil.date2String(cameraSpace.getSpaceEndTime(), DateUtil.YYYY_MM_DD_DATE_FORMAT));
  166. if(Days.daysBetween(new DateTime(), dateTime).getDays() < 7){
  167. cameraSpaceList.add(cameraSpace);
  168. cameraSpaceAll.add(cameraSpace);
  169. continue;
  170. }
  171. cameraSpaceAll.add(cameraSpace);
  172. }
  173. }
  174. }
  175. HashMap<String,Object> map = new HashMap<>();
  176. map.put("nickName", user.getNickName());
  177. if(cameraSpaceList.size() > 0){
  178. map.put("isExpire", true);
  179. Collections.sort(cameraSpaceList);
  180. map.put("cameraList", cameraSpaceList);
  181. return map;
  182. }
  183. map.put("isExpire", false);
  184. Collections.sort(cameraSpaceAll);
  185. map.put("cameraList", cameraSpaceAll);
  186. return map;
  187. }
  188. @Override
  189. public void unbind(Long cameraId, String ids,String userName) {
  190. if(cameraId != null){
  191. ids = cameraId.toString();
  192. }
  193. String[] split = ids.split(",");
  194. List<Long> cameraIds = new ArrayList<>();
  195. for (String id : split) {
  196. cameraIds.add(Long.valueOf(id));
  197. }
  198. cameraDetailService.unbindCamera(cameraIds); //取消相机用户关联
  199. userIncrementService.unbindCamera(cameraIds); //取消关联用户权益
  200. sceneService.bindOrUnCamera(cameraIds,null); //取消关联场景
  201. sceneProService.bindOrUnCamera(cameraIds,null); //取消关联场景
  202. scenePlusService.bindOrUnCamera(cameraIds,null); //取消关联场景
  203. sceneResourceCameraService.deleteByCameraIds(cameraIds); //删除协作相机
  204. }
  205. @Override
  206. public void bind(Integer cameraType, String snCodes, String username) {
  207. User user = userService.getByUserName(username);
  208. String[] snCodeArr = snCodes.split(",");
  209. List<String> snCodeList = Arrays.asList(snCodeArr);
  210. LambdaUpdateWrapper<Camera> wrapper = new LambdaUpdateWrapper<>();
  211. wrapper.in(Camera::getSnCode,snCodeList);
  212. List<Camera> list = this.list(wrapper);
  213. List<Long> cameraIds = list.parallelStream().map(Camera::getId).collect(Collectors.toList());
  214. cameraDetailService.bindCamera(cameraIds,user.getId());
  215. sceneService.bindOrUnCamera(cameraIds,user.getId());
  216. sceneProService.bindOrUnCamera(cameraIds,user.getId());
  217. scenePlusService.bindOrUnCamera(cameraIds,user.getId());
  218. }
  219. @Override
  220. public void saveCooperationUser(String ids, Long id,String resourceIds, String username) {
  221. if (id != null) {
  222. ids = id.toString();
  223. }
  224. String[] split = ids.split(",");
  225. List<Long> cameraIds = new ArrayList<>();
  226. for (String cameraId : split) {
  227. cameraIds.add(Long.valueOf(cameraId));
  228. }
  229. HashMap<Long, CameraDetail> detailMap = cameraDetailService.getByCameraIds(cameraIds);
  230. HashMap<Long, Camera> cameraMap = this.getByIds(cameraIds);
  231. User user = userService.getByUserName(username);
  232. cameraDetailService.updateCooperationByIds(cameraIds, user.getId());
  233. sceneResourceCameraService.deleteByCameraIds(cameraIds);
  234. //保存相机和场景资源的关联关系你
  235. List<Long> resourceIdList = new ArrayList<>();
  236. if (StringUtils.isNotEmpty(resourceIds)) {
  237. for (String rId : resourceIds.split(",")) {
  238. resourceIdList.add(Long.valueOf(rId));
  239. }
  240. sceneResourceCameraService.saveBatchByCameraIds(cameraIds,resourceIdList);
  241. }
  242. List<ScenePro> sceneProList = sceneProService.getListByCameraIds(cameraIds);
  243. List<ScenePlus> scenePlusList = scenePlusService.getListByCameraIds(cameraIds);
  244. sceneCooperationService.deleteCooperationList(sceneProList,scenePlusList);
  245. sceneCooperationService.saveBatchByList(sceneProList,scenePlusList,user.getId(),resourceIdList);
  246. fdkkLaserService.disableCooperation(detailMap, cameraMap);
  247. fdkkLaserService.enableCameraCooperation(detailMap, cameraMap, username);
  248. }
  249. @Override
  250. public void deleteCooperationUser(Long cameraId) {
  251. List<Long> cameraIds = new ArrayList<>();
  252. cameraIds.add(cameraId);
  253. HashMap<Long, CameraDetail> detailMap = cameraDetailService.getByCameraIds(cameraIds);
  254. HashMap<Long, Camera> cameraMap = this.getByIds(cameraIds);
  255. cameraDetailService.updateCooperationByIds(cameraIds, null);
  256. sceneResourceCameraService.deleteByCameraIds(cameraIds);
  257. List<ScenePro> sceneProList = sceneProService.getListByCameraIds(cameraIds);
  258. List<ScenePlus> scenePlusList = scenePlusService.getListByCameraIds(cameraIds);
  259. sceneCooperationService.deleteCooperationList(sceneProList,scenePlusList);
  260. fdkkLaserService.disableCooperation(detailMap, cameraMap);
  261. }
  262. @Override
  263. public Camera getBySnCodeAndPassword(String appUserName, String appPassword) {
  264. LambdaQueryWrapper<Camera> wrapper = new LambdaQueryWrapper<>();
  265. wrapper.eq(Camera::getChildName,appUserName)
  266. .eq(Camera::getChildPassword,appPassword);
  267. List<Camera> list = this.list(wrapper);
  268. if(list != null && list.size() >0){
  269. return list.get(0);
  270. }
  271. return null;
  272. }
  273. @Override
  274. public Camera getByChildName(String childName) {
  275. LambdaQueryWrapper<Camera> wrapper = new LambdaQueryWrapper<>();
  276. wrapper.eq(Camera::getChildName,childName);
  277. List<Camera> list = this.list(wrapper);
  278. if(list != null && list.size() >0){
  279. return list.get(0);
  280. }
  281. return null;
  282. }
  283. @Override
  284. public List<CameraAppVo> getCameraInfo(String childName, String childPassword) {
  285. return this.getBaseMapper().getCameraInfo(childName,childPassword);
  286. }
  287. @Override
  288. public List<Camera> getAllList(String childName,String token) {
  289. User user = userService.getByToken(token);
  290. if(user == null){
  291. return new ArrayList<>();
  292. }
  293. return this.getBaseMapper().getAllListByUser(user.getId(),childName);
  294. }
  295. }