package com.uas.eis.service.Impl; import com.fasterxml.jackson.core.JsonParser; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.node.TextNode; import com.uas.eis.core.support.TokenProperties; import com.uas.eis.dao.BaseDao; import com.uas.eis.dao.SqlRowList; import com.uas.eis.sdk.converter.ObjectToMapConverter; import com.uas.eis.sdk.dto.*; import com.uas.eis.service.ERPService; import com.uas.eis.task.httpclient.EDIX5JsonHttpClient; import com.uas.eis.utils.SqlUtil; import com.uas.eis.utils.X5StringUtils; import com.uas.eis.utils.XMEDIConfiguration; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import javax.annotation.Resource; import java.net.URLDecoder; import java.nio.charset.StandardCharsets; import java.text.SimpleDateFormat; import java.util.*; /** * @author koul * @email koul@usoftchina.com * @date 2021-12-06 18:27 */ @Service public class ERPServiceImpl implements ERPService { private final Logger logger = LoggerFactory.getLogger(this.getClass()); private static Map tokenConfig = TokenProperties.getAllProperty(); @Autowired private BaseDao baseDao; @Resource(name = "xmediConfiguration") private XMEDIConfiguration xmediConfiguration; @Transactional @Override public Map savePurchase(String data) { Map result = new HashMap<>(); Map header_result = new HashMap<>(); String resultSuccessMsg = ""; String resultErrorMsg = ""; String sign = null; String body = null; try { ObjectMapper objectMapper = new ObjectMapper(); // String decode = URLDecoder.decode(data, StandardCharsets.UTF_8.toString()); String jsonString = X5StringUtils.decodeBase64(data); Map map = objectMapper.readValue(jsonString, Map.class); if(map.size()>0 && map.get("header")!=null ){ Map header_map = (Map) map.get("header"); sign = header_map.get("sign") == null ? "" : header_map.get("sign").toString(); } boolean checkSign = X5StringUtils.checkSign(sign, "test", jsonString, "218A05A252469F5A62B0B3204F669E1D"); if(checkSign || true){ try { if(map.get("body")!=null){ body = map.get("body") == null ? "" : map.get("body").toString(); PurchaseMain purchaseMain = objectMapper.readValue(body, PurchaseMain.class); String purord_num = purchaseMain.getPURORD_NUM(); resultSuccessMsg="采购订单:"+purord_num+"处理成功"; resultErrorMsg="采购订单:"+purord_num+"处理失败,"; String operation_type = purchaseMain.getOPERATION_TYPE(); if("I".equals(operation_type)){ int count = baseDao.getCount("select * from PurchaseMainXM where PURORD_NUM='" + purord_num + "'"); if (count == 0){ //主表 Map purchaseMain_map = ObjectToMapConverter.objectToMap(purchaseMain); purchaseMain_map.remove("ET_ITEM"); purchaseMain_map.put("PMXID", 0); baseDao.execute(SqlUtil.getInsertSql(purchaseMain_map,"PurchaseMainXM","PMXID")); Object PMXID = baseDao.getFieldDataByCondition("PurchaseMainXM", "PMXID", "GUID='" + purchaseMain_map.get("GUID")+"'"); //从表 List et_item = purchaseMain.getET_ITEM(); for (PurchaseItem purchaseItem:et_item) { //List ItemSqls = new ArrayList<>(); Map purchaseItem_map = ObjectToMapConverter.objectToMap(purchaseItem); purchaseItem_map.remove("ET_ITEM_AMOUNT"); purchaseItem_map.put("PIXID",0); purchaseItem_map.put("PIXPMXID",PMXID); String insertItemSql = SqlUtil.getInsertSql(purchaseItem_map, "PURCHASEITEMXM", "PIXID"); //ItemSqls.add(insertItemSql); baseDao.execute(insertItemSql); Object PIXID = baseDao.getFieldDataByCondition("PURCHASEITEMXM left join PurchaseMainXM on PIXPMXID=PMXID", "PIXID", "GUID='" + purchaseMain_map.get("GUID")+"' and PURORD_ITEM_NUM='"+purchaseItem_map.get("PURORD_ITEM_NUM")+"'"); List et_item_amount = purchaseItem.getET_ITEM_AMOUNT(); List ItemAmountSqls = new ArrayList<>(); for (PurchaseItemAmount purchaseItemAmount:et_item_amount) { Map purchaseItemAmount_map = ObjectToMapConverter.objectToMap(purchaseItemAmount); purchaseItemAmount_map.put("PIMAID",0); purchaseItemAmount_map.put("PIMAPMXID",PMXID); purchaseItemAmount_map.put("PIMAPIXID",PIXID); String insertItemAmountSql = SqlUtil.getInsertSql(purchaseItemAmount_map, "PURCHASEITEMAMOUNTXM", "PIMAID"); ItemAmountSqls.add(insertItemAmountSql); } baseDao.execute(ItemAmountSqls); } }else{ //更新 header_result.put("code", "400"); header_result.put("desc", resultErrorMsg+"重复推送,采购单号已存在!"); result.put("header", header_result); return result; } }else{ int count = baseDao.getCount("select * from PurchaseMainXM where PURORD_NUM='" + purord_num + "'"); if (count == 0){ //更新 header_result.put("code", "400"); header_result.put("desc", resultErrorMsg+"采购单号不存在,不能执行更新操作!"); result.put("header", header_result); return result; }else{ //更新 header_result.put("code", "200"); header_result.put("desc", resultSuccessMsg+"执行更新操作中!"); result.put("header", header_result); return result; } } header_result.put("code", "200"); header_result.put("desc", resultSuccessMsg); result.put("header", header_result); }else{ header_result.put("code", "400"); header_result.put("desc", resultErrorMsg+"body数据不存在!"); result.put("header", header_result); return result; } }catch (Exception e){ logger.info("同步收料单异常信息:"+body); header_result.put("code", "400"); header_result.put("desc", resultErrorMsg+"解析body失败!"); result.put("header", header_result); return result; } }else{ header_result.put("code", "400"); header_result.put("desc", resultErrorMsg+"身份验证失败!"); result.put("header", header_result); return result; } }catch (Exception e){ logger.info("同步收料单异常信息:"+e.getMessage()); header_result.put("code", "400"); header_result.put("desc", resultErrorMsg+"未知错误!"); result.put("header", header_result); return result; } return result; } @Override public void confirmPurchase(String id) { Map data = new HashMap<>(); UUID uuid = UUID.randomUUID(); ConfirmPurchase confirmPurchase = new ConfirmPurchase(); confirmPurchase.setSYSTEMID("SRM"); confirmPurchase.setGUID(uuid.toString().toUpperCase()); SqlRowList rs = baseDao.queryForRowSet("select pu_code from purchase where pu_id=" + id); // 获取当前日期 Date currentDate = new Date(); // 创建SimpleDateFormat对象,指定格式 SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd"); SimpleDateFormat hhmmss = new SimpleDateFormat("yyyyMMddHHMMSS"); if (rs.next()){ String pu_code = rs.getGeneralString("pu_code"); confirmPurchase.setPURORD_NUM(pu_code); confirmPurchase.setCONFIRM_STATUS("B"); confirmPurchase.setCONFIRM_DATE(sdf.format(currentDate)); confirmPurchase.setCONFIRM_TIME(hhmmss.format(currentDate)); confirmPurchase.setSUPPLIER_CODE("108207"); data.put("data", confirmPurchase); EDIX5JsonHttpClient edix5JsonHttpClient = new EDIX5JsonHttpClient(xmediConfiguration.getX5appid(), xmediConfiguration.getX5appkey(), xmediConfiguration.getX5confirmUrl()); edix5JsonHttpClient.post(data, xmediConfiguration, baseDao, "ConfirmPurchase"); } } @Override public Map closePurchase(String data) { Map result = new HashMap<>(); Map header_result = new HashMap<>(); Map body_result = new HashMap<>(); Map it_return_result = new HashMap<>(); String resultSuccessMsg = ""; String resultErrorMsg = ""; String sign = null; String body = null; try { ObjectMapper objectMapper = new ObjectMapper(); //String decode = URLDecoder.decode(data, StandardCharsets.UTF_8.toString()); String jsonString = X5StringUtils.decodeBase64(data); Map map = objectMapper.readValue(jsonString, Map.class); if(map.size()>0 && map.get("header")!=null ){ Map header_map = (Map) map.get("header"); sign = header_map.get("sign") == null ? "" : header_map.get("sign").toString(); } boolean checkSign = X5StringUtils.checkSign(sign, "test", jsonString, "218A05A252469F5A62B0B3204F669E1D"); if(checkSign || true){ try { if(map.get("body")!=null){ String item_guid = null; String purord_item_num = null; body = map.get("body") == null ? "" : map.get("body").toString(); PurchaseCloseMain purchaseCloseMain = objectMapper.readValue(body, PurchaseCloseMain.class); String purord_num = purchaseCloseMain.getPURORD_NUM(); List et_close_item1 = purchaseCloseMain.getET_CLOSE_ITEM(); if(et_close_item1.size()>0){ purord_item_num = et_close_item1.get(0).getPURORD_ITEM_NUM(); } String guid = purchaseCloseMain.getGUID(); resultSuccessMsg="采购订单:"+purord_num+",行号:"+purord_item_num+"执行成功,具体结果请查看IT_RETURN反馈明细表"; resultErrorMsg="采购订单:"+purord_num+",行号:"+purord_item_num+"执行失败,"; it_return_result.put("ITEM_GUID",guid); it_return_result.put("PURORD_NUM",purord_num); it_return_result.put("PURORD_ITEM_NUM",purord_item_num); it_return_result.put("MSG_TYPE","S"); it_return_result.put("MESSAGE","处理成功"); body_result.put("IT_RETURN",it_return_result); int count = baseDao.getCount("select * from PurchaseMainXM where PURORD_NUM='" + purord_num + "'"); if (count > 0){ //主表 Map purchaseCloseMain_map = ObjectToMapConverter.objectToMap(purchaseCloseMain); purchaseCloseMain_map.remove("ET_CLOSE_ITEM"); purchaseCloseMain_map.put("pcxid", 0); baseDao.execute(SqlUtil.getInsertSql(purchaseCloseMain_map,"purchaseCloseMainXM","pcxid")); Object pcxid = baseDao.getFieldDataByCondition("purchaseCloseMainXM", "pcxid", "GUID='" + purchaseCloseMain_map.get("GUID")+"'"); //从表 List et_close_item = purchaseCloseMain.getET_CLOSE_ITEM(); for (PurchaseCloseItem purchaseCloseItem:et_close_item) { //List ItemSqls = new ArrayList<>(); Map purchaseCloseItem_map = ObjectToMapConverter.objectToMap(purchaseCloseItem); purchaseCloseItem_map.remove("ET_ITEM_AMOUNT"); purchaseCloseItem_map.put("pcixid",0); purchaseCloseItem_map.put("pcixpcxid",pcxid); String insertItemSql = SqlUtil.getInsertSql(purchaseCloseItem_map, "PurchaseCloseItemXM", "pcixid"); //ItemSqls.add(insertItemSql); baseDao.execute(insertItemSql); item_guid = purchaseCloseItem.getITEM_GUID(); } int DEAL_STATUS = baseDao.getCount("select * from PurchaseCloseItemXM where DEAL_STATUS=-1 and ITEM_GUID='" + item_guid + "'"); if(DEAL_STATUS == 0){ //关闭订单逻辑 }else{ header_result.put("code", "400"); header_result.put("desc", resultErrorMsg+"重复推送!"); result.put("header", header_result); return result; } }else{ header_result.put("code", "400"); header_result.put("desc", resultErrorMsg+"采购单号不存在!"); result.put("header", header_result); return result; } header_result.put("code", "200"); header_result.put("desc", resultSuccessMsg); result.put("header", header_result); result.put("body",body_result); }else{ header_result.put("code", "400"); header_result.put("desc", resultErrorMsg+"body数据不存在!"); result.put("header", header_result); return result; } }catch (Exception e){ logger.info("同步收料单异常信息:"+e); logger.info("同步收料单异常信息:"+body); header_result.put("code", "400"); header_result.put("desc", resultErrorMsg+"解析body失败!"); result.put("header", header_result); return result; } }else{ header_result.put("code", "400"); header_result.put("desc", resultErrorMsg+"身份验证失败!"); result.put("header", header_result); return result; } }catch (Exception e){ logger.info("同步收料单异常信息:"+e.getMessage()); header_result.put("code", "400"); header_result.put("desc", resultErrorMsg+"未知错误!"); result.put("header", header_result); return result; } return result; } @Override public Map deliInstruction(String data) { Map result = new HashMap<>(); Map header_result = new HashMap<>(); String resultSuccessMsg = ""; String resultErrorMsg = ""; String sign = null; String body = null; try { ObjectMapper objectMapper = new ObjectMapper(); String decode = URLDecoder.decode(data, StandardCharsets.UTF_8.toString()); String jsonString = X5StringUtils.decodeBase64(decode); Map map = objectMapper.readValue(jsonString, Map.class); if(map.size()>0 && map.get("header")!=null ){ Map header_map = (Map) map.get("header"); sign = header_map.get("sign") == null ? "" : header_map.get("sign").toString(); } boolean checkSign = X5StringUtils.checkSign(sign, "test", jsonString, "218A05A252469F5A62B0B3204F669E1D"); if(checkSign || true){ try { if(map.get("body")!=null){ body = map.get("body") == null ? "" : map.get("body").toString(); DeliInstruction deliInstruction = objectMapper.readValue(body, DeliInstruction.class); String deli_instru_no = deliInstruction.getDELI_INSTRU_NO(); String guid = deliInstruction.getGUID(); resultSuccessMsg="送货指令:"+deli_instru_no+"处理成功"; resultErrorMsg="送货指令:"+deli_instru_no+"处理失败,"; String operation_type = deliInstruction.getOPERATION_TYPE(); if("I".equals(operation_type) || "U".equals(operation_type)){ int count = baseDao.getCount("select * from deliInstructionXM where GUID='" + guid + "'"); if (count == 0){ //主表 Map deliInstruction_map = ObjectToMapConverter.objectToMap(deliInstruction); deliInstruction_map.remove("ET_ITEM"); deliInstruction_map.put("DI_ID", 0); baseDao.execute(SqlUtil.getInsertSql(deliInstruction_map,"deliInstructionXM","DI_ID")); Object DI_ID = baseDao.getFieldDataByCondition("deliInstructionXM", "DI_ID", "GUID='" + guid+"'"); //从表 List et_item = deliInstruction.getET_ITEM(); for (DeliInstructionItem deliInstructionItem:et_item) { //List ItemSqls = new ArrayList<>(); Map deliInstructionItem_map = ObjectToMapConverter.objectToMap(deliInstructionItem); deliInstructionItem_map.remove("ET_ITEM_AMOUNT"); deliInstructionItem_map.put("DID_ID",0); deliInstructionItem_map.put("DID_DIID",DI_ID); String insertItemSql = SqlUtil.getInsertSql(deliInstructionItem_map, "deliInstructionDetailXM", "DID_ID"); baseDao.execute(insertItemSql); } }else{ //更新 header_result.put("code", "400"); header_result.put("desc", resultErrorMsg+"具体报错原因:重复推送,GUID已存在!"); result.put("header", header_result); return result; } }else{ int count = baseDao.getCount("select * from deliInstructionXM where GUID='" + guid + "'"); if (count == 0){ //更新 header_result.put("code", "400"); header_result.put("desc", resultErrorMsg+"具体报错原因:采购单号不存在,不能执行更新操作!"); result.put("header", header_result); return result; }else{ //更新 header_result.put("code", "200"); header_result.put("desc", resultSuccessMsg+"执行更新操作中!"); result.put("header", header_result); return result; } } header_result.put("code", "200"); header_result.put("desc", resultSuccessMsg); result.put("header", header_result); }else{ header_result.put("code", "400"); header_result.put("desc", resultErrorMsg+"具体报错原因:body数据不存在!"); result.put("header", header_result); return result; } }catch (Exception e){ logger.info("同步收料单异常信息:"+body); header_result.put("code", "400"); header_result.put("desc", resultErrorMsg+"具体报错原因:解析body失败!"); result.put("header", header_result); return result; } }else{ header_result.put("code", "400"); header_result.put("desc", resultErrorMsg+"具体报错原因:身份验证失败!"); result.put("header", header_result); return result; } }catch (Exception e){ logger.info("同步收料单异常信息:"+e.getMessage()); header_result.put("code", "400"); header_result.put("desc", resultErrorMsg+"具体报错原因:JSON解析失败!!"); result.put("header", header_result); return result; } return result; } public static String unescapeJsonWithJackson(String jsonString) { try { ObjectMapper mapper = new ObjectMapper(); // 配置解析器允许特殊字符 mapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_CONTROL_CHARS, true); // 方法1:使用readTree解析后转为字符串 String unescaped = mapper.readTree(jsonString).toString(); return unescaped; } catch (JsonProcessingException e) { // 如果解析失败,可能是字符串本身,尝试使用TextNode try { ObjectMapper mapper = new ObjectMapper(); TextNode textNode = mapper.readValue("\"" + jsonString + "\"", TextNode.class); return textNode.asText(); } catch (Exception ex) { return jsonString; } } catch (Exception e) { return jsonString; } } }