123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208 |
- 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.dingtalk.DingTalkSendUtils;
- import com.fdkankan.manage.common.PageInfo;
- import com.fdkankan.manage.common.ResultCode;
- 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.SendMailUtils;
- 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.util.Date;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Set;
- 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;
- @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);
- redisUtil.expire(redisKey,time);
- return JSONObject.parseObject(jsonStr, RtkAccount.class);
- }
- LambdaQueryWrapper<RtkAccount> wrapper = new LambdaQueryWrapper<>();
- wrapper.in(RtkAccount::getStatus,0,1);
- wrapper.orderByAsc(RtkAccount::getUpdateTime);
- wrapper.orderByAsc(RtkAccount::getId);
- List<RtkAccount> list = this.list(wrapper);
- if(list == null || list.isEmpty()){
- //账号库存不足,钉钉通知
- dingdingService.sendDingDingMsg(0);
- throw new BusinessException(ResultCode.RTK_ACCOUNT_NOT_EXIT);
- }
- dingdingService.modelThreshold(list.size() -1,this.count());
- 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){
- rtkAccount = rtkAccount2;
- }
- }
- if(rtkAccount == null){
- rtkAccount = list.get(0);
- }
- updateAccountStatus(rtkAccount.getId(),2);
- redisUtil.set(redisKey,JSONObject.toJSONString(rtkAccount),time);
- return rtkAccount;
- }
- @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(String rtkSnCode) {
- String redisKey = "4dkankan:rtk:snCode:"+rtkSnCode;
- if(redisUtil.hasKey(redisKey)){
- String jsonStr = redisUtil.get(redisKey);
- RtkAccount rtkAccount = JSONObject.parseObject(jsonStr, RtkAccount.class);
- 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.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_SN_EXIST);
- }
- if(rtkAccount != null && !param.getId().equals(rtkAccount.getId())){
- throw new BusinessException(ResultCode.RTK_SN_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);
- }
- }
|