lyhzzz 1 year ago
parent
commit
aea7ff84bd

+ 9 - 62
src/main/java/com/fdkankan/manage/service/impl/RtkAccountServiceImpl.java

@@ -15,6 +15,8 @@ import com.fdkankan.manage.service.IRtkAccountService;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
 import com.fdkankan.manage.service.IRtkUseLogService;
 import com.fdkankan.manage.service.ISysUserService;
+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;
@@ -52,6 +54,10 @@ public class RtkAccountServiceImpl extends ServiceImpl<IRtkAccountMapper, RtkAcc
     IRtkUseLogService rtkUseLogService;
     @Autowired
     ISysUserService sysUserService;
+    @Autowired
+    DingdingService dingdingService;
+    @Autowired
+    TaskService taskService;
 
     @Override
     public RtkAccount getOneNotUseAccount(String rtkSnCode) {
@@ -62,16 +68,17 @@ public class RtkAccountServiceImpl extends ServiceImpl<IRtkAccountMapper, RtkAcc
             return JSONObject.parseObject(jsonStr, RtkAccount.class);
         }
 
+
         LambdaQueryWrapper<RtkAccount> wrapper = new LambdaQueryWrapper<>();
         wrapper.in(RtkAccount::getStatus,0,1);
         wrapper.orderByAsc(RtkAccount::getUpdateTime);
         List<RtkAccount> list = this.list(wrapper);
         if(list == null || list.isEmpty()){
             //账号库存不足,钉钉通知
-            sendDingDingMsg(0);
+            dingdingService.sendDingDingMsg(0);
             throw new BusinessException(ResultCode.RTK_ACCOUNT_NOT_EXIT);
         }
-        modelThreshold(list.size() -1);
+        dingdingService.modelThreshold(list.size() -1,this.count());
 
         RtkAccount rtkAccount = null;
         RtkUseLog rtkUseLog = rtkUseLogService.getByRtkSn(rtkSnCode);
@@ -109,73 +116,13 @@ public class RtkAccountServiceImpl extends ServiceImpl<IRtkAccountMapper, RtkAcc
         }
     }
 
-    private void modelThreshold(int size) {
-        if(size == 0){
-            sendDingDingMsg(size);
-            return;
-        }
-        BigDecimal totalCount = new BigDecimal(this.count());
-        BigDecimal dbCount = new BigDecimal(size);
 
-        BigDecimal divideCount = totalCount.divide(dbCount).setScale(2, RoundingMode.HALF_DOWN);
-        BigDecimal thresholdCount = new BigDecimal(this.threshold);
-        if(divideCount.compareTo(thresholdCount) >= 0 ){
-            sendDingDingMsg(size);
-        }
-    }
 
 
-    /**
-     *
-     */
-    @Scheduled(initialDelay = 2000, fixedDelay = 1000 * 60)
-    public void checkAccount() {
-        HashMap<String,RtkAccount> map = new HashMap<>();
-        String redisKey = "4dkankan:rtk:snCode:*";
-        Set<String> 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(),rtkAccount);
-            }
-        }
-        log.info("正在使用的rtk账号-map:{}",map);
-        LambdaQueryWrapper<RtkAccount> wrapper = new LambdaQueryWrapper<>();
-        wrapper.eq(RtkAccount::getStatus,2);
-        List<RtkAccount> list = this.list(wrapper);
-        for (RtkAccount rtkAccount : list) {
-           if(map.containsKey(rtkAccount.getUserName())){
-               continue;
-           }
-           updateAccountStatus(rtkAccount.getId(),1);
-        }
-    }
-
-
-    @Autowired
-    DingTalkSendUtils dingTalkSendUtils;
 
 
-    private static String msgPattern =
-            "**域名**: %s\n\n" +
-            "**库存数量**: %s\n\n" ;
 
 
-    @Value("${main.url}")
-    String mainUrl;
-    @Value("${dingtalk.threshold:80}")
-    String threshold;
-
-    private void sendDingDingMsg(Integer count){
-        try {
-            String format = String.format(msgPattern, mainUrl, count);
-            dingTalkSendUtils.sendActioncardMsgToDingRobot(format,"RTK账号库存预警");
-        }catch (Exception e){
-            log.info("发送钉钉消息失败:{}",e);
-        }
-    }
-
 
     @Override
     public Object pageList(RtkInfoParam param) {

+ 58 - 0
src/main/java/com/fdkankan/manage/task/DingdingService.java

@@ -0,0 +1,58 @@
+package com.fdkankan.manage.task;
+
+import com.fdkankan.dingtalk.DingTalkSendUtils;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.stereotype.Service;
+
+import java.math.BigDecimal;
+import java.math.RoundingMode;
+
+@Service
+@Slf4j
+public class DingdingService {
+
+    @Autowired
+    DingTalkSendUtils dingTalkSendUtils;
+
+
+    private static String msgPattern =
+            "**域名**: %s\n\n" +
+                    "**库存数量**: %s\n\n" ;
+
+
+    @Value("${main.url}")
+    String mainUrl;
+    @Value("${dingtalk.threshold:80}")
+    String threshold;
+
+    public void sendDingDingMsg(Integer count){
+        try {
+            String format = String.format(msgPattern, mainUrl, count);
+            dingTalkSendUtils.sendActioncardMsgToDingRobot(format,"RTK账号库存预警");
+        }catch (Exception e){
+            log.info("发送钉钉消息失败:{}",e);
+        }
+    }
+
+    public void modelThreshold(int size,long total) {
+        try {
+            if(size == 0){
+                this.sendDingDingMsg(size);
+                return;
+            }
+            BigDecimal totalCount = new BigDecimal(total);
+            BigDecimal dbCount = new BigDecimal(size);
+
+            BigDecimal divideCount = totalCount.divide(dbCount).setScale(2, RoundingMode.HALF_DOWN);
+            BigDecimal thresholdCount = new BigDecimal(this.threshold);
+            if(divideCount.compareTo(thresholdCount) >= 0 ){
+                this.sendDingDingMsg(size);
+            }
+        }catch (Exception e){
+            log.info("modelThreshold--error",e);
+        }
+
+    }
+}

+ 92 - 0
src/main/java/com/fdkankan/manage/task/TaskService.java

@@ -0,0 +1,92 @@
+package com.fdkankan.manage.task;
+
+
+import com.alibaba.fastjson.JSONObject;
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
+import com.fdkankan.dingtalk.DingTalkSendUtils;
+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.HashMap;
+import java.util.List;
+import java.util.Set;
+
+@Service
+@Slf4j
+public class TaskService {
+
+    @Autowired
+    RedisUtil redisUtil;
+    @Autowired
+    IRtkAccountService rtkAccountService;
+    @Autowired
+    DingdingService dingdingService;
+
+    @Scheduled(initialDelay = 2000, fixedDelay = 1000 * 60)
+    public void task() {
+        try {
+            checkAccount();
+        }catch (Exception e){
+            log.info("定时任务checkAccount出错:",e);
+        }
+
+        try {
+            checkAccountDb();
+        }catch (Exception e){
+            log.info("定时任务checkAccountDb出错:",e);
+        }
+    }
+
+    /**
+     * 检查账号
+     */
+    public void checkAccount() {
+        HashMap<String, RtkAccount> map = new HashMap<>();
+        String redisKey = "4dkankan:rtk:snCode:*";
+        Set<String> 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(),rtkAccount);
+            }
+        }
+        log.info("正在使用的rtk账号-map:{}",map);
+        LambdaQueryWrapper<RtkAccount> wrapper = new LambdaQueryWrapper<>();
+        wrapper.eq(RtkAccount::getStatus,2);
+        List<RtkAccount> list = rtkAccountService.list(wrapper);
+        for (RtkAccount rtkAccount : list) {
+            if(map.containsKey(rtkAccount.getUserName())){
+                continue;
+            }
+            rtkAccountService.updateAccountStatus(rtkAccount.getId(),1);
+        }
+
+    }
+
+
+    /**
+     * 检查账号   账号库存不足,钉钉通知
+     */
+    public void checkAccountDb() {
+        LambdaQueryWrapper<RtkAccount> wrapper = new LambdaQueryWrapper<>();
+        wrapper.in(RtkAccount::getStatus,0,1);
+        wrapper.orderByAsc(RtkAccount::getUpdateTime);
+        List<RtkAccount> list = rtkAccountService.list(wrapper);
+        if(list == null || list.isEmpty()){
+            dingdingService.sendDingDingMsg(0);
+            return;
+        }
+        dingdingService.modelThreshold(list.size() -1,rtkAccountService.count());
+    }
+
+
+}