package com.tenwa.message; import com.alibaba.fastjson.JSONObject; import com.amarsoft.are.jbo.*; import com.tenwa.lease.util.SendMessageUtil; import jbo.com.tenwa.lease.comm.LB_SMS_NOTICE_TASKS; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.quartz.Job; import org.quartz.JobExecutionContext; import org.quartz.JobExecutionException; import java.text.SimpleDateFormat; import java.util.Date; public class JobSendMessage implements Job{ private Log logger = LogFactory.getLog(this.getClass()); private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); @Override public void execute(JobExecutionContext context) throws JobExecutionException { JBOTransaction tx = null; String smsnoticeid = null; try { tx= JBOFactory.createJBOTransaction(); smsnoticeid = context.getJobDetail().getJobDataMap().getString("smsnoticeid"); logger.info("short message running..... LB_SMS_NOTICE_TASKS[ID]: "+smsnoticeid); BizObjectManager bomNotice = JBOFactory.getBizObjectManager(LB_SMS_NOTICE_TASKS.CLASS_NAME,tx); BizObject notice = bomNotice.createQuery("id=:id").setParameter("id", smsnoticeid).getSingleResult(true); if(notice != null){ if(notice.getAttribute("send_flag").getInt() == 0){ String result = null; boolean flagErrStatus = true; try { result = SendMessageUtil.sendMessageByHttpClient(notice.getAttribute("phone_number").getString(), notice.getAttribute("sms_content").getString()); if(!result.matches("^[0-9].*")){ JSONObject jsonObject = JSONObject.parseObject(result); if(jsonObject.getIntValue("status")==100){ flagErrStatus = false; result = "全部成功"; }else{ result = getResultCodeInfoNew(jsonObject.getString("status")); } } } catch (Exception e) { logger.error("短信发送任务执行失败,LB_SMS_NOTICE_TASKS[ID] : "+smsnoticeid, e); result = "发送失败,发送遇到异常:" + e.getMessage(); } notice.setAttributeValue("sms_type", "IMMEDIATELY"); notice.setAttributeValue("send_result", result); notice.setAttributeValue("send_time", sdf.format(new Date())); notice.setAttributeValue("send_flag", flagErrStatus ? 0 : 1); bomNotice.saveObject(notice); } } tx.commit(); } catch (Exception e) { logger.error("short message error LB_SMS_NOTICE_TASKS[ID] : "+smsnoticeid ,e); try { tx.rollback(); } catch (JBOException e1) { e1.printStackTrace(); } } } public static String getResultCodeInfoNew(String code){ String codeMessage = ""; switch(code){ case "101": codeMessage = "参数错误"; break; case "102": codeMessage = "号码错误"; break; case "103": codeMessage = "当日余量不足"; break; case "104": codeMessage = "请求超时"; break; case "105": codeMessage = "用户余量不足"; break; case "106": codeMessage = "非法用户"; break; case "107": codeMessage = "提交号码超限"; break; case "111": codeMessage = "签名不合法"; break; case "120": codeMessage = "内容长度超长,请不要超过500个字"; break; case "121": codeMessage = "内容中有屏蔽词"; break; case "131": codeMessage = "IP非法"; break; default : codeMessage = code ; break; } return codeMessage; } public static String getResultCodeInfo(String code){ String codeMessage = ""; switch(code){ case "-1": codeMessage = "签权失败"; break; case "-2": codeMessage = "未检索到被叫号码"; break; case "-3": codeMessage = "被叫号码过多"; break; case "-4": codeMessage = "内容未签名"; break; case "-5": codeMessage = "内容过长"; break; case "-6": codeMessage = "余额不足"; break; case "-7": codeMessage = "暂停发送"; break; case "-8": codeMessage = "保留"; break; case "-9": codeMessage = "定时发送时间格式错误"; break; case "-10": codeMessage = "下发内容为空"; break; case "-11": codeMessage = "账户无效"; break; case "-12": codeMessage = "Ip地址非法"; break; case "-13": codeMessage = "操作频率快"; break; case "-14": codeMessage = "操作失败"; break; case "-15": codeMessage = "拓展码无效"; break; case "-16": codeMessage = "取消定时,seqid错误"; break; case "-18": codeMessage = "暂留"; break; case "-19": codeMessage = "未开通上行"; break; case "-20": codeMessage = "暂留"; break; case "-21": codeMessage = "包含屏蔽词"; break; default : codeMessage = code; break; } if("0".equals(codeMessage.substring(0,1))){ codeMessage = "操作成功"; } return codeMessage; } }