备料计划同步高压MES改异步调用

This commit is contained in:
mzr 2025-12-20 17:45:41 +08:00
parent 277823f24d
commit c79c178e8d
1 changed files with 74 additions and 39 deletions

View File

@ -4,7 +4,6 @@ import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import nc.bs.businessevent.bd.BDCommonEvent;
import nc.bs.dao.BaseDAO;
import nc.bs.dao.DAOException;
import nc.bs.framework.common.NCLocator;
@ -25,13 +24,15 @@ import nc.vo.mmpac.pickm.entity.PickmItemVO;
import nc.vo.org.OrgVO;
import nc.vo.pmpub.project.ProjectHeadVO;
import nc.vo.pub.BusinessException;
import nc.vo.pubapp.pattern.exception.ExceptionUtils;
import nc.vo.scmpub.util.ArrayUtil;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.stream.Stream;
/**
@ -40,6 +41,8 @@ import java.util.stream.Stream;
public class SyncGyMesPickmUtil {
private static final String LOG_INFO_NAME = "gymeslog";
private static final Log logger = Log.getInstance(LOG_INFO_NAME);
// 异步调用线程池
private static final ExecutorService asyncExecutor = Executors.newFixedThreadPool(1);
private Map<String, String> configParams;
public void process(AggPickmVO[] aggPickmVOS) {
@ -51,7 +54,7 @@ public class SyncGyMesPickmUtil {
buildSyncData(aggPickmVOS);
} catch (Exception e) {
logger.error("同步备料计划到高压MES系统失败: " + e.getMessage(), e);
ExceptionUtils.wrappException(e);
// ExceptionUtils.wrappException(e);
}
}
@ -168,7 +171,9 @@ public class SyncGyMesPickmUtil {
list.put("list", jsonArray);
}
if (!list.isEmpty()) {
// 异步推送数据不阻塞主线程
pushData(list);
logger.error("gyMes-Pickm-异步任务已提交,主线程继续执行");
}
}
@ -185,39 +190,59 @@ public class SyncGyMesPickmUtil {
res = defdocVOs[0].getCode().trim();
}
} catch (DAOException e) {
logger.error("gyMes-SaleOrder-error,getDefCode[" + e.getMessage() + "]");
logger.error("gyMes-Pickm-error,getDefCode[" + e.getMessage() + "]");
}
return res;
}
/**
* 推送同步数据
* 异步推送同步数据
* 注意此方法立即返回不会阻塞调用线程
* 实际的HTTP请求在独立线程中执行即使接口响应很慢也不会影响主任务
*/
private void pushData(JSONObject param) throws BusinessException {
// 转json字符串的时候保留null值
String jsonStr = JSON.toJSONString(param,
SerializerFeature.WriteMapNullValue,
SerializerFeature.WriteNullStringAsEmpty
);
logger.error("gyMes-Pickm-param = " + jsonStr);
String baseUrl = configParams.get("mesBaseUrl");
String requestUrl = baseUrl + configParams.get("materialRequirementsUpdate");
logger.error("gyMes-Pickm-url = " + requestUrl);
String result = ThirdPartyPostRequestUtil.sendPostRequest(requestUrl, jsonStr);
JSONObject resultObj = JSONObject.parseObject(result);
logger.error("gyMes-Pickm-res = " + result);
private void pushData(JSONObject param) {
// 复制配置参数避免异步任务执行时configParams被修改
final Map<String, String> configParamsCopy = new HashMap<>(configParams);
if (!"200".equals(resultObj.getString("code"))) {
logger.error("gyMes-Pickm-error,result[" + resultObj.toJSONString() + "]");
throw new BusinessException("备料计划推送高压MES错误:" + resultObj.getString("msg"));
}
// 提交异步任务立即返回不等待结果
CompletableFuture.runAsync(() -> {
try {
logger.error("gyMes-Pickm-异步任务开始执行");
// 转json字符串的时候保留null值
String jsonStr = JSON.toJSONString(param,
SerializerFeature.WriteMapNullValue,
SerializerFeature.WriteNullStringAsEmpty
);
logger.error("gyMes-Pickm-param = " + jsonStr);
String baseUrl = configParamsCopy.get("mesBaseUrl");
String requestUrl = baseUrl + configParamsCopy.get("materialRequirementsUpdate");
logger.error("gyMes-Pickm-url = " + requestUrl);
// 这里会阻塞异步线程但不会阻塞主线程
String result = ThirdPartyPostRequestUtil.sendPostRequest(requestUrl, jsonStr);
JSONObject resultObj = JSONObject.parseObject(result);
logger.error("gyMes-Pickm-res = " + result);
if (!"200".equals(resultObj.getString("code"))) {
logger.error("gyMes-Pickm-error,result[" + resultObj.toJSONString() + "]");
logger.error("备料计划推送高压MES失败:" + resultObj.getString("msg"));
} else {
logger.error("gyMes-Pickm-推送成功");
}
} catch (Exception e) {
logger.error("异步推送备料计划到高压MES系统失败: " + e.getMessage(), e);
}
}, asyncExecutor);
// 方法立即返回主线程继续执行不会等待接口响应
}
private boolean checkIfOrg(String code, Map<String, String> configParams) throws BusinessException {
private boolean checkIfOrg(String code, Map<String, String> configParams) {
String targetCode = configParams.get("gyOrg");
if (targetCode == null || nc.vo.am.common.util.StringUtils.isEmpty(targetCode)) {
throw new BusinessException("未配置组织参数");
logger.error("gyMes-Pickm-未配置组织参数");
return true; // 未配置时跳过
}
String[] orgItem = targetCode.split(",");
for (String orgCode : orgItem) {
@ -229,23 +254,28 @@ public class SyncGyMesPickmUtil {
}
public static Map<String, MaterialPlanVO> queryMaterialPlanInfoByPks(String[] pks, String pk_stockorg,
String[] fields) throws BusinessException {
String[] fields) {
Map<String, MaterialPlanVO> map = null;
List<String> vids = Stream.of(pks).filter(Objects::nonNull).distinct().toList();
if (vids.isEmpty()) {
try {
List<String> vids = Stream.of(pks).filter(Objects::nonNull).distinct().toList();
if (vids.isEmpty()) {
map = new HashMap<>();
} else {
map = NCLocator.getInstance().lookup(IMaterialPubService.class)
.queryMaterialPlanInfoByPks(vids.toArray(new String[0]), pk_stockorg, fields);
}
} catch (Exception e) {
logger.error("gyMes-Pickm-查询物料计划信息失败: " + e.getMessage(), e);
map = new HashMap<>();
} else {
map = NCLocator.getInstance().lookup(IMaterialPubService.class)
.queryMaterialPlanInfoByPks(vids.toArray(new String[0]), pk_stockorg, fields);
}
return map;
}
private boolean checkBillType(String code, Map<String, String> configParams) throws BusinessException {
private boolean checkBillType(String code, Map<String, String> configParams) {
String targetCode = configParams.get("vbusitype");
if (targetCode == null || nc.vo.am.common.util.StringUtils.isEmpty(targetCode)) {
throw new BusinessException("未配置单据类型参数");
logger.error("gyMes-Pickm-未配置单据类型参数");
return false; // 未配置时不跳过
}
String[] types = targetCode.split(",");
for (String type : types) {
@ -256,11 +286,16 @@ public class SyncGyMesPickmUtil {
return false;
}
private Map getStockInfo(String pkMaterial,String pkOrg) throws BusinessException {
String sql = " select def16,bd_stordoc.code as pk_stordoc" +
" from bd_materialstock left join bd_stordoc on bd_stordoc.pk_stordoc = bd_materialstock.pk_stordoc " +
" where pk_material = '" + pkMaterial + "' " + " and bd_materialstock.pk_org = '" + pkOrg + "' ";
Map map = (Map) new BaseDAO().executeQuery(sql, new MapProcessor());
return map;
private Map getStockInfo(String pkMaterial, String pkOrg) {
try {
String sql = " select def16,bd_stordoc.code as pk_stordoc" +
" from bd_materialstock left join bd_stordoc on bd_stordoc.pk_stordoc = bd_materialstock.pk_stordoc " +
" where pk_material = '" + pkMaterial + "' " + " and bd_materialstock.pk_org = '" + pkOrg + "' ";
Map map = (Map) new BaseDAO().executeQuery(sql, new MapProcessor());
return map != null ? map : new HashMap();
} catch (Exception e) {
logger.error("gyMes-Pickm-查询库存信息失败: " + e.getMessage(), e);
return new HashMap();
}
}
}