DingdingService.java 1.7 KB

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