箱变-物料-定时重推推送启源

This commit is contained in:
mzr 2025-12-13 16:46:33 +08:00
parent 845885e418
commit 2e5fb548bc
1 changed files with 261 additions and 0 deletions

View File

@ -0,0 +1,261 @@
package nc.bs.uapbd.task.mes.qy;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import nc.bs.dao.BaseDAO;
import nc.bs.dao.DAOException;
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.bd.material.marbasclass.MarBasClassVO;
import nc.vo.fi.pub.SqlUtils;
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.List;
import java.util.Map;
/**
* 箱变-物料-定时重推推送启源
*
* @author mzr
* @date 2025-12-13
*/
public class MaterialToQmsAgainPlugin implements IBackgroundWorkPlugin {
private static final String LOG_INFO_NAME = "qyMesLog";
private static final Log logger = Log.getInstance(LOG_INFO_NAME);
private static final String reqUrl = "/IF_QyErpApi.ashx?action=addwlxx";
private Map<String, String> configParams;
private String startDate = "";
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("xb-config", null);
if (configParams.isEmpty()) {
throw new BusinessException("箱变的QMS接口缺少配置");
}
try {
List<Map<String, String>> pkList = 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()) {
logger.error("MaterialToQmsAgainPlugin-size:" + voList.size());
buildSyncData(voList);
}
} catch (Exception e) {
logger.error("MaterialToQmsPlugin-exp:" + e.getMessage(), e);
}
return null;
}
private List<Map<String, String>> getPkList() throws BusinessException {
String targetCode = configParams.get("xbOrg");
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);
LocalDateTime nowTime = LocalDateTime.now();
String timeString = nowTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
String rangeTimeString = timeString + " 00:00:00";
if (MMValueCheck.isNotEmpty(startDate)) {
// 如果有指定的开始时间使用指定的时间限制查询条件
rangeTimeString = startDate;
}
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 a.def36 != 'Y'" +
" AND " + inStrPkOrg +
" AND (b.ts > '" + rangeTimeString + "' OR a.ts > '" + rangeTimeString + "')";
logger.error("MaterialToQmsAgainPlugin-sql:" + sql);
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 pk_marbasclass = vo.getPk_marbasclass();
String mrlTypeName = MyHelper.getStrValByCondition(MarBasClassVO.getDefaultTableName(), MarBasClassVO.CODE,
"pk_marbasclass = '" + pk_marbasclass + "'");
// 计量单位
// 过滤物料分类
String isPushtoQms = MyHelper.getStrValByCondition(MarBasClassVO.getDefaultTableName(), MarBasClassVO.DEF3,
"pk_marbasclass = '" + pk_marbasclass + "'");
// 计量单位
if (null != isPushtoQms && "Y".equals(isPushtoQms)) {
continue;
}
// 过滤物料分类
if (checkMrlType(mrlTypeName, configParams)) {
continue;
}
Map unitMap = getGoodsInfo(pkMaterial);
// 1=未启用;2=已启用;3=已停用;
Integer enablestate = vo.getEnablestate();
String statusCode = (3 == enablestate) ? "1" : "0";
// 组装数据
JSONObject singleObj = new JSONObject();
// 批次编号用于唯一标识当前传输的物料数据批次便于追溯和批量处理
long cts = System.currentTimeMillis();
singleObj.put("batchid", cts);
JSONObject contentObj = new JSONObject();
contentObj.put("meswlbh", vo.getCode()); // 启源物料编码
contentObj.put("bipwlbh", vo.getCode()); // BIP物料编码
contentObj.put("wlmc", vo.getName()); // 物料名称
String wlxhgg = MyHelper.skipNull(vo.getMaterialtype()) + MyHelper.skipNull(vo.getMaterialspec());
contentObj.put("wlxhgg", wlxhgg); // 物料型号规格
contentObj.put("wlfl", mrlTypeName); // 物料分类
contentObj.put("wlbz", getMrlType(pkMaterial, configParams.get("mrlOrg"))); // 物料标志
contentObj.put("sjjldw", unitMap.get("unitname")); // 设计计量单位
contentObj.put("cgjldw", unitMap.get("deputy_unitname")); // 采购计量单位
contentObj.put("zhxs", unitMap.getOrDefault("convertRate", "1.00")); // 转换系数
contentObj.put("flag_fq", statusCode); // 1/0 传递1代表物料废弃
contentObj.put("bz", vo.getMemo()); // 备注信息
JSONArray contentArr = new JSONArray();
contentArr.add(contentObj);
singleObj.put("Content", contentArr);
/*
{
"batchid": "10001",
"Content": [
{
"meswlbh": "ACC26711", 启源物料编码
"bipwlbh": "ACC26711", BIP物料编码
"wlmc": "铜排",物料名称
"wlxhgg": "15558190831", 物料型号规格
"wlfl": "铜排", 物料分类
"cpfl": "", 如果物料是产品需要填写产品分类
"sjjldw": "", 设计计量单位
"cgjldw": "KG", 采购计量单位
"zhxs": "6", 转换系数用于实物单位与采购单位的换算 1 根铜排的长度为 6.00 精确到两位小数提升换算准确性
"bz": "备注" 备注信息
}
]
}
*/
pushData(singleObj);
updateBs(pkMaterial);
}
}
private void updateBs(String pkMaterial) throws DAOException {
String sql = "update BD_MATERIAL set def36='Y' where pk_material='"
+ pkMaterial + "'";
getDao().executeUpdate(sql);
}
private Object getMrlType(String pkMaterial, String xbOrg) throws DAOException {
String sql = " select martype from bd_materialstock left join org_stockorg on org_stockorg.pk_stockorg = bd_materialstock.pk_org " +
" where pk_material = '" + pkMaterial + "' AND bd_materialstock.dr=0 " + " and org_stockorg.code = '" + xbOrg + "' ";
Object map = (Object) new BaseDAO().executeQuery(sql, new ColumnProcessor());
return map;
}
private boolean checkMrlType(String mrlTypeName, Map<String, String> configParams) throws BusinessException {
// 从配置中获取目标类型字符串
String targetCode = configParams.get("mrlType");
// 配置为空则直接返回false
if (targetCode == null || nc.vo.am.common.util.StringUtils.isEmpty(targetCode)) {
return false;
}
// 按逗号拆分多个类型
String[] typeItems = targetCode.split(",");
// 遍历每个类型检查是否以mrlTypeName为前缀
for (String typeCode : typeItems) {
// 跳过空字符串且判断当前类型是否以目标名称为前缀
if (!typeCode.isEmpty() && typeCode.startsWith(mrlTypeName)) {
return true;
}
}
// 无匹配则返回false
return false;
}
/**
* 推送同步数据
*/
private void pushData(JSONObject param) throws BusinessException {
// 转json字符串的时候保留null值
String jsonStr = JSON.toJSONString(param,
SerializerFeature.WriteMapNullValue,
SerializerFeature.WriteNullStringAsEmpty
);
logger.error("QMS-Material-param = " + jsonStr);
// NCCForUAPLogger.debug("QMS-Material-param = " + jsonStr);
String baseUrl = configParams.get("qmsBaseUrl");
String requestUrl = baseUrl + reqUrl;
logger.error("QMS-Material-url = " + requestUrl);
String result = ThirdPartyPostRequestUtil.sendPostRequest(requestUrl, jsonStr);
logger.error("QMS-Material-res = " + result);
JSONObject resultObj = JSONObject.parseObject(result);
if (!"true".equals(resultObj.getString("success"))) {
logger.error("QMS-Material-error,result[" + resultObj.toJSONString() + "]");
// throw new BusinessException("QMS-Material-error:" + resultObj.getString("message"));
}
}
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 + "' ";
Map map = (Map) new BaseDAO().executeQuery(sql, new MapProcessor());
map.put("convertRate", MyHelper.transferSpecialField(map.get("measrate") + ""));
return map;
}
private void initParams(BgWorkingContext bgwc) {
this.startDate = ValueUtils.getString(bgwc.getKeyMap().get("startDate"));
}
}