122 lines
4.2 KiB
Java

package com.tenwa.lease.app.message;
import java.util.List;
import jbo.com.tenwa.lease.comm.LB_MESSAGE_CONFIG;
import jbo.com.tenwa.lease.comm.LB_SMS_NOTICE_TASKS;
import net.sf.json.JSONObject;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.log4j.Logger;
import com.amarsoft.are.jbo.BizObject;
import com.amarsoft.are.jbo.BizObjectManager;
import com.amarsoft.are.jbo.JBOFactory;
import com.amarsoft.are.jbo.JBOTransaction;
import com.amarsoft.are.util.StringFunction;
import com.tenwa.lease.util.SendMessageUtil;
public class SmsController {
private String ids;
private Logger logger = Logger.getLogger(this.getClass());
public String getIds() {
return ids;
}
public void setIds(String ids) {
this.ids = ids;
}
public String sendMessage(JBOTransaction tx){
try{
String[] smsID = ids.split("@");
BizObjectManager bomLSNT = JBOFactory.getBizObjectManager(LB_SMS_NOTICE_TASKS.CLASS_NAME,tx);
for(String id:smsID){
BizObject boLSNT = bomLSNT.createQuery("ID=:ID").setParameter("ID", id).getSingleResult(true);
String result = null;
try {
result = SendMessageUtil.sendMessageByHttpClient(boLSNT.getAttribute("PHONE_NUMBER").getString(), boLSNT.getAttribute("SMS_CONTENT").getString());
} catch (Exception e) {
logger.error("短信发送任务执行失败。", e);
result = "发送失败,发送遇到异常:" + e.getMessage();
}
boLSNT.setAttributeValue("SMS_TYPE","IMMEDIATELY");
boLSNT.setAttributeValue("SEND_RESULT",SmsController.getResultCodeInfo(result));
boLSNT.setAttributeValue("SEND_TIME",StringFunction.getTodayNow());
boLSNT.setAttributeValue("SEND_FLAG",1);
bomLSNT.saveObject(boLSNT);
}
}catch(Exception e){
e.printStackTrace();
return "ERROR";
}
return "SUCCESS";
}
public String selectBalance(JBOTransaction tx){
try {
//获取数据库中短信配置的相关参数
List<BizObject> messageConfigList = JBOFactory.getBizObjectManager(LB_MESSAGE_CONFIG.CLASS_NAME, tx).createQuery("1=1").getResultList(false);
BizObject messageConfig = messageConfigList.get(0);
//用户名
String sendUsername = messageConfig.getAttribute("SENDER_").getString();
//密码
String sendPassword = messageConfig.getAttribute("SENDER_PASSWORD_").getString();
HttpClient httpClient = new HttpClient();
//发送url
String url = "http://api.app2e.com/getBalance.api.php?pwd="+sendPassword+"&username="+sendUsername;
GetMethod getMethod = new GetMethod(url);
int statusCode = httpClient.executeMethod(getMethod);
System.out.println("状态码:"+statusCode);
String result = "";
if (statusCode == HttpStatus.SC_OK) {
result = getMethod.getResponseBodyAsString();
}else{
result = statusCode + "";
}
JSONObject jsonObject=JSONObject.fromObject(result);
String balance=jsonObject.get("balance").toString();
getMethod.releaseConnection();
return balance;
} catch (Exception e) {
e.printStackTrace();
return "查询失败";
}
}
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;
}
}