package com.fdkankan.manage.task; import com.alibaba.fastjson.JSONObject; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.fdkankan.manage.common.ResultCode; import com.fdkankan.manage.entity.RtkAccount; import com.fdkankan.manage.exception.BusinessException; import com.fdkankan.manage.service.IRtkAccountService; import com.fdkankan.redis.util.RedisUtil; import lombok.extern.slf4j.Slf4j; 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.util.Date; import java.util.HashMap; import java.util.List; import java.util.Set; import java.util.stream.Collectors; @Service @Slf4j public class TaskService { @Autowired RedisUtil redisUtil; @Autowired IRtkAccountService rtkAccountService; @Autowired DingdingService dingdingService; @Scheduled(cron = "0 */1 * * * ?") public void task() { try { checkAccount(); }catch (Exception e){ log.info("定时任务checkAccount出错:",e); } } @Scheduled(cron = "0 0 * * * ?") public void task2() { try { checkAccountDb(); }catch (Exception e){ log.info("定时任务checkAccountDb出错:",e); } } /** * 检查账号 */ public void checkAccount() { HashMap map = new HashMap<>(); String redisKey = "4dkankan:rtk:snCode:*"; Set keys = redisUtil.keys(redisKey); if(keys!= null && !keys.isEmpty()){ for (String key : keys) { String jsonStr = redisUtil.get(key); RtkAccount rtkAccount = JSONObject.parseObject(jsonStr, RtkAccount.class); map.put(rtkAccount.getUserName(),key); } } LambdaQueryWrapper wrapper = new LambdaQueryWrapper<>(); wrapper.eq(RtkAccount::getStatus,2); List list = rtkAccountService.list(wrapper); for (RtkAccount rtkAccount : list) { if(map.containsKey(rtkAccount.getUserName())){ continue; } rtkAccountService.updateAccountStatus(rtkAccount.getId(),1); } List rtkAccounts = rtkAccountService.getByNotFailure(); for (RtkAccount rtkAccount : rtkAccounts) { rtkAccount.setStatus(3); rtkAccountService.updateAccountStatus(rtkAccount.getId(),3); if(map.containsKey(rtkAccount.getUserName())){ redisUtil.del(map.get(rtkAccount.getUserName())); } } } /** * 检查账号 账号库存不足,钉钉通知 */ public void checkAccountDb() { LambdaQueryWrapper wrapper = new LambdaQueryWrapper<>(); wrapper.in(RtkAccount::getStatus,0,1); wrapper.orderByAsc(RtkAccount::getUpdateTime); List list = rtkAccountService.list(wrapper); if(list == null || list.isEmpty()){ dingdingService.sendDingDingMsg(0); return; } dingdingService.modelThreshold(list.size(),rtkAccountService.count()); } }