123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338 |
- package com.fdkankan.manage.service.impl;
- import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
- import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
- import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
- import com.fdkankan.common.constant.Constant;
- import com.fdkankan.manage.common.CacheUtil;
- import com.fdkankan.manage.common.PageInfo;
- import com.fdkankan.manage.common.ResultCode;
- import com.fdkankan.manage.entity.*;
- import com.fdkankan.manage.exception.BusinessException;
- import com.fdkankan.manage.httpClient.service.LaserService;
- import com.fdkankan.manage.mapper.ICameraDetailMapper;
- import com.fdkankan.manage.service.*;
- import com.fdkankan.manage.vo.request.SceneParam;
- import com.fdkankan.manage.vo.response.GroupByCount;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Service;
- import java.util.*;
- import java.util.stream.Collectors;
- /**
- * <p>
- * 相机子表 服务实现类
- * </p>
- *
- * @author
- * @since 2022-06-16
- */
- @Service
- public class CameraDetailServiceImpl extends ServiceImpl<ICameraDetailMapper, CameraDetail> implements ICameraDetailService {
- @Autowired
- ICameraService cameraService;
- @Autowired
- ISceneCooperationService sceneCooperationService;
- @Autowired
- ISceneProService sceneProService;
- @Autowired
- ISceneService sceneService;
- @Autowired
- IScenePlusService scenePlusService;
- @Autowired
- LaserService fdkkLaserService;
- @Autowired
- IUserService userService;
- @Autowired
- LaserService laserService;
- @Autowired
- IUserIncrementService userIncrementService;
- @Autowired
- IIncrementTypeService incrementTypeService;
- @Autowired
- IFolderSceneService folderSceneService;
- @Override
- public void unbindCamera(Long cameraId) {
- CameraDetail cameraDetail = this.getByCameraId(cameraId);
- if(cameraDetail == null){
- throw new BusinessException(ResultCode.CAMERA_NOT_EXIST);
- }
- User user = userService.getById(cameraDetail.getUserId());
- if(user == null){
- throw new BusinessException(ResultCode.USER_NOT_EXIST);
- }
- String snCode = null;
- String cooperationUserName = null;
- if(cameraDetail.getType() == 10 || cameraDetail.getType() == 11){
- Camera cameraEntity = cameraService.getById(cameraDetail.getCameraId());
- snCode = cameraEntity.getSnCode();
- cooperationUserName = user.getUserName();
- fdkkLaserService.disableCooperation(snCode,cooperationUserName); //通知深时删除协作场景
- fdkkLaserService.toBind(snCode); //通知深时删除协作场景
- }
- sceneCooperationService.deleteCooperation(cameraId); //删除协作场景关系
- LambdaUpdateWrapper<CameraDetail> wrapper = new LambdaUpdateWrapper<>();
- wrapper.eq(CameraDetail::getCameraId,cameraId);
- wrapper.set(CameraDetail::getUserId,null);
- wrapper.set(CameraDetail::getCooperationUser,null);
- //恢复10G基本容量
- this.update(wrapper);
- if(!"local".equals(CacheUtil.uploadType) && cameraDetail.getType() !=10 && cameraDetail.getType() !=11){
- sceneProService.lockOrUnLockBySpace(cameraDetail); //封存场景
- }
- //解绑删除相机。文件夹与场景绑定关系
- folderSceneService.delByCameraId(cameraId);
- }
- @Override
- public CameraDetail getByCameraId(Long cameraId) {
- LambdaQueryWrapper<CameraDetail> wrapper = new LambdaQueryWrapper<>();
- wrapper.eq(CameraDetail::getCameraId,cameraId);
- List<CameraDetail> list = this.list(wrapper);
- if(list == null || list.size() <=0){
- return null;
- }
- return list.get(0);
- }
- @Override
- public List<CameraDetail> getByCameraIds(List<Long> cameraIds) {
- LambdaQueryWrapper<CameraDetail> wrapper = new LambdaQueryWrapper<>();
- wrapper.in(CameraDetail::getCameraId,cameraIds);
- return this.list(wrapper);
- }
- @Override
- public void deleteByCameraId(Long cameraId) {
- LambdaQueryWrapper<CameraDetail> wrapper = new LambdaQueryWrapper<>();
- wrapper.eq(CameraDetail::getCameraId,cameraId);
- this.remove(wrapper);
- }
- @Override
- public HashMap<Long, Long> getCountGroupByUserId(List<Long> userIdList) {
- HashMap<Long,Long> map = new HashMap<>();
- List<GroupByCount> result = this.getBaseMapper().getCountGroupByUserId(userIdList);
- result.forEach(entity ->map.put(entity.getId(),entity.getCount()));
- return map;
- }
- @Override
- public HashMap<Long, Long> getCountGroupByCompanyId() {
- List<GroupByCount> result = this.getBaseMapper().getCountGroupByCompanyId();
- HashMap<Long,Long> map = new HashMap<>();
- result.forEach(entity ->map.put(entity.getId(),entity.getCount()));
- return map;
- }
- @Override
- public HashMap<Long, Long> getSceneCountGroupByCameraId() {
- LambdaQueryWrapper<CameraDetail> wrapper = new LambdaQueryWrapper<>();
- wrapper.isNotNull(CameraDetail::getCompanyId);
- List<CameraDetail> list = this.list(wrapper);
- Set<Long> cameraIds = list.parallelStream().map(CameraDetail::getCameraId).collect(Collectors.toSet());
- HashMap<Long, Long> resultMap = new HashMap<>();
- if(cameraIds.size() >0){
- HashMap<Long, Long> sceneProMap = sceneProService.getCountGroupByCameraId(new ArrayList<>(cameraIds));
- HashMap<Long, Long> sceneMap = sceneService.getCountGroupByCameraId(new ArrayList<>(cameraIds));
- HashMap<Long, Long> scenePlusMap = scenePlusService.getCountGroupByCameraId(new ArrayList<>(cameraIds));
- HashMap<Long,Camera> cameraHashMap = new HashMap<>();
- List<Camera> cameraList = cameraService.listByIds(cameraIds);
- cameraList.forEach(entity -> cameraHashMap.put(entity.getId(),entity));
- HashMap<Long,List<String>> companySnCodeMap = new HashMap<>();
- for (CameraDetail cameraDetail : list) {
- Long sceneProCount = sceneProMap.get(cameraDetail.getCameraId()) == null ? 0L : sceneProMap.get(cameraDetail.getCameraId());
- Long sceneCount = sceneMap.get(cameraDetail.getCameraId()) == null ? 0L : sceneMap.get(cameraDetail.getCameraId());
- Long scenePlusCount = scenePlusMap.get(cameraDetail.getCameraId()) == null ? 0L : scenePlusMap.get(cameraDetail.getCameraId());
- Long count = sceneProCount + sceneCount + scenePlusCount;
- List<String> snCodeList = companySnCodeMap.computeIfAbsent(cameraDetail.getCompanyId(), k -> new ArrayList<>());
- Camera camera = cameraHashMap.get(cameraDetail.getCameraId());
- if(camera != null){
- snCodeList.add(camera.getSnCode());
- }
- resultMap.merge(cameraDetail.getCompanyId(), count, Long::sum);
- }
- for (Long companyId : companySnCodeMap.keySet()) {
- List<String> snCodeList = companySnCodeMap.get(companyId);
- if(snCodeList == null || snCodeList.size() <=0){
- continue;
- }
- SceneParam param = new SceneParam();
- param.setSnCodes(snCodeList);
- PageInfo pageInfo = laserService.pageList(param);
- resultMap.merge(companyId, pageInfo.getTotal(), Long::sum);
- }
- }
- return resultMap;
- }
- @Override
- public Long getCountByCompanyId(Long companyId) {
- LambdaQueryWrapper<CameraDetail> wrapper = new LambdaQueryWrapper<>();
- wrapper.eq(CameraDetail::getCompanyId,companyId);
- return this.count(wrapper);
- }
- @Override
- public List<CameraDetail> getListByCompanyId(Integer companyId) {
- LambdaQueryWrapper<CameraDetail> wrapper = new LambdaQueryWrapper<>();
- wrapper.eq(CameraDetail::getCompanyId,companyId);
- return this.list(wrapper);
- }
- @Override
- public List<CameraDetail> getByUserName(String userName) {
- LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
- wrapper.like(User::getUserName,userName);
- List<User> list = userService.list(wrapper);
- if(list.size() >0){
- List<Long> userIds = list.stream().map(User::getId).collect(Collectors.toList());
- if(userIds.size() >0){
- LambdaQueryWrapper<CameraDetail> dtW = new LambdaQueryWrapper<>();
- dtW.in(CameraDetail::getUserId,userIds);
- return this.list(dtW);
- }
- }
- return null;
- }
- @Override
- public void delAgentId(Integer agentId) {
- LambdaUpdateWrapper<CameraDetail> wrapper = new LambdaUpdateWrapper<>();
- wrapper.eq(CameraDetail::getAgentId,agentId);
- wrapper.set(CameraDetail::getAgentId,null);
- this.update(wrapper);
- }
- @Override
- public List<CameraDetail> getByUserId(Long userId) {
- LambdaQueryWrapper<CameraDetail> wrapper = new LambdaQueryWrapper<>();
- wrapper.eq(CameraDetail::getUserId,userId);
- return this.list(wrapper);
- }
- @Override
- public void addUsedSpace(Long cameraId,Long space) {
- if(space == null){
- return;
- }
- CameraDetail cameraDetail = this.getByCameraId(cameraId);
- long usedSpace = cameraDetail.getUsedSpace() - space ;
- cameraDetail.setUsedSpace(usedSpace < 0 ? 0L :usedSpace);
- //解封封存场景
- if(cameraDetail.getType() != 10 && cameraDetail.getType() !=11){
- sceneProService.lockOrUnLockBySpace(cameraDetail);
- }
- this.updateById(cameraDetail);
- }
- @Override
- public Long getTotalSpaceByCameraId(Long cameraId) {
- CameraDetail cameraDetail = this.getByCameraId(cameraId);
- if(cameraDetail == null){
- return 0L;
- }
- return getTotalSpaceByCameraId(cameraDetail);
- }
- @Override
- public Long getTotalSpaceByCameraId(CameraDetail cameraDetail) {
- UserIncrement userIncrement = userIncrementService.getByCameraId(cameraDetail.getCameraId());
- if(userIncrement != null && userIncrement.getIsExpired() == 0){
- IncrementType incrementType = incrementTypeService.getById(userIncrement.getIncrementTypeId());
- return getTotalSpace(cameraDetail,incrementType);
- }
- return getTotalSpace(cameraDetail,null);
- }
- @Override
- public Boolean checkSpace(Long cameraId, Long space) {
- CameraDetail cameraDetail = this.getByCameraId(cameraId);
- if(cameraDetail == null){
- return false;
- }
- return checkSpace(cameraDetail,space);
- }
- @Override
- public Boolean checkSpace(CameraDetail detailEntity, Long space) {
- if(!"aws".equals(CacheUtil.uploadType) && (detailEntity.getType() == 10 || detailEntity.getType() == 11)){
- return true;
- }
- UserIncrement userIncrement = userIncrementService.getByCameraId(detailEntity.getCameraId());
- if(userIncrement == null || userIncrement.getIsExpired() == 1){
- return checkSpace(detailEntity,null,space);
- }
- IncrementType incrementType = incrementTypeService.getById(userIncrement.getIncrementTypeId());
- return checkSpace(detailEntity,incrementType,space);
- }
- @Override
- public Boolean checkSpace(CameraDetail detailEntity, IncrementType incrementType, Long space) {
- Long totalSpace = 0L;
- if("SP".equals(detailEntity.getUnit())){
- if(incrementType!=null && incrementType.getCameraSpace() == -1){
- return true;
- }
- totalSpace = incrementType != null ?incrementType.getCameraSpace() : detailEntity.getTotalSpace();
- return detailEntity.getUsedSpace() + 1 <= totalSpace;
- }
- if("GB".equals(detailEntity.getUnit())){
- if(incrementType!=null && incrementType.getCameraCapacity() == -1){
- return true;
- }
- totalSpace = incrementType != null ?incrementType.getCameraCapacity() * 1024 * 1024 * 1024L: detailEntity.getTotalSpace();
- return detailEntity.getUsedSpace() + space <= totalSpace ;
- }
- return false;
- }
- public Long getTotalSpace(CameraDetail detailEntity, IncrementType incrementType) {
- if("SP".equals(detailEntity.getUnit())){
- if(incrementType!=null && incrementType.getCameraSpace() == -1){
- return -1L;
- }
- return incrementType != null ?incrementType.getCameraSpace() : detailEntity.getTotalSpace();
- }
- if("GB".equals(detailEntity.getUnit())){
- if(incrementType!=null && incrementType.getCameraCapacity() == -1){
- return -1L;
- }
- return incrementType != null ?incrementType.getCameraCapacity() * 1024 * 1024 * 1024L: detailEntity.getTotalSpace();
- }
- return 0L;
- }
- @Override
- public void initSpace(Long cameraId) {
- CameraDetail cameraDetail = this.getByCameraId(cameraId);
- if(cameraDetail != null){
- initSpace(cameraDetail);
- }
- }
- @Override
- public void initSpace(CameraDetail cameraDetail) {
- LambdaUpdateWrapper<CameraDetail> wrapper = new LambdaUpdateWrapper<>();
- wrapper.eq(CameraDetail::getId,cameraDetail.getId());
- if("GB".equals(cameraDetail.getUnit())){
- Long proSpace = sceneProService.getSpaceSumByCameraId(cameraDetail.getCameraId());
- Long plusSpace = scenePlusService.getSpaceSumByCameraId(cameraDetail.getCameraId());
- wrapper.set(CameraDetail::getUsedSpace,(plusSpace == null ?0L:plusSpace) + (proSpace == null ?0L:proSpace));
- }
- if("SP".equals(cameraDetail.getUnit())){
- Long proSpace = sceneProService.getCountByCameraId(cameraDetail.getCameraId());
- Long plusSpace = scenePlusService.getCountByCameraId(cameraDetail.getCameraId());
- wrapper.set(CameraDetail::getUsedSpace,(plusSpace == null ?0L:plusSpace )+( proSpace == null ?0L:proSpace));
- }
- this.update(wrapper);
- }
- }
|