TaskService.java 3.2 KB

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