| @@ -1,21 +1,24 @@ | |||||
| package com.iformall.schedule; | package com.iformall.schedule; | ||||
| import com.alibaba.fastjson.JSONArray; | |||||
| import com.alibaba.fastjson.JSONObject; | import com.alibaba.fastjson.JSONObject; | ||||
| import com.iformall.domain.po.WxMsg; | import com.iformall.domain.po.WxMsg; | ||||
| import com.iformall.domain.po.WxMsgConfig; | import com.iformall.domain.po.WxMsgConfig; | ||||
| import com.iformall.enums.EnumMsgSendStatus; | |||||
| import com.iformall.enums.EnumMsgStatus; | |||||
| import com.iformall.enums.EnumVerifyCode; | import com.iformall.enums.EnumVerifyCode; | ||||
| import com.iformall.exception.MallinkException; | |||||
| import com.iformall.mapper.WxMsgConfigMapper; | import com.iformall.mapper.WxMsgConfigMapper; | ||||
| import com.iformall.mapper.WxMsgMapper; | import com.iformall.mapper.WxMsgMapper; | ||||
| import com.iformall.service.PushLimitService; | import com.iformall.service.PushLimitService; | ||||
| import com.iformall.utils.*; | |||||
| import com.iformall.utils.DateUtils; | |||||
| import com.iformall.utils.WiwideUtil; | |||||
| import org.slf4j.Logger; | import org.slf4j.Logger; | ||||
| import org.slf4j.LoggerFactory; | import org.slf4j.LoggerFactory; | ||||
| import org.springframework.beans.factory.annotation.Autowired; | import org.springframework.beans.factory.annotation.Autowired; | ||||
| import org.springframework.scheduling.annotation.Scheduled; | import org.springframework.scheduling.annotation.Scheduled; | ||||
| import org.springframework.stereotype.Component; | import org.springframework.stereotype.Component; | ||||
| import java.util.*; | |||||
| import java.util.List; | |||||
| @Component | @Component | ||||
| public class MsgSendingSchedule { | public class MsgSendingSchedule { | ||||
| @@ -32,75 +35,59 @@ public class MsgSendingSchedule { | |||||
| private PushLimitService pushLimitService; | private PushLimitService pushLimitService; | ||||
| @Scheduled(cron = "0 1 * * * ?") // 每小时第一分钟执行 | |||||
| @Scheduled(cron = "0 46 * * * ?") // 每小时第一分钟执行 | |||||
| public void sendmsgschedule() { | public void sendmsgschedule() { | ||||
| logger.info("sendmsg定时任务启动"); | logger.info("sendmsg定时任务启动"); | ||||
| String systemTime = DateUtils.getSystemTime("yyyy-MM-dd HH:00:00"); | |||||
| String systemTime = DateUtils.getSystemTime("yyyy-MM-dd 14:00:00"); | |||||
| WxMsg wxMsg = new WxMsg(); | WxMsg wxMsg = new WxMsg(); | ||||
| wxMsg.setIsright(0); | wxMsg.setIsright(0); | ||||
| wxMsg.setSendtime(systemTime); | wxMsg.setSendtime(systemTime); | ||||
| List<WxMsg> list = wxMsgMapper.findList(wxMsg); | List<WxMsg> list = wxMsgMapper.findList(wxMsg); | ||||
| logger.info("将要发送的短信列表:" + JSONArray.toJSONString(list)); | |||||
| for(WxMsg msg:list){ | for(WxMsg msg:list){ | ||||
| boolean checkTime = false; | |||||
| try { | |||||
| checkTime = pushLimitService.checkSendTime(wxMsg.getTenantId()); | |||||
| } catch (MallinkException e) { | |||||
| logger.error(e.getMessage()); | |||||
| } | |||||
| if (checkTime) { | |||||
| sendmsg(msg); | |||||
| } | |||||
| sendmsg(msg); | |||||
| } | } | ||||
| logger.info("sendmsg定时任务结束"); | logger.info("sendmsg定时任务结束"); | ||||
| } | } | ||||
| public void sendmsg(WxMsg wxMsg){ | public void sendmsg(WxMsg wxMsg){ | ||||
| logger.info("发送短信开始----------"); | |||||
| //从短信配置中查询密钥 bid 等信息 | //从短信配置中查询密钥 bid 等信息 | ||||
| WxMsgConfig wxMsgConfig = new WxMsgConfig(); | WxMsgConfig wxMsgConfig = new WxMsgConfig(); | ||||
| wxMsgConfig.setTenantId(wxMsg.getTenantId()); | wxMsgConfig.setTenantId(wxMsg.getTenantId()); | ||||
| List<WxMsgConfig> wxMsgConfigs = wxMsgConfigMapper.findList(wxMsgConfig); | List<WxMsgConfig> wxMsgConfigs = wxMsgConfigMapper.findList(wxMsgConfig); | ||||
| if (wxMsgConfigs.size() == 0) return; | |||||
| if (wxMsgConfigs.size() == 0) { | |||||
| logger.info("短信相关配置不存在"); | |||||
| return; | |||||
| } | |||||
| wxMsgConfig = wxMsgConfigs.get(0); | wxMsgConfig = wxMsgConfigs.get(0); | ||||
| if(wxMsgConfig.getRemains()==0){ | if(wxMsgConfig.getRemains()==0){ | ||||
| logger.info("短信数量为0"); | logger.info("短信数量为0"); | ||||
| return; | return; | ||||
| } | } | ||||
| if(wxMsgConfig.getRemains()<wxMsg.getExpectSendNumber()){ | if(wxMsgConfig.getRemains()<wxMsg.getExpectSendNumber()){ | ||||
| logger.info("短信数量不足"); | logger.info("短信数量不足"); | ||||
| return; | return; | ||||
| } | } | ||||
| String secret = wxMsgConfig.getSecret(); | String secret = wxMsgConfig.getSecret(); | ||||
| String bid = wxMsgConfig.getBid(); | String bid = wxMsgConfig.getBid(); | ||||
| String publickey = wxMsgConfig.getPublickey(); | String publickey = wxMsgConfig.getPublickey(); | ||||
| String phone = wxMsg.getPhones(); | String phone = wxMsg.getPhones(); | ||||
| String signature = wxMsg.getSignature(); | String signature = wxMsg.getSignature(); | ||||
| String msg = wxMsg.getMsg(); | String msg = wxMsg.getMsg(); | ||||
| String notifyUrl = wxMsgConfig.getNotifyurl(); | String notifyUrl = wxMsgConfig.getNotifyurl(); | ||||
| String result = WiwideUtil.sendMsg(secret, bid, publickey, phone, signature, msg, notifyUrl,EnumVerifyCode.NO.getCode().toString()); | String result = WiwideUtil.sendMsg(secret, bid, publickey, phone, signature, msg, notifyUrl,EnumVerifyCode.NO.getCode().toString()); | ||||
| logger.info("短信返回结果:" + result); | |||||
| JSONObject jsonObjectResult = JSONObject.parseObject(result); | JSONObject jsonObjectResult = JSONObject.parseObject(result); | ||||
| String ret = jsonObjectResult.get("ret").toString(); | String ret = jsonObjectResult.get("ret").toString(); | ||||
| if (ret.equals("1")) { | if (ret.equals("1")) { | ||||
| wxMsg.setSendstatus(1); | |||||
| wxMsg.setSendstatus(EnumMsgSendStatus.MSG_SEND_SUCCESS.getCode()); | |||||
| } else { | } else { | ||||
| wxMsg.setSendstatus(0); | |||||
| wxMsg.setSendstatus(EnumMsgSendStatus.MSG_SEND_FAIL.getCode()); | |||||
| } | } | ||||
| wxMsg.setStatus(1); | |||||
| wxMsg.setStatus(EnumMsgStatus.MSG_STATUS_SENDED.getCode()); | |||||
| wxMsgMapper.updateByPrimaryKeySelective(wxMsg); | wxMsgMapper.updateByPrimaryKeySelective(wxMsg); | ||||
| logger.info("短信发送结束:" + jsonObjectResult.toJSONString()); | |||||
| } | } | ||||
| } | } | ||||