feat(mes): 新增电力电子物料定时推送艾普MES功能

This commit is contained in:
mzr 2025-11-11 21:03:15 +08:00
parent 1ac9e4d4f7
commit 258d1b5f15
1 changed files with 251 additions and 0 deletions

View File

@ -0,0 +1,251 @@
package nc.bs.uapbd.task.mes.epic;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import nc.bs.dao.BaseDAO;
import nc.bs.logging.Log;
import nc.bs.pub.pa.PreAlertObject;
import nc.bs.pub.taskcenter.BgWorkingContext;
import nc.bs.pub.taskcenter.IBackgroundWorkPlugin;
import nc.bs.trade.business.HYPubBO;
import nc.bs.uapbd.util.MyHelper;
import nc.bs.uapbd.util.ThirdPartyPostRequestUtil;
import nc.jdbc.framework.processor.ColumnProcessor;
import nc.jdbc.framework.processor.MapListProcessor;
import nc.jdbc.framework.processor.MapProcessor;
import nc.util.mmf.framework.base.MMValueCheck;
import nc.vo.bd.material.MaterialVO;
import nc.vo.fi.pub.SqlUtils;
import nc.vo.org.OrgVO;
import nc.vo.pub.BusinessException;
import nc.vo.pubapp.pattern.data.ValueUtils;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* 电力电子-物料-定时推送艾普MES
*
* @author mzr
* @date 2025-11-11
*/
public class MaterialToEpicMesCronPlugin implements IBackgroundWorkPlugin {
private static final String LOG_INFO_NAME = "dldzlog";
private static final Log logDl = Log.getInstance(LOG_INFO_NAME);
private static final String reqUrl = "/prj-v5-web/ext/api/mrl";
private Map<String, String> configParams;
private int timeRange = 0;
private BaseDAO dao;
public BaseDAO getDao() {
if (dao == null) {
dao = new BaseDAO();
}
return dao;
}
@Override
public PreAlertObject executeTask(BgWorkingContext bgWorkingContext) throws BusinessException {
this.initParams(bgWorkingContext);
configParams = MyHelper.getConfigParams("Dldz-config", null);
if (configParams.isEmpty()) {
throw new BusinessException("电力电子的艾普MES接口缺少配置");
}
try {
List<Map<String, String>> pkList = this.getPkList();
if (pkList == null || pkList.isEmpty()) {
return null;
}
HYPubBO hyPub = new HYPubBO();
List<MaterialVO> voList = new ArrayList<>();
for (Map<String, String> map : pkList) {
String pkMaterial = map.get("pk_material");
MaterialVO materialVO = (MaterialVO) hyPub.queryByPrimaryKey(MaterialVO.class, pkMaterial);
if (materialVO != null) {
voList.add(materialVO);
}
}
if (!voList.isEmpty()) {
buildSyncData(voList);
}
} catch (Exception e) {
logDl.error("MaterialToEpicMesPlugin-exp:" + e.getMessage(), e);
}
return null;
}
private List<Map<String, String>> getPkList() throws BusinessException {
String targetCode = configParams.get("dldzOrg");
if (targetCode == null || MMValueCheck.isEmpty(targetCode)) {
throw new BusinessException("未配置组织参数");
}
String[] orgItem = targetCode.split(",");
String inStr = SqlUtils.getInStr("code", orgItem, true);
String orgSql = " SELECT pk_stockorg,code,name FROM org_stockorg WHERE DR = 0 AND " + inStr;
List<Map<String, String>> orgList = (List<Map<String, String>>) getDao().executeQuery(orgSql, new MapListProcessor());
if (orgList.isEmpty()) {
return null;
}
String[] stockorgs = orgList.stream().map(map -> map.get("pk_stockorg")).distinct().toArray(String[]::new);
String inStrPkOrg = SqlUtils.getInStr("b.pk_org", stockorgs, true);
// 计算 timeRange 小时前的时间
LocalDateTime rangeTime = LocalDateTime.now().minusHours(timeRange);
String rangeTimeString = rangeTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
// 查询最近改过基本信息和库存信息的物料
String sql = " SELECT DISTINCT b.pk_material" +
" FROM bd_materialstock b" +
" LEFT JOIN bd_material a ON a.pk_material = b.pk_material" +
" WHERE b.dr = 0 AND a.dr = 0" +
" AND " + inStrPkOrg +
" AND (b.ts > '" + rangeTimeString + "' OR a.ts > '" + rangeTimeString + "')";
List<Map<String, String>> pkList = (List<Map<String, String>>) getDao().executeQuery(sql, new MapListProcessor());
return pkList;
}
/**
* 构建同步数据
*/
private void buildSyncData(List<MaterialVO> useVOs) throws BusinessException {
for (MaterialVO vo : useVOs) {
String pkMaterial = vo.getPk_material();
String pkOrg = vo.getPk_org();
String orgCode = MyHelper.transferField(OrgVO.getDefaultTableName(), OrgVO.CODE, OrgVO.PK_ORG, pkOrg);
// 计量单位
Map unitMap = getGoodsInfo(pkMaterial);
// 1=未启用;2=已启用;3=已停用;
Integer enablestate = vo.getEnablestate();
String statusCode = (3 == enablestate) ? "N" : "Y";
// 组装数据
JSONObject singleObj = new JSONObject();
singleObj.put("id", null);// 唯一标识主键
singleObj.put("siteCode", orgCode);// 工厂编码
singleObj.put("mrlCode", vo.getCode());// 物料编码
singleObj.put("mrlName", vo.getName());// 物料名称
singleObj.put("unit", unitMap.get("unitname"));// 单位
singleObj.put("model", vo.getMaterialtype());// 型号
singleObj.put("specification", vo.getMaterialspec());// 规格
singleObj.put("type", "");// 类型I:新增 U:修改 D:删除
singleObj.put("deputyUnit", unitMap.get("deputy_unitname"));// 副单位
singleObj.put("auditCode", "1");// 审核码
singleObj.put("statusCode", statusCode);// 状态码Y表示启用N表示停用
// singleObj.put("mrlTypeErp", getGoodsProject(pkMaterial));// 物料类型ERP1:专用件3:通用件
singleObj.put("mrlType", getGoodsType(pkMaterial));// 物料分类 制造1 其它0
singleObj.put("convertRate", unitMap.getOrDefault("convertRate", "1"));// 单位换算率
// singleObj.put("isCheck", "1");// 是否选中1:0:
pushData(singleObj, pkMaterial);
}
}
/**
* 推送同步数据
*/
private void pushData(JSONObject param, String pkMaterial) throws BusinessException {
// String jsonString = param.toJSONString();
// 转json字符串的时候保留null值
String jsonStr = JSON.toJSONString(param,
SerializerFeature.WriteMapNullValue,
SerializerFeature.WriteNullStringAsEmpty
);
logDl.error("EpicMes-Material-param = " + jsonStr);
// NCCForUAPLogger.debug("EpicMes-Material-param = " + jsonStr);
String baseUrl = configParams.get("epicMesUrl");
String requestUrl = baseUrl + reqUrl;
logDl.error("EpicMes-Material-url = " + requestUrl);
String result = ThirdPartyPostRequestUtil.sendPostRequest(requestUrl, jsonStr);
JSONObject resultObj = JSONObject.parseObject(result);
logDl.error("EpicMes-Material-res = " + result);
if (!"1".equals(resultObj.getString("flag"))) {
// throw new BusinessException("EpicMes-Material-error:" + resultObj.getString("msg"));
logDl.error("EpicMes-Material-error,result[" + resultObj.toJSONString() + "]");
} else {
handleSyncMaterialExp(pkMaterial, "Y");
}
}
private String getType(String eventType) {
// 类型I:新增 U:修改 D:删除
Map<String, String> map = new HashMap<>();
map.put("1002", "I");
map.put("1009", "I");
map.put("1004", "U");
map.put("1069", "U");
map.put("1071", "U");
return map.getOrDefault(eventType, "I");
}
private Map getGoodsInfo(String pkMaterial) throws BusinessException {
String sql = " select a.pk_measdoc, c.name unitname, b.pk_measdoc deputyUnit, d.name deputy_unitname, nvl(b.measrate, '1/1') measrate " +
"from bd_material a " +
"left join bd_materialconvert b on a.pk_material = b.pk_material " +
"left join bd_measdoc c on a.pk_measdoc = c.pk_measdoc " +
"left join bd_measdoc d on b.pk_measdoc = d.pk_measdoc " +
"where a.pk_material = '" + pkMaterial + "' ";
// logDl.error("EpicMes-Material-getUnitInfo-sql = " + sql);
Map map = (Map) new BaseDAO().executeQuery(sql, new MapProcessor());
map.put("convertRate", MyHelper.transferSpecialField(map.get("measrate") + ""));
return map;
}
private String getGoodsType(String pkMaterial) throws BusinessException {
String targetCode = configParams.get("dldzOrg");
String[] orgItem = targetCode.split(",");
String inStr = SqlUtils.getInStr("b.code", orgItem, Boolean.TRUE);
// 物料-库存信息-物料类型 DR=分销补货;FR=工厂补货;MR=制造件;PR=采购件;OT=委外件;ET=其他
// mes物料类型 制造1 其它0(ERP多个组织中如果有一个是制造件或虚拟件(其他)就传1)
String mesType = "0";
// String martype = MyHelper.transferField(MaterialStockVO.getDefaultTableName(), MaterialStockVO.MARTYPE, MaterialStockVO.PK_MATERIAL, pkMaterial);
String countSql = "SELECT count(1)" +
" FROM bd_materialstock a" +
" LEFT JOIN org_stockorg b ON a.pk_org = b.pk_stockorg" +
" WHERE a.dr = 0 AND a.martype IN ('MR','ET') " +
" AND a.pk_material = '[pkMaterial]'" +
" AND " + inStr;
countSql = countSql.replace("[pkMaterial]", pkMaterial);
Integer num = (Integer) new BaseDAO().executeQuery(countSql, new ColumnProcessor());
if (num > 0) {
mesType = "1";
}
// 判断物料是否是虚拟件只要有一个组织是就赋值0
String countSql1 = "SELECT count(1)" +
" FROM bd_materialplan a" +
" LEFT JOIN org_orgs b ON a.pk_org = b.pk_org" +
" WHERE a.dr = 0 AND a.isvirtual = 'Y' " +
" AND a.pk_material = '[pkMaterial]'" +
" AND b.isbusinessunit = 'Y'" +
" AND " + inStr;
countSql1 = countSql1.replace("[pkMaterial]", pkMaterial);
Integer num1 = (Integer) new BaseDAO().executeQuery(countSql1, new ColumnProcessor());
if (num1 > 0) {
mesType = "0";
}
return mesType;
}
/**
* 更新物料推送标志
*/
private void handleSyncMaterialExp(String pk_material, String errorCode) {
if (MMValueCheck.isEmpty(pk_material)) {
return;
}
try {
String updateSql = "update bd_material set def30 = '[errorCode]' where pk_material = '[pk_material]'";
updateSql = updateSql.replace("[pk_material]", pk_material);
updateSql = updateSql.replace("[errorCode]", errorCode);
int updatedRows = getDao().executeUpdate(updateSql);
} catch (BusinessException e) {
logDl.error("EpicMes-Material-handleSyncMaterialExp = " + e.getMessage(), e);
}
}
private void initParams(BgWorkingContext bgwc) {
this.timeRange = ValueUtils.getInt(bgwc.getKeyMap().get("timeRange"));
}
}