1、完成客户余额监控

master
郑大仙丶 2 years ago
parent 773bcde542
commit 89a220d8c0

@ -1,9 +1,11 @@
package com.mashibing.monitor.client;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import java.util.Map;
import java.util.Set;
/**
@ -15,4 +17,7 @@ public interface CacheClient {
@PostMapping(value = "/cache/keys/{pattern}")
Set<String> keys(@PathVariable String pattern);
@GetMapping("/cache/hgetall/{key}")
Map hGetAll(@PathVariable(value = "key")String key);
}

@ -0,0 +1,53 @@
package com.mashibing.monitor.task;
import com.mashibing.monitor.client.CacheClient;
import com.mashibing.monitor.util.MailUtil;
import com.xxl.job.core.handler.annotation.XxlJob;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.mail.MessagingException;
import java.util.Map;
import java.util.Set;
/**
* @author
* @version V1.0.0
*/
@Component
public class MonitorClientBalanceTask {
// 客户余额限制小于500大洋直接发送信息
private final long balanceLimit = 500000;
private final String CLIENT_BALANCE_PATTERN = "client_balance:*";
private final String BALANCE = "balance";
private final String EMAIL = "extend1";
private String text = "客户您好,你在【烽火短信平台】内的余额仅剩余%s元请您及时补充金额避免影响你您的短信发送";
@Autowired
private CacheClient cacheClient;
@Autowired
private MailUtil mailUtil;
@XxlJob("monitorClientBalanceTask")
public void monitor() throws MessagingException {
//1、查询客户余额信息。
Set<String> keys = cacheClient.keys(CLIENT_BALANCE_PATTERN);
for (String key : keys) {
Map map = cacheClient.hGetAll(key);
Long balance = Long.parseLong(map.get(BALANCE) + "");
String email = (String) map.get(EMAIL);
//2、判断余额是否小于限制小于发送邮件
if(balance < balanceLimit){
mailUtil.sendEmail(email,"【烽火短信平台】提醒您余额不足。",String.format(text,balance/1000));
}
}
}
}

@ -8,6 +8,7 @@ import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
@ -25,7 +26,7 @@ public class MailUtil {
@Value("${spring.mail.tos}")
private String tos;
@Autowired
@Resource
private JavaMailSender javaMailSender;
@ -35,14 +36,31 @@ public class MailUtil {
// 给邮件指定信息
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage);
helper.setFrom(from);
helper.setTo(tos.split(","));
helper.setSubject(subject);
helper.setText(text);
setInfo(subject, text, helper);
// 发送邮件
javaMailSender.send(mimeMessage);
}
public void sendEmail(String to,String subject,String text) throws MessagingException {
// 构建MimeMessage对象
MimeMessage mimeMessage = javaMailSender.createMimeMessage();
// 给邮件指定信息
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage);
helper.setTo(to);
setInfo(subject, text, helper);
// 发送邮件
javaMailSender.send(mimeMessage);
}
private void setInfo(String subject, String text, MimeMessageHelper helper) throws MessagingException {
helper.setFrom(from);
helper.setSubject(subject);
helper.setText(text);
}
}

Loading…
Cancel
Save