参数是快递单号,调用快递信息查询接口,返回快递详细信息。
数据持久化操作通过单独的现成进行。
/**
* 快递信息查询方法
* @param expressCode 快递单号
* @param waybillService 快递基本信息服务类
* @param waybillDescService 快递详情服务类
* @param openId 微信用户编号
* @return
* @author liuwy
*/
public DataMessage search(String expressCode, String openId){
DataMessage dm = new DataMessage();
if (expressCode == null || expressCode == "") {
dm.setResult(1);
return dm;
}
//调用快递查询接口,查快递信息
AgentKdapiService agentKdapiService = (AgentKdapiService)ToolSpring.getBean("agentKdapiService");
TokenService tokenService = (TokenService)ToolSpring.getBean("tokenService");
String result = agentKdapiService.query(expressCode);
JSONObject json = JSONObject.fromObject(result);
// 物流状态
int status = Integer.parseInt(json.getString("status"));
// 物流信息
String data = json.getString("data");
// 存放快递公司信息
Carrier carrier = new Carrier();
if (json.containsKey("carrierCode")) {
carrier.setCarrierCode(json.getString("carrierCode"));
List<Carrier> allc = CarriersConstant.ALL_CARRIERS2;
if (allc!=null && !allc.isEmpty()) {
for (Carrier c : allc) {
if (c.getCarrierCode().equals(carrier.getCarrierCode())) {
carrier.setCarrierId(c.getCarrierId());
carrier.setCarrierName(c.getCarrierName());
break;
}
}
}
}
// 查不到数据的情况下返回result为1
if (data.equals("[]")) {
dm.setResult(1);
} else {
dm.setResult(0);
}
dm.setMessage(null);
dm.setData(data);
// 线程执行增加删除修改
Waybill add = new Waybill();
add.setUserId(0L);
add.setServiceStatus(status); // 物流状态
add.setServiceStatusDesc(data);// 物流信息
add.setCarrierId(carrier.getCarrierId());// 快递公司id
add.setCarrierName(carrier.getCarrierName());// 快递公司名称
add.setCarrierCode(carrier.getCarrierCode());// 快递公司code
add.setThirdId(openId);// 微信查询人id
add.setWaybillType(3);// 类型
add.setExpressCode(expressCode);// 快递单号
// 调用线程池执行增加,需改操作
WaybillService waybillService = (WaybillService)ToolSpring.getBean("waybillService");
WaybillDescService waybillDescService = (WaybillDescService)ToolSpring.getBean("waybillDescService");
try {
waybill_ex.execute(new WaybillRunnable(add, waybillService,
waybillDescService));
} catch (Exception e) {
e.printStackTrace();
}
//发送模板消息
JSONArray waybillDescArray = JSONArray.fromObject(data);
String firstDesc = null;
if(status >1){
firstDesc = JSONObject.fromObject(waybillDescArray.get(0)).getString("time")+" "+JSONObject.fromObject(waybillDescArray.get(0)).getString("content");
}else{
firstDesc = "您的运单暂时没有最新物流信息";
}
String jsonMsg = TemplateUtil.makeRouteMessage(openId, "Xv5UHt9vkr1d3zXUa4OJyo8uTUzXmp_SsJRzYDGNZ38", "http://yst.tunnel.qydev.com/wechat/waybill/toSearchDetails.action?expressCode="+expressCode, "#ff6600", carrier.getCarrierName(), expressCode, firstDesc+"");
Token t = tokenService.queryById(1L);
TemplateUtil.sendTemplateMessage(t.getWxKey(), jsonMsg);
return dm;
}