apzl_leasing/src_tenwa/com/tenwa/messagesend/cache/MessageSendClient.java
2019-12-24 14:07:07 +08:00

156 lines
5.6 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package com.tenwa.messagesend.cache;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import com.alibaba.fastjson.JSON;
import com.amarsoft.are.jbo.BizObject;
import com.amarsoft.are.jbo.BizObjectManager;
import com.amarsoft.are.jbo.JBOException;
import com.amarsoft.are.jbo.JBOFactory;
import com.amarsoft.are.jbo.JBOTransaction;
import com.amarsoft.ars.utils.DateUtils;
import jbo.com.tenwa.lease.comm.T_MESSAGE_NOTICE_TASKS;
/**
* 云帆 短信发送
* @author yangdm
*
*/
public class MessageSendClient {
//修改为您的apikey.apikey可在官网http://www.yunpian.com)登录后获取
private static String apikey = "c49617079abdf64ec1c09e5800ff2213";
//智能匹配模板发送接口的http地址
private static String URI_SEND_SMS = "https://sms.yunpian.com/v2/sms/single_send.json";
//编码格式。发送编码格式统一用UTF-8
private static String ENCODING = "UTF-8";
private String ids;
public String sendSms(String apikey, String text,
String mobile) throws IOException {
Map < String, String > params = new HashMap < String, String > ();
params.put("apikey", apikey);
params.put("text", text);
params.put("mobile", mobile);
return post(URI_SEND_SMS, params);
}
/**
* 基于HttpClient 4.3的通用POST方法
*
* @param url 提交的URL
* @param paramsMap 提交<参数,值>Map
* @return 提交响应
*/
public String post(String url, Map < String, String > paramsMap) {
CloseableHttpClient client = HttpClients.createDefault();
String responseText = "";
CloseableHttpResponse response = null;
try {
HttpPost method = new HttpPost(url);
if (paramsMap != null) {
List < NameValuePair > paramList = new ArrayList <
NameValuePair > ();
for (Map.Entry < String, String > param: paramsMap.entrySet()) {
NameValuePair pair = new BasicNameValuePair(param.getKey(),
param.getValue());
paramList.add(pair);
}
method.setEntity(new UrlEncodedFormEntity(paramList,
ENCODING));
}
response = client.execute(method);
HttpEntity entity = response.getEntity();
if (entity != null) {
responseText = EntityUtils.toString(entity, ENCODING);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
response.close();
} catch (Exception e) {
e.printStackTrace();
}
}
return responseText;
}
@SuppressWarnings("unchecked")
public String sendMultiMsg(JBOTransaction tx) throws JBOException, IOException{
String[] idArry = ids.split("@");
StringBuilder parm = new StringBuilder();
for (int i = 0; i < idArry.length; i++) {
if(parm.length() == 0)
parm.append("'"+idArry[i]+"'");
else
parm.append(",'"+idArry[i]+"'");
}
BizObjectManager bm = JBOFactory.getFactory().getManager(T_MESSAGE_NOTICE_TASKS.CLASS_NAME);
List<BizObject> msgList = bm.createQuery("O.id in ("+parm.toString()+")").getResultList(false);
for (BizObject bo : msgList) {
String mobile = bo.getAttribute("MESSAGE_NUMBER").toString().trim();
String text = bo.getAttribute("MESSAGE_CONTENT").toString().trim();
Map<String, Object> resMap = (Map<String, Object>) JSON.parse(sendSms(apikey, text, mobile));// msgSingleMt(sendNumber, sendContext);
//如果发送成功,更新短信发送状态
if(resMap.containsKey("code") && "0".equals(String.valueOf(resMap.get("code")))){
bm.createQuery("update O set SEND_STATUS = 'M',SEND_DATE = '"+DateUtils.getToday()+"' where O.id=:id").setParameter("id", bo.getAttribute("id").toString()).executeUpdate();
}
}
return "success";
}
/**
* 自动发送短信
* @param id
* @return
* @throws JBOException
*/
@SuppressWarnings("unchecked")
public String sendMultiMsg(String id) throws JBOException{
BizObjectManager bm = JBOFactory.getFactory().getManager(T_MESSAGE_NOTICE_TASKS.CLASS_NAME);
List<BizObject> msgList = bm.createQuery("O.id in ("+id+")").getResultList(false);
for (BizObject bo : msgList) {
String sendNumber = bo.getAttribute("MESSAGE_NUMBER").toString().trim();
String sendContext = bo.getAttribute("MESSAGE_CONTENT").toString().trim();
HashMap<String, String> resMap = new HashMap<String, String>();//msgSingleMt(sendNumber, sendContext);
//如果发送成功,更新短信发送状态
if(resMap.containsKey("mterrcode") && "000".equals(resMap.get("mterrcode")) && resMap.containsKey("mtstat") && "ACCEPTD".equals(resMap.get("mtstat"))){
bm.createQuery("update O set SEND_STATUS = 'M',SEND_DATE = '"+DateUtils.getToday()+"' where O.id=:id").setParameter("id", bo.getAttribute("id").toString()).executeUpdate();
}
System.out.println(resMap);
}
return "success";
}
public String getIds() {
return ids;
}
public void setIds(String ids) {
this.ids = ids;
}
}