package com.fdkankan.manage.service.impl;
import com.alibaba.nacos.shaded.org.checkerframework.checker.units.qual.A;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fdkankan.common.constant.Constant;
import com.fdkankan.common.util.MD5;
import com.fdkankan.manage.common.*;
import com.fdkankan.manage.entity.*;
import com.fdkankan.manage.exception.BusinessException;
import com.fdkankan.common.util.DateUtil;
import com.fdkankan.manage.constant.CameraConstant;
import com.fdkankan.manage.mapper.ICameraMapper;
import com.fdkankan.manage.service.*;
import com.fdkankan.manage.util.Dateutils;
import com.fdkankan.manage.vo.request.CameraInOutParam;
import com.fdkankan.manage.vo.request.CameraParam;
import com.fdkankan.manage.vo.response.CameraDataVo;
import com.fdkankan.manage.vo.response.CameraDetailVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import java.util.*;
import java.util.stream.Collectors;
/**
*
* 相机主表 服务实现类
*
*
* @author
* @since 2022-06-16
*/
@Service
@Slf4j
public class CameraServiceImpl extends ServiceImpl implements ICameraService {
@Autowired
ICameraDetailService cameraDetailService;
@Autowired
ISceneService sceneService;
@Autowired
ISceneProService sceneProService;
@Autowired
IUserIncrementService userIncrementService;
@Autowired
ISceneResourceCameraService sceneResourceCameraService;
@Autowired
IScenePlusService scenePlusService;
@Autowired
ICompanyService companyService;
@Autowired
IExcelService excelService;
@Autowired
IOrderService orderService;
@Autowired
ICameraTypeService cameraTypeService;
@Override
public List getListByCameraIdList(List cameraIdList) {
LambdaQueryWrapper wrapper = new LambdaQueryWrapper<>();
wrapper.in(Camera::getId,cameraIdList);
return this.list(wrapper);
}
@Override
public Camera getBySnCode(String snCode) {
LambdaQueryWrapper wrapper = new LambdaQueryWrapper<>();
wrapper.eq(Camera::getSnCode,snCode);
List list = this.list(wrapper);
if(list == null || list.size() <=0 ){
return null;
}
return list.get(0);
}
private List getBySnCodes(List snCodeList) {
LambdaQueryWrapper wrapper = new LambdaQueryWrapper<>();
wrapper.in(Camera::getSnCode,snCodeList);
return this.list(wrapper);
}
private List getByOutSnCodes(List snCodeList) {
return this.getBaseMapper().getByOutSnCodes(snCodeList);
}
@Override
public void unbindCamera(Long cameraId) {
userIncrementService.unbindCamera(cameraId); //取消关联用户权益
cameraDetailService.unbindCamera(cameraId); //取消相机用户关联
sceneService.unbindCamera(cameraId); //取消关联场景
sceneProService.unbindCamera(cameraId); //取消关联场景
scenePlusService.unbindCamera(cameraId); //取消关联场景
sceneResourceCameraService.unbindCamera(cameraId); //删除协作相机
}
@Override
public PageInfo pageList(CameraParam param) {
Page voPage = this.getBaseMapper().pageList(new Page<>(param.getPageNum(),param.getPageSize()),param);
return PageInfo.PageInfo(voPage);
}
@Override
public void in(String wifiName) {
if(StringUtils.isEmpty(wifiName) ){
throw new BusinessException(ResultCode.MISSING_REQUIRED_PARAMETERS);
}
List list = cameraTypeService.list();
Set wifiNamePrefixList = list.stream().map(CameraType::getWifiNamePrefix).collect(Collectors.toSet());
if(!wifiName.contains("_")){
throw new BusinessException(ResultCode.WIFI_NAME_ERROR);
}
String[] split = wifiName.split("_");
String cameraPrefix = split[0] +"_";
String snCode = split[1];
if(!wifiNamePrefixList.contains(cameraPrefix)){
throw new BusinessException(ResultCode.WIFI_NAME_ERROR);
}
HashMap cameraTypeMap = new HashMap<>();
list.forEach(entity->cameraTypeMap.put(entity.getWifiNamePrefix(),entity));
if(cameraTypeMap.get(cameraPrefix) == null){
throw new BusinessException(ResultCode.WIFI_NAME_ERROR);
}
LambdaQueryWrapper wrapper = new LambdaQueryWrapper<>();
wrapper.eq(Camera::getSnCode,snCode);
long count = this.count(wrapper);
if(count > 0){
throw new BusinessException(ResultCode.WIFI_NAME_REPEAT);
}
saveBatchCamera(Collections.singletonList(wifiName),cameraTypeMap);
}
private Integer saveBatchCamera(List wifiNameList,HashMap cameraTypeMap){
HashSet wifiNameSet = new HashSet<>(wifiNameList);
List cameraList = new ArrayList<>();
for (String wifiName : wifiNameSet) {
String[] res = wifiName.split("_");
if(res.length !=2 || StringUtils.isBlank(res[1])){
throw new BusinessException(ResultCode.WIFI_NAME_ERROR);
}
if(cameraTypeMap.get(res[0] +"_") == null){
throw new BusinessException(ResultCode.WIFI_NAME_ERROR);
}
Camera camera = new Camera();
camera.setWifiName(wifiName);
camera.setSnCode(res[1]);
camera.setChildName(res[1]);
camera.setWifiPassword(CameraConstant.WIFI_PASSWORD_VALUE);
camera.setChildPassword(CameraConstant.CHILD_PASSWORD_VALUE);
camera.setActivatedTime(DateUtil.date2String(new Date(),DateUtil.DEFAULT_DATE_FORMAT));
cameraList.add(camera);
}
this.saveBatch(cameraList);
List cameraDetailList = new ArrayList<>();
for (Camera camera : cameraList) {
CameraDetail cameraDetail = new CameraDetail();
cameraDetail.setAgency(CameraConstant.DEFAULT_AGENT);
cameraDetail.setCameraId(camera.getId());
cameraDetail.setCountry(0);//默认中国
String wifiNamePrix = camera.getWifiName().split("_")[0]+"_";
CameraType cameraType = cameraTypeMap.get(wifiNamePrix);
cameraDetail.setType(cameraType.getCameraType());
if (1 == cameraDetail.getType() || 9 == cameraDetail.getType() || 10 == cameraDetail.getType() || 13 == cameraDetail.getType()){
cameraDetail.setTotalSpace(Long.valueOf(Constant.CAMERA_BASE_SPACE_VALUE));
cameraDetail.setUsedSpace(0L);
}
if("aws".equals(CacheUtil.uploadType)){
cameraDetail.setCountry(1);//1-国外
}
cameraDetail.setGoodsId(null);
if("local".equals(CacheUtil.uploadType)) { //本地版本 ,入库即出库,并且添加无限期会员权限
cameraDetail.setCountry(2);
cameraDetail.setOwn(2);
cameraDetail.setOutTime(Dateutils.getDate(new Date()));
}
cameraDetailList.add(cameraDetail);
}
return cameraDetailService.saveBatch(cameraDetailList) ? cameraList.size() : 0;
}
@Override
public Integer ins(List wifiNameList){
List cameraTypeList = cameraTypeService.list();
Set wifiNamePrefixList = cameraTypeList.stream().map(CameraType::getWifiNamePrefix).collect(Collectors.toSet());
List errorRow = getErrorRow(wifiNameList,wifiNamePrefixList);
HashMap cameraTypeMap = new HashMap<>();
cameraTypeList.forEach(entity->cameraTypeMap.put(entity.getWifiNamePrefix(),entity));
excelService.toExcelError(errorRow);
List snCodeList = new ArrayList<>();
for (String wifiName : wifiNameList) {
snCodeList.add(wifiName.split("_")[1]);
}
LambdaQueryWrapper wrapper = new LambdaQueryWrapper<>();
wrapper.in(Camera::getSnCode,snCodeList);
List list = this.list(wrapper);
if(list.size() >0){
List newList = list.parallelStream().map(Camera::getSnCode).collect(Collectors.toList());
List errorRow2 = getErrorRow(snCodeList, newList);
excelService.toExcelError(errorRow2);
}
return saveBatchCamera(wifiNameList,cameraTypeMap);
}
private List getErrorRow(List wifiNameList,Set wifiNamePrefixList){
List errorIndexList = new ArrayList<>();
Set wifiNameSet = new HashSet<>();
Integer index = 0;
for (String wifiName : wifiNameList) {
index ++;
if(StringUtils.isBlank(wifiName)){
errorIndexList.add(index);
continue;
}
if(wifiNameSet.contains(wifiName)){
errorIndexList.add(index);
continue;
}
String[] split = wifiName.split("_");
String cameraPrefix = split[0] +"_";
String snCode = split[1];
if(!wifiNamePrefixList.contains(cameraPrefix)){
errorIndexList.add(index);
}
wifiNameSet.add(wifiName);
}
return errorIndexList;
}
private List getErrorRow(List wifiNameList,List dbList){
List errorIndexList = new ArrayList<>();
if(dbList.size() <=0){
return errorIndexList;
}
Integer index = 0;
for (String wifiName : wifiNameList) {
index ++;
if(dbList.contains(wifiName)){
errorIndexList.add(index);
}
}
return errorIndexList;
}
@Override
public void out(CameraInOutParam param) {
if(param.getOutType() == null || param.getId() == null){
throw new BusinessException(ResultCode.MISSING_REQUIRED_PARAMETERS);
}
if(StringUtils.isNotBlank(param.getCompanyName())){
Company company = companyService.getCompanyByName(param.getCompanyName());
if(company == null){
throw new BusinessException(ResultCode.COMPANY_NAME_NOT_EXIST);
}
param.setCompanyId(company.getId());
}
Camera camera = this.getById(param.getId());
if(camera == null){
throw new BusinessException(ResultCode.CAMERA_NOT_EXIST);
}
CameraDetail detail = cameraDetailService.getByCameraId(camera.getId());
if(detail ==null){
throw new BusinessException(ResultCode.CAMERA_NOT_IN);
}
HashMap detailMap = new HashMap<>();
detailMap.put(detail.getCameraId(),detail);
this.saveBatchDetail(detailMap, Collections.singletonList(param));
}
@Override
public Integer outs(List params){
setCompanyIdByDb(params);
checkOrderSn(params);
HashMap resultMap = getResultMap(params,1);
HashMap detailMap = (HashMap) resultMap.get("detailMap");
HashMap snCodeMap = (HashMap) resultMap.get("snCodeMap");
for (CameraInOutParam param : params) {
if(param.getId() == null){
param.setId(snCodeMap.get(param.getSnCode()).getId());
}
}
return this.saveBatchDetail(detailMap,params);
}
private void checkOrderSn(List params) {
List orderSnList = params.stream().map(CameraInOutParam::getOrderSn).collect(Collectors.toList());
if(orderSnList.size() >0){
List orders = orderService.getByOrderSnList(orderSnList);
List dbOrderSn = orders.stream().map(Order::getOrderSn).collect(Collectors.toList());
List errorList = new ArrayList<>();
Integer index = 0;
for (String sn : orderSnList) {
index ++;
if(StringUtils.isBlank(sn)){
continue;
}
if(!dbOrderSn.contains(sn)){
log.error("excel-export-error-->:订单号不存在不存在:index:{},orderSn:{}",index+3,sn);
errorList.add(index);
}
}
excelService.toExcelError(errorList);
}
}
private Integer saveBatchDetail(HashMap detailMap, List params){
List cameraDetails = new ArrayList<>();
for (CameraInOutParam param : params) {
CameraDetail cameraDetail = detailMap.get(param.getId());
if(cameraDetail == null){
throw new BusinessException(ResultCode.CAMERA_NOT_IN);
}
if(param.getAgentId() != null){
cameraDetail.setAgentId(param.getAgentId());
}
cameraDetail.setOrderSn(param.getOrderSn());
cameraDetail.setOwn(param.getOutType());
cameraDetail.setCompanyId(param.getCompanyId());
cameraDetail.setOutTime(Dateutils.getDate(new Date()));
cameraDetails.add(cameraDetail);
}
return cameraDetailService.saveOrUpdateBatch(cameraDetails) ? cameraDetails.size() : 0;
}
private void checkSnCode(List snCodeList,List dbSnCode) {
HashMap map = new HashMap<>();
List errorList = new ArrayList<>();
Integer index = 0;
for (String snCode : snCodeList) {
index ++;
if(StringUtils.isBlank(snCode)){
log.error("excel-export-error-->:index:{},snCode不存在:snCode:{}",index+3,snCode);
errorList.add(index);
}
if(map.get(snCode) == null){
map.put(snCode,1);
}else {
map.put(snCode,2);
log.error("excel-export-error-->:index:{},snCode已存在:snCode:{}",index+3,snCode);
errorList.add(index);
}
}
excelService.toExcelError(errorList);
Integer index2 = 0;
for (String snCode : snCodeList) {
index2 ++;
if(!dbSnCode.contains(snCode)){
log.error("excel-export-error-->:index:{},snCode未入库:snCode:{}",index2+3,snCode);
errorList.add(index2);
}
}
excelService.toExcelError(errorList);
}
@Override
public void updateCamera(CameraInOutParam param) {
if(param.getId() == null|| param.getOutType() == null){
throw new BusinessException(ResultCode.MISSING_REQUIRED_PARAMETERS);
}
CameraDetail cameraDetail = cameraDetailService.getByCameraId(param.getId());
if(cameraDetail == null){
throw new BusinessException(ResultCode.MISSING_REQUIRED_PARAMETERS);
}
LambdaUpdateWrapper wrapper = new LambdaUpdateWrapper<>();
wrapper.eq(CameraDetail::getId,cameraDetail.getId());
if(StringUtils.isNotBlank(param.getCompanyName())){
Company company = companyService.getCompanyByName(param.getCompanyName());
if(company == null){
throw new BusinessException(ResultCode.COMPANY_NAME_NOT_EXIST);
}
wrapper.set(CameraDetail::getCompanyId,company.getId());
}else {
wrapper.set(CameraDetail::getCompanyId,null);
}
if(StringUtils.isNotBlank(param.getOrderSn())){
Order orderSn = orderService.getByOrderSn(param.getOrderSn());
if(orderSn == null ){
throw new BusinessException(ResultCode.ORDER_SN_ERROR);
}
wrapper.set(CameraDetail::getOrderSn,orderSn.getOrderSn());
}else {
wrapper.set(CameraDetail::getOrderSn,null);
}
if(param.getAgentId() != null){
wrapper.set(CameraDetail::getAgentId,param.getAgentId());
}else {
wrapper.set(CameraDetail::getAgentId,null);
}
wrapper.set(CameraDetail::getOwn,param.getOutType());
cameraDetailService.update(wrapper);
}
@Override
public void deleteCamera(Long id) {
if(id == null){
throw new BusinessException(ResultCode.MISSING_REQUIRED_PARAMETERS);
}
CameraDetail cameraDetail = cameraDetailService.getByCameraId(id);
if(cameraDetail !=null && cameraDetail.getUserId()!=null){
throw new BusinessException(ResultCode.CAMERA_DEL_ERROR);
}
this.removeById(id);
cameraDetailService.deleteByCameraId(id);
}
@Override
public Integer updateCompany(List companyParams) {
setCompanyIdByDb(companyParams);
HashMap resultMap = getResultMap(companyParams,2);
HashMap detailMap = (HashMap) resultMap.get("detailMap");
HashMap snCodeMap = (HashMap) resultMap.get("snCodeMap");
List updateDetailList = new ArrayList<>();
for (CameraInOutParam companyParam : companyParams) {
Camera camera = snCodeMap.get(companyParam.getSnCode());
CameraDetail cameraDetail = detailMap.get(camera.getId());
cameraDetail.setCompanyId(companyParam.getCompanyId());
updateDetailList.add(cameraDetail);
}
return cameraDetailService.updateBatchById(updateDetailList) ? updateDetailList.size() : 0;
}
private void setCompanyIdByDb(List companyParams){
List companyNames = companyParams.parallelStream().map(CameraInOutParam::getCompanyName).collect(Collectors.toList());
List companyList = companyService.getCompanyByNames(companyNames);
HashMap companyNameMap = new HashMap<>();
for (Company company : companyList) {
companyNameMap.put(company.getCompanyName(),company.getId());
}
List errorList = new ArrayList<>();
Integer index = 0;
for (CameraInOutParam companyParam : companyParams) {
index ++;
if(StringUtils.isNotBlank(companyParam.getCompanyName())){
if(companyNameMap.get(companyParam.getCompanyName()) == null){
log.error("outError-->出库错误:客户名称不存在:snCode:{},outType:{},companyName:{},orderSn:{},agentName:{}"
,companyParam.getSnCode(),companyParam.getOutType(),companyParam.getCompanyName(),companyParam.getOrderSn(),companyParam.getAgentId());
errorList.add(index);
}
companyParam.setCompanyId(companyNameMap.get(companyParam.getCompanyName()));
}
}
excelService.toExcelError(errorList);
}
private HashMap getResultMap(List params,Integer type){
HashMap resultMap = new HashMap<>();
List snCodeList = params.parallelStream().map(CameraInOutParam::getSnCode).collect(Collectors.toList());
List cameraList = new ArrayList<>();
if(type == 1){
cameraList = this.getBySnCodes(snCodeList);
}
if(type == 2){
cameraList = this.getByOutSnCodes(snCodeList);
}
checkSnCode(snCodeList,cameraList.stream().map(Camera::getSnCode).collect(Collectors.toList()));
List cameraIds = cameraList.parallelStream().map(Camera::getId).collect(Collectors.toList());
List cameraDetails = cameraDetailService.getByCameraIds(cameraIds);
HashMap detailMap = new HashMap<>();
for (CameraDetail cameraDetail : cameraDetails) {
detailMap.put(cameraDetail.getCameraId(),cameraDetail);
}
HashMap snCodeMap = new HashMap<>();
for (Camera camera : cameraList) {
snCodeMap.put(camera.getSnCode(),camera);
}
List errorList = new ArrayList<>();
for (String snCode : snCodeList) {
Camera camera = snCodeMap.get(snCode);
if(camera == null || detailMap.get(camera.getId()) ==null){
log.error("excel-export-error-->:相机未入库:snCode:{}",snCode);
errorList.add(snCodeList.indexOf(snCode) );
}
}
excelService.toExcelError(errorList);
resultMap.put("detailMap",detailMap);
resultMap.put("snCodeMap",snCodeMap);
return resultMap;
}
@Override
public void initAllCameraSpace(Long userId) {
List cameraIds ;
List cameraDetails;
if(userId != null){
cameraDetails = cameraDetailService.getByUserId(userId);
}else {
List list = this.list();
cameraIds = list.stream().map(Camera::getId).collect(Collectors.toList());
cameraDetails = cameraDetailService.getByCameraIds(cameraIds);
}
if(cameraDetails == null || cameraDetails.size() <=0){
return;
}
HashMap map = sceneProService.getSpaceGroupByCameraId();
for (CameraDetail cameraDetail : cameraDetails) {
Long space = map.get(cameraDetail.getCameraId());
if(space != null){
space = space > 0 ? space : 0;
LambdaUpdateWrapper wrapper = new LambdaUpdateWrapper<>();
wrapper.eq(CameraDetail::getId,cameraDetail.getId());
wrapper.set(CameraDetail::getUsedSpace,space);
cameraDetailService.update(wrapper);
}
}
}
}