| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193 |
- package cn.com.goldenwater.dcproj.task;
- import cn.com.goldenwater.dcproj.model.BisInspMtprg;
- import cn.com.goldenwater.dcproj.model.BisInspMtprgSmsTm;
- import cn.com.goldenwater.dcproj.model.GwSmsLog;
- import cn.com.goldenwater.dcproj.param.BisInspMtprgParam;
- import cn.com.goldenwater.dcproj.param.BisInspMtprgSmsParam;
- import cn.com.goldenwater.dcproj.service.BisInspMtprgService;
- import cn.com.goldenwater.dcproj.service.BisInspMtprgSmsService;
- import cn.com.goldenwater.dcproj.service.BisInspMtprgSmsTmService;
- import cn.com.goldenwater.dcproj.service.GwSmsLogService;
- import cn.com.goldenwater.dcproj.utils.Builder;
- import cn.com.goldenwater.dcproj.utils.HttpClientUtils;
- import cn.com.goldenwater.dcproj.utils.SmsUtil;
- import cn.com.goldenwater.dcproj.vo.BisInspMtprgSmsVo;
- import cn.com.goldenwater.target.CheckException;
- import com.alibaba.fastjson.JSON;
- import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
- import org.apache.commons.collections.CollectionUtils;
- import org.apache.commons.collections.MapUtils;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.beans.factory.annotation.Value;
- import org.springframework.scheduling.annotation.Scheduled;
- import org.springframework.stereotype.Component;
- import java.time.LocalDate;
- import java.util.*;
- import java.util.stream.Collectors;
- /**
- * 月报定时任务
- *
- * @author: LinQiLong
- * @create: 2022/2/7 17:59
- */
- @Component
- public class MtprgSmsTask {
- private Logger logger = LoggerFactory.getLogger(getClass());
- @Value("${api.url}")
- private String apiUrl;
- @Value("${api.key}")
- private String apiKey;
- @Value("${api.secrect}")
- private String apiSecrect;
- @Value("${sms.template}")
- private String smsTemplate;
- @Value("${sms.mwr.aliyun}")
- private String smsMwrAliyun;
- @Autowired
- private BisInspMtprgSmsService bisInspMtprgSmsService;
- @Autowired
- private BisInspMtprgService bisInspMtprgService;
- @Autowired
- private GwSmsLogService gwSmsLogService;
- @Autowired
- private BisInspMtprgSmsTmService bisInspMtprgSmsTmService;
- private Map<String, String> templateParam = new HashMap<>(2);
- /**
- * 每月底倒数第三天 8:30
- */
- // @Scheduled(cron = "0 30 8 25-28 * ?")
- // @Scheduled(cron = "0 0/2 * * * ?")
- /**
- * 每天 10:30
- */
- @Scheduled(cron = "0 30 10 * * ?")
- public void sms() {
- final Calendar calendar = Calendar.getInstance();
- if (calendar.get(Calendar.DATE) == calendar.getActualMaximum(Calendar.DATE) - 3) {
- sendMonthlyReport();
- }
- }
- private void sendMonthlyReport() {
- LocalDate now = LocalDate.now();
- templateParam.put("year", String.valueOf(now.getYear()));
- templateParam.put("month", String.valueOf(now.getMonthValue()));
- int dayOfMonth = now.getDayOfMonth();
- BisInspMtprgSmsParam param = Builder.of(BisInspMtprgSmsParam::new)
- .with(BisInspMtprgSmsParam::setIsRemind, "1")
- .build();
- List<BisInspMtprgSmsVo> list = bisInspMtprgSmsService.list(param);
- List<BisInspMtprg> bisInspMtprgList = bisInspMtprgService.findList(Builder.of(BisInspMtprgParam::new)
- .with(BisInspMtprgParam::setYear, MapUtils.getLongValue(templateParam, "year"))
- .build());
- // 筛去当月已填报人员ID
- Set<String> perIdSet = bisInspMtprgList.stream()
- .filter(m -> getMonths(m.getMonth()).contains(templateParam.get("month")))
- .map(BisInspMtprg::getPersId)
- .collect(Collectors.toSet());
- list = list.stream()
- .filter(v -> CollectionUtils.isEmpty(perIdSet) || !perIdSet.contains(v.getUserId()))
- .collect(Collectors.toList());
- if (CollectionUtils.isNotEmpty(list)) {
- List<BisInspMtprgSmsTm> all = bisInspMtprgSmsTmService.findAll();
- Optional.ofNullable(all).orElseThrow(() -> new CheckException("月报短信提醒 异常"));
- Map<String, List<BisInspMtprgSmsTm>> map = all.stream().collect(Collectors.groupingBy(BisInspMtprgSmsTm::getOrgId));
- for (BisInspMtprgSmsVo vo : list) {
- List<BisInspMtprgSmsTm> tms = map.get(vo.getOrgId());
- //当前日 是设置的推送日。就进行推送
- if (CollectionUtils.isNotEmpty(tms)) {
- List<Long> dayList = tms.stream().map(BisInspMtprgSmsTm::getDaysOfMonth).collect(Collectors.toList());
- if (dayList.contains((long) dayOfMonth)) {
- this.sendMessage(vo);
- }
- }
- }
- }
- }
- private Set<String> getMonths(String month) {
- Set<String> monthSet = new HashSet<>();
- String[] monthArr = month.split(",");
- for (String m : monthArr) {
- if (m.contains("-")) {
- String[] mArr = m.split("-");
- int s = Integer.parseInt(mArr[0]);
- int e = Integer.parseInt(mArr[1]);
- while (e >= s) {
- monthSet.add(String.valueOf(s));
- s++;
- }
- } else {
- monthSet.add(m);
- }
- }
- return monthSet;
- }
- /**
- * 发送短信(阿里服务)
- *
- * @param smsVo 邮件信息
- */
- private void sendMessage(BisInspMtprgSmsVo smsVo) {
- Map<String, String> params = new HashMap<>(6);
- params.put("apiKey", apiKey);
- params.put("apiSecrect", apiSecrect);
- params.put("signName", "金水云平台");
- params.put("templeteCode", "SMS_234156378");
- params.put("mobile", smsVo.getMobilenumb());
- params.put("templateParam", JSON.toJSONString(templateParam));
- String content = "";
- try {
- if ("0".equals(smsMwrAliyun)) {
- SendSmsResponse sendSmsResponse = SmsUtil.send(params.get("mobile"), params.get("templateParam"), params.get("apiSecrect"), params.get("templeteCode"), params.get("signName"));
- content = sendSmsResponse.getMessage();
- } else if ("1".equals(smsMwrAliyun)) {
- content = HttpClientUtils.simplePostInvoke(apiUrl + "/gateway/api/sms/send", params);
- }
- logger.info(">> 发送短信【{}】:{}", smsVo.getMobilenumb(), content);
- } catch (Exception e) {
- logger.error(e.getMessage(), e);
- } finally {
- // 记录日志
- recordLog(smsVo, "统计月报提醒:请您填写" + templateParam.get("year") + "年" + templateParam.get("month") + "月监督检查工作月报。");
- }
- }
- /**
- * 记录日志
- *
- * @param smsVo
- */
- private void recordLog(BisInspMtprgSmsVo smsVo, String content) {
- GwSmsLog log = new GwSmsLog();
- log.setType("1");
- log.setPhone(smsVo.getMobilenumb());
- log.setContent(content);
- log.setReturnCode("");
- log.setCode("");
- log.setUserId("0");
- log.setRelUserId(smsVo.getUserId());
- gwSmsLogService.insert(log);
- }
- }
|