TaskService.java 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. package com.fdkankan.manage.task;
  2. import com.alibaba.fastjson.JSONObject;
  3. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  4. import com.fdkankan.dingtalk.DingTalkSendUtils;
  5. import com.fdkankan.manage.common.ResultCode;
  6. import com.fdkankan.manage.entity.RtkAccount;
  7. import com.fdkankan.manage.exception.BusinessException;
  8. import com.fdkankan.manage.service.IRtkAccountService;
  9. import com.fdkankan.redis.util.RedisUtil;
  10. import lombok.extern.slf4j.Slf4j;
  11. import org.springframework.beans.factory.annotation.Autowired;
  12. import org.springframework.beans.factory.annotation.Value;
  13. import org.springframework.scheduling.annotation.Scheduled;
  14. import org.springframework.stereotype.Service;
  15. import java.util.HashMap;
  16. import java.util.List;
  17. import java.util.Set;
  18. @Service
  19. @Slf4j
  20. public class TaskService {
  21. @Autowired
  22. RedisUtil redisUtil;
  23. @Autowired
  24. IRtkAccountService rtkAccountService;
  25. @Autowired
  26. DingdingService dingdingService;
  27. @Scheduled(initialDelay = 2000, fixedDelay = 1000 * 60 )
  28. public void task() {
  29. try {
  30. checkAccount();
  31. }catch (Exception e){
  32. log.info("定时任务checkAccount出错:",e);
  33. }
  34. }
  35. @Scheduled(initialDelay = 3500, fixedDelay = 1000 * 60 * 60 )
  36. public void task2() {
  37. try {
  38. checkAccountDb();
  39. }catch (Exception e){
  40. log.info("定时任务checkAccountDb出错:",e);
  41. }
  42. }
  43. /**
  44. * 检查账号
  45. */
  46. public void checkAccount() {
  47. HashMap<String, RtkAccount> map = new HashMap<>();
  48. String redisKey = "4dkankan:rtk:snCode:*";
  49. Set<String> keys = redisUtil.keys(redisKey);
  50. if(keys!= null && !keys.isEmpty()){
  51. for (String key : keys) {
  52. String jsonStr = redisUtil.get(key);
  53. RtkAccount rtkAccount = JSONObject.parseObject(jsonStr, RtkAccount.class);
  54. map.put(rtkAccount.getUserName(),rtkAccount);
  55. }
  56. }
  57. LambdaQueryWrapper<RtkAccount> wrapper = new LambdaQueryWrapper<>();
  58. wrapper.eq(RtkAccount::getStatus,2);
  59. List<RtkAccount> list = rtkAccountService.list(wrapper);
  60. for (RtkAccount rtkAccount : list) {
  61. if(map.containsKey(rtkAccount.getUserName())){
  62. continue;
  63. }
  64. rtkAccountService.updateAccountStatus(rtkAccount.getId(),1);
  65. }
  66. }
  67. /**
  68. * 检查账号 账号库存不足,钉钉通知
  69. */
  70. public void checkAccountDb() {
  71. LambdaQueryWrapper<RtkAccount> wrapper = new LambdaQueryWrapper<>();
  72. wrapper.in(RtkAccount::getStatus,0,1);
  73. wrapper.orderByAsc(RtkAccount::getUpdateTime);
  74. List<RtkAccount> list = rtkAccountService.list(wrapper);
  75. if(list == null || list.isEmpty()){
  76. dingdingService.sendDingDingMsg(0);
  77. return;
  78. }
  79. dingdingService.modelThreshold(list.size(),rtkAccountService.count());
  80. }
  81. }