ic: 其它出入库判断pk改为判断电缆code

- 优化了 AfterSignRuleSyncInToMesProcess 和 AfterSignRuleSyncOutToMesProcess 类的结构
This commit is contained in:
maolei 2025-05-22 09:58:29 +08:00
parent 57be63bfa4
commit 4045e216eb
3 changed files with 257 additions and 203 deletions

View File

@ -3,17 +3,23 @@ package nc.bs.ic.m4a.sign.rule;
import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import nc.bs.dao.BaseDAO;
import nc.bs.framework.common.NCLocator; import nc.bs.framework.common.NCLocator;
import nc.bs.logging.Log; import nc.bs.logging.Log;
import nc.impl.pubapp.pattern.rule.IRule; import nc.impl.pubapp.pattern.rule.IRule;
import nc.itf.uif.pub.IUifService; import nc.itf.uif.pub.IUifService;
import nc.jdbc.framework.processor.ColumnProcessor;
import nc.vo.bd.material.MaterialVersionVO; import nc.vo.bd.material.MaterialVersionVO;
import nc.vo.bd.rack.RackVO;
import nc.vo.bd.stordoc.StordocVO; import nc.vo.bd.stordoc.StordocVO;
import nc.vo.cmp.util.StringUtils;
import nc.vo.ic.m4a.entity.GeneralInBodyVO; import nc.vo.ic.m4a.entity.GeneralInBodyVO;
import nc.vo.ic.m4a.entity.GeneralInHeadVO; import nc.vo.ic.m4a.entity.GeneralInHeadVO;
import nc.vo.ic.m4a.entity.GeneralInVO; import nc.vo.ic.m4a.entity.GeneralInVO;
import nc.vo.org.StockOrgVO;
import nc.vo.pub.BusinessException; import nc.vo.pub.BusinessException;
import nc.vo.pubapp.pattern.exception.ExceptionUtils; import nc.vo.pubapp.pattern.exception.ExceptionUtils;
import nc.vo.pubapp.pattern.pub.SqlBuilder;
import nccloud.pubift.commen.itf.utils.IHttpPostOtherSys; import nccloud.pubift.commen.itf.utils.IHttpPostOtherSys;
import java.util.Objects; import java.util.Objects;
@ -28,6 +34,8 @@ public class AfterSignRuleSyncInToMesProcess implements IRule<GeneralInVO> {
private static final IUifService IUIFSERVICE = NCLocator.getInstance().lookup(IUifService.class); private static final IUifService IUIFSERVICE = NCLocator.getInstance().lookup(IUifService.class);
private static final BaseDAO DAO = new BaseDAO();
public void process(GeneralInVO[] vos) { public void process(GeneralInVO[] vos) {
if (vos != null && vos.length != 0) { if (vos != null && vos.length != 0) {
@ -49,7 +57,6 @@ public class AfterSignRuleSyncInToMesProcess implements IRule<GeneralInVO> {
// 填充details // 填充details
for (GeneralInBodyVO body : generalInVO.getBodys()) { for (GeneralInBodyVO body : generalInVO.getBodys()) {
JSONObject temp = new JSONObject(); JSONObject temp = new JSONObject();
temp.put("orderNo", head.getVbillcode()); // 通知单ID
temp.put("sequenceNum", body.getCrowno()); // 通知单序号 temp.put("sequenceNum", body.getCrowno()); // 通知单序号
temp.put("type", "QTRK"); // 事务类型 temp.put("type", "QTRK"); // 事务类型
@ -58,11 +65,15 @@ public class AfterSignRuleSyncInToMesProcess implements IRule<GeneralInVO> {
throw new BusinessException("物料信息不存在, 主键:" + body.getCmaterialvid()); throw new BusinessException("物料信息不存在, 主键:" + body.getCmaterialvid());
} }
temp.put("materialId", materialVersionVO.getCode()); // 物料ID temp.put("materialId", materialVersionVO.getCode()); // 物料ID
temp.put("recQty", body.getNnum() != null ? body.getNnum().doubleValue() : null); // 应收数量 temp.put("recQty", body.getNnum() != null ? body.getNnum().doubleValue() : 0); // 应收数量
temp.put("actQty", body.getNshouldnum() != null ? body.getNshouldnum().doubleValue() : null); // 实收数量 temp.put("actQty", body.getNshouldnum() != null ? body.getNshouldnum().doubleValue() : 0); // 实收数量
temp.put("assistActQry", body.getNassistnum() != null ? body.getNassistnum().doubleValue() : 0); // 辅助实收数量 temp.put("assistActQry", body.getNassistnum() != null ? body.getNassistnum().doubleValue() : 0); // 辅助实收数量
temp.put("productNum", null); // 产品编号 temp.put("productNum", null); // 产品编号
String RackVOCode = transferCodeByPk(RackVO.getDefaultTableName(), RackVO.CODE, RackVO.PK_RACK, body.getClocationid());
if (RackVOCode != null && !RackVOCode.isBlank()) {
temp.put("storageId", body.getClocationid()); // 库位 temp.put("storageId", body.getClocationid()); // 库位
}
temp.put("batchNum", body.getVbatchcode()); // 物料批号 temp.put("batchNum", body.getVbatchcode()); // 物料批号
temp.put("customId", null); // 客户ID temp.put("customId", null); // 客户ID
temp.put("supplierId", null); // 供应商ID temp.put("supplierId", null); // 供应商ID
@ -77,24 +88,18 @@ public class AfterSignRuleSyncInToMesProcess implements IRule<GeneralInVO> {
JSONObject info = new JSONObject(); JSONObject info = new JSONObject();
info.put("details", details); // 明细 info.put("details", details); // 明细
// 从head中取数 // 从head中取数
info.put("orderNo", head.getVbillcode()); // 通知单ID
info.put("type", "QTRK"); // 事务类型 info.put("type", "QTRK"); // 事务类型
info.put("actureDate", head.getDmakedate() != null ? head.getDmakedate().toString() : null); // 实际日期 info.put("actureDate", head.getDmakedate() != null ? head.getDmakedate().toString() : null); // 实际日期
StordocVO warehouse = (StordocVO) IUIFSERVICE.queryByPrimaryKey(StordocVO.class, head.getCwarehouseid());
if (warehouse.getCode() != null && warehouse.getCode().length() > 4) {
throw new BusinessException("MES系统仓库编码[" + warehouse.getCode() + "]不能超过4位请选择合适的仓库!");
} else {
info.put("storeId", warehouse.getCode());
}
info.put("department", null); // 部门 info.put("department", null); // 部门
info.put("customId", null); // 客户 info.put("customId", null); // 客户
info.put("supplierId", null); // 供应商 info.put("supplierId", null); // 供应商
StordocVO warehouse = (StordocVO) IUIFSERVICE.queryByPrimaryKey(StordocVO.class, head.getCwarehouseid());
if (Objects.isNull(warehouse)) {
throw new BusinessException("仓库信息不存在, 主键:" + head.getCwarehouseid());
}
if (warehouse.getCode() != null && warehouse.getCode().length() > 4) {
throw new BusinessException("MES系统仓库编码[" + warehouse.getCode() + "]不能超过4位请选择合适的仓库!");
}
info.put("storeId", warehouse.getCode()); // 仓库ID
info.put("workerNo", null); // 工人编号 info.put("workerNo", null); // 工人编号
info.put("worker", null); // 工人 info.put("worker", null); // 工人
info.put("storeKeeper", head.getCwhsmanagerid()); // 库管员 info.put("storeKeeper", head.getCwhsmanagerid()); // 库管员
@ -116,15 +121,17 @@ public class AfterSignRuleSyncInToMesProcess implements IRule<GeneralInVO> {
GeneralInHeadVO head = generalInVO.getHead(); GeneralInHeadVO head = generalInVO.getHead();
obmlog.debug("触发同步其它入库单(M4A), 单据号:" + head.getVbillcode() + ", 出库类型编码:" + head.getVtrantypecode() + "开始拼接数据"); obmlog.debug("触发同步其它入库单(M4A), 单据号:" + head.getVbillcode() + ", 出库类型编码:" + head.getVtrantypecode() + "开始拼接数据");
// if (!generalOutVO.getHead().getPk_org().equals("0001A110000000000677")) { String orgCode = transferCodeByPk(StockOrgVO.getDefaultTableName(), StockOrgVO.CODE, StockOrgVO.PK_STOCKORG, head.getPk_org());
// obmlog.debug("仅同步山东泰开电缆有限公司,当前组织:" + generalOutVO.getHead().getPk_org());
// return; // 仅操作山东泰开电缆有限公司
// }
// 调拨入库 if (!Objects.equals(orgCode, "C034")) {
if ("4A-02".equals(head.getVtrantypecode())) { obmlog.debug("仅同步山东泰开电缆有限公司,当前组织:" + generalInVO.getHead().getPk_org());
syncTransferIn(generalInVO); return; // 仅操作山东泰开电缆有限公司
} }
// // 调拨入库
// if ("4A-02".equals(head.getVtrantypecode())) {
// syncTransferIn(generalInVO);
// }
// 其它入库 // 其它入库
else if ("4A-01".equals(head.getVtrantypecode())) { else if ("4A-01".equals(head.getVtrantypecode())) {
syncOtherIn(generalInVO); syncOtherIn(generalInVO);
@ -205,4 +212,19 @@ public class AfterSignRuleSyncInToMesProcess implements IRule<GeneralInVO> {
obmlog.debug("调拨入库单 " + head.getVbillcode() + " 同步成功"); obmlog.debug("调拨入库单 " + head.getVbillcode() + " 同步成功");
} }
private String transferCodeByPk(String tableName, String selectField, String pkField, String pk) throws BusinessException {
if (StringUtils.isEmpty(pk)) {
return null;
}
SqlBuilder sqlBuilder = new SqlBuilder();
sqlBuilder.append(" select " + selectField);
sqlBuilder.append(" from " + tableName);
sqlBuilder.append(" where ");
sqlBuilder.append(pkField, pk);
Object o = DAO.executeQuery(sqlBuilder.toString(), new ColumnProcessor());
if (o == null) {
throw new BusinessException("未查询到编码信息sql【" + sqlBuilder + "");
}
return o.toString();
}
} }

View File

@ -3,17 +3,22 @@ package nc.bs.ic.m4i.sign.rule;
import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import nc.bs.dao.BaseDAO;
import nc.bs.framework.common.NCLocator; import nc.bs.framework.common.NCLocator;
import nc.bs.logging.Log; import nc.bs.logging.Log;
import nc.impl.pubapp.pattern.rule.IRule; import nc.impl.pubapp.pattern.rule.IRule;
import nc.itf.uif.pub.IUifService; import nc.itf.uif.pub.IUifService;
import nc.jdbc.framework.processor.ColumnProcessor;
import nc.vo.bd.material.MaterialVersionVO; import nc.vo.bd.material.MaterialVersionVO;
import nc.vo.bd.stordoc.StordocVO; import nc.vo.bd.stordoc.StordocVO;
import nc.vo.cmp.util.StringUtils;
import nc.vo.ic.m4i.entity.GeneralOutBodyVO; import nc.vo.ic.m4i.entity.GeneralOutBodyVO;
import nc.vo.ic.m4i.entity.GeneralOutHeadVO; import nc.vo.ic.m4i.entity.GeneralOutHeadVO;
import nc.vo.ic.m4i.entity.GeneralOutVO; import nc.vo.ic.m4i.entity.GeneralOutVO;
import nc.vo.org.StockOrgVO;
import nc.vo.pub.BusinessException; import nc.vo.pub.BusinessException;
import nc.vo.pubapp.pattern.exception.ExceptionUtils; import nc.vo.pubapp.pattern.exception.ExceptionUtils;
import nc.vo.pubapp.pattern.pub.SqlBuilder;
import nccloud.pubift.commen.itf.utils.IHttpPostOtherSys; import nccloud.pubift.commen.itf.utils.IHttpPostOtherSys;
import java.util.Objects; import java.util.Objects;
@ -27,6 +32,8 @@ public class AfterSignRuleSyncOutToMesProcess implements IRule<GeneralOutVO> {
private static final Log obmlog = Log.getInstance(logginfo); private static final Log obmlog = Log.getInstance(logginfo);
private static final BaseDAO DAO = new BaseDAO();
private final IUifService iUifService = NCLocator.getInstance().lookup(IUifService.class); private final IUifService iUifService = NCLocator.getInstance().lookup(IUifService.class);
public void process(GeneralOutVO[] vos) { public void process(GeneralOutVO[] vos) {
@ -42,24 +49,14 @@ public class AfterSignRuleSyncOutToMesProcess implements IRule<GeneralOutVO> {
} }
} }
private void syncOtherOut(GeneralOutVO generalOutVO) throws BusinessException {
private void syncOtherSystem(GeneralOutVO generalOutVO) throws BusinessException { GeneralOutHeadVO head = generalOutVO.getHead();
obmlog.debug("触发同步其它出库单,出库类型编码:" + generalOutVO.getHead().getVtrantypecode() + "开始拼接数据"); obmlog.debug("开始处理其它出库单(4I-01): " + head.getVbillcode());
// if (!generalOutVO.getHead().getPk_org().equals("0001A110000000000677")) {
// obmlog.debug("仅同步山东泰开电缆有限公司,当前组织:" + generalOutVO.getHead().getPk_org());
// return; // 仅操作山东泰开电缆有限公司
// }
// 其它出库
if (generalOutVO.getHead().getVtrantypecode().equals("4I-01")) {
// 表体body信息拼接 // 表体body信息拼接
JSONArray details = new JSONArray(); JSONArray details = new JSONArray();
GeneralOutHeadVO head = generalOutVO.getHead();
// 填充details // 填充details
for (GeneralOutBodyVO body : generalOutVO.getBodys()) {// body中取数 for (GeneralOutBodyVO body : generalOutVO.getBodys()) {// body中取数
JSONObject temp = new JSONObject(); JSONObject temp = new JSONObject();
temp.put("orderNo", head.getVbillcode()); // 通知单ID
temp.put("sequenceNum", body.getCrowno()); // 通知单序号 temp.put("sequenceNum", body.getCrowno()); // 通知单序号
temp.put("type", "QTCK"); // 事务类型 temp.put("type", "QTCK"); // 事务类型
MaterialVersionVO materialVersionVO = (MaterialVersionVO) iUifService.queryByPrimaryKey(MaterialVersionVO.class, body.getCmaterialoid()); MaterialVersionVO materialVersionVO = (MaterialVersionVO) iUifService.queryByPrimaryKey(MaterialVersionVO.class, body.getCmaterialoid());
@ -82,16 +79,12 @@ public class AfterSignRuleSyncOutToMesProcess implements IRule<GeneralOutVO> {
JSONObject info = new JSONObject(); JSONObject info = new JSONObject();
info.put("details", details); info.put("details", details);
// 从head中取数 // 从head中取数
info.put("orderNo", head.getVbillcode()); // 通知单ID
info.put("type", "QTCK"); // 事务类型 info.put("type", "QTCK"); // 事务类型
info.put("actureDate", head.getDmakedate().toString()); // 实际日期 info.put("actureDate", head.getDmakedate().toString()); // 实际日期
StordocVO warehouse = (StordocVO) iUifService.queryByPrimaryKey(StordocVO.class, head.getCwarehouseid()); StordocVO warehouse = (StordocVO) iUifService.queryByPrimaryKey(StordocVO.class, head.getCwarehouseid());
if (Objects.isNull(warehouse)) { if (Objects.isNull(warehouse)) {
throw new BusinessException("仓库信息不存在,主键:" + head.getCwarehouseid()); throw new BusinessException("仓库信息不存在,主键:" + head.getCwarehouseid());
} }
if (warehouse.getCode() != null && warehouse.getCode().length() > 4) {
throw new BusinessException("MES系统仓库编码[" + warehouse.getCode() + "]不能超过4位请选择合适的仓库!");
}
info.put("storeId", warehouse.getCode()); // 仓库ID info.put("storeId", warehouse.getCode()); // 仓库ID
info.put("workerNo", null); info.put("workerNo", null);
info.put("worker", null); info.put("worker", null);
@ -104,15 +97,38 @@ public class AfterSignRuleSyncOutToMesProcess implements IRule<GeneralOutVO> {
JSONObject requestData = new JSONObject(); JSONObject requestData = new JSONObject();
requestData.put("operation_type", "I");// 操作 requestData.put("operation_type", "I");// 操作
requestData.put("info", info); requestData.put("info", info);
obmlog.debug("其它出库requestData:" + requestData.toJSONString()); obmlog.debug("其它出库(4I-01) requestData:" + requestData.toJSONString());
IHttpPostOtherSys httpService = NCLocator.getInstance().lookup(IHttpPostOtherSys.class); HTTP_POST_OTHER_SYS.sendToExternalSystem("/GTHINKING/AjaxService/N_KCSJJS/101527004.ashx/gt_rec_api_qtckdj", requestData);
httpService.sendToExternalSystem("/GTHINKING/AjaxService/N_KCSJJS/101527004.ashx/gt_rec_api_qtckdj", requestData); obmlog.debug("其它出库单(4I-01) " + head.getVbillcode() + " 同步成功");
} }
// 调拨出库/转库单
if (generalOutVO.getHead().getVtrantypecode().equals("4I-02")) { private void syncOtherSystem(GeneralOutVO generalOutVO) throws BusinessException {
obmlog.debug("触发同步其它出库单,出库类型编码:" + generalOutVO.getHead().getVtrantypecode() + "开始拼接数据");
String orgCode = transferCodeByPk(StockOrgVO.getDefaultTableName(), StockOrgVO.CODE, StockOrgVO.PK_STOCKORG, generalOutVO.getHead().getPk_org());
if (!Objects.equals(orgCode, "C034")) {
obmlog.debug("仅同步山东泰开电缆有限公司,当前组织:" + generalOutVO.getHead().getPk_org());
return; // 仅操作山东泰开电缆有限公司
}
String trantypecode = generalOutVO.getHead().getVtrantypecode();
// 其它出库
if ("4I-01".equals(trantypecode)) {
syncOtherOut(generalOutVO);
}
// // 调拨出库/转库单
// else if ("4I-02".equals(trantypecode)) {
// syncTransferOut(generalOutVO);
// }
}
private void syncTransferOut(GeneralOutVO generalOutVO) throws BusinessException {
GeneralOutHeadVO head = generalOutVO.getHead();
obmlog.debug("开始处理调拨出库/转库单(4I-02): " + head.getVbillcode());
// 表体body信息拼接 // 表体body信息拼接
JSONArray details = new JSONArray(); JSONArray details = new JSONArray();
GeneralOutHeadVO head = generalOutVO.getHead();
// 填充details // 填充details
for (GeneralOutBodyVO body : generalOutVO.getBodys()) {// body中取数 for (GeneralOutBodyVO body : generalOutVO.getBodys()) {// body中取数
JSONObject temp = new JSONObject(); JSONObject temp = new JSONObject();
@ -153,18 +169,12 @@ public class AfterSignRuleSyncOutToMesProcess implements IRule<GeneralOutVO> {
if (Objects.isNull(outWarehouse)) { if (Objects.isNull(outWarehouse)) {
throw new BusinessException("出库仓库信息不存在,主键:" + head.getCwarehouseid()); throw new BusinessException("出库仓库信息不存在,主键:" + head.getCwarehouseid());
} }
if (outWarehouse.getCode() != null && outWarehouse.getCode().length() > 4) {
throw new BusinessException("MES系统出库仓库编码[" + outWarehouse.getCode() + "]不能超过4位请选择合适的仓库!");
}
info.put("outStoreId", outWarehouse.getCode()); // 仓库ID info.put("outStoreId", outWarehouse.getCode()); // 仓库ID
StordocVO inWarehouse = (StordocVO) iUifService.queryByPrimaryKey(StordocVO.class, head.getCotherwhid()); StordocVO inWarehouse = (StordocVO) iUifService.queryByPrimaryKey(StordocVO.class, head.getCotherwhid());
if (Objects.isNull(inWarehouse)) { if (Objects.isNull(inWarehouse)) {
throw new BusinessException("入库仓库信息不存在,主键:" + head.getCotherwhid()); throw new BusinessException("入库仓库信息不存在,主键:" + head.getCotherwhid());
} }
if (inWarehouse.getCode() != null && inWarehouse.getCode().length() > 4) {
throw new BusinessException("MES系统入库仓库编码[" + inWarehouse.getCode() + "]不能超过4位请选择合适的仓库!");
}
info.put("inStoreId", inWarehouse.getCode()); // 仓库ID info.put("inStoreId", inWarehouse.getCode()); // 仓库ID
info.put("workerNo", null); info.put("workerNo", null);
info.put("worker", null); info.put("worker", null);
@ -177,12 +187,27 @@ public class AfterSignRuleSyncOutToMesProcess implements IRule<GeneralOutVO> {
JSONObject requestData = new JSONObject(); JSONObject requestData = new JSONObject();
requestData.put("operation_type", "I");// 操作 requestData.put("operation_type", "I");// 操作
requestData.put("info", info); requestData.put("info", info);
obmlog.debug("其它出库requestData:" + requestData.toJSONString()); obmlog.debug("调拨出库/转库单(4I-02) requestData:" + requestData.toJSONString());
HTTP_POST_OTHER_SYS.sendToExternalSystem("/GTHINKING/AjaxService/N_KCSJJS/101527009.ashx/gt_rec_api_dbckdj", requestData); HTTP_POST_OTHER_SYS.sendToExternalSystem("/GTHINKING/AjaxService/N_KCSJJS/101527009.ashx/gt_rec_api_dbckdj", requestData);
obmlog.debug("调拨出库/转库单(4I-02) " + head.getVbillcode() + " 同步成功");
} }
}
private String transferCodeByPk(String tableName, String selectField, String pkField, String pk) throws BusinessException {
if (StringUtils.isEmpty(pk)) {
return null;
}
SqlBuilder sqlBuilder = new SqlBuilder();
sqlBuilder.append(" select " + selectField);
sqlBuilder.append(" from " + tableName);
sqlBuilder.append(" where ");
sqlBuilder.append(pkField, pk);
Object o = DAO.executeQuery(sqlBuilder.toString(), new ColumnProcessor());
if (o == null) {
throw new BusinessException("未查询到编码信息sql【" + sqlBuilder + "");
}
return o.toString();
}
} }

View File

@ -25,21 +25,22 @@ public class HttpPostOtherSysImpl implements IHttpPostOtherSys {
private static final String PASSWORD = "BIP@2025bip"; private static final String PASSWORD = "BIP@2025bip";
private static final String CLIENT_TYPE = "S"; private static final String CLIENT_TYPE = "S";
private static final String EP_ID = ""; private static final String EP_ID = "";
private static final String LOGIN_URL = "/GTHINKING/AjaxService/N_MISPRO/100208057.ashx/Login";
private static final String LOGIN_URL = "/GTHINKING/AjaxService/N_MISPRO/100208057.ashx/Login"; // 登录接口路径
private static final String LOG_INFO_NAME = "OALOG"; private static final String LOG_INFO_NAME = "OALOG";
private static final Log obmlog = Log.getInstance(LOG_INFO_NAME); private static final Log obmlog = Log.getInstance(LOG_INFO_NAME);
@Override @Override
public String callMes(String url, JSONObject json) { public String callMes(String url, JSONObject json) {
// String leip = SysParaInitQuery.getParaString(PubEnv.getPk_group(), "LEIP"); // String leip = SysParaInitQuery.getParaString(PubEnv.getPk_group(), "LEIP"); // 从系统参数获取IP地址示例
String mesip = "http://192.168.29.32"; String mesip = "http://192.168.29.32"; // MES系统IP地址实际应从配置读取
String baseurl = mesip + url; String baseurl = mesip + url; // 拼接完整的请求URL
String tokenValue = this.getMESToken(mesip); String tokenValue = this.getMESToken(mesip); // 获取MES系统的认证Token
Map<String, String> headers = new HashMap<>(); Map<String, String> headers = new HashMap<>();
if (tokenValue != null) { if (tokenValue != null) {
headers.put("Cookie", ".ASPXAUTH=" + tokenValue); headers.put("Cookie", ".ASPXAUTH=" + tokenValue); // 将Token放入请求头的Cookie中
} }
return doPost(baseurl, headers, json); return doPost(baseurl, headers, json); // 执行POST请求
} }
/** /**
@ -47,26 +48,26 @@ public class HttpPostOtherSysImpl implements IHttpPostOtherSys {
*/ */
public void sendToExternalSystem(String apiPath, Map<String, Object> requestData) throws BusinessException { public void sendToExternalSystem(String apiPath, Map<String, Object> requestData) throws BusinessException {
try { try {
obmlog.debug("HttpPostOtherSys request :" + JSONObject.toJSONString(requestData)); obmlog.debug("HttpPostOtherSys 请求数据 :" + JSONObject.toJSONString(requestData)); // 记录请求数据
JSONObject jsonRequest = new JSONObject(requestData); JSONObject jsonRequest = new JSONObject(requestData); // 将Map转换为JSONObject
String response = callMes(apiPath, jsonRequest); String response = callMes(apiPath, jsonRequest); // 调用MES接口
JSONObject jsonResponse = JSONObject.parseObject(response); JSONObject jsonResponse = JSONObject.parseObject(response); // 解析返回的JSON字符串
obmlog.debug("三方接口返回:" + jsonResponse.toJSONString()); obmlog.debug("三方接口返回:" + jsonResponse.toJSONString()); // 记录三方接口返回的数据
String success = jsonResponse.getString("Success"); String success = jsonResponse.getString("Success"); // 获取返回结果中的Success字段
if ("false".equals(success)) { if ("false".equals(success)) { // 判断请求是否成功
String errorMessage = jsonResponse.getString("ErrorMessage"); String errorMessage = jsonResponse.getString("ErrorMessage"); // 获取错误信息
if (errorMessage == null) { if (errorMessage == null) {
errorMessage = "No error message provided by the external system."; errorMessage = "外部系统未提供错误消息。";
} }
throw new BusinessException("同步mes系统失败,错误消息:" + errorMessage); throw new BusinessException("同步mes系统失败,错误消息:" + errorMessage); // 抛出业务异常
} }
} catch (BusinessException e) { } catch (BusinessException e) {
throw e; throw e; // 直接抛出已捕获的业务异常
} catch (Exception e) { } catch (Exception e) {
throw new BusinessException("调用外部接口失败:" + e.getMessage(), e); throw new BusinessException("调用外部接口失败:" + e.getMessage(), e); // 捕获其他异常并包装为业务异常抛出
} }
} }
@ -74,13 +75,13 @@ public class HttpPostOtherSysImpl implements IHttpPostOtherSys {
* 业务请求post方法 * 业务请求post方法
*/ */
private String doPost(String baseurl, Map<String, String> headers, JSONObject jsonPayload) { private String doPost(String baseurl, Map<String, String> headers, JSONObject jsonPayload) {
try (CloseableHttpClient httpClient = HttpClients.createDefault()) { try (CloseableHttpClient httpClient = HttpClients.createDefault()) { // 创建HttpClient实例
HttpPost httpPost = new HttpPost(baseurl); HttpPost httpPost = new HttpPost(baseurl); // 创建HttpPost请求
// Set standard headers // 设置标准请求头
httpPost.setHeader("Content-Type", "application/json;charset=UTF-8"); httpPost.setHeader("Content-Type", "application/json;charset=UTF-8");
// Set custom headers from the map // 设置自定义请求头
if (headers != null) { if (headers != null) {
for (Map.Entry<String, String> entry : headers.entrySet()) { for (Map.Entry<String, String> entry : headers.entrySet()) {
httpPost.setHeader(entry.getKey(), entry.getValue()); httpPost.setHeader(entry.getKey(), entry.getValue());
@ -88,69 +89,75 @@ public class HttpPostOtherSysImpl implements IHttpPostOtherSys {
} }
if (jsonPayload != null) { if (jsonPayload != null) {
StringEntity stringEntity = new StringEntity(jsonPayload.toJSONString(), "UTF-8"); StringEntity stringEntity = new StringEntity(jsonPayload.toJSONString(), "UTF-8"); // 创建请求体
httpPost.setEntity(stringEntity); httpPost.setEntity(stringEntity); // 设置请求体
} }
try (CloseableHttpResponse response = httpClient.execute(httpPost)) { try (CloseableHttpResponse response = httpClient.execute(httpPost)) { // 执行请求
HttpEntity entity = response.getEntity(); HttpEntity entity = response.getEntity(); // 获取响应实体
String responseString = EntityUtils.toString(entity, "UTF-8"); String responseString = EntityUtils.toString(entity, "UTF-8"); // 将响应实体转换为字符串
return responseString; return responseString;
} }
} catch (IOException e) { } catch (IOException e) {
// In a real application, use a proper logging framework // 在实际应用中应使用合适的日志框架记录错误
// e.printStackTrace(); // e.printStackTrace();
throw new RuntimeException("HTTP POST request to " + baseurl + " failed: " + e.getMessage(), e); throw new RuntimeException("HTTP POST 请求到 " + baseurl + " 失败: " + e.getMessage(), e); // 抛出运行时异常
} }
} }
// 提取 .ASPXAUTH 的值 - This method remains unchanged // 提取 .ASPXAUTH 的值 - 此方法保持不变
private String extractAspxAuth(String setCookieHeaderLine) { private String extractAspxAuth(String setCookieHeaderLine) {
// setCookieHeaderLine 是一行完整的 Set-Cookie 响应头例如: // setCookieHeaderLine 是一行完整的 Set-Cookie 响应头例如:
// ".ASPXAUTH=TOKEN_VALUE; path=/; HttpOnly" // ".ASPXAUTH=TOKEN_VALUE; path=/; HttpOnly"
// ".ASPXAUTH=TOKEN_VALUE;" // ".ASPXAUTH=TOKEN_VALUE;"
String[] cookieAttributes = setCookieHeaderLine.split(";"); // 按分号分割 String[] cookieAttributes = setCookieHeaderLine.split(";"); // 按分号分割属性
for (String attribute : cookieAttributes) { for (String attribute : cookieAttributes) {
String trimmedAttribute = attribute.trim(); // 去除前后空格 String trimmedAttribute = attribute.trim(); // 去除前后空格
if (trimmedAttribute.startsWith(".ASPXAUTH=")) { if (trimmedAttribute.startsWith(".ASPXAUTH=")) { // 检查是否是 .ASPXAUTH
return trimmedAttribute.substring(".ASPXAUTH=".length()); // 提取等号后的值 return trimmedAttribute.substring(".ASPXAUTH=".length()); // 提取等号后的值
} }
} }
return null; // 没有找到 .ASPXAUTH return null; // 找到 .ASPXAUTH
} }
/**
* 获取MES系统的认证Token (通常是从Cookie中获取 .ASPXAUTH)
*
* @param mesIpBase MES系统的基础IP地址
* @return .ASPXAUTH 的Token值如果未找到则返回null
*/
private String getMESToken(String mesIpBase) { private String getMESToken(String mesIpBase) {
JSONObject jsonObject = new JSONObject(); JSONObject jsonObject = new JSONObject(); // 构建登录请求的JSON对象
jsonObject.put("userId", USER_ID); jsonObject.put("userId", USER_ID);
jsonObject.put("password", PASSWORD); jsonObject.put("password", PASSWORD);
jsonObject.put("clientType", CLIENT_TYPE); jsonObject.put("clientType", CLIENT_TYPE);
jsonObject.put("epId", EP_ID); jsonObject.put("epId", EP_ID);
String loginUrl = mesIpBase + LOGIN_URL; String loginUrl = mesIpBase + LOGIN_URL; // 拼接完整的登录URL
try (CloseableHttpClient httpClient = HttpClients.createDefault()) { try (CloseableHttpClient httpClient = HttpClients.createDefault()) { // 创建HttpClient实例
HttpPost httpPost = new HttpPost(loginUrl); HttpPost httpPost = new HttpPost(loginUrl); // 创建HttpPost请求用于登录
httpPost.setHeader("Content-Type", "application/json;charset=UTF-8"); httpPost.setHeader("Content-Type", "application/json;charset=UTF-8"); // 设置请求内容类型
StringEntity stringEntity = new StringEntity(jsonObject.toJSONString(), "UTF-8"); StringEntity stringEntity = new StringEntity(jsonObject.toJSONString(), "UTF-8"); // 创建登录请求体
httpPost.setEntity(stringEntity); httpPost.setEntity(stringEntity); // 设置登录请求体
try (CloseableHttpResponse response = httpClient.execute(httpPost)) { try (CloseableHttpResponse response = httpClient.execute(httpPost)) { // 执行登录请求
Header[] setCookieHeaders = response.getHeaders("Set-Cookie"); Header[] setCookieHeaders = response.getHeaders("Set-Cookie"); // 获取响应头中的Set-Cookie字段
if (setCookieHeaders != null) { if (setCookieHeaders != null) {
for (Header header : setCookieHeaders) { for (Header header : setCookieHeaders) {
String aspxAuthValue = extractAspxAuth(header.getValue()); String aspxAuthValue = extractAspxAuth(header.getValue()); // 从Set-Cookie中提取.ASPXAUTH的值
if (aspxAuthValue != null) { if (aspxAuthValue != null) {
return aspxAuthValue; // Found and return .ASPXAUTH token value return aspxAuthValue; // 找到并返回 .ASPXAUTH token
} }
} }
} }
return null; // Return null if .ASPXAUTH cookie is not found, as per return null; // 如果未找到 .ASPXAUTH cookie则返回 null
} }
} catch (IOException e) { } catch (IOException e) {
// 记录IO异常例如网络问题或服务器不可达
throw new RuntimeException("Failed to get MESToken (cookie) from " + loginUrl + " due to IO issue: " + e.getMessage(), e); throw new RuntimeException(" " + loginUrl + " 获取MESToken (cookie) 失败发生IO问题: " + e.getMessage(), e);
} }
} }