You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
245 lines
11 KiB
245 lines
11 KiB
package com.fr.plugin.http.handler; |
|
|
|
|
|
import com.fanruan.api.log.LogKit; |
|
import com.fr.decision.authority.data.User; |
|
import com.fr.decision.fun.impl.BaseHttpHandler; |
|
import com.fr.decision.webservice.v10.user.UserService; |
|
import com.fr.json.JSONArray; |
|
import com.fr.json.JSONObject; |
|
import com.fr.log.FineLoggerFactory; |
|
import com.fr.plugin.utils.ConnectSAPServer; |
|
import com.fr.plugin.utils.DBUtils; |
|
import com.fr.third.springframework.web.bind.annotation.RequestMethod; |
|
import com.fr.web.utils.WebUtils; |
|
import com.sap.conn.jco.*; |
|
|
|
import javax.servlet.http.Cookie; |
|
import javax.servlet.http.HttpServletRequest; |
|
import javax.servlet.http.HttpServletResponse; |
|
import java.io.BufferedReader; |
|
|
|
public class ALLSapApi1Handler extends BaseHttpHandler { |
|
@Override |
|
public RequestMethod getMethod() { |
|
return null; |
|
} |
|
|
|
@Override |
|
public String getPath() { |
|
return "/sapApi"; |
|
} |
|
|
|
@Override |
|
public boolean isPublic() { |
|
return true; |
|
} |
|
|
|
@Override |
|
public void handle(HttpServletRequest req, HttpServletResponse res) throws Exception { |
|
String requestBody = getRequestBody(req); |
|
LogKit.info("收到的JSON:{}", requestBody); |
|
JSONArray array = new JSONArray(requestBody); |
|
DBUtils dbUtils = new DBUtils(); |
|
//check exist |
|
int resq = array.size(); |
|
// JSONArray reqJSONArr = new JSONArray(); |
|
// for (int i = 0; i < resq; i++) { |
|
// JSONObject jsonObject = array.getJSONObject(i); |
|
// String matnr = jsonObject.getString("MATNR"); |
|
// boolean exist = dbUtils.checkExist("select count(1) c from TPV_OMP_MATERIAL_DATA where TPV_MODEL=?", matnr); |
|
// if (!exist) { |
|
// reqJSONArr.add(jsonObject); |
|
// } |
|
// } |
|
LogKit.info("发送到接口的JSON为:{}", array); |
|
// GUANJIAConfig config = GUANJIAConfig.getInstance(); |
|
// String apiUrl = config.getApiUrl(); |
|
// String resp = HttpKit.postJSON(apiUrl, reqJSONArr, new HashMap<>()); |
|
// LogKit.info("请求:{},响应:{}", apiUrl, resp); |
|
// JSONArray jsonArray = new JSONArray(resp); |
|
JSONObject retJSON = send2SAP(array); |
|
JSONArray jsonArray = retJSON.getJSONArray("arr1"); |
|
LogKit.info("SAP返回的为:{}", jsonArray); |
|
String user ="admin"; |
|
try { |
|
User user1 = UserService.getInstance().getUserByRequestCookie(req); |
|
user=user1.getUserName(); |
|
}catch (Exception e){ |
|
} |
|
//更新TPV_OMP_MATERIAL_DATA表 |
|
dbUtils.saveJSONArr(jsonArray, user ); |
|
//更新状态表 |
|
JSONArray arr2 = retJSON.getJSONArray("arr2"); |
|
int size1 = arr2.size(); |
|
for (int i = 0; i < size1; i++) { |
|
JSONObject o = arr2.getJSONObject(i); |
|
dbUtils.saveReJSON(o, user ); |
|
} |
|
WebUtils.printAsString(res, "入库成功"); |
|
} |
|
|
|
|
|
private JSONObject send2SAP(JSONArray inArr) throws JCoException { |
|
JCoDestination jCoDestination = ConnectSAPServer.Connect(); |
|
|
|
JCoFunction function = jCoDestination.getRepository().getFunction("ZRFC_OMP_DATAMASTER"); |
|
if (function == null) |
|
throw new RuntimeException("Function not found in SAP."); |
|
// JCoParameterList tableParameterList = function.getTableParameterList(); |
|
JCoParameterList importParameterList = function.getImportParameterList(); |
|
JCoTable input_tab = importParameterList.getTable("IT_INPUT"); |
|
// JCoTable input_tab = tableParameterList.getTable("IT_INPUT"); |
|
int size = inArr.size(); |
|
for (int i = 0; i < size; i++) { |
|
input_tab.appendRow(); |
|
JSONObject jsonObject = inArr.getJSONObject(i); |
|
input_tab.setValue("MATNR", jsonObject.getString("MATNR")); |
|
input_tab.setValue("WERKS", jsonObject.getString("WERKS")); |
|
} |
|
function.execute(jCoDestination); |
|
JCoTable returnTable = function.getTableParameterList().getTable("IT_OUTPUT"); |
|
int numRows = returnTable.getNumRows(); |
|
LogKit.info("接收到条数:{}", numRows); |
|
JSONArray jsonArray = new JSONArray(); |
|
if (numRows > 0) { |
|
for (int i = 0; i < numRows; i++) { |
|
JSONObject jsonObject = new JSONObject(); |
|
for (JCoField jCoField : returnTable) { |
|
LogKit.info("jcoFile:{} ,value:{}", jCoField.getName(), jCoField.getValue()); |
|
} |
|
jsonObject.put("MATNR", toString2(returnTable.getValue("MATNR"))); |
|
jsonObject.put("BISMT",toString2( returnTable.getValue("BISMT"))); |
|
jsonObject.put("MAKTX", toString2(returnTable.getValue("MAKTX"))); |
|
jsonObject.put("BRGEW",toString2( returnTable.getValue("BRGEW"))); |
|
jsonObject.put("BTGEW",toString2( returnTable.getValue("NTGEW"))); |
|
jsonObject.put("VOLUM",toString2( returnTable.getValue("VOLUM"))); |
|
jsonObject.put("SPART",toString2( returnTable.getValue("SPART"))); |
|
jsonObject.put("MATKL",toString2( returnTable.getValue("MATKL"))); |
|
jsonObject.put("MEINS",toString2( returnTable.getValue("MEINS"))); |
|
jsonObject.put("NORMT",toString2( returnTable.getValue("NORMT"))); |
|
jsonObject.put("FERTH",toString2( returnTable.getValue("FERTH"))); |
|
jsonObject.put("Z_BRDTYPE", toString2(returnTable.getValue("Z_BRDTYPE"))); |
|
jsonObject.put("Z_BRDID",toString2( returnTable.getValue("Z_BRDID"))); |
|
jsonObject.put("Z_REGION", toString2(returnTable.getValue("Z_REGION"))); |
|
jsonObject.put("Z_CMNFTYPE",toString2( returnTable.getValue("Z_CMNFTYPE"))); |
|
jsonObject.put("Z_PRDLINE",toString2( returnTable.getValue("Z_PRDLINE"))); |
|
jsonObject.put("Z_STYLEID",toString2( returnTable.getValue("Z_STYLEID"))); |
|
jsonObject.put("Z_SRCTYPE",toString2( returnTable.getValue("Z_SRCTYPE"))); |
|
jsonObject.put("Z_ASSM",toString2( returnTable.getValue("Z_ASSM"))); |
|
jsonObject.put("Z_WPNL",toString2( returnTable.getValue("Z_WPNL"))); |
|
jsonObject.put("Z_SIZE", toString2(returnTable.getValue("Z_SIZE"))); |
|
jsonObject.put("Z_RATIO", toString2(returnTable.getValue("Z_RATIO"))); |
|
jsonObject.put("Z_PNLLIGHT",toString2( returnTable.getValue("Z_PNLLIGHT"))); |
|
jsonObject.put("Z_PNLSUPTYPE", toString2(returnTable.getValue("Z_PNLSUPTYPE"))); |
|
jsonObject.put("Z_PNLBRDID",toString2( returnTable.getValue("Z_PNLBRDID"))); |
|
jsonObject.put("Z_FUNTYPE", toString2(returnTable.getValue("Z_FUNTYPE"))); |
|
jsonObject.put("Z_SGNTYPE", toString2(returnTable.getValue("Z_SGNTYPE"))); |
|
jsonObject.put("Z_CUSTNAME",toString2( returnTable.getValue("Z_CUSTNAME"))); |
|
jsonObject.put("Z_PARSE", toString2(returnTable.getValue("Z_PARSE"))); |
|
jsonObject.put("Z_BG", toString2(returnTable.getValue("Z_BG"))); |
|
jsonObject.put("Z_SMARTTV", toString2(returnTable.getValue("Z_SMARTTV"))); |
|
jsonObject.put("Z_DOBLY", toString2(returnTable.getValue("Z_DOBLY"))); |
|
jsonObject.put("GEBSQ", toString2(returnTable.getValue("GEBSQ"))); |
|
jsonObject.put("HSCODE",toString2( returnTable.getValue("HSCODE"))); |
|
jsonObject.put("CMODEL", toString2(returnTable.getValue("CMODEL"))); |
|
jsonObject.put("MATNR_LCM", toString2(returnTable.getValue("MATNR_LCM"))); |
|
jsonObject.put("MAKTX_LCM", toString2(returnTable.getValue("MAKTX_LCM"))); |
|
jsonArray.add(jsonObject); |
|
returnTable.nextRow(); |
|
} |
|
} |
|
|
|
JCoTable extable = function.getTableParameterList().getTable("ET_RETURN"); |
|
int exCount = extable.getNumRows(); |
|
LogKit.info("接收到extable 条数:{}", exCount); |
|
JSONArray jsonexArray = new JSONArray(); |
|
if (exCount > 0) { |
|
for (int i = 0; i < exCount; i++) { |
|
JSONObject jsonObject = new JSONObject(); |
|
for (JCoField jCoField : extable) { |
|
LogKit.info("jcoFile:{} ,value:{}", jCoField.getName(), jCoField.getValue()); |
|
// jsonObject.put(jCoField.getName(), jCoField.getString()); |
|
} |
|
jsonObject.put("MATNR", toString2(extable.getValue("MATNR"))); |
|
jsonObject.put("WERKS", toString2(extable.getValue("WERKS"))); |
|
jsonObject.put("RETURN", toString2(extable.getValue("RETURN"))); |
|
jsonexArray.add(jsonObject); |
|
extable.nextRow(); |
|
} |
|
} |
|
JSONObject returnObj = new JSONObject(); |
|
returnObj.put("arr1", jsonArray); |
|
returnObj.put("arr2", jsonexArray); |
|
LogKit.info("两个表数据:{}",returnObj); |
|
return returnObj; |
|
} |
|
|
|
private String toString2(Object obj) { |
|
if (obj != null) { |
|
return obj.toString(); |
|
} |
|
return ""; |
|
} |
|
|
|
public static String getRequestBody(HttpServletRequest req) { |
|
StringBuffer sb = new StringBuffer(); |
|
String line = null; |
|
try { |
|
BufferedReader reader = req.getReader(); |
|
while ((line = reader.readLine()) != null) |
|
sb.append(line); |
|
} catch (Exception e) { |
|
} |
|
|
|
return sb.toString(); |
|
} |
|
|
|
/** |
|
* 根据key获取cookie |
|
* |
|
* @param req |
|
* @return |
|
*/ |
|
public static String getCookieByKey(HttpServletRequest req, String key) { |
|
Cookie[] cookies = req.getCookies(); |
|
String cookie = ""; |
|
|
|
if (cookies == null || cookies.length <= 0) { |
|
return ""; |
|
} |
|
|
|
for (int i = 0; i < cookies.length; i++) { |
|
Cookie item = cookies[i]; |
|
if (item.getName().equalsIgnoreCase(key)) { |
|
cookie = item.getValue(); |
|
} |
|
} |
|
|
|
LogKit.info("cookie:" + cookie); |
|
|
|
return cookie; |
|
} |
|
|
|
private String deleteCookieByName(HttpServletRequest request, HttpServletResponse response, String name) { |
|
Cookie[] cookies = request.getCookies(); |
|
if (null == cookies) { |
|
FineLoggerFactory.getLogger().debug("没有cookie"); |
|
} else { |
|
for (Cookie cookie : cookies) { |
|
if (cookie.getName().equals(name)) { |
|
String cookieValue = cookie.getValue(); |
|
//设置值为null |
|
cookie.setValue(null); |
|
//立即销毁cookie |
|
cookie.setMaxAge(0); |
|
cookie.setPath("/"); |
|
FineLoggerFactory.getLogger().debug("被删除的cookie名字为:{}", cookie.getName(), cookieValue); |
|
response.addCookie(cookie); |
|
return cookieValue; |
|
} |
|
} |
|
} |
|
return ""; |
|
} |
|
}
|
|
|