277 lines
16 KiB
Java
277 lines
16 KiB
Java
package com.tenwa.message;
|
|
|
|
import com.amarsoft.are.ARE;
|
|
import com.amarsoft.are.jbo.*;
|
|
import com.amarsoft.awe.util.ASResultSet;
|
|
import com.amarsoft.awe.util.SqlObject;
|
|
import com.amarsoft.awe.util.Transaction;
|
|
import com.tenwa.quartz.DateUtil;
|
|
import com.tenwa.reckon.util.UUIDUtil;
|
|
import jbo.com.tenwa.lease.comm.LB_SMS_NOTICE_TASKS;
|
|
import jbo.sys.MSG_CONFIG;
|
|
import org.quartz.*;
|
|
import org.quartz.impl.StdSchedulerFactory;
|
|
|
|
import java.text.SimpleDateFormat;
|
|
import java.util.Calendar;
|
|
import java.util.Date;
|
|
import java.util.HashMap;
|
|
import java.util.Map;
|
|
|
|
public class QuartzJobMsg implements Job {
|
|
|
|
public static final String MSG_GROUP_NAME = "MSG_DEFAULT_GROUP";
|
|
|
|
@Override
|
|
public void execute(JobExecutionContext context) throws JobExecutionException {
|
|
JBOTransaction tx = null;
|
|
JBOTransaction inner_tx = null;
|
|
try {
|
|
tx = JBOFactory.createJBOTransaction();
|
|
Transaction tran = Transaction.createTransaction(tx);
|
|
Trigger trigger = context.getTrigger();
|
|
JobDataMap data = context.getJobDetail().getJobDataMap();
|
|
//获取主键
|
|
String id = data.getString("id");
|
|
//获取sql语句
|
|
String msgsql = data.getString("msgSql");
|
|
//获取关键字
|
|
String msgsqlparam = data.getString("msgsqlparam");
|
|
//获取短信内容
|
|
String msgcontent = data.getString("msgcontent");
|
|
//获取触发方式
|
|
String triggermode = data.getString("triggermode");
|
|
//获取短信发送时间类型、短信延迟发送天数、短信发送时间点
|
|
String msgsendtype = data.getString("msgsendtype");//短信发送类型
|
|
String msgsendtime = "";//短信发送时间点
|
|
Date timeSet = null;
|
|
boolean checkTime = false;
|
|
Date timeNow = new Date();
|
|
if (data.getString("msgsendtime") != null) {
|
|
msgsendtime = data.getString("msgsendtime");
|
|
timeSet = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(new SimpleDateFormat("yyyy-MM-dd").format(timeNow) + " " + msgsendtime);
|
|
}
|
|
String delayday = "";//延迟发送天数
|
|
if (data.getString("delayday") != null) {
|
|
delayday = data.getString("delayday");
|
|
}
|
|
//获得过期是否执行
|
|
String isoverdateexecute = data.getString("isoverdateexecute");
|
|
//获取短信类型
|
|
String msgtype = data.getString("msgType");
|
|
if ("Tl_Business".equals(msgtype) || "Tl_ChangeBusiness".equals(msgtype) || "Tl_MakeContract".equals(msgtype) || "Tl_ChangeContract".equals(msgtype) || "Tl_AfterRenting".equals(msgtype)) {
|
|
msgcontent = msgcontent.replace("@", "=");
|
|
}
|
|
//获取是否按日期去重
|
|
String isrepatebydate = data.getString("isrepeatbydate");
|
|
//获取短信替换参数
|
|
int oldlength = msgcontent.length();
|
|
int newlength = msgcontent.replace("}", "").length();
|
|
String[] params = new String[oldlength - newlength];
|
|
if (params.length > 0) {
|
|
int index = 0;
|
|
for (int i = 0; i < params.length; i++) {
|
|
int start = msgcontent.indexOf("{", index);
|
|
int end = msgcontent.indexOf("}", index);
|
|
params[i] = msgcontent.substring(start + 1, end);
|
|
index = end + 1;
|
|
}
|
|
}
|
|
JBOFactory.getBizObjectManager(MSG_CONFIG.CLASS_NAME, tx).createQuery("update O set previous_fire_time=:previousFireTime , next_fire_time=:nextFireTime where id='" + id + "'")
|
|
.setParameter("previousFireTime", trigger.getPreviousFireTime().getTime())
|
|
.setParameter("nextFireTime", trigger.getNextFireTime() == null ? "" : String.valueOf(trigger.getNextFireTime().getTime())).executeUpdate();
|
|
ASResultSet rs = tran.getASResultSet(new SqlObject(msgsql));
|
|
Scheduler scheduler = getScheduler();
|
|
Transaction inner_tran = null;
|
|
while (rs.next()) {
|
|
boolean flag = false;
|
|
for (int p = 0; p < params.length; p++) {
|
|
if (rs.getString(params[p]) == null) {
|
|
flag = true;
|
|
}
|
|
}
|
|
if (flag) {
|
|
continue;
|
|
}
|
|
String rsMsgContent = msgcontent;
|
|
Object phonenumber = rs.getString("phonenumber");
|
|
if (phonenumber == null || phonenumber.toString().length() != 11) {
|
|
continue;
|
|
}
|
|
//新起数据库连接
|
|
inner_tx = JBOFactory.createJBOTransaction();
|
|
inner_tran = Transaction.createTransaction(inner_tx);
|
|
//查询关联记录
|
|
String sql = "select * from msg_send_relative where 1=1 ";
|
|
String[] sqlparams = msgsqlparam.split("&");
|
|
if (sqlparams.length > 0) {
|
|
for (int i = 0; i < sqlparams.length; i++) {
|
|
sql += "and " + sqlparams[i] + "='" + rs.getString(sqlparams[i]).toString() + "' ";
|
|
}
|
|
}
|
|
if (isrepatebydate.equals("yes")) {//根据日期去重
|
|
sql += "and inputtime='" + rs.getString("checkdate") + "'";
|
|
}
|
|
if (msgtype.startsWith("holiday") || msgtype.startsWith("birthday")) {//如果是生日祝福和节假日祝福必须添加短信类型和日期条件筛选
|
|
sql += "and msg_type='" + msgtype + "' and inputtime='" + new SimpleDateFormat("yyyy-MM-dd").format(new Date()) + "'";
|
|
} else {
|
|
sql += "and msg_type='" + msgtype + "'";
|
|
}
|
|
sql = sql.replaceAll(":", "△");
|
|
SqlObject so = new SqlObject(sql);
|
|
so.setRunSql(so.getRunSql().replaceAll("△", ":"));
|
|
ASResultSet msgSend = inner_tran.getASResultSet(so);
|
|
|
|
//关联记录不存在,新建短信任务,发送短信通知
|
|
if (!msgSend.next()) {
|
|
for (int i = 0; i < params.length; i++) {
|
|
rsMsgContent = rsMsgContent.replace(params[i], rs.getString(params[i]));
|
|
}
|
|
rsMsgContent = rsMsgContent.replace("{", "");
|
|
rsMsgContent = rsMsgContent.replace("}", "");
|
|
//创建短信发送记录
|
|
BizObjectManager bomLSNT = JBOFactory.getBizObjectManager(LB_SMS_NOTICE_TASKS.CLASS_NAME, inner_tx);
|
|
BizObject sn = bomLSNT.newObject();
|
|
sn.setAttributeValue("inputuserid", rs.getString("proj_manage") != null ? rs.getString("proj_manage") : data.getString("userID"));
|
|
sn.setAttributeValue("customer_id", rs.getString("customer_id"));
|
|
sn.setAttributeValue("phone_number", phonenumber.toString());
|
|
sn.setAttributeValue("send_flag", 0);
|
|
sn.setAttributeValue("sms_content", rsMsgContent);
|
|
sn.setAttributeValue("sms_type", "DELAYED");
|
|
sn.setAttributeValue("inputtime", DateUtil.getDateTime(new Date()));
|
|
bomLSNT.saveObject(sn);
|
|
|
|
if ("trigger_mode02".equals(triggermode)) {//手动触发,不需要定时代理
|
|
continue;
|
|
}
|
|
|
|
//关联表生成记录
|
|
String insertSql = "insert into msg_send_relative (id,sms_id,msg_type,column) values('" + UUIDUtil.getUUID() + "','" + sn.getAttribute("ID").getString() + "','" + msgtype + "',data)";
|
|
String column = "";
|
|
String value = "";
|
|
for (int m = 0; m < sqlparams.length; m++) {
|
|
column += "," + sqlparams[m];
|
|
value += ",'" + rs.getString(sqlparams[m]).toString() + "'";
|
|
}
|
|
column = column.substring(1, column.length());
|
|
value = value.substring(1, value.length());
|
|
if (msgtype.startsWith("holiday") || msgtype.startsWith("birthday")) {
|
|
column += ",inputtime";
|
|
value += ",'" + new SimpleDateFormat("yyyy-MM-dd").format(new Date()) + "'";
|
|
}
|
|
if (isrepatebydate.equals("yes")) {//根据日期去重
|
|
column += ",inputtime";
|
|
value += ",'" + rs.getString("checkdate") + "'";
|
|
}
|
|
|
|
String jobName = UUIDUtil.getUUID();
|
|
String triggerName = UUIDUtil.getUUID();
|
|
//记录短信发送定时代理
|
|
column += ",msg_send_jobname,msg_send_triggername";
|
|
value += ",'" + jobName + "','" + triggerName + "'";
|
|
insertSql = insertSql.replace("column", column);
|
|
insertSql = insertSql.replace("data", value);
|
|
|
|
insertSql = insertSql.replaceAll(":", "△");
|
|
SqlObject soInsert = new SqlObject(insertSql);
|
|
soInsert.setRunSql(soInsert.getRunSql().replaceAll("△", ":"));
|
|
inner_tran.executeSQL(new SqlObject(insertSql));
|
|
|
|
Date startTime = DateUtil.getTimeByFormat(DateUtil.getDateTime(new Date()), "yyyy-MM-dd HH:mm:ss");
|
|
//动态创建时间表达式
|
|
String cron = "";
|
|
Calendar ca = Calendar.getInstance();
|
|
ca.setTime(new Date());
|
|
if (timeSet != null) {
|
|
if (timeSet.compareTo(timeNow) <= 0) {
|
|
checkTime = true;
|
|
}
|
|
cron += msgsendtime.split(":")[2] + " ";
|
|
cron += msgsendtime.split(":")[1] + " ";
|
|
cron += msgsendtime.split(":")[0] + " ";
|
|
if (checkTime) {
|
|
cron = "";
|
|
String tNow = new SimpleDateFormat("HH:mm:ss").format(new Date(new Date().getTime() + 2000));
|
|
cron += tNow.split(":")[2] + " ";
|
|
cron += tNow.split(":")[1] + " ";
|
|
cron += tNow.split(":")[0] + " ";
|
|
}
|
|
}
|
|
|
|
//定时任务处理
|
|
Map<String, Object> dataMap = new HashMap<String, Object>();
|
|
dataMap.put("smsnoticeid", sn.getAttribute("ID").getString());
|
|
JobDetail jobDetail = JobBuilder.newJob(JobSendMessage.class).withIdentity(jobName, MSG_GROUP_NAME)
|
|
.usingJobData(new JobDataMap(dataMap)).requestRecovery("1".equals(isoverdateexecute) ? true : false).storeDurably(false).build();
|
|
CronExpression ce = null;
|
|
switch (msgsendtype) {
|
|
case "msg_time_type01"://当天立即
|
|
cron = "";
|
|
String tNow = new SimpleDateFormat("HH:mm:ss").format(new Date(new Date().getTime() + 2000));
|
|
cron += tNow.split(":")[2] + " ";
|
|
cron += tNow.split(":")[1] + " ";
|
|
cron += tNow.split(":")[0] + " ";
|
|
cron += ca.get(Calendar.DAY_OF_MONTH) + " ";
|
|
cron += (ca.get(Calendar.MONTH) + 1) + " ? *";
|
|
ce = new CronExpression(cron);
|
|
trigger = TriggerBuilder.newTrigger().forJob(jobName, MSG_GROUP_NAME).withIdentity(triggerName, MSG_GROUP_NAME).startAt(startTime).endAt(new Date(new Date().getTime() + (24 * 60 * 60 * 1000)))
|
|
.withSchedule(CronScheduleBuilder.cronSchedule(ce).withMisfireHandlingInstructionDoNothing())
|
|
.build();
|
|
sn.setAttributeValue("sms_type", "IMMEDIATELY");
|
|
ARE.getLog().warn(triggerName + " cron : " + cron);
|
|
break;
|
|
case "msg_time_type02"://当天指定时间
|
|
cron += ca.get(Calendar.DAY_OF_MONTH) + " ";
|
|
cron += (ca.get(Calendar.MONTH) + 1) + " ? *";
|
|
ce = new CronExpression(cron);
|
|
trigger = TriggerBuilder.newTrigger().forJob(jobName, MSG_GROUP_NAME).withIdentity(triggerName, MSG_GROUP_NAME).startAt(startTime).endAt(new Date(new Date().getTime() + (24 * 60 * 60 * 1000)))
|
|
.withSchedule(CronScheduleBuilder.cronSchedule(ce).withMisfireHandlingInstructionDoNothing())
|
|
.build();
|
|
break;
|
|
case "msg_time_type03"://本周最后一天
|
|
cron += ca.getActualMaximum(Calendar.DAY_OF_WEEK_IN_MONTH) + " ";
|
|
cron += (ca.get(Calendar.MONTH) + 1) + " ? *";
|
|
ce = new CronExpression(cron);
|
|
trigger = TriggerBuilder.newTrigger().forJob(jobName, MSG_GROUP_NAME).withIdentity(triggerName, MSG_GROUP_NAME).startAt(startTime).endAt(new Date(new Date().getTime() + (8 * 24 * 60 * 60 * 1000)))
|
|
.withSchedule(CronScheduleBuilder.cronSchedule(ce).withMisfireHandlingInstructionDoNothing())
|
|
.build();
|
|
break;
|
|
case "msg_time_type04"://本月最后一天
|
|
cron += ca.getActualMaximum(Calendar.DAY_OF_MONTH) + " ";
|
|
cron += (ca.get(Calendar.MONTH) + 1) + " ? *";
|
|
ce = new CronExpression(cron);
|
|
trigger = TriggerBuilder.newTrigger().forJob(jobName, MSG_GROUP_NAME).withIdentity(triggerName, MSG_GROUP_NAME).startAt(startTime).endAt(new Date(new Date().getTime() + (31 * 24 * 60 * 60 * 1000)))
|
|
.withSchedule(CronScheduleBuilder.cronSchedule(ce).withMisfireHandlingInstructionDoNothing())
|
|
.build();
|
|
break;
|
|
case "msg_time_type05"://延迟天
|
|
cron += (ca.get(Calendar.DAY_OF_MONTH) + Integer.parseInt(delayday)) + " ";
|
|
cron += (ca.get(Calendar.MONTH) + 1) + " ? *";
|
|
ce = new CronExpression(cron);
|
|
trigger = TriggerBuilder.newTrigger().forJob(jobName, MSG_GROUP_NAME).withIdentity(triggerName, MSG_GROUP_NAME).startAt(startTime).endAt(new Date(new Date().getTime() + (Integer.parseInt(delayday) * 24 * 60 * 60 * 1000)))
|
|
.withSchedule(CronScheduleBuilder.cronSchedule(ce).withMisfireHandlingInstructionDoNothing())
|
|
.build();
|
|
break;
|
|
}
|
|
sn.setAttributeValue("notice_time", DateUtil.getDateTime(ce.getNextValidTimeAfter(startTime)));
|
|
bomLSNT.saveObject(sn);
|
|
scheduler.scheduleJob(jobDetail, trigger);
|
|
}
|
|
inner_tran.commit();
|
|
}
|
|
tran.commit();
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
try {
|
|
if (tx != null) tx.rollback();
|
|
if (inner_tx != null) inner_tx.rollback();
|
|
} catch (JBOException e1) {
|
|
e1.printStackTrace();
|
|
}
|
|
}
|
|
}
|
|
|
|
public Scheduler getScheduler() throws Exception {
|
|
return StdSchedulerFactory.getDefaultScheduler();
|
|
}
|
|
} |