| 
									
										
										
										
											2025-10-27 07:30:18 +00:00
										 |  |  |  | package tqq9.lc123.cloud.app.plugin.operate.pm;
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-10-30 09:34:30 +00:00
										 |  |  |  | import com.alibaba.fastjson.JSON;
 | 
					
						
							| 
									
										
										
										
											2025-10-27 07:30:18 +00:00
										 |  |  |  | import com.alibaba.fastjson.JSONArray;
 | 
					
						
							|  |  |  |  | import com.alibaba.fastjson.JSONObject;
 | 
					
						
							| 
									
										
										
										
											2025-10-30 09:34:30 +00:00
										 |  |  |  | import com.fasterxml.jackson.databind.JsonNode;
 | 
					
						
							|  |  |  |  | import com.fasterxml.jackson.databind.ObjectMapper;
 | 
					
						
							| 
									
										
										
										
											2025-10-27 07:30:18 +00:00
										 |  |  |  | import kd.bos.dataentity.entity.DynamicObject;
 | 
					
						
							|  |  |  |  | import kd.bos.dataentity.entity.DynamicObjectCollection;
 | 
					
						
							|  |  |  |  | import kd.bos.entity.plugin.AbstractOperationServicePlugIn;
 | 
					
						
							|  |  |  |  | import kd.bos.entity.plugin.args.AfterOperationArgs;
 | 
					
						
							|  |  |  |  | import kd.bos.exception.KDBizException;
 | 
					
						
							|  |  |  |  | import kd.bos.logging.Log;
 | 
					
						
							|  |  |  |  | import kd.bos.logging.LogFactory;
 | 
					
						
							| 
									
										
										
										
											2025-10-30 09:34:30 +00:00
										 |  |  |  | import kd.bos.orm.query.QCP;
 | 
					
						
							|  |  |  |  | import kd.bos.orm.query.QFilter;
 | 
					
						
							| 
									
										
										
										
											2025-10-27 07:30:18 +00:00
										 |  |  |  | import kd.bos.servicehelper.BusinessDataServiceHelper;
 | 
					
						
							|  |  |  |  | import kd.bos.servicehelper.operation.SaveServiceHelper;
 | 
					
						
							|  |  |  |  | import org.apache.commons.lang3.StringUtils;
 | 
					
						
							|  |  |  |  | import tqq9.lc123.cloud.app.plugin.utils.ConfigUtils;
 | 
					
						
							|  |  |  |  | import tqq9.lc123.cloud.app.plugin.utils.HttpRequestUtils;
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | import java.io.IOException;
 | 
					
						
							|  |  |  |  | import java.util.HashMap;
 | 
					
						
							|  |  |  |  | import java.util.Map;
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-10-30 09:34:30 +00:00
										 |  |  |  | //收货通知单待上架查询
 | 
					
						
							| 
									
										
										
										
											2025-10-27 07:30:18 +00:00
										 |  |  |  | public class PmReceiptNoticeOpPlugin extends AbstractOperationServicePlugIn {
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     private final static Log logger = LogFactory.getLog(PmReceiptNoticeOpPlugin.class);
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     @Override
 | 
					
						
							|  |  |  |  |     public void afterExecuteOperationTransaction(AfterOperationArgs e) {
 | 
					
						
							|  |  |  |  |         super.afterExecuteOperationTransaction(e);
 | 
					
						
							|  |  |  |  |         DynamicObject[] dataEntities1 = e.getDataEntities();
 | 
					
						
							|  |  |  |  |         if (null == dataEntities1 || dataEntities1.length == 0) {
 | 
					
						
							|  |  |  |  |             throw new KDBizException("请至少选择一条数据");
 | 
					
						
							|  |  |  |  |         } else {
 | 
					
						
							|  |  |  |  |             for (DynamicObject dataEntity : dataEntities1) {
 | 
					
						
							|  |  |  |  |                 dataEntity = BusinessDataServiceHelper.loadSingle(dataEntity.getLong("id"), "pm_receiptnotice");
 | 
					
						
							|  |  |  |  |                 String ownerCode = "LICHI";
 | 
					
						
							|  |  |  |  |                 String billno = dataEntity.getString("billno");
 | 
					
						
							| 
									
										
										
										
											2025-10-30 09:34:30 +00:00
										 |  |  |  |                 DynamicObject receiveorg = dataEntity.getDynamicObject("receiveorg");
 | 
					
						
							| 
									
										
										
										
											2025-10-27 07:30:18 +00:00
										 |  |  |  |                 DynamicObjectCollection billentry = dataEntity.getDynamicObjectCollection("billentry");
 | 
					
						
							| 
									
										
										
										
											2025-10-30 09:34:30 +00:00
										 |  |  |  |                 if (null != receiveorg) {
 | 
					
						
							|  |  |  |  |                     String number = receiveorg.getString("number");
 | 
					
						
							|  |  |  |  |                     //BJLC	北京励齿
 | 
					
						
							|  |  |  |  |                     //GZLC	广州励齿
 | 
					
						
							|  |  |  |  |                     //SHLC	上海励齿
 | 
					
						
							|  |  |  |  |                     HashMap<String, Object> body = new HashMap<>();
 | 
					
						
							|  |  |  |  |                     body.put("cVouCode", billno);
 | 
					
						
							|  |  |  |  |                     body.put("VoucherType", "采购订单");
 | 
					
						
							|  |  |  |  |                     String jsonBody = JSON.toJSONString(body);
 | 
					
						
							|  |  |  |  |                     Map<String, String> jsonHeadMap = new HashMap<>();
 | 
					
						
							|  |  |  |  |                     if ("BJLC".equals(number)) {
 | 
					
						
							|  |  |  |  |                         DynamicObject BJURL = BusinessDataServiceHelper.loadSingle("tqq9_thirdconfig", "name",
 | 
					
						
							|  |  |  |  |                                 new QFilter[]{new QFilter("number", QCP.equals, "BJ_POSTURL")});
 | 
					
						
							|  |  |  |  |                         String BJ_URL = BJURL.getString("name");
 | 
					
						
							|  |  |  |  |                         logger.info("北京待上架库存查询接口body:" + jsonBody);
 | 
					
						
							|  |  |  |  |                         try {
 | 
					
						
							|  |  |  |  |                             String response = HttpRequestUtils.postJson(BJ_URL + "/api/WMS/ASNInfo", jsonBody, jsonHeadMap);
 | 
					
						
							|  |  |  |  |                             logger.info("北京待上架库存查询接口返回结果:" + response);
 | 
					
						
							| 
									
										
										
										
											2025-10-27 07:30:18 +00:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-10-30 09:34:30 +00:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  |                             Map<Long, Double> result = BGQtyResult(response);
 | 
					
						
							|  |  |  |  |                             if (null != result && result.size() > 0) {
 | 
					
						
							|  |  |  |  |                                 for (DynamicObject entry : billentry) {
 | 
					
						
							|  |  |  |  |                                     long entryId = entry.getLong("id");
 | 
					
						
							|  |  |  |  |                                     if (result.containsKey(entryId)) {
 | 
					
						
							|  |  |  |  |                                         entry.set("tqq9_ckqty", result.get(entryId));
 | 
					
						
							|  |  |  |  |                                     }
 | 
					
						
							|  |  |  |  |                                 }
 | 
					
						
							|  |  |  |  |                                 SaveServiceHelper.save(new DynamicObject[]{dataEntity});
 | 
					
						
							|  |  |  |  |                             }
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |                         } catch (IOException ex) {
 | 
					
						
							|  |  |  |  |                             throw new RuntimeException(ex);
 | 
					
						
							|  |  |  |  |                         }
 | 
					
						
							|  |  |  |  |                     } else if ("GZLC".equals(number)) {
 | 
					
						
							|  |  |  |  |                         DynamicObject GZURL = BusinessDataServiceHelper.loadSingle("tqq9_thirdconfig", "name",
 | 
					
						
							|  |  |  |  |                                 new QFilter[]{new QFilter("number", QCP.equals, "GZ_POSTURL")});
 | 
					
						
							|  |  |  |  |                         String GZ_URL = GZURL.getString("name");
 | 
					
						
							|  |  |  |  |                         logger.info("广州待上架库存查询接口body:" + jsonBody);
 | 
					
						
							|  |  |  |  |                         try {
 | 
					
						
							|  |  |  |  |                             String response = HttpRequestUtils.postJson(GZ_URL + "/api/WMS/ASNInfo", jsonBody, jsonHeadMap);
 | 
					
						
							|  |  |  |  |                             logger.info("广州待上架库存查询接口返回结果:" + response);
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |                             Map<Long, Double> result = BGQtyResult(response);
 | 
					
						
							|  |  |  |  |                             if (null != result && result.size() > 0) {
 | 
					
						
							|  |  |  |  |                                 for (DynamicObject entry : billentry) {
 | 
					
						
							|  |  |  |  |                                     long entryId = entry.getLong("id");
 | 
					
						
							|  |  |  |  |                                     if (result.containsKey(entryId)) {
 | 
					
						
							|  |  |  |  |                                         entry.set("tqq9_ckqty", result.get(entryId));
 | 
					
						
							|  |  |  |  |                                     }
 | 
					
						
							|  |  |  |  |                                 }
 | 
					
						
							|  |  |  |  |                                 SaveServiceHelper.save(new DynamicObject[]{dataEntity});
 | 
					
						
							|  |  |  |  |                             }
 | 
					
						
							|  |  |  |  |                         } catch (IOException ex) {
 | 
					
						
							|  |  |  |  |                             throw new RuntimeException(ex);
 | 
					
						
							|  |  |  |  |                         }
 | 
					
						
							|  |  |  |  |                     } else if ("SHLC".equals(number)) {
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |                         String warehouseCode = null;
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |                         for (DynamicObject entry : billentry) {
 | 
					
						
							|  |  |  |  |                             DynamicObject warehouse = entry.getDynamicObject("warehouse");
 | 
					
						
							|  |  |  |  |                             if (null != warehouse) {
 | 
					
						
							|  |  |  |  |                                 warehouseCode = warehouse.getString("number");
 | 
					
						
							|  |  |  |  |                             }
 | 
					
						
							|  |  |  |  |                         }
 | 
					
						
							|  |  |  |  |                         String ttx_customerId = ConfigUtils.getThirdConfigByNumber("Ttx_CustomerId");
 | 
					
						
							|  |  |  |  |                         String ttx_mainUrl = ConfigUtils.getThirdConfigByNumber("Ttx_MainURL");
 | 
					
						
							|  |  |  |  |                         Map<String, String> headMap = new HashMap<>();
 | 
					
						
							|  |  |  |  |                         Map<String, Object> request = new HashMap<>();
 | 
					
						
							|  |  |  |  |                         request.put("receiptCode", billno);
 | 
					
						
							|  |  |  |  |                         request.put("ownerCode", ownerCode);
 | 
					
						
							|  |  |  |  |                         request.put("warehouseCode", warehouseCode);
 | 
					
						
							|  |  |  |  |                         Map<String, Object> finalMap = new HashMap<>();
 | 
					
						
							|  |  |  |  |                         finalMap.put("request", request);
 | 
					
						
							|  |  |  |  |                         StringBuilder xmlBuilder = new StringBuilder();
 | 
					
						
							|  |  |  |  |                         xmlBuilder.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
 | 
					
						
							|  |  |  |  |                         HttpRequestUtils.buildXml(xmlBuilder, finalMap, 0);
 | 
					
						
							|  |  |  |  |                         logger.info("上海待上架库存查询接口xml:" + xmlBuilder);
 | 
					
						
							|  |  |  |  |                         ttx_mainUrl = ttx_mainUrl + "?method=checkInReport.query&v=2.0&format=xml&customerId=" + ttx_customerId;
 | 
					
						
							|  |  |  |  |                         try {
 | 
					
						
							|  |  |  |  |                             String response = HttpRequestUtils.postXml(ttx_mainUrl, xmlBuilder.toString(), headMap);
 | 
					
						
							|  |  |  |  |                             logger.info("上海待上架库存查询接口结果:" + response);
 | 
					
						
							|  |  |  |  |                             if (StringUtils.isNotBlank(response)) {
 | 
					
						
							|  |  |  |  |                                 JSONObject resJSON = HttpRequestUtils.xmlToJson(response);
 | 
					
						
							|  |  |  |  |                                 if (null != resJSON) {
 | 
					
						
							|  |  |  |  |                                     JSONObject responseJson = resJSON.getJSONObject("response");
 | 
					
						
							|  |  |  |  |                                     if (null != responseJson) {
 | 
					
						
							|  |  |  |  |                                         String flag = responseJson.getString("flag");
 | 
					
						
							|  |  |  |  |                                         String code = responseJson.getString("code");
 | 
					
						
							|  |  |  |  |                                         JSONObject containerDetail = responseJson.getJSONObject("containerDetail");
 | 
					
						
							|  |  |  |  |                                         if (null != containerDetail && "success".equals(flag) && "200".equals(code)) {
 | 
					
						
							|  |  |  |  |                                             Map<String, Integer> result = new HashMap<>();
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |                                             JSONObject items = responseJson.getJSONObject("items");
 | 
					
						
							|  |  |  |  |                                             if (null != items) {
 | 
					
						
							|  |  |  |  |                                                 Object item1 = items.get("item");
 | 
					
						
							|  |  |  |  |                                                 if (item1 instanceof JSONObject) {
 | 
					
						
							|  |  |  |  |                                                     JSONObject item = items.getJSONObject("item");
 | 
					
						
							| 
									
										
										
										
											2025-10-29 07:00:02 +00:00
										 |  |  |  |                                                     String erpOrderLineNum = item.getString("erpOrderLineNum").replace("\"", "");//行号
 | 
					
						
							| 
									
										
										
										
											2025-10-30 09:34:30 +00:00
										 |  |  |  |                                                     int quantity = item.getIntValue("quantity");
 | 
					
						
							| 
									
										
										
										
											2025-10-29 07:00:02 +00:00
										 |  |  |  |                                                     if (result.containsKey(erpOrderLineNum)) {
 | 
					
						
							|  |  |  |  |                                                         int currentSum = result.get(erpOrderLineNum);
 | 
					
						
							|  |  |  |  |                                                         result.put(erpOrderLineNum, currentSum + quantity);
 | 
					
						
							|  |  |  |  |                                                     } else {
 | 
					
						
							|  |  |  |  |                                                         result.put(erpOrderLineNum, quantity);
 | 
					
						
							|  |  |  |  |                                                     }
 | 
					
						
							| 
									
										
										
										
											2025-10-30 09:34:30 +00:00
										 |  |  |  |                                                 } else if (item1 instanceof JSONArray) {
 | 
					
						
							|  |  |  |  |                                                     JSONArray jsonArray = items.getJSONArray("item");
 | 
					
						
							|  |  |  |  |                                                     if (null != jsonArray && jsonArray.size() > 0) {
 | 
					
						
							|  |  |  |  |                                                         for (int i = 0; i < jsonArray.size(); i++) {
 | 
					
						
							|  |  |  |  |                                                             JSONObject item = jsonArray.getJSONObject(i);
 | 
					
						
							|  |  |  |  |                                                             String erpOrderLineNum = item.getString("erpOrderLineNum").replace("\"", "");//行号
 | 
					
						
							|  |  |  |  |                                                             int quantity = item.getIntValue("quantity");//数量
 | 
					
						
							|  |  |  |  |                                                             if (result.containsKey(erpOrderLineNum)) {
 | 
					
						
							|  |  |  |  |                                                                 int currentSum = result.get(erpOrderLineNum);
 | 
					
						
							|  |  |  |  |                                                                 result.put(erpOrderLineNum, currentSum + quantity);
 | 
					
						
							|  |  |  |  |                                                             } else {
 | 
					
						
							|  |  |  |  |                                                                 result.put(erpOrderLineNum, quantity);
 | 
					
						
							|  |  |  |  |                                                             }
 | 
					
						
							|  |  |  |  |                                                         }
 | 
					
						
							|  |  |  |  |                                                     }
 | 
					
						
							| 
									
										
										
										
											2025-10-27 07:30:18 +00:00
										 |  |  |  |                                                 }
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-10-29 07:00:02 +00:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-10-30 09:34:30 +00:00
										 |  |  |  |                                             }
 | 
					
						
							| 
									
										
										
										
											2025-10-27 07:30:18 +00:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-10-30 09:34:30 +00:00
										 |  |  |  |                                             if (result.size() > 0) {
 | 
					
						
							|  |  |  |  |                                                 for (DynamicObject entry : billentry) {
 | 
					
						
							|  |  |  |  |                                                     String entryId = String.valueOf(entry.getLong("id"));
 | 
					
						
							|  |  |  |  |                                                     if (result.containsKey(entryId)) {
 | 
					
						
							|  |  |  |  |                                                         entry.set("tqq9_ckqty", result.get(entryId));
 | 
					
						
							|  |  |  |  |                                                     }
 | 
					
						
							|  |  |  |  |                                                 }
 | 
					
						
							|  |  |  |  |                                                 SaveServiceHelper.save(new DynamicObject[]{dataEntity});
 | 
					
						
							| 
									
										
										
										
											2025-10-27 07:30:18 +00:00
										 |  |  |  |                                             }
 | 
					
						
							|  |  |  |  |                                         }
 | 
					
						
							|  |  |  |  |                                     }
 | 
					
						
							|  |  |  |  |                                 }
 | 
					
						
							|  |  |  |  |                             }
 | 
					
						
							| 
									
										
										
										
											2025-10-30 09:34:30 +00:00
										 |  |  |  |                         } catch (IOException ex) {
 | 
					
						
							|  |  |  |  |                             throw new RuntimeException(ex);
 | 
					
						
							| 
									
										
										
										
											2025-10-27 07:30:18 +00:00
										 |  |  |  |                         }
 | 
					
						
							|  |  |  |  |                     }
 | 
					
						
							| 
									
										
										
										
											2025-10-30 09:34:30 +00:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-10-27 07:30:18 +00:00
										 |  |  |  |                 }
 | 
					
						
							| 
									
										
										
										
											2025-10-30 09:34:30 +00:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-10-27 07:30:18 +00:00
										 |  |  |  |             }
 | 
					
						
							|  |  |  |  |         }
 | 
					
						
							|  |  |  |  |     }
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-10-30 09:34:30 +00:00
										 |  |  |  |     /**
 | 
					
						
							|  |  |  |  |      * 返回北广待上架数量
 | 
					
						
							|  |  |  |  |      *
 | 
					
						
							|  |  |  |  |      * @param response 北广返回信息
 | 
					
						
							|  |  |  |  |      * @return
 | 
					
						
							|  |  |  |  |      */
 | 
					
						
							|  |  |  |  |     private static Map<Long, Double> BGQtyResult(String response) {
 | 
					
						
							|  |  |  |  |         Map<Long, Double> result = new HashMap<>();
 | 
					
						
							|  |  |  |  |         try {
 | 
					
						
							|  |  |  |  |             ObjectMapper objectMapper = new ObjectMapper();
 | 
					
						
							|  |  |  |  |             JsonNode rootNode = objectMapper.readTree(response);
 | 
					
						
							|  |  |  |  |             // 获取基本字段
 | 
					
						
							|  |  |  |  |             String code = rootNode.get("code").asText();
 | 
					
						
							|  |  |  |  |             String message = rootNode.get("message").asText();
 | 
					
						
							|  |  |  |  |             // 遍历数组
 | 
					
						
							|  |  |  |  |             JsonNode dataArray = rootNode.get("data");
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |             for (JsonNode item : dataArray) {
 | 
					
						
							|  |  |  |  |                 long iVouID = item.get("iVouID").asLong();
 | 
					
						
							|  |  |  |  |                 double iTQuantity = item.get("iTQuantity").asDouble();//收货数量
 | 
					
						
							|  |  |  |  |                 double iGQuantity = item.get("iGQuantity").asDouble();// 上架数量
 | 
					
						
							|  |  |  |  |                 double qty = iTQuantity - iGQuantity;
 | 
					
						
							|  |  |  |  |                 if (result.containsKey(iVouID)) {
 | 
					
						
							|  |  |  |  |                     double currentSum = result.get(iVouID);
 | 
					
						
							|  |  |  |  |                     result.put(iVouID, currentSum + qty);
 | 
					
						
							|  |  |  |  |                 } else {
 | 
					
						
							|  |  |  |  |                     result.put(iVouID, qty);
 | 
					
						
							|  |  |  |  |                 }
 | 
					
						
							|  |  |  |  |             }
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |             return result;
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |         } catch (Exception exception) {
 | 
					
						
							|  |  |  |  |             exception.printStackTrace();
 | 
					
						
							|  |  |  |  |         }
 | 
					
						
							|  |  |  |  |         return result;
 | 
					
						
							|  |  |  |  |     }
 | 
					
						
							| 
									
										
										
										
											2025-10-27 07:30:18 +00:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | }
 |