RtkAccountServiceImpl.java 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. package com.fdkankan.manage.service.impl;
  2. import com.alibaba.fastjson.JSONObject;
  3. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  4. import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
  5. import com.fdkankan.dingtalk.DingTalkSendUtils;
  6. import com.fdkankan.manage.common.ResultCode;
  7. import com.fdkankan.manage.entity.RtkAccount;
  8. import com.fdkankan.manage.entity.RtkInfo;
  9. import com.fdkankan.manage.exception.BusinessException;
  10. import com.fdkankan.manage.mapper.IRtkAccountMapper;
  11. import com.fdkankan.manage.service.IRtkAccountService;
  12. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  13. import com.fdkankan.manage.util.SendMailUtils;
  14. import com.fdkankan.redis.util.RedisUtil;
  15. import lombok.extern.slf4j.Slf4j;
  16. import org.springframework.beans.factory.annotation.Autowired;
  17. import org.springframework.beans.factory.annotation.Value;
  18. import org.springframework.stereotype.Service;
  19. import java.math.BigDecimal;
  20. import java.math.RoundingMode;
  21. import java.util.List;
  22. /**
  23. * <p>
  24. * 服务实现类
  25. * </p>
  26. *
  27. * @author
  28. * @since 2024-07-22
  29. */
  30. @Service
  31. @Slf4j
  32. public class RtkAccountServiceImpl extends ServiceImpl<IRtkAccountMapper, RtkAccount> implements IRtkAccountService {
  33. @Autowired
  34. RedisUtil redisUtil;
  35. @Override
  36. public RtkAccount getOneNotUseAccount(String rtkSnCode) {
  37. String redisKey = "4dkankan:rtk:snCode:"+rtkSnCode;
  38. if(redisUtil.hasKey(redisKey)){
  39. String jsonStr = redisUtil.get(redisKey);
  40. redisUtil.expire(redisKey,60);
  41. return JSONObject.parseObject(jsonStr, RtkAccount.class);
  42. }
  43. LambdaQueryWrapper<RtkAccount> wrapper = new LambdaQueryWrapper<>();
  44. wrapper.in(RtkAccount::getStatus,0,1);
  45. wrapper.orderByAsc(RtkAccount::getId);
  46. List<RtkAccount> list = this.list(wrapper);
  47. if(list == null || list.isEmpty()){
  48. //账号库存不足,钉钉通知
  49. sendDingDingMsg(0);
  50. throw new BusinessException(ResultCode.RTK_SN_CODE_NOT_EXIT);
  51. }
  52. long count = this.count();
  53. modelThreshold(count,list.size() -1);
  54. RtkAccount rtkAccount = list.get(0);
  55. updateAccountStatus(rtkAccount.getId(),2);
  56. redisUtil.set(redisKey,JSONObject.toJSONString(rtkAccount));
  57. return rtkAccount;
  58. }
  59. @Override
  60. public void updateAccountStatus(Integer id, int status) {
  61. LambdaUpdateWrapper<RtkAccount> wrapper1 = new LambdaUpdateWrapper<>();
  62. wrapper1.eq(RtkAccount::getId,id);
  63. wrapper1.set(RtkAccount::getStatus,status);
  64. this.update(wrapper1);
  65. }
  66. private void modelThreshold(long count, int size) {
  67. BigDecimal totalCount = new BigDecimal(count);
  68. BigDecimal dbCount = new BigDecimal(size);
  69. BigDecimal divideCount = totalCount.divide(dbCount).setScale(2, RoundingMode.HALF_DOWN);
  70. BigDecimal thresholdCount = new BigDecimal(this.threshold);
  71. if(divideCount.compareTo(thresholdCount) >= 0 ){
  72. sendDingDingMsg(size);
  73. }
  74. }
  75. @Autowired
  76. DingTalkSendUtils dingTalkSendUtils;
  77. private static String msgPattern =
  78. "**域名**: %s\n\n" +
  79. "**库存数量**: %s\n\n" ;
  80. @Value("${main.url}")
  81. String mainUrl;
  82. @Value("${dingtalk.threshold:80}")
  83. String threshold;
  84. private void sendDingDingMsg(Integer count){
  85. try {
  86. String format = String.format(msgPattern, mainUrl, count);
  87. dingTalkSendUtils.sendActioncardMsgToDingRobot(format,"RTK账号库存预警");
  88. }catch (Exception e){
  89. log.info("发送钉钉消息失败:{}",e);
  90. }
  91. }
  92. }