package com.fdkankan.manage.service.impl; import com.alibaba.fastjson.JSONObject; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper; import com.fdkankan.dingtalk.DingTalkSendUtils; import com.fdkankan.manage.common.ResultCode; import com.fdkankan.manage.entity.RtkAccount; import com.fdkankan.manage.entity.RtkInfo; import com.fdkankan.manage.exception.BusinessException; import com.fdkankan.manage.mapper.IRtkAccountMapper; import com.fdkankan.manage.service.IRtkAccountService; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.fdkankan.manage.util.SendMailUtils; import com.fdkankan.redis.util.RedisUtil; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; import java.math.BigDecimal; import java.math.RoundingMode; import java.util.List; /** *

* 服务实现类 *

* * @author * @since 2024-07-22 */ @Service @Slf4j public class RtkAccountServiceImpl extends ServiceImpl implements IRtkAccountService { @Autowired RedisUtil redisUtil; @Override public RtkAccount getOneNotUseAccount(String rtkSnCode) { String redisKey = "4dkankan:rtk:snCode:"+rtkSnCode; if(redisUtil.hasKey(redisKey)){ String jsonStr = redisUtil.get(redisKey); redisUtil.expire(redisKey,60); return JSONObject.parseObject(jsonStr, RtkAccount.class); } LambdaQueryWrapper wrapper = new LambdaQueryWrapper<>(); wrapper.in(RtkAccount::getStatus,0,1); wrapper.orderByAsc(RtkAccount::getId); List list = this.list(wrapper); if(list == null || list.isEmpty()){ //账号库存不足,钉钉通知 sendDingDingMsg(0); throw new BusinessException(ResultCode.RTK_SN_CODE_NOT_EXIT); } long count = this.count(); modelThreshold(count,list.size() -1); RtkAccount rtkAccount = list.get(0); updateAccountStatus(rtkAccount.getId(),2); redisUtil.set(redisKey,JSONObject.toJSONString(rtkAccount)); return rtkAccount; } @Override public void updateAccountStatus(Integer id, int status) { LambdaUpdateWrapper wrapper1 = new LambdaUpdateWrapper<>(); wrapper1.eq(RtkAccount::getId,id); wrapper1.set(RtkAccount::getStatus,status); this.update(wrapper1); } private void modelThreshold(long count, int size) { BigDecimal totalCount = new BigDecimal(count); BigDecimal dbCount = new BigDecimal(size); BigDecimal divideCount = totalCount.divide(dbCount).setScale(2, RoundingMode.HALF_DOWN); BigDecimal thresholdCount = new BigDecimal(this.threshold); if(divideCount.compareTo(thresholdCount) >= 0 ){ sendDingDingMsg(size); } } @Autowired DingTalkSendUtils dingTalkSendUtils; private static String msgPattern = "**域名**: %s\n\n" + "**库存数量**: %s\n\n" ; @Value("${main.url}") String mainUrl; @Value("${dingtalk.threshold:80}") String threshold; private void sendDingDingMsg(Integer count){ try { String format = String.format(msgPattern, mainUrl, count); dingTalkSendUtils.sendActioncardMsgToDingRobot(format,"RTK账号库存预警"); }catch (Exception e){ log.info("发送钉钉消息失败:{}",e); } } }