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.exception.BusinessException;
import com.fdkankan.manage.entity.Camera;
import com.fdkankan.manage.entity.CameraDetail;
import com.fdkankan.manage.entity.User;
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.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
/**
*
* 相机子表 服务实现类
*
*
* @author
* @since 2022-06-16
*/
@Service
public class CameraDetailServiceImpl extends ServiceImpl 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;
@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 wrapper = new LambdaUpdateWrapper<>();
wrapper.eq(CameraDetail::getCameraId,cameraId);
wrapper.set(CameraDetail::getUserId,null);
wrapper.set(CameraDetail::getCooperationUser,null);
//恢复10G基本容量
wrapper.set(CameraDetail::getTotalSpace,Long.parseLong(Constant.EXPANSION_SPACE_VALUE_1G ) * 10L);
this.update(wrapper);
if(!"local".equals(CacheUtil.uploadType) && cameraDetail.getType() !=10){
sceneProService.lockOrUnLockBySpace(cameraDetail,cameraId,-2); //封存场景
}
}
@Override
public CameraDetail getByCameraId(Long cameraId) {
LambdaQueryWrapper wrapper = new LambdaQueryWrapper<>();
wrapper.eq(CameraDetail::getCameraId,cameraId);
List list = this.list(wrapper);
if(list == null || list.size() <=0){
return null;
}
return list.get(0);
}
@Override
public List getByCameraIds(List cameraIds) {
LambdaQueryWrapper wrapper = new LambdaQueryWrapper<>();
wrapper.in(CameraDetail::getCameraId,cameraIds);
return this.list(wrapper);
}
@Override
public void deleteByCameraId(Long cameraId) {
LambdaQueryWrapper wrapper = new LambdaQueryWrapper<>();
wrapper.eq(CameraDetail::getCameraId,cameraId);
this.remove(wrapper);
}
@Override
public HashMap getCountGroupByUserId(List userIdList) {
HashMap map = new HashMap<>();
List result = this.getBaseMapper().getCountGroupByUserId(userIdList);
result.forEach(entity ->map.put(entity.getId(),entity.getCount()));
return map;
}
@Override
public HashMap getCountGroupByCompanyId() {
List result = this.getBaseMapper().getCountGroupByCompanyId();
HashMap map = new HashMap<>();
result.forEach(entity ->map.put(entity.getId(),entity.getCount()));
return map;
}
@Override
public HashMap getSceneCountGroupByCameraId() {
LambdaQueryWrapper wrapper = new LambdaQueryWrapper<>();
wrapper.isNotNull(CameraDetail::getCompanyId);
List list = this.list(wrapper);
Set cameraIds = list.parallelStream().map(CameraDetail::getCameraId).collect(Collectors.toSet());
HashMap resultMap = new HashMap<>();
if(cameraIds.size() >0){
HashMap sceneProMap = sceneProService.getCountGroupByCameraId(new ArrayList<>(cameraIds));
HashMap sceneMap = sceneService.getCountGroupByCameraId(new ArrayList<>(cameraIds));
HashMap scenePlusMap = scenePlusService.getCountGroupByCameraId(new ArrayList<>(cameraIds));
HashMap cameraHashMap = new HashMap<>();
List cameraList = cameraService.listByIds(cameraIds);
cameraList.forEach(entity -> cameraHashMap.put(entity.getId(),entity));
HashMap> 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 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 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 wrapper = new LambdaQueryWrapper<>();
wrapper.eq(CameraDetail::getCompanyId,companyId);
return this.count(wrapper);
}
@Override
public List getListByCompanyId(Integer companyId) {
LambdaQueryWrapper wrapper = new LambdaQueryWrapper<>();
wrapper.eq(CameraDetail::getCompanyId,companyId);
return this.list(wrapper);
}
@Override
public List getByUserName(String userName) {
LambdaQueryWrapper wrapper = new LambdaQueryWrapper<>();
wrapper.like(User::getUserName,userName);
List list = userService.list(wrapper);
if(list.size() >0){
List userIds = list.stream().map(User::getId).collect(Collectors.toList());
if(userIds.size() >0){
LambdaQueryWrapper dtW = new LambdaQueryWrapper<>();
dtW.in(CameraDetail::getUserId,userIds);
return this.list(dtW);
}
}
return null;
}
@Override
public void delAgentId(Integer agentId) {
LambdaUpdateWrapper wrapper = new LambdaUpdateWrapper<>();
wrapper.eq(CameraDetail::getAgentId,agentId);
wrapper.set(CameraDetail::getAgentId,null);
this.update(wrapper);
}
@Override
public List getByUserId(Long userId) {
LambdaQueryWrapper 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){
sceneProService.lockOrUnLockBySpace(cameraDetail,cameraDetail.getCameraId(),1);
}
this.updateById(cameraDetail);
}
}