123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375 |
- 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;
- /**
- * <p>
- * 服务实现类
- * </p>
- *
- * @author
- * @since 2024-07-22
- */
- @Service
- @Slf4j
- public class RtkAccountServiceImpl extends ServiceImpl<IRtkAccountMapper, RtkAccount> 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<RtkAccount> 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<RtkAccount> 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<RtkAccount> 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<RtkAccount> page = this.page(new Page<>(param.getPageNum(), param.getPageSize()), wrapper);
- Set<Long> createUserIds = page.getRecords().stream().map(RtkAccount::getCreateUserId).collect(Collectors.toSet());
- HashMap<Long, SysUser> 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<RtkAccount> 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<RtkAccount> wrapper = new LambdaQueryWrapper<>();
- wrapper.eq(RtkAccount::getUserName,userName);
- return this.getOne(wrapper);
- }
- @Override
- public Integer insExcelList(List<HashMap<Integer, String>> excelRowList) {
- List<RtkAccountInParam> params = new ArrayList<>();
- List<Integer> errorIndex = new ArrayList<>();
- Integer index = 0;
- for (HashMap<Integer, String> 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<RtkAccountInParam> params) {
- List<RtkAccount> 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<HashMap<Integer, String>> excelRowList) {
- List<RtkAccountInParam> params = new ArrayList<>();
- List<Integer> errorIndex = new ArrayList<>();
- Integer index = 0;
- for (HashMap<Integer, String> 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<RtkAccountInParam> params) {
- for (RtkAccountInParam param : params) {
- LambdaUpdateWrapper<RtkAccount> wrapper = new LambdaUpdateWrapper<>();
- wrapper.eq(RtkAccount::getUserName,param.getUserName());
- wrapper.set(RtkAccount::getFailureTime,param.getFailureTime());
- this.update(wrapper);
- }
- return params.size();
- }
- @Override
- public List<RtkAccount> getByNotUseList() {
- return this.getBaseMapper().getByNotUseList();
- }
- @Override
- public List<RtkAccount> getByCanUseList() {
- return this.getBaseMapper().getByCanUseList();
- }
- }
|