package com.fdkankan.manage.service.impl; import cn.dev33.satoken.stp.StpUtil; import com.alibaba.fastjson.JSONObject; 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.fdkankan.manage.common.PageInfo; import com.fdkankan.manage.common.ResultCode; import com.fdkankan.manage.constant.RtkTypeEnum; import com.fdkankan.manage.entity.*; import com.fdkankan.manage.exception.BusinessException; import com.fdkankan.manage.mapper.ICameraDetailMapper; import com.fdkankan.manage.mapper.IRtkAccountMapper; import com.fdkankan.manage.service.*; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.fdkankan.manage.task.DingdingService; import com.fdkankan.manage.task.TaskService; import com.fdkankan.manage.util.DateUtils; import com.fdkankan.manage.util.SendMailUtils; import com.fdkankan.manage.vo.request.RtkAccountInParam; import com.fdkankan.manage.vo.request.RtkDeviceInParam; import com.fdkankan.manage.vo.request.RtkInfoParam; import com.fdkankan.redis.util.RedisUtil; 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.scheduling.annotation.Scheduled; import org.springframework.stereotype.Service; import java.math.BigDecimal; import java.math.RoundingMode; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.*; import java.util.stream.Collectors; /** *

* 服务实现类 *

* * @author * @since 2024-07-22 */ @Service @Slf4j public class RtkAccountServiceImpl extends ServiceImpl implements IRtkAccountService { @Autowired RedisUtil redisUtil; @Autowired IRtkUseLogService rtkUseLogService; @Autowired ISysUserService sysUserService; @Autowired DingdingService dingdingService; @Autowired TaskService taskService; @Autowired ICameraService cameraService; @Autowired ICameraDetailService cameraDetailService; @Autowired IExcelService excelService; @Override public RtkAccount getOneNotUseAccount(String rtkSnCode,String cameraSn) { String redisKey = "4dkankan:rtk:snCode:"+rtkSnCode; Long time = 8 * 60 * 60L; if(StringUtils.isNotBlank(cameraSn)){ Camera camera = cameraService.getBySnCode(cameraSn); if(camera != null){ CameraDetail cameraDetail = cameraDetailService.getByCameraId(camera.getId()); if(cameraDetail != null && ( cameraDetail.getType() == 10 || cameraDetail.getType() == 11)){ time = 4 * 60 * 60L; } } } if(redisUtil.hasKey(redisKey)){ String jsonStr = redisUtil.get(redisKey); RtkAccount rtkAccount = JSONObject.parseObject(jsonStr, RtkAccount.class); log.info("rtkAccount:{}",jsonStr); RtkAccount dbRtkAccount = this.getById(rtkAccount.getId()); Boolean flag = checkAccountFailureTime(dbRtkAccount); if(flag){ redisUtil.set(redisKey,JSONObject.toJSONString(dbRtkAccount),time); return dbRtkAccount; }else { redisUtil.del(redisKey); } } List list = this.getByNotUseList(); if(list == null || list.isEmpty()){ //账号库存不足,钉钉通知 dingdingService.sendDingDingMsg(0); throw new BusinessException(ResultCode.RTK_ACCOUNT_NOT_EXIT); } dingdingService.modelThreshold(list.size() -1,this.getByCanUseList().size()); RtkAccount rtkAccount = null; RtkUseLog rtkUseLog = rtkUseLogService.getByRtkSn(rtkSnCode); if(rtkUseLog != null && rtkUseLog.getRtkAccountId() != null){ RtkAccount rtkAccount2 = this.getById(rtkUseLog.getRtkAccountId()); if(rtkAccount2 != null && rtkAccount2.getStatus() == 1 && rtkAccount2.getUseStatus() == 0 && rtkAccount2.getFailureTime() !=null && rtkAccount2.getFailureTime().getTime() > new Date().getTime()){ rtkAccount = rtkAccount2; } } if(rtkAccount == null){ rtkAccount = list.get(0); } updateAccountStatus(rtkAccount.getId(),2); redisUtil.set(redisKey,JSONObject.toJSONString(rtkAccount),time); return rtkAccount; } private Boolean checkAccountFailureTime(RtkAccount dbRtkAccount) { if(dbRtkAccount == null || dbRtkAccount.getUseStatus() == 1){ return false; } if(dbRtkAccount.getFailureTime() != null && dbRtkAccount.getFailureTime().getTime() <= new Date().getTime() ){ return false; } return true; } @Override public void updateAccountStatus(Integer id, int status) { LambdaUpdateWrapper wrapper1 = new LambdaUpdateWrapper<>(); wrapper1.eq(RtkAccount::getId,id); wrapper1.set(RtkAccount::getStatus,status); this.update(wrapper1); } @Override public void stop(RtkDevice rtkDevice,String clientIP,String cameraSn) { String redisKey = "4dkankan:rtk:snCode:"+rtkDevice.getRtkSnCode(); if(redisUtil.hasKey(redisKey)){ String jsonStr = redisUtil.get(redisKey); RtkAccount rtkAccount = JSONObject.parseObject(jsonStr, RtkAccount.class); rtkUseLogService.saveLog(rtkDevice,clientIP,rtkAccount,cameraSn); updateAccountStatus(rtkAccount.getId(),1); redisUtil.del(redisKey); } } @Override public Object pageList(RtkInfoParam param) { LambdaQueryWrapper wrapper = new LambdaQueryWrapper<>(); if(StringUtils.isNotBlank(param.getUserName())){ wrapper.like(RtkAccount::getUserName,param.getUserName()); } if(StringUtils.isNotBlank(param.getOperator())){ wrapper.like(RtkAccount::getOperator,param.getOperator()); } if(StringUtils.isNotBlank(param.getStartTime())&& StringUtils.isNotBlank(param.getEndTime()) ) { wrapper.between(RtkAccount::getCreateTime,param.getStartTime(),param.getEndTime()); } wrapper.orderByDesc(RtkAccount::getCreateTime); wrapper.orderByDesc(RtkAccount::getId); Page page = this.page(new Page<>(param.getPageNum(), param.getPageSize()), wrapper); Set createUserIds = page.getRecords().stream().map(RtkAccount::getCreateUserId).collect(Collectors.toSet()); HashMap byIds = sysUserService.getByIds(createUserIds); for (RtkAccount record : page.getRecords()) { SysUser sysUser = byIds.get(record.getCreateUserId()); if(sysUser != null){ record.setCreateUserName(sysUser.getUserName()); record.setCreateNickName(sysUser.getNickName()); } } return PageInfo.PageInfo(page); } @Override public void saveOrEditEntity(RtkAccount param) { if(StringUtils.isBlank(param.getUserName()) || param.getPassword() == null){ throw new BusinessException(ResultCode.MISSING_REQUIRED_PARAMETERS); } RtkAccount rtkAccount = this.getByUserName(param.getUserName()); if(rtkAccount != null && param.getId() == null){ throw new BusinessException(ResultCode.RTK_USERNAME_EXIST); } if(rtkAccount != null && !param.getId().equals(rtkAccount.getId())){ throw new BusinessException(ResultCode.RTK_USERNAME_EXIST); } if(param.getId()== null){ param.setCreateUserId(Long.valueOf(StpUtil.getLoginId().toString())); } if(param.getId()!= null){ param.setUpdateUserId(Long.valueOf(StpUtil.getLoginId().toString())); } this.saveOrUpdate(param); } @Override public void del(RtkAccount param) { if(param.getId() == null){ throw new BusinessException(ResultCode.MISSING_REQUIRED_PARAMETERS); } RtkAccount rtkAccount = this.getById(param.getId()); if(rtkAccount != null){ LambdaUpdateWrapper wrapper = new LambdaUpdateWrapper<>(); wrapper.eq(RtkAccount::getId,param.getId()); wrapper.set(RtkAccount::getDelUserId,Long.valueOf(StpUtil.getLoginId().toString())); wrapper.set(RtkAccount::getRecStatus,rtkAccount.getId()); this.update(wrapper); } } @Override public RtkAccount getByUserName(String userName) { LambdaQueryWrapper wrapper = new LambdaQueryWrapper<>(); wrapper.eq(RtkAccount::getUserName,userName); return this.getOne(wrapper); } @Override public Integer insExcelList(List> excelRowList) { List params = new ArrayList<>(); List errorIndex = new ArrayList<>(); Integer index = 0; for (HashMap map : excelRowList) { index ++; if(map.isEmpty()){ continue; } if(index == 0 && !map.get(0).equals("差分账号批量导入模板")){ throw new BusinessException(ResultCode.TEMPLATE_TYPE_ERROR); } if(index <4){ continue; } String userName = map.get(0); String password = map.get(1); String operator = map.get(2); String ipAddr = map.get(3); String mountPoint = map.get(4); String port = map.get(5); String failureTimeStr = map.get(6); if(StringUtils.isBlank(userName) && StringUtils.isBlank(password) && StringUtils.isBlank(operator) && StringUtils.isBlank(ipAddr) && StringUtils.isBlank(mountPoint)&& StringUtils.isBlank(port)&& StringUtils.isBlank(failureTimeStr)){ continue; } log.info("rtkAccount-excel-in--userName:{},password:{},operator:{},ipAddr:{},mountPoint:{},port:{},failureTime:{}",userName,password,operator,ipAddr,mountPoint,port,failureTimeStr); if(StringUtils.isBlank(userName) || StringUtils.isBlank(password) || StringUtils.isBlank(operator)|| StringUtils.isBlank(ipAddr) || StringUtils.isBlank(mountPoint) || StringUtils.isBlank(port)){ errorIndex.add(index -3); continue; } RtkAccount rtkAccount = this.getByUserName(userName); if(rtkAccount !=null){ errorIndex.add(index -3); continue; } Date failureTime = null; if(StringUtils.isNotBlank(failureTimeStr)){ try { failureTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(failureTimeStr); } catch (ParseException e) { errorIndex.add(index -3); continue; } } RtkAccountInParam param = new RtkAccountInParam(userName,password,operator,ipAddr,mountPoint,port,failureTime); params.add(param); } excelService.toExcelError(errorIndex); if(params.size() <=0){ throw new BusinessException(ResultCode.RTK_DEVICEIN_TEMPLATE_EMPTY); } return this.ins(params); } private Integer ins(List params) { List dbs = new ArrayList<>(); for (RtkAccountInParam param : params) { RtkAccount rtkAccount = new RtkAccount(); rtkAccount.setUserName(param.getUserName()); rtkAccount.setPassword(param.getPassword()); rtkAccount.setIpAddr(param.getIpAddr()); rtkAccount.setMountPoint(param.getMountPoint()); rtkAccount.setPort(param.getPort()); rtkAccount.setOperator(param.getOperator()); rtkAccount.setFailureTime(param.getFailureTime()); rtkAccount.setCreateUserId(Long.valueOf( StpUtil.getLoginId().toString())); dbs.add(rtkAccount); } if(dbs.isEmpty()){ return 0; } this.saveBatch(dbs); return dbs.size(); } @Override public Integer insFailureTimeExcelList(List> excelRowList) { List params = new ArrayList<>(); List errorIndex = new ArrayList<>(); Integer index = 0; for (HashMap map : excelRowList) { index++; if (map.isEmpty()) { continue; } if (index == 0 && !map.get(0).equals("到期时间批量导入模板")) { throw new BusinessException(ResultCode.TEMPLATE_TYPE_ERROR); } if (index < 4) { continue; } String userName = map.get(0); String failureTimeStr = map.get(1); if(StringUtils.isBlank(userName) && StringUtils.isBlank(failureTimeStr)){ continue; } log.info("rtkAccount-excel-in--userName:{},failureTime:{}",userName,failureTimeStr); if(StringUtils.isBlank(userName) || StringUtils.isBlank(failureTimeStr)){ errorIndex.add(index -3); continue; } RtkAccount rtkAccount = this.getByUserName(userName); if(rtkAccount == null){ continue; } Date failureTime = null; if(StringUtils.isNotBlank(failureTimeStr)){ try { failureTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(failureTimeStr); } catch (ParseException e) { //errorIndex.add(index -3); continue; } } RtkAccountInParam param = new RtkAccountInParam(userName,failureTime); params.add(param); } excelService.toExcelError(errorIndex); if(params.size() <=0){ throw new BusinessException(ResultCode.RTK_USERNAME_TEMPLATE_EMPTY); } return this.updateFailureTime(params); } private Integer updateFailureTime(List params) { for (RtkAccountInParam param : params) { LambdaUpdateWrapper wrapper = new LambdaUpdateWrapper<>(); wrapper.eq(RtkAccount::getUserName,param.getUserName()); wrapper.set(RtkAccount::getFailureTime,param.getFailureTime()); this.update(wrapper); } return params.size(); } @Override public List getByNotUseList() { return this.getBaseMapper().getByNotUseList(); } @Override public List getByCanUseList() { return this.getBaseMapper().getByCanUseList(); } }