package com.gk.firework.Scheduls.WarningTask; import com.alibaba.fastjson.JSONObject; import com.gk.firework.Domain.*; import com.gk.firework.Domain.Utils.HttpUtils; import com.gk.firework.Domain.Utils.StringUtils; import com.gk.firework.Domain.Vo.WarnContentVo; import com.gk.firework.Service.*; import org.apache.log4j.LogManager; import org.apache.log4j.Logger; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.context.annotation.Configuration; import org.springframework.scheduling.annotation.EnableScheduling; import org.springframework.scheduling.annotation.Scheduled; import java.io.UnsupportedEncodingException; import java.net.URLEncoder; import java.text.SimpleDateFormat; import java.util.*; @Configuration @EnableScheduling @ConditionalOnProperty(prefix = "scheduling",name = "enabled",havingValue = "true") public class MessageSendTask { private Logger logger = LogManager.getLogger(MessageSendTask.class); @Autowired SmsLogService smsLogService; @Autowired WarnContentService warnContentService; @Autowired SaleOrderService saleOrderInfoService; @Autowired EnterpriseService enterpriseService; @Autowired UserService userService; @Autowired RegisterService registerService; public static String key = "f89279278b739f972ed922fa708eafa0"; public final static String URL = "http://v.juhe.cn/sms/send"; @Scheduled(cron = "0/5 * * * * ?") //每隔5秒执行一次 private void EmailSend() throws UnsupportedEncodingException, InterruptedException { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date now = new Date(); Calendar calendar = Calendar.getInstance(); calendar.setTime(now); calendar.add(Calendar.MINUTE, -10); Date starttime = calendar.getTime(); String starttimeStr = sdf.format(starttime); String endtimeStr = sdf.format(now); //查询10分钟内需要发短信,但又没发送过的记录 List warnContentInfoList = warnContentService.selectNeedMail(starttimeStr,endtimeStr); if (warnContentInfoList.size() > 0) { for (WarnContentVo warnContentInfo : warnContentInfoList){ if (warnContentInfo.getWarntype().equals("购买超量")){ Long enterpriseid = saleOrderInfoService.selectByCustomer(warnContentInfo.getCustomid()); List mobiles = selectMobilesByEnterprise(enterpriseid); String content = "姓名:"+warnContentInfo.getPurchasename()+",身份证号:"+warnContentInfo.getIdcard()+" "+warnContentInfo.getWarncontent(); String head = ""; boolean issend = sendMessage(head,mobiles,content); if (issend){ warnContentInfo.setIssend((byte)1); warnContentService.updateById(warnContentInfo); } }else if (warnContentInfo.getWarntype().equals("库存超量")){ List mobiles = selectMobilesByEnterprise(warnContentInfo.getEnterpriseid()); String content = "企业名称:"+warnContentInfo.getEnterprisename()+","+warnContentInfo.getWarncontent(); String head = ""; boolean issend = sendMessage(head,mobiles,content); if (issend){ warnContentInfo.setIssend((byte)1); warnContentService.updateById(warnContentInfo); } } } } } @Scheduled(cron = "0/5 * * * * ?") //每隔5秒执行一次 private void registerEmailSend() throws UnsupportedEncodingException { List registerInfos = registerService.needSendSmsList(); if (registerInfos.size() > 0){ for (RegisterInfo registerInfo : registerInfos){ String tplId; if (registerInfo.getReviewresult().equals((byte)1)){ tplId = "235091"; }else { tplId = "235094"; } boolean isSend = sendSms(registerInfo.getEnterprisename(),registerInfo.getLegalpersonphone(),tplId); if (isSend){ registerInfo.setSmsat(new Date()); registerInfo.setSmstimes(registerInfo.getSmstimes()+1); registerInfo.setIssms((byte)1); registerService.updateById(registerInfo); } } } } // private boolean issendTest = false; // @Scheduled(cron = "0/5 * * * * ?") //每隔5秒执行一次 // private void EmailSendTest() throws UnsupportedEncodingException, InterruptedException { // SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); // Date now = new Date(); // Calendar calendar = Calendar.getInstance(); // calendar.setTime(now); // calendar.add(Calendar.MINUTE, -10); // Date starttime = calendar.getTime(); // // List mobiles = new ArrayList<>(); // mobiles.add("15651251027"); // String content = "测试短信"; // String head = ""; // if (!issendTest){ // issendTest = sendMessage(head,mobiles,content); // } // } private List selectMobilesByEnterprise(Long enterpriseid) { List mobiles = new ArrayList<> (); //先查直属监管部门,若不存在则再查上一级,直到查到对应监管部门用户 Enterprise enterprise = enterpriseService.getById(enterpriseid); UserInfo userInfo = userService.selectSupervise(enterprise.getProvince(),enterprise.getCity(),enterprise.getDistrict(),enterprise.getStreet(),enterprise.getCommittee()); if (userInfo == null){ userInfo = userService.selectSupervise(enterprise.getProvince(),enterprise.getCity(),enterprise.getDistrict(),enterprise.getStreet(),null); if (userInfo == null){ userInfo = userService.selectSupervise(enterprise.getProvince(),enterprise.getCity(),enterprise.getDistrict(),null,null); if (userInfo == null){ userInfo = userService.selectSupervise(enterprise.getProvince(),enterprise.getCity(),null,null,null); if (userInfo == null){ userInfo = userService.selectSupervise(enterprise.getProvince(),null,null,null,null); if (userInfo == null){ userInfo = userService.selectSupervise(null,null,null,null,null); } } } } } if (userInfo != null && StringUtils.isNotBlank(userInfo.getMobile())){ mobiles.add(userInfo.getMobile()); } return mobiles; } public Boolean sendMessage(String head,List mobiles,String content) throws UnsupportedEncodingException { String url ="http://v.juhe.cn/sms/send";//请求接口地址 String tpl_id = "231913"; String dtype = "json"; String tpl_value = "#name#=" + head + "&#code#=" + content; SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); SimpleDateFormat sdfstart = new SimpleDateFormat("yyyy-MM-dd 00:00:00"); SimpleDateFormat sdfend = new SimpleDateFormat("yyyy-MM-dd 23:59:59"); Date now = new Date(); String starttime = sdfstart.format(now); String endtime = sdfend.format(now); Calendar calendar = Calendar.getInstance(); calendar.setTime(now); calendar.add(Calendar.HOUR_OF_DAY, -1); Date onehour = calendar.getTime(); String onehourstart = sdf.format(onehour); String onehourend = sdf.format(now); boolean issend = false; for (String mobile : mobiles) { /**运营商限制同1个号码同1个签名的内容1分钟内只能接收1条,10分钟3条,1小时内4条,一天20条,否则可能会被运营商屏蔽**/ SmsLogInfo smsLogInfo = smsLogService.selectByTel(mobile,starttime,endtime,(byte)2); if (null != smsLogInfo && smsLogInfo.getTimes()>=20){ logger.error("手机号:" + mobile + ",在时间为" + sdf.format(now) + ",当天已发出20条,无法继续发送"); continue; } SmsLogInfo smsLogInfoHour = smsLogService.selectByTel(mobile,onehourstart,onehourend,(byte)1); if (null != smsLogInfoHour && smsLogInfoHour.getTimes()>=4){ logger.error("手机号:" + mobile + ",在时间为" + sdf.format(now) + ",本小时已发出4条,无法继续发送"); continue; } String respnse = null; Map params = new HashMap();//请求参数 params.put("mobile", mobile);//接收短信的手机号码 params.put("tpl_id", tpl_id);//短信模板ID,请参考个人中心短信模板设置 params.put("tpl_value", tpl_value); params.put("key", key);//应用APPKEY(应用详细页查询) params.put("dtype", dtype);//返回数据的格式,xml或json,默认json try { respnse = HttpUtils.net(url, params, "GET"); JSONObject jsonResult = JSONObject.parseObject(respnse); String errorcode = jsonResult.getString("error_code"); String reason = jsonResult.getString("reason"); if (errorcode.equals("0")) { //增加发送次数 JSONObject result = jsonResult.getJSONObject("result"); Integer count = result.getInteger("count"); if (null != smsLogInfo){ smsLogInfo.setTimes(smsLogInfo.getTimes()+count); smsLogService.updateById(smsLogInfo); }else { SmsLogInfo smsLogInfo1 = new SmsLogInfo(); smsLogInfo1.setMobile(mobile); smsLogInfo1.setSendtime(now); smsLogInfo1.setTimes(count); smsLogInfo1.setType((byte)2); smsLogService.save(smsLogInfo1); } issend = true; if (null != smsLogInfoHour){ smsLogInfoHour.setTimes(smsLogInfoHour.getTimes()+count); smsLogService.updateById(smsLogInfoHour); }else { SmsLogInfo smsLogInfo1 = new SmsLogInfo(); smsLogInfo1.setMobile(mobile); smsLogInfo1.setSendtime(now); smsLogInfo1.setTimes(count); smsLogInfo1.setType((byte)1); smsLogService.save(smsLogInfo1); } } else { logger.error("手机号:" + mobile + ",在时间为" + sdf.format(now) + "发送短信失败,原因为:" + reason); } } catch (Exception e) { e.printStackTrace(); } } return issend; } public Boolean sendSms(String head, String mobile, String tplId) throws UnsupportedEncodingException { String dtype = "json"; String tplValue = "#name#=" + head; SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); SimpleDateFormat sdfstart = new SimpleDateFormat("yyyy-MM-dd 00:00:00"); SimpleDateFormat sdfend = new SimpleDateFormat("yyyy-MM-dd 23:59:59"); Date now = new Date(); String starttime = sdfstart.format(now); String endtime = sdfend.format(now); Calendar calendar = Calendar.getInstance(); calendar.setTime(now); calendar.add(Calendar.HOUR_OF_DAY, -1); Date onehour = calendar.getTime(); String onehourstart = sdf.format(onehour); String onehourend = sdf.format(now); boolean isSend = false; /** 运营商限制同1个号码同1个签名的内容1分钟内只能接收1条,10分钟3条,1小时内4条,一天20条,否则可能会被运营商屏蔽 **/ SmsLogInfo smsLogInfo = smsLogService.selectByTel(mobile,starttime,endtime,(byte)2); if (null != smsLogInfo && smsLogInfo.getTimes()>= 20){ logger.error("手机号:" + mobile + ",在时间为" + sdf.format(now) + ",当天已发出20条,无法继续发送"); return false; } SmsLogInfo smsLogInfoHour = smsLogService.selectByTel(mobile,onehourstart,onehourend,(byte)1); if (null != smsLogInfoHour && smsLogInfoHour.getTimes()>= 4){ logger.error("手机号:" + mobile + ",在时间为" + sdf.format(now) + ",本小时已发出4条,无法继续发送"); return false; } String response; Map params = new HashMap();//请求参数 params.put("mobile", mobile);//接收短信的手机号码 params.put("tpl_id", tplId);//短信模板ID,请参考个人中心短信模板设置 params.put("tpl_value", tplValue); params.put("key", key);//应用APPKEY(应用详细页查询) params.put("dtype", dtype);//返回数据的格式,xml或json,默认json try { response = HttpUtils.net(URL, params, "GET"); JSONObject jsonResult = JSONObject.parseObject(response); String errorcode = jsonResult.getString("error_code"); String reason = jsonResult.getString("reason"); if (errorcode.equals("0")) { //增加发送次数 JSONObject result = jsonResult.getJSONObject("result"); Integer count = result.getInteger("count"); if (null != smsLogInfo){ smsLogInfo.setTimes(smsLogInfo.getTimes()+count); smsLogService.updateById(smsLogInfo); }else { SmsLogInfo smsLogInfo1 = new SmsLogInfo(); smsLogInfo1.setMobile(mobile); smsLogInfo1.setSendtime(now); smsLogInfo1.setTimes(count); smsLogInfo1.setType((byte)2); smsLogService.save(smsLogInfo1); } isSend = true; if (null != smsLogInfoHour){ smsLogInfoHour.setTimes(smsLogInfoHour.getTimes()+count); smsLogService.updateById(smsLogInfoHour); }else { SmsLogInfo smsLogInfo1 = new SmsLogInfo(); smsLogInfo1.setMobile(mobile); smsLogInfo1.setSendtime(now); smsLogInfo1.setTimes(count); smsLogInfo1.setType((byte)1); smsLogService.save(smsLogInfo1); } } else { logger.error("手机号:" + mobile + ",在时间为" + sdf.format(now) + "发送短信失败,原因为:" + reason); } } catch (Exception e) { e.printStackTrace(); } return isSend; } }