代码更新
This commit is contained in:
369
mes/hd/nladmin-system/src/test/java/org/nl/day01/Test.java
Normal file
369
mes/hd/nladmin-system/src/test/java/org/nl/day01/Test.java
Normal file
@@ -0,0 +1,369 @@
|
||||
/*
|
||||
package org.nl.day01;
|
||||
|
||||
import com.alibaba.fastjson.JSONArray;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import org.nl.exception.WDKException;
|
||||
import org.nl.wql.WQL;
|
||||
import org.nl.wql.core.bean.WQLObject;
|
||||
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.*;
|
||||
|
||||
public class Test {
|
||||
|
||||
*/
|
||||
/**
|
||||
* 根据输入天数day,获取距离今天day天数的字符串日期
|
||||
*
|
||||
* @param day
|
||||
* 距离今天天数
|
||||
* @return 如day为0 则输出2019-06-09当天日期
|
||||
*//*
|
||||
|
||||
public static String getDateStr(int day) {
|
||||
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
|
||||
Date todayDate = new Date();
|
||||
long beforeTime = (todayDate.getTime() / 1000) - 60 * 60 * 24 * day;
|
||||
todayDate.setTime(beforeTime * 1000);
|
||||
String beforeDate = formatter.format(todayDate);
|
||||
return beforeDate;
|
||||
}
|
||||
*/
|
||||
/**
|
||||
* 更新交货单
|
||||
*
|
||||
* @param ctx
|
||||
* @param account
|
||||
*//*
|
||||
|
||||
@WDKTransaction
|
||||
public void updateDeliveryOrderInfo(String code) {
|
||||
Date date1 = new Date();
|
||||
System.out.println("updateDeliveryOrderInfo发送数据-----------------");
|
||||
String url = "http://192.168.0.82:8000/sap/bc/srt/rfc/sap/zfg_ecc_to_zhwl/800/zfg_ecc_to_zhwl/binding";
|
||||
String method = "ZfmGetDnZjwl";
|
||||
HashMap<String, String> form = new HashMap<>();
|
||||
// 获得前两天日期
|
||||
JSONObject ZfmGetSoResponse = null;
|
||||
if (code.equals("")) {
|
||||
form.put("IErdatFrm", getDateStr(1));
|
||||
form.put("IErdatTo",getDateStr(0));
|
||||
form.put("IVbeln", ""); // 非必填字段
|
||||
} else {
|
||||
form.put("IErdatFrm", "");
|
||||
form.put("IErdatTo", "");
|
||||
form.put("IVbeln", code); // 非必填字段
|
||||
}
|
||||
try {
|
||||
JSONObject ret = WebServiceUtil.process(url, method, form);
|
||||
System.out.println("getXML发送数据-----------------" + ret.toString());
|
||||
JSONObject Envelope = ret.optJSONObject("soap-env:Envelope");
|
||||
JSONObject Body = Envelope.optJSONObject("soap-env:Body");
|
||||
ZfmGetSoResponse = Body.optJSONObject("n0:ZfmGetDnZjwlResponse");
|
||||
String check = ZfmGetSoResponse.optString("OResultcode");
|
||||
if ("1005".equals(check)) {
|
||||
System.out.println(ZfmGetSoResponse.optString("OResultmsg"));
|
||||
return;
|
||||
} else if (!"1000".equals(check)) {
|
||||
throw new WDKException(ZfmGetSoResponse.optString("OResultmsg"));
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
JSONObject TZtsalesorder = ZfmGetSoResponse.optJSONObject("TZtdn");
|
||||
JSONArray item = TZtsalesorder.optJSONArray("item");
|
||||
|
||||
if (item.size() == 0 || TZtsalesorder == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
// 发货通知单(未拆分)主表
|
||||
WQLObject wqlMst = WQLObject.getWQLObject("SAL_OD_DeliveryNotice");
|
||||
// 发货通知单明细(未拆分)表
|
||||
WQLObject wqlDtl = WQLObject.getWQLObject("SAL_OD_DeliveryNoticeDtl");
|
||||
// 交货单接口表
|
||||
WQLObject wqld = WQLObject.getWQLObject("SAP_OD_Delivery");
|
||||
|
||||
// 字典表单据类型
|
||||
WQLObject wqlDic = WQLObject.getWQLObject("PF_PB_SysDicInfo");
|
||||
JSONArray diclist = wqlDic.query("sysdic_type ='TC_SALE_SENDBIZTYPE' and sysdic_para2 !='' ")
|
||||
.getResultJSONArray(0);
|
||||
HashMap<String, String> DicMap = new HashMap<>();
|
||||
for (int i = 0; i < diclist.size(); i++) {
|
||||
JSONObject nowdiclist = diclist.getJSONObject(i);
|
||||
DicMap.put(nowdiclist.optString("sysdic_para2"), nowdiclist.optString("sysdic_code"));
|
||||
}
|
||||
//查询当前仓库
|
||||
JSONArray jaStor = WQL.getWO("QSTOST_MTATERUTIL_01")
|
||||
.addParam("flag", "6")
|
||||
.process()
|
||||
.getResultJSONArray(0);
|
||||
|
||||
HashMap<String, JSONObject> mapStor = new HashMap<>();
|
||||
for(int i=0;i<jaStor.size();i++){
|
||||
JSONObject jo = jaStor.optJSONObject(i);
|
||||
String stor_code = jo.optString("stor_code");
|
||||
if(!mapStor.containsKey(stor_code)){
|
||||
mapStor.put(stor_code, jo);
|
||||
}
|
||||
}
|
||||
for (int i = 0; i < item.size(); i++) {
|
||||
JSONObject nowitem = item.getJSONObject(i);
|
||||
//当前订单类型非智能车间
|
||||
if (!DicMap.containsKey(nowitem.optString("Lfart"))) {
|
||||
continue;
|
||||
}
|
||||
//当前仓库非智能车间
|
||||
if(!nowitem.optString("Lgort","").equals("") &&!mapStor.containsKey(nowitem.optString("Lgort",""))){
|
||||
continue;
|
||||
}
|
||||
// SAP_OD_Delivery
|
||||
JSONObject joto = wqld
|
||||
.query("Vbeln =? and Posnr = ?",
|
||||
new String[] { nowitem.optString("Vbeln"), nowitem.optString("Posnr") }, "")
|
||||
.uniqueResult(0);
|
||||
if (joto == null) {
|
||||
JSONObject jo = item.getJSONObject(i);
|
||||
String id = WDK.getUUID();
|
||||
jo.put("id", id);
|
||||
jo.put("create_time", WDK.getDateTime());
|
||||
String newStr = jo.optString("Matnr").replaceAll("^(0+)", "");
|
||||
jo.put("Matnr", newStr);
|
||||
jo.put("sysflag", 0);
|
||||
wqld.insert(jo);
|
||||
}
|
||||
}
|
||||
|
||||
// SAP_OD_Delivery
|
||||
// 查询所有未导入的单据
|
||||
JSONArray ja = WQL.getWO("QSTOST_MTATERUTIL_01")
|
||||
.addParam("flag", "5")
|
||||
.process()
|
||||
.getResultJSONArray(0);
|
||||
System.out.println(ja.size());
|
||||
HashMap<String, JSONObject> mapMst = new HashMap<String, JSONObject>();
|
||||
for(int i=0;i<ja.size();i++){
|
||||
JSONObject jo = ja.optJSONObject(i);
|
||||
String vbeln = jo.optString("vbeln");
|
||||
//判断是否已存在该主表
|
||||
JSONObject joM = wqlMst.query("bill_code = '"+vbeln+"'").uniqueResult(0);
|
||||
//不存在
|
||||
if(joM == null || joM.isEmpty()){
|
||||
jo.put("detail_count", "0");
|
||||
jo.put("deliverynotice_uuid", WDK.getUUID());
|
||||
|
||||
jo.put("bill_type_scode", "22");
|
||||
jo.put("biztype_scode", DicMap.get(jo.optString("lfart")));
|
||||
jo.put("trans_type_uuid", "18"); // TODO 普通销售发货
|
||||
jo.put("bill_code", jo.optString("vbeln"));
|
||||
jo.put("bill_date", jo.optString("erdatps"));
|
||||
if(jo.optString("saleorg_uuid","").equals("")){
|
||||
jo.put("saleorg_uuid","94E889A7BDC54BCF8EAA2C46F96090E2");
|
||||
jo.put("saleorg_code",jo.optString("vkorg",""));
|
||||
jo.put("saleorg_name","铜管道事业部");
|
||||
}
|
||||
if(jo.optString("optor_uuid","").equals("")){
|
||||
jo.put("optor_uuid","7782D11F01D549999063379A9C2A3D86");
|
||||
}
|
||||
String lgort = jo.optString("lgort","");
|
||||
if(mapStor.containsKey(lgort)){
|
||||
JSONObject joStor = mapStor.get(lgort);
|
||||
jo.put("org_uuid", joStor.optString("org_uuid"));
|
||||
jo.put("org_code", joStor.optString("org_code"));
|
||||
jo.put("org_name", joStor.optString("org_name"));
|
||||
jo.put("store_point_uuid",joStor.optString("store_point_uuid"));
|
||||
jo.put("store_point_name", joStor.optString("store_point_name"));
|
||||
}else{
|
||||
jo.put("org_uuid", "94E889A7BDC54BCF8EAA2C46F96090E2");
|
||||
jo.put("org_code", "2012");
|
||||
jo.put("org_name", "铜管道事业部");
|
||||
jo.put("store_point_uuid","E864B2CBEA4F49C7B465704A50CFFFC8");// TODO
|
||||
jo.put("store_point_name", "铜管仓库库点");// TODO 库点
|
||||
}
|
||||
jo.put("currency_scode", "01");// TODO 默认人民币
|
||||
jo.put("exchange_rate", "0");
|
||||
jo.put("sale_type_scode", "01");// TODO 销售类型 默认 多种经营
|
||||
jo.put("create_mode_scode", "02");// 02外部接口产生
|
||||
jo.put("bill_status", "30");
|
||||
jo.put("print_count", "0");
|
||||
jo.put("delivery_address", jo.optString("streetdz"));
|
||||
|
||||
jo.put("source_bill_type", jo.optString("bill_type_scode",""));
|
||||
jo.put("source_bill_uuid", jo.optString("order_uuid",""));
|
||||
|
||||
jo.put("input_optuuid", "7782D11F01D549999063379A9C2A3D86");// TODO 接口制单人
|
||||
jo.put("input_optname", "订单管理员");
|
||||
jo.put("input_time", WDK.getDateTime());
|
||||
jo.put("syscreatoruuid", "7782D11F01D549999063379A9C2A3D86"); // 创建人
|
||||
jo.put("syscreatedate", WDK.getDateTime());
|
||||
jo.put("sysdeptuuid", "65D9D062414346CE9BFB39DC44803DAA"); // TODO 部门
|
||||
jo.put("syscompanyuuid", "94E889A7BDC54BCF8EAA2C46F96090E2");// TODO 公司
|
||||
jo.put("sysisdelete", "0");
|
||||
jo.put("is_allbillbom", "0");
|
||||
jo.put("is_workshop", "0");
|
||||
jo.put("is_insert", "1");
|
||||
}else{//存在
|
||||
jo = joM;
|
||||
jo.put("is_insert", "0");
|
||||
jo.put("is_workshop", "0");
|
||||
jo.put("sysupdatoruuid", "7782D11F01D549999063379A9C2A3D86");
|
||||
jo.put("sysupdatedate", WDK.getDateTime());
|
||||
}
|
||||
mapMst.put(vbeln, jo);
|
||||
}
|
||||
|
||||
//KG
|
||||
JSONObject job = WQLObject.getWQLObject("MD_PB_MeasureUnit").query("unit_name = ?", new String[] { "千克" }, "")
|
||||
.uniqueResult(0);// 计量单位
|
||||
|
||||
JSONArray materRows = WQL.getWO("QSTOST_MTATERUTIL_01")
|
||||
.addParam("flag", "8")
|
||||
.process()
|
||||
.getResultJSONArray(0);
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.append("'");
|
||||
for (int i = 0; i < materRows.size(); i++) {
|
||||
JSONObject jrow = materRows.getJSONObject(i);
|
||||
String matnr = jrow.optString("matnr").replaceAll("^(0+)", "");
|
||||
sb.append(matnr);
|
||||
sb.append("','");
|
||||
}
|
||||
sb.append("'");
|
||||
String roleIds = sb.toString();
|
||||
//获取当前mater集合
|
||||
JSONArray materja = WQL.getWO("QSTOST_MTATERUTIL_02")
|
||||
.addParam("flag", "6")
|
||||
.addParam("ids", roleIds)
|
||||
.process()
|
||||
.getResultJSONArray(0);
|
||||
HashMap<String, String> mapMater = new HashMap<>();
|
||||
for(int i=0;i<materja.size();i++){
|
||||
JSONObject jo = materja.optJSONObject(i);
|
||||
String material_code = jo.optString("material_code");
|
||||
if(!mapMater.containsKey(material_code)){
|
||||
mapMater.put(material_code, jo.optString("material_uuid"));
|
||||
}
|
||||
}
|
||||
//明细表
|
||||
JSONArray dtlRows = WQL.getWO("QSTOST_MTATERUTIL_01")
|
||||
.addParam("flag", "7")
|
||||
.process()
|
||||
.getResultJSONArray(0);
|
||||
StringBuilder sbdtl = new StringBuilder();
|
||||
sbdtl.append("'");
|
||||
for(int i=0;i<dtlRows.size();i++){
|
||||
JSONObject jo = dtlRows.optJSONObject(i);
|
||||
String vbeln = jo.optString("vbeln");
|
||||
if(!mapMst.containsKey(vbeln)){
|
||||
continue;
|
||||
}
|
||||
JSONObject joMst = mapMst.get(vbeln);
|
||||
|
||||
String uuid = WDK.getUUID();
|
||||
jo.put("deliverynoticedtl_uuid", uuid);
|
||||
jo.put("deliverynotice_uuid", joMst.optString("deliverynotice_uuid"));
|
||||
jo.put("seq_no", jo.optString("posnr"));
|
||||
|
||||
String newStr = jo.optString("matnr").replaceAll("^(0+)", "");
|
||||
if(!mapMater.containsKey(newStr)){
|
||||
continue;
|
||||
}
|
||||
joMst.put("detail_count", (joMst.optInt("detail_count")+1)+"");
|
||||
|
||||
mapMst.put(vbeln, joMst);
|
||||
jo.put("material_uuid", mapMater.get(newStr)); // TODO 假数据
|
||||
jo.put("material_code", newStr);
|
||||
jo.put("material_name", jo.optString("arktx"));
|
||||
|
||||
if (jo.optString("unit_uuid").equals("")) {
|
||||
throw new WDKException("单据编号" + vbeln + ",物料编码" + newStr + "计量单位异常,未在单位表查询到该单位:"
|
||||
+ jo.optString("vrkme"));
|
||||
}
|
||||
jo.put("base_unit_uuid", jo.optString("unit_uuid"));
|
||||
jo.put("base_unit_qty", "0");
|
||||
jo.put("aid_unit_uuid", jo.optString("unit_uuid"));
|
||||
jo.put("aid_unit_qty", "0");
|
||||
|
||||
jo.put("delivery_qty", jo.optString("lfimg"));
|
||||
// 发货和出库相关量
|
||||
jo.put("total_shippedqty", "0");
|
||||
jo.put("total_unshippedqty", jo.optString("lfimg"));
|
||||
jo.put("total_outqty", "0");
|
||||
jo.put("total_unoutqty", jo.optString("lfimg"));
|
||||
jo.put("total_dumpqty", "0");
|
||||
jo.put("total_undumpqty", jo.optString("lfimg"));
|
||||
|
||||
jo.put("tax_price", "0");
|
||||
jo.put("tax_rate", "0");
|
||||
jo.put("tax_amt", "0");
|
||||
jo.put("withtax_amt", "0");
|
||||
jo.put("notax_price", "0");
|
||||
jo.put("nowith_taxamt", "0");
|
||||
if (jo.optString("meins").equals("KG")) {
|
||||
jo.put("weight_unit_uuid", job.optString("measure_unit_uuid"));// 基本计量单位
|
||||
jo.put("weight_unit_name", job.optString("unit_name"));
|
||||
}
|
||||
|
||||
jo.put("net_weight", jo.optString("ntgew"));
|
||||
|
||||
jo.put("status", "30");
|
||||
|
||||
String lgort = jo.optString("lgort","");
|
||||
if(mapStor.containsKey(lgort)){
|
||||
JSONObject joStor = mapStor.get(lgort);
|
||||
jo.put("apply_org_uuid", joStor.optString("org_uuid"));
|
||||
jo.put("apply_org_name", joStor.optString("org_name"));
|
||||
}else{
|
||||
jo.put("apply_org_uuid", "94E889A7BDC54BCF8EAA2C46F96090E2");
|
||||
jo.put("apply_org_name", "铜管道事业部");
|
||||
}
|
||||
|
||||
jo.put("delivery_date", jo.optString("wadatist"));
|
||||
jo.put("ship_date", jo.optString("wadatist"));
|
||||
jo.put("is_notcontqty", "1");
|
||||
jo.put("is_notcontdate", "1");
|
||||
|
||||
// 销售订单单据号 行项目
|
||||
jo.put("ext_id", jo.optString("vgbel"));
|
||||
jo.put("ext_seqno", jo.optString("vgpos"));
|
||||
|
||||
jo.put("base_billdtl_uuid", uuid);
|
||||
jo.put("base_bill_type", jo.optString("lfart"));
|
||||
jo.put("base_bill_code", jo.optString("vbeln"));
|
||||
jo.put("is_workshop", "0");
|
||||
wqlDtl.insert(jo);
|
||||
sbdtl.append(jo.optString("id"));
|
||||
sbdtl.append("','");
|
||||
|
||||
}
|
||||
sbdtl.append("'");
|
||||
// 创建对象存取明细
|
||||
HashMap<String, String> mapCount2 = new HashMap<>();
|
||||
mapCount2.put("sysflag", "1");
|
||||
// 更新中间主表
|
||||
wqld.update(mapCount2, "id in("+sbdtl.toString()+")");
|
||||
|
||||
Set<Map.Entry<String, JSONObject>> entrySet = mapMst.entrySet();
|
||||
Iterator<Map.Entry<String, JSONObject>> iter = entrySet.iterator();
|
||||
//新增主表
|
||||
while (iter.hasNext())
|
||||
{
|
||||
Map.Entry<String, JSONObject> entry = iter.next();
|
||||
JSONObject jo = entry.getValue();
|
||||
if(jo.optInt("detail_count")==0){
|
||||
continue;
|
||||
}
|
||||
if(jo.optString("is_insert").equals("1")){
|
||||
wqlMst.insert(entry.getValue());
|
||||
}else if(jo.optString("is_insert").equals("0")){
|
||||
wqlMst.update(entry.getValue());
|
||||
}
|
||||
}
|
||||
Date date2 = new Date();
|
||||
System.out.println("共计秒:"+(date2.getTime()-date1.getTime())/1000);
|
||||
System.out.println("销售发货通知数据同步成功!");
|
||||
}
|
||||
|
||||
}
|
||||
*/
|
||||
62
mes/hd/nladmin-system/src/test/java/org/nl/day01/Test2.java
Normal file
62
mes/hd/nladmin-system/src/test/java/org/nl/day01/Test2.java
Normal file
@@ -0,0 +1,62 @@
|
||||
package org.nl.day01;
|
||||
|
||||
import net.sf.json.JSONObject;
|
||||
|
||||
import java.util.HashMap;
|
||||
|
||||
public class Test2 {
|
||||
public static void main(String[] args) throws Exception {
|
||||
|
||||
String url = "http://192.168.0.82:8000/sap/bc/srt/rfc/sap/z_sd0002_send_mat_md/800/z_sd0002_send_mat_md/binding";
|
||||
String method = "ZSd0002SendMatMd"; //物料接口
|
||||
HashMap<String, String> form = new HashMap<String, String>();
|
||||
form.put("MatnrFr", "24018347S");
|
||||
form.put("MatnrTo", "24018347S");
|
||||
//非必填字段
|
||||
JSONObject ret = WebServiceUtil.process(url, method, form);
|
||||
System.out.println("getXML返回数据-----------------" + ret.toString());
|
||||
JSONObject Envelope = ret.optJSONObject("soap-env:Envelope");
|
||||
JSONObject Body = Envelope.optJSONObject("soap-env:Body");
|
||||
JSONObject ZfmGetSoResponse = Body.optJSONObject("n0:ZSd0002SendMatMdResponse");
|
||||
JSONObject TZtsalesorder = ZfmGetSoResponse.optJSONObject("TZtsalesorder");
|
||||
/* JSONArray item = TZtsalesorder.optJSONArray("item");
|
||||
System.out.println("例子:查询结果字段OResultmsg-----------------" + ZfmGetSoResponse.optString("OResultmsg"));
|
||||
System.out.println("例子:查询结果数组size-----------------" + item.size());*/
|
||||
|
||||
/*String url = "http://192.168.0.82:8000/sap/bc/srt/rfc/sap/zfg_ecc_to_zhwl/800/zfg_ecc_to_zhwl/binding";
|
||||
String method="ZfmGetSo"; //获取销售订单
|
||||
HashMap<String,String> form = new HashMap<String,String>();
|
||||
form.put("IErdatFrm", "2022-05-01");
|
||||
form.put("IErdatTo", "2022-05-08");
|
||||
//非必填字段
|
||||
form.put("IVbeln", "");
|
||||
JSONObject ret = WebServiceUtil.process(url,method,form);
|
||||
System.out.println("getXML发送数据-----------------"+ret.toString());
|
||||
JSONObject Envelope = ret.getJSONObject("soap-env:Envelope");
|
||||
JSONObject Body = Envelope.getJSONObject("soap-env:Body");
|
||||
JSONObject ZfmGetSoResponse = Body.getJSONObject("n0:ZfmGetSoResponse");
|
||||
JSONObject TZtsalesorder = ZfmGetSoResponse.getJSONObject("TZtsalesorder");
|
||||
JSONArray item = TZtsalesorder.optJSONArray("item");
|
||||
System.out.println("例子:查询结果字段OResultmsg-----------------"+ZfmGetSoResponse.optString("OResultmsg"));
|
||||
System.out.println("例子:查询结果数组size-----------------"+item.size());*/
|
||||
|
||||
|
||||
/* String url = "http://192.168.0.82:8000/sap/bc/srt/rfc/sap/zfg_ecc_to_zhwl/800/zfg_ecc_to_zhwl/binding";
|
||||
String method = "ZfmGetDnZjwl"; //获取交货单
|
||||
HashMap<String, String> form = new HashMap<String, String>();
|
||||
form.put("IErdatFrm", "2019-05-01");
|
||||
form.put("IErdatTo", "2019-05-08");
|
||||
//非必填字段
|
||||
form.put("IVbeln", "");
|
||||
//非必填字段
|
||||
JSONObject ret = WebServiceUtil.process(url, method, form);
|
||||
System.out.println("getXML发送数据-----------------" + ret.toString());
|
||||
JSONObject Envelope = ret.optJSONObject("soap-env:Envelope");
|
||||
JSONObject Body = Envelope.optJSONObject("soap-env:Body");
|
||||
JSONObject ZfmGetDnZjwlResponse = Body.getJSONObject("n0:ZfmGetDnZjwlResponse");
|
||||
JSONObject TZtdn = ZfmGetDnZjwlResponse.getJSONObject("TZtdn");
|
||||
JSONArray item = TZtdn.getJSONArray("item");
|
||||
System.out.println("例子:查询结果字段OResultmsg-----------------" + ZfmGetDnZjwlResponse.getString("OResultmsg"));
|
||||
System.out.println("例子:查询结果数组size-----------------" + item.size());*/
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package org.nl.day01;
|
||||
|
||||
public class Test4 {
|
||||
public static void main(String[] args) {
|
||||
System.out.println();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,190 @@
|
||||
package org.nl.day01;
|
||||
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import cn.hutool.json.XML;
|
||||
import net.sf.json.JSONArray;
|
||||
import net.sf.json.JSONObject;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.OutputStream;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.URL;
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
|
||||
/**
|
||||
* 后端调用第三方webservice接口
|
||||
*
|
||||
* @author zds 2018-12-27 16:33:50
|
||||
*/
|
||||
public class WebServiceUtil {
|
||||
/**
|
||||
* @param url 第三方系统提供webservice的接口url
|
||||
* @param method 第三方系统提供webservice对应的方法名
|
||||
* @param form 第三方系统提供webservice对应的方法请求参数
|
||||
* @return 第三方系统返回的JSONObject调用结果
|
||||
* @throws Exception 工具类一般不处理具体异常,抛出由调用方处理,否则容易形成黑箱
|
||||
*/
|
||||
public static JSONObject process(String url, String method, HashMap<String, String> form) throws Exception {
|
||||
|
||||
|
||||
//第一步:创建服务地址
|
||||
URL netUrl = new URL(url);
|
||||
|
||||
//第二步:打开一个通向服务地址的连接
|
||||
HttpURLConnection connection = (HttpURLConnection) netUrl.openConnection();
|
||||
|
||||
//第三步:设置参数
|
||||
connection.setRequestMethod("POST");
|
||||
//设置超时时间
|
||||
connection.setConnectTimeout(50000);
|
||||
|
||||
//3.2设置数据格式:content-type
|
||||
connection.setRequestProperty("content-type", "text/xml;charset=utf-8");
|
||||
|
||||
//3.3设置输入输出,因为默认新创建的connection没有读写权限,
|
||||
connection.setDoInput(true);
|
||||
|
||||
connection.setDoOutput(true);
|
||||
|
||||
|
||||
//第四步:组织SOAP数据,发送请求
|
||||
String soapXML = getXML(form, method);
|
||||
|
||||
//将信息以流的方式发送出去
|
||||
OutputStream os = connection.getOutputStream();
|
||||
|
||||
os.write(soapXML.getBytes());
|
||||
|
||||
//第五步:接收服务端响应,打印
|
||||
|
||||
int responseCode = connection.getResponseCode();
|
||||
|
||||
//返回结果
|
||||
String resultStr = null;
|
||||
if (200 == responseCode) {//表示服务端响应成功
|
||||
|
||||
//获取当前连接请求返回的数据流
|
||||
InputStream is = connection.getInputStream();
|
||||
|
||||
InputStreamReader isr = new InputStreamReader(is, "utf-8");
|
||||
|
||||
BufferedReader br = new BufferedReader(isr);
|
||||
|
||||
StringBuilder sb = new StringBuilder();
|
||||
|
||||
String temp = null;
|
||||
|
||||
while (null != (temp = br.readLine())) {
|
||||
sb.append(temp);
|
||||
}
|
||||
|
||||
//打印结果
|
||||
String ret = sb.toString();
|
||||
if (ret.startsWith("<?"))
|
||||
ret = ret.replaceAll("\\<\\?.+\\?\\>", "<?xml version='1.0' encoding='UTF-8'?>");
|
||||
else
|
||||
ret = (new StringBuilder("<?xml version='1.0' encoding='UTF-8'?>")).append(ret).toString();
|
||||
|
||||
resultStr = XML.toJSONObject(ret).toString();
|
||||
|
||||
|
||||
is.close();
|
||||
isr.close();
|
||||
br.close();
|
||||
//关闭连接
|
||||
connection.disconnect();
|
||||
}
|
||||
os.close();
|
||||
return ObjectUtil.isEmpty(resultStr) ? null : JSONObject.fromObject(resultStr);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获得要发送的webservice的xml形式的参数
|
||||
*
|
||||
* @param form查询条件
|
||||
* @return
|
||||
*/
|
||||
private static String getXML(HashMap<String, String> form, String method) {
|
||||
StringBuffer sb = new StringBuffer();
|
||||
sb.append("<?xml version=\"1.0\" encoding=\"utf-8\"?> ");
|
||||
sb.append("<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:urn=\"urn:sap-com:document:sap:soap:functions:mc-style\">");
|
||||
sb.append("<soapenv:Header/>");
|
||||
sb.append("<soapenv:Body>");
|
||||
sb.append("<urn:" + method + ">");
|
||||
if (method.contains("ZSd0002SendMatMd"))
|
||||
sb.append("<In>");
|
||||
// 设置请求参数
|
||||
for (Iterator<String> it = form.keySet().iterator(); it.hasNext(); ) {
|
||||
String key = it.next();
|
||||
String value = form.get(key);
|
||||
sb.append(" <" + key + ">" + value + "</" + key + ">");
|
||||
}
|
||||
if (method.contains("ZSd0002SendMatMd"))
|
||||
sb.append("</In>");
|
||||
sb.append("</urn:" + method + ">");
|
||||
sb.append("</soapenv:Body>");
|
||||
sb.append("</soapenv:Envelope>");
|
||||
System.out.println("getXML发送数据-----------------" + sb.toString());
|
||||
return sb.toString();
|
||||
|
||||
}
|
||||
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
|
||||
String url = "http://192.168.0.82:8000/sap/bc/srt/rfc/sap/z_sd0002_send_mat_md/800/z_sd0002_send_mat_md/binding";
|
||||
String method = "ZSd0002SendMatMd"; //物料接口
|
||||
HashMap<String, String> form = new HashMap<String, String>();
|
||||
form.put("MatnrFr", "24018862S");
|
||||
form.put("MatnrTo", "24018862S");
|
||||
//非必填字段
|
||||
JSONObject ret = WebServiceUtil.process(url, method, form);
|
||||
System.out.println("getXML返回数据-----------------" + ret.toString());
|
||||
JSONObject Envelope = ret.optJSONObject("soap-env:Envelope");
|
||||
JSONObject Body = Envelope.optJSONObject("soap-env:Body");
|
||||
JSONObject ZfmGetSoResponse = Body.optJSONObject("n0:ZSd0002SendMatMdResponse");
|
||||
JSONObject TZtsalesorder = ZfmGetSoResponse.optJSONObject("TZtsalesorder");
|
||||
JSONArray item = TZtsalesorder.optJSONArray("item");
|
||||
System.out.println("例子:查询结果字段OResultmsg-----------------" + ZfmGetSoResponse.optString("OResultmsg"));
|
||||
System.out.println("例子:查询结果数组size-----------------" + item.size());
|
||||
|
||||
/*String url = "http://192.168.0.82:8000/sap/bc/srt/rfc/sap/zfg_ecc_to_zhwl/800/zfg_ecc_to_zhwl/binding";
|
||||
String method="ZfmGetSo"; //获取销售订单
|
||||
HashMap<String,String> form = new HashMap<String,String>();
|
||||
form.put("IErdatFrm", "2022-05-01");
|
||||
form.put("IErdatTo", "2022-05-08");
|
||||
//非必填字段
|
||||
form.put("IVbeln", "");
|
||||
JSONObject ret = WebServiceUtil.process(url,method,form);
|
||||
System.out.println("getXML发送数据-----------------"+ret.toString());
|
||||
JSONObject Envelope = ret.getJSONObject("soap-env:Envelope");
|
||||
JSONObject Body = Envelope.getJSONObject("soap-env:Body");
|
||||
JSONObject ZfmGetSoResponse = Body.getJSONObject("n0:ZfmGetSoResponse");
|
||||
JSONObject TZtsalesorder = ZfmGetSoResponse.getJSONObject("TZtsalesorder");
|
||||
JSONArray item = TZtsalesorder.optJSONArray("item");
|
||||
System.out.println("例子:查询结果字段OResultmsg-----------------"+ZfmGetSoResponse.optString("OResultmsg"));
|
||||
System.out.println("例子:查询结果数组size-----------------"+item.size());*/
|
||||
|
||||
|
||||
/* String url = "http://192.168.0.82:8000/sap/bc/srt/rfc/sap/zfg_ecc_to_zhwl/800/zfg_ecc_to_zhwl/binding";
|
||||
String method = "ZfmGetDnZjwl"; //获取交货单
|
||||
HashMap<String, String> form = new HashMap<String, String>();
|
||||
form.put("IErdatFrm", "2019-05-01");
|
||||
form.put("IErdatTo", "2019-05-08");
|
||||
//非必填字段
|
||||
form.put("IVbeln", "");
|
||||
//非必填字段
|
||||
JSONObject ret = WebServiceUtil.process(url, method, form);
|
||||
System.out.println("getXML发送数据-----------------" + ret.toString());
|
||||
JSONObject Envelope = ret.optJSONObject("soap-env:Envelope");
|
||||
JSONObject Body = Envelope.optJSONObject("soap-env:Body");
|
||||
JSONObject ZfmGetDnZjwlResponse = Body.getJSONObject("n0:ZfmGetDnZjwlResponse");
|
||||
JSONObject TZtdn = ZfmGetDnZjwlResponse.getJSONObject("TZtdn");
|
||||
JSONArray item = TZtdn.getJSONArray("item");
|
||||
System.out.println("例子:查询结果字段OResultmsg-----------------" + ZfmGetDnZjwlResponse.getString("OResultmsg"));
|
||||
System.out.println("例子:查询结果数组size-----------------" + item.size());*/
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user