|
|
@@ -1,6 +1,8 @@
|
|
|
package com.yw.contract.common.sync.mes;
|
|
|
|
|
|
import com.yw.contract.contract.model.ContractCompany;
|
|
|
+import com.yw.contract.contract.model.ContractInfo;
|
|
|
+import com.yw.contract.contract.model.ContractMaterial;
|
|
|
import com.yw.contract.contract.model.ContractPurchaseDetail;
|
|
|
import com.yw.contract.contract.model.ContractPurchaseForm;
|
|
|
import com.yw.contract.contract.model.ContractPurchaseOrder;
|
|
|
@@ -18,6 +20,7 @@ import org.springframework.util.ObjectUtils;
|
|
|
|
|
|
import java.io.IOException;
|
|
|
import java.util.ArrayList;
|
|
|
+import java.util.Date;
|
|
|
import java.util.HashMap;
|
|
|
import java.util.Iterator;
|
|
|
import java.util.List;
|
|
|
@@ -608,4 +611,98 @@ public class MesCommonHelper extends MesHelper{
|
|
|
}
|
|
|
return null;
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 同步销售合同到MES销售订单
|
|
|
+ * @param contractInfo 合同信息
|
|
|
+ * @param contractMaterialList 合同物料明细
|
|
|
+ * @return MES返回的结果
|
|
|
+ * @throws Exception
|
|
|
+ */
|
|
|
+ public JSONObject syncSaleOrderToMes(ContractInfo contractInfo, List<ContractMaterial> contractMaterialList) throws Exception {
|
|
|
+ Map<String, Object> dats = new HashMap<>();
|
|
|
+
|
|
|
+ // 设置销售订单基本信息
|
|
|
+ dats.put("clientCode", contractInfo.getSupplierCode()); // supplierCode对应clientCode
|
|
|
+ dats.put("clientName", contractInfo.getSupplierName()); // supplierName对应clientName
|
|
|
+ dats.put("salseDate", new Date());
|
|
|
+ dats.put("status", "CONFIRMED"); // 设置为确认状态
|
|
|
+ dats.put("contractId", contractInfo.getUniversalid());
|
|
|
+ dats.put("contractNumber", contractInfo.getContract_number());
|
|
|
+
|
|
|
+ // 设置物料明细
|
|
|
+ if(!ObjectUtils.isEmpty(contractMaterialList)){
|
|
|
+ List<Map<String,Object>> details = new ArrayList<>();
|
|
|
+ for(ContractMaterial material : contractMaterialList){
|
|
|
+ Map<String, Object> detailMap = new HashMap<>();
|
|
|
+ detailMap.put("itemCode", material.getItemCode());
|
|
|
+ detailMap.put("itemName", material.getItemName());
|
|
|
+ detailMap.put("specification", material.getSpecification());
|
|
|
+ detailMap.put("measureName", material.getMeasureName());
|
|
|
+ detailMap.put("quantityOrder", material.getQty());
|
|
|
+ detailMap.put("price", material.getPrice());
|
|
|
+ detailMap.put("priceTax", material.getPriceTax());
|
|
|
+ detailMap.put("cess", material.getCess());
|
|
|
+
|
|
|
+ // 计算含税总金额:数量 * 单价 * (1 + 税率%),空值默认为0
|
|
|
+ double qty = material.getQty() != null ? material.getQty().doubleValue() : 0;
|
|
|
+ double price = material.getPrice() != null ? material.getPrice().doubleValue() : 0;
|
|
|
+ double cess = material.getCess() != null ? material.getCess().doubleValue() : 0;
|
|
|
+ double totalPrice = qty * price * (1 + cess * 0.01);
|
|
|
+ detailMap.put("totalPrice", Math.round(totalPrice * 100.0) / 100.0); // 保留两位小数
|
|
|
+
|
|
|
+ //detailMap.put("status", "0");
|
|
|
+ details.add(detailMap);
|
|
|
+ }
|
|
|
+ dats.put("wmSaleOrderLineList", details);
|
|
|
+ }
|
|
|
+
|
|
|
+ Request request = postRequest(SyncConstant.SALEORDER_ADD_URL, dats);
|
|
|
+ try {
|
|
|
+ OkHttpClient client = new OkHttpClient().newBuilder().build();
|
|
|
+ Response response = client.newCall(request).execute();
|
|
|
+ if (response.isSuccessful()) {
|
|
|
+ if(response.body()!=null) {
|
|
|
+ String resp = response.body().string();
|
|
|
+
|
|
|
+ // 先尝试使用公共的checkResp处理(处理401等通用逻辑)
|
|
|
+ try {
|
|
|
+ int isResp = checkResp(resp);
|
|
|
+ if(isResp == 0){
|
|
|
+ return syncSaleOrderToMes(contractInfo, contractMaterialList);
|
|
|
+ }
|
|
|
+ // checkResp成功,返回结果
|
|
|
+ return new JSONObject(resp);
|
|
|
+ } catch (Exception e) {
|
|
|
+ // checkResp抛出异常,尝试获取MES返回的具体错误信息
|
|
|
+ try {
|
|
|
+ JSONObject json = new JSONObject(resp);
|
|
|
+ String msg = json.optString("msg", "同步销售订单失败");
|
|
|
+ LogUtil.info("同步销售订单失败:" + msg);
|
|
|
+ // 构造错误响应,而不是抛出异常
|
|
|
+ JSONObject errorResp = new JSONObject();
|
|
|
+ errorResp.put("code", json.optInt("code", 500));
|
|
|
+ errorResp.put("msg", msg);
|
|
|
+ return errorResp;
|
|
|
+ } catch (Exception ex) {
|
|
|
+ // 如果解析失败,构造通用错误响应
|
|
|
+ JSONObject errorResp = new JSONObject();
|
|
|
+ errorResp.put("code", 500);
|
|
|
+ errorResp.put("msg", e.getMessage());
|
|
|
+ return errorResp;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }else{
|
|
|
+ System.out.println("请求成功,没有数据返回");
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ LogUtil.info(response.body().string());
|
|
|
+ throw new Exception("同步销售订单到MES失败");
|
|
|
+ }
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ throw e;
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
}
|