DingdingService.java 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. package com.fdkankan.manage.task;
  2. import com.fdkankan.dingtalk.DingTalkSendUtils;
  3. import com.fdkankan.manage.config.NacosScopeConfig;
  4. import lombok.extern.slf4j.Slf4j;
  5. import org.springframework.beans.factory.annotation.Autowired;
  6. import org.springframework.beans.factory.annotation.Value;
  7. import org.springframework.cloud.context.config.annotation.RefreshScope;
  8. import org.springframework.context.annotation.Scope;
  9. import org.springframework.stereotype.Service;
  10. import java.math.BigDecimal;
  11. import java.math.RoundingMode;
  12. @Service
  13. @Slf4j
  14. @RefreshScope
  15. public class DingdingService {
  16. @Autowired
  17. DingTalkSendUtils dingTalkSendUtils;
  18. private static String msgPattern =
  19. "**域名**: %s\n\n" +
  20. "**库存数量**: %s\n\n" ;
  21. @Value("${main.url}")
  22. String mainUrl;
  23. public void sendDingDingMsg(Integer count){
  24. try {
  25. String format = String.format(msgPattern, mainUrl, count);
  26. dingTalkSendUtils.sendActioncardMsgToDingRobot(format,"RTK账号库存预警");
  27. }catch (Exception e){
  28. log.info("发送钉钉消息失败:{}",e);
  29. }
  30. }
  31. public void modelThreshold(int size,long total) {
  32. try {
  33. if(size == 0){
  34. this.sendDingDingMsg(size);
  35. return;
  36. }
  37. BigDecimal totalCount = new BigDecimal(total);
  38. BigDecimal dbCount = new BigDecimal(size);
  39. BigDecimal divideCount = dbCount.divide(totalCount,2,RoundingMode.HALF_DOWN);
  40. BigDecimal thresholdCount = new BigDecimal(NacosScopeConfig.threshold).setScale(2,RoundingMode.HALF_DOWN);
  41. log.info("modelThreshold--{},{},{},{}",NacosScopeConfig.threshold,divideCount,thresholdCount,divideCount.compareTo(thresholdCount));
  42. if(divideCount.compareTo(thresholdCount) >= 0 ){
  43. this.sendDingDingMsg(size);
  44. }
  45. }catch (Exception e){
  46. log.info("modelThreshold--error:{},{}",size,total,e);
  47. }
  48. }
  49. }