123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536 |
- 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;
- /**
- * <p>
- * 相机主表 服务实现类
- * </p>
- *
- * @author
- * @since 2022-06-16
- */
- @Service
- @Slf4j
- public class CameraServiceImpl extends ServiceImpl<ICameraMapper, Camera> 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<Camera> getListByCameraIdList(List<Long> cameraIdList) {
- LambdaQueryWrapper<Camera> wrapper = new LambdaQueryWrapper<>();
- wrapper.in(Camera::getId,cameraIdList);
- return this.list(wrapper);
- }
- @Override
- public Camera getBySnCode(String snCode) {
- LambdaQueryWrapper<Camera> wrapper = new LambdaQueryWrapper<>();
- wrapper.eq(Camera::getSnCode,snCode);
- List<Camera> list = this.list(wrapper);
- if(list == null || list.size() <=0 ){
- return null;
- }
- return list.get(0);
- }
- private List<Camera> getBySnCodes(List<String> snCodeList) {
- LambdaQueryWrapper<Camera> wrapper = new LambdaQueryWrapper<>();
- wrapper.in(Camera::getSnCode,snCodeList);
- return this.list(wrapper);
- }
- private List<Camera> getByOutSnCodes(List<String> 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<CameraDetailVo> 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<CameraType> list = cameraTypeService.list();
- Set<String> 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<String,CameraType> cameraTypeMap = new HashMap<>();
- list.forEach(entity->cameraTypeMap.put(entity.getWifiNamePrefix(),entity));
- if(cameraTypeMap.get(cameraPrefix) == null){
- throw new BusinessException(ResultCode.WIFI_NAME_ERROR);
- }
- LambdaQueryWrapper<Camera> 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<String> wifiNameList,HashMap<String,CameraType> cameraTypeMap){
- HashSet<String> wifiNameSet = new HashSet<>(wifiNameList);
- List<Camera> 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<CameraDetail> 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<String> wifiNameList){
- List<CameraType> cameraTypeList = cameraTypeService.list();
- Set<String> wifiNamePrefixList = cameraTypeList.stream().map(CameraType::getWifiNamePrefix).collect(Collectors.toSet());
- List<Integer> errorRow = getErrorRow(wifiNameList,wifiNamePrefixList);
- HashMap<String,CameraType> cameraTypeMap = new HashMap<>();
- cameraTypeList.forEach(entity->cameraTypeMap.put(entity.getWifiNamePrefix(),entity));
- excelService.toExcelError(errorRow);
- List<String> snCodeList = new ArrayList<>();
- for (String wifiName : wifiNameList) {
- snCodeList.add(wifiName.split("_")[1]);
- }
- LambdaQueryWrapper<Camera> wrapper = new LambdaQueryWrapper<>();
- wrapper.in(Camera::getSnCode,snCodeList);
- List<Camera> list = this.list(wrapper);
- if(list.size() >0){
- List<String> newList = list.parallelStream().map(Camera::getSnCode).collect(Collectors.toList());
- List<Integer> errorRow2 = getErrorRow(snCodeList, newList);
- excelService.toExcelError(errorRow2);
- }
- return saveBatchCamera(wifiNameList,cameraTypeMap);
- }
- private List<Integer> getErrorRow(List<String> wifiNameList,Set<String> wifiNamePrefixList){
- List<Integer> errorIndexList = new ArrayList<>();
- Set<String> 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<Integer> getErrorRow(List<String> wifiNameList,List<String> dbList){
- List<Integer> 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<Long,CameraDetail> detailMap = new HashMap<>();
- detailMap.put(detail.getCameraId(),detail);
- this.saveBatchDetail(detailMap, Collections.singletonList(param));
- }
- @Override
- public Integer outs(List<CameraInOutParam> params){
- setCompanyIdByDb(params);
- checkOrderSn(params);
- HashMap<String, Object> resultMap = getResultMap(params,1);
- HashMap<Long,CameraDetail> detailMap = (HashMap<Long, CameraDetail>) resultMap.get("detailMap");
- HashMap<String, Camera> snCodeMap = (HashMap<String, Camera>) 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<CameraInOutParam> params) {
- List<String> orderSnList = params.stream().map(CameraInOutParam::getOrderSn).collect(Collectors.toList());
- if(orderSnList.size() >0){
- List<Order> orders = orderService.getByOrderSnList(orderSnList);
- List<String> dbOrderSn = orders.stream().map(Order::getOrderSn).collect(Collectors.toList());
- List<Integer> 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<Long, CameraDetail> detailMap, List<CameraInOutParam> params){
- List<CameraDetail> 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<String> snCodeList,List<String> dbSnCode) {
- HashMap<String,Integer> map = new HashMap<>();
- List<Integer> 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<CameraDetail> 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<CameraInOutParam> companyParams) {
- setCompanyIdByDb(companyParams);
- HashMap<String, Object> resultMap = getResultMap(companyParams,2);
- HashMap<Long, CameraDetail> detailMap = (HashMap<Long, CameraDetail>) resultMap.get("detailMap");
- HashMap<String, Camera> snCodeMap = (HashMap<String, Camera>) resultMap.get("snCodeMap");
- List<CameraDetail> 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<CameraInOutParam> companyParams){
- List<String> companyNames = companyParams.parallelStream().map(CameraInOutParam::getCompanyName).collect(Collectors.toList());
- List<Company> companyList = companyService.getCompanyByNames(companyNames);
- HashMap<String,Long> companyNameMap = new HashMap<>();
- for (Company company : companyList) {
- companyNameMap.put(company.getCompanyName(),company.getId());
- }
- List<Integer> 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<String, Object> getResultMap(List<CameraInOutParam> params,Integer type){
- HashMap<String,Object> resultMap = new HashMap<>();
- List<String> snCodeList = params.parallelStream().map(CameraInOutParam::getSnCode).collect(Collectors.toList());
- List<Camera> 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<Long> cameraIds = cameraList.parallelStream().map(Camera::getId).collect(Collectors.toList());
- List<CameraDetail> cameraDetails = cameraDetailService.getByCameraIds(cameraIds);
- HashMap<Long,CameraDetail> detailMap = new HashMap<>();
- for (CameraDetail cameraDetail : cameraDetails) {
- detailMap.put(cameraDetail.getCameraId(),cameraDetail);
- }
- HashMap<String,Camera> snCodeMap = new HashMap<>();
- for (Camera camera : cameraList) {
- snCodeMap.put(camera.getSnCode(),camera);
- }
- List<Integer> 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<Long> cameraIds ;
- List<CameraDetail> cameraDetails;
- if(userId != null){
- cameraDetails = cameraDetailService.getByUserId(userId);
- }else {
- List<Camera> list = this.list();
- cameraIds = list.stream().map(Camera::getId).collect(Collectors.toList());
- cameraDetails = cameraDetailService.getByCameraIds(cameraIds);
- }
- if(cameraDetails == null || cameraDetails.size() <=0){
- return;
- }
- HashMap<Long, Long> map = sceneProService.getSpaceGroupByCameraId();
- for (CameraDetail cameraDetail : cameraDetails) {
- Long space = map.get(cameraDetail.getCameraId());
- if(space != null){
- space = space > 0 ? space : 0;
- LambdaUpdateWrapper<CameraDetail> wrapper = new LambdaUpdateWrapper<>();
- wrapper.eq(CameraDetail::getId,cameraDetail.getId());
- wrapper.set(CameraDetail::getUsedSpace,space);
- cameraDetailService.update(wrapper);
- }
- }
- }
- }
|