381 lines
17 KiB
Java
381 lines
17 KiB
Java
package nc.bs.uapbd.task;
|
||
|
||
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.framework.common.NCLocator;
|
||
import nc.bs.logging.Log;
|
||
import nc.bs.logging.Logger;
|
||
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.trade.business.HYSuperDMO;
|
||
import nc.bs.uapbd.util.MyHelper;
|
||
import nc.bs.uapbd.util.ThirdPartyPostRequestUtil;
|
||
import nc.jdbc.framework.processor.MapListProcessor;
|
||
import nc.jdbc.framework.processor.MapProcessor;
|
||
import nc.pubitf.uapbd.IMaterialPubService;
|
||
import nc.util.mmf.framework.base.MMValueCheck;
|
||
import nc.vo.am.common.util.StringUtils;
|
||
import nc.vo.bd.material.MaterialVO;
|
||
import nc.vo.bd.material.marbasclass.MarBasClassVO;
|
||
import nc.vo.bd.material.plan.MaterialPlanVO;
|
||
import nc.vo.bd.material.prod.MaterialProdVO;
|
||
import nc.vo.bd.material.stock.MaterialStockVO;
|
||
import nc.vo.org.OrgVO;
|
||
import nc.vo.pub.BusinessException;
|
||
import nc.vo.pub.lang.UFBoolean;
|
||
import nc.vo.pub.lang.UFDouble;
|
||
import nc.vo.sm.UserVO;
|
||
import uap.mw.trans.TransactionFactory;
|
||
|
||
import java.text.SimpleDateFormat;
|
||
import java.util.*;
|
||
import java.util.stream.Stream;
|
||
|
||
/**
|
||
* 描述:物料同步到高压mes-已弃用
|
||
*
|
||
* @author lihao
|
||
*/
|
||
public class MaterialToGyMesTaskPlugin implements IBackgroundWorkPlugin {
|
||
private HYSuperDMO superDMO = null;
|
||
private static final String LOG_INFO_NAME = "gymeslog";
|
||
private static final Log logger = Log.getInstance(LOG_INFO_NAME);
|
||
private static final String addUrl = "/bip/order/materialAdd";
|
||
private static final String updateUrl = "/bip/order/materialUpdate";
|
||
private Map<String, String> configParams;
|
||
private String orgId = "";
|
||
|
||
public HYSuperDMO getSuperDMO() {
|
||
|
||
if (superDMO == null) {
|
||
superDMO = new HYSuperDMO();
|
||
}
|
||
|
||
return superDMO;
|
||
}
|
||
|
||
@Override
|
||
public PreAlertObject executeTask(BgWorkingContext arg0) throws BusinessException {
|
||
Logger.error("---start----任务开始运行--");
|
||
try {
|
||
BaseDAO dao = new BaseDAO();
|
||
configParams = MyHelper.getConfigParams("gy-config", null);
|
||
if (configParams.isEmpty()) {
|
||
throw new BusinessException("高压的gymes接口缺少配置");
|
||
}
|
||
|
||
// 获取组织ID
|
||
orgId = MyHelper.getStrValByCondition(
|
||
OrgVO.getDefaultTableName(),
|
||
OrgVO.PK_ORG,
|
||
OrgVO.CODE + " = 'C046' and ISBUSINESSUNIT = 'Y'"
|
||
);
|
||
UFDouble hours = configParams.get("hours") != null ? new UFDouble(configParams.get("hours")) : UFDouble.ONE_DBL.add(UFDouble.ONE_DBL);
|
||
// 在Java中计算时间范围(当前时间前两小时到当前时间)
|
||
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
||
Date endTime = new Date(); // 当前时间
|
||
Calendar calendar = Calendar.getInstance();
|
||
calendar.setTime(endTime);
|
||
calendar.add(Calendar.HOUR_OF_DAY, UFDouble.ZERO_DBL.sub(hours).intValue()); // 减去2小时
|
||
Date startTime = calendar.getTime();
|
||
|
||
// 格式化时间字符串
|
||
String startStr = sdf.format(startTime);
|
||
String endStr = sdf.format(endTime);
|
||
|
||
// 拼接SQL(不使用占位符,直接拼接参数)
|
||
String sql = "SELECT pk_material " +
|
||
"FROM bd_material " +
|
||
"WHERE pk_org = '" + orgId + "' " +
|
||
" AND ts BETWEEN '" + startStr + "' AND '" + endStr + "' " +
|
||
"UNION " +
|
||
"SELECT pk_material " +
|
||
"FROM bd_materialplan " +
|
||
"WHERE pk_org = '" + orgId + "' " +
|
||
" AND ts BETWEEN '" + startStr + "' AND '" + endStr + "' " +
|
||
"UNION " +
|
||
"SELECT pk_material " +
|
||
"FROM bd_materialprod " +
|
||
"WHERE pk_org = '" + orgId + "' " +
|
||
" AND ts BETWEEN '" + startStr + "' AND '" + endStr + "' " +
|
||
"UNION " +
|
||
"SELECT pk_material " +
|
||
"FROM bd_materialstock " +
|
||
"WHERE pk_org = '" + orgId + "' " +
|
||
" AND ts BETWEEN '" + startStr + "' AND '" + endStr + "'";
|
||
|
||
ArrayList<HashMap<String, Object>> al = (ArrayList<HashMap<String, Object>>) dao.executeQuery(sql, new MapListProcessor());
|
||
if (al.isEmpty()) {
|
||
return null;
|
||
}
|
||
List<MaterialVO> list = new ArrayList<>();
|
||
for (HashMap<String, Object> map : al) {
|
||
HYPubBO hyPub = new HYPubBO();
|
||
MaterialVO materialVO = (MaterialVO) hyPub.queryByPrimaryKey((Class) MaterialVO.class, (String) map.get("pk_material"));
|
||
String pkMaterial = materialVO.getPk_material();
|
||
// 如果修改基本信息,判断物料所属组织中是否存在高压生产中心,不存在则跳过
|
||
Integer num = MyHelper.checkIfOrg(configParams, pkMaterial, "gyOrg");
|
||
if (num <= 0) {
|
||
continue;
|
||
}
|
||
list.add(materialVO);
|
||
}
|
||
Logger.error("---alData------" + al);
|
||
|
||
if (!list.isEmpty()) {
|
||
MaterialVO[] useVOs = list.toArray(new MaterialVO[0]);
|
||
buildSyncData(useVOs);
|
||
}
|
||
} catch (Exception e) {
|
||
e.printStackTrace();
|
||
// 异常回滚
|
||
TransactionFactory.getTMProxy().end(e);
|
||
Logger.error("writeBack Error: ", e);
|
||
}
|
||
return null;
|
||
}
|
||
|
||
/**
|
||
* 构建同步数据
|
||
*/
|
||
private void buildSyncData(MaterialVO[] useVOs) throws BusinessException {
|
||
BaseDAO baseDAO = new BaseDAO();
|
||
JSONArray dataArr = new JSONArray();
|
||
for (MaterialVO vo : useVOs) {
|
||
String pkMaterial = vo.getPk_material();
|
||
// 字段值翻译
|
||
String pk_marbasclass = vo.getPk_marbasclass();
|
||
String mrlTypeCode = MyHelper.getStrValByCondition(MarBasClassVO.getDefaultTableName(), MarBasClassVO.CODE,
|
||
"pk_marbasclass = '" + pk_marbasclass + "'");
|
||
// 计量单位
|
||
Map unitMap = getGoodsInfo(pkMaterial);
|
||
Map<String, MaterialPlanVO> planMap = queryMaterialPlanInfoByPks(new String[]{pkMaterial}, orgId,
|
||
new String[]{MaterialPlanVO.ISVIRTUAL, MaterialPlanVO.DEF1});
|
||
UFBoolean isvirtual = new UFBoolean(false);
|
||
String drawingNumber = "";
|
||
if (MMValueCheck.isNotEmpty(planMap) &&
|
||
MMValueCheck.isNotEmpty(planMap.get(pkMaterial))) {
|
||
isvirtual = planMap.get(pkMaterial).getIsvirtual();
|
||
drawingNumber = planMap.get(pkMaterial).getDef1();
|
||
}
|
||
// 库存
|
||
Map stockMap = getStockInfo(pkMaterial);
|
||
// 生产报告
|
||
Map prodMap = getProdInfo(pkMaterial);
|
||
// creator 创建人
|
||
String creator = vo.getCreator();
|
||
String creatorName = MyHelper.getStrValByCondition("sm_user", UserVO.USER_NAME,
|
||
UserVO.CUSERID + " = '" + creator + "'");
|
||
// 1=未启用;2=已启用;3=已停用;
|
||
Integer enablestate = vo.getEnablestate();
|
||
// 组装数据
|
||
JSONObject singleObj = new JSONObject();
|
||
singleObj.put("materialCode", vo.getCode()); // 物料编码
|
||
singleObj.put("bipMaterialId", pkMaterial); // 物料主键
|
||
singleObj.put("materialName", vo.getName()); // 物料名称
|
||
singleObj.put("materialCategory", mrlTypeCode); // 物料分类
|
||
singleObj.put("specification", vo.getMaterialspec()); // 规格
|
||
singleObj.put("model", vo.getMaterialtype()); // 型号文字
|
||
singleObj.put("intervalModel", vo.getMaterialtype()); // 产品类型 例如35-KV-断路器(物料自定义字段)
|
||
// 主单位
|
||
singleObj.put("primaryUnit", unitMap.get("unitname"));
|
||
singleObj.put("materialUnit", unitMap.get("unitname"));
|
||
// singleObj.put("drawingNumber", vo.getGraphid()); // 图号编码
|
||
singleObj.put("drawingNumber", drawingNumber);
|
||
singleObj.put("enableStatus", enablestate - 1); // 启用状态 0 未启动 1 已启动 2 已停用
|
||
singleObj.put("materialType", stockMap.get("martype")); // 物料类型编码
|
||
singleObj.put("batchManagement", stockMap.get("wholemanaflag")); // 批次管理
|
||
singleObj.put("outboundPriority", stockMap.get("outpriority")); // 出库优先级
|
||
singleObj.put("mainWarehouse", stockMap.get("pk_stordoc")); // 主仓库
|
||
singleObj.put("reservation", stockMap.get("remain")); // 预留
|
||
singleObj.put("virtualItem", isvirtual.toString()); // 虚项
|
||
if (prodMap != null && !prodMap.isEmpty()) {
|
||
singleObj.put("productionDepartment", prodMap.get("deptcode")); // 生产部门编码
|
||
singleObj.put("matchingComponents", prodMap.get("iscreatesonprodorder")); // 配套子件
|
||
singleObj.put("issuance", prodMap.get("issend")); // 发料
|
||
singleObj.put("backflushMethod", prodMap.get("conversemethod")); // 倒冲方式
|
||
singleObj.put("postCompletionProcessing", prodMap.get("wghxcl")); // 完工后续处理
|
||
}
|
||
// 库存供货组织编码获取
|
||
String mrlsql = "select code from org_stockorg where pk_stockorg = '" + stockMap.get("pk_suplystockorg") + "' ";
|
||
Map<String, Object> stockorg = (Map<String, Object>) baseDAO.executeQuery(mrlsql, new MapProcessor());
|
||
if (stockorg != null && !stockorg.isEmpty()) {
|
||
singleObj.put("supplyInventoryOrg", stockorg.get("code"));// 库存供货组织
|
||
}
|
||
|
||
singleObj.put("bipCreateBy", creatorName); // 创建人
|
||
dataArr.add(singleObj);
|
||
/**
|
||
* {
|
||
* "list": [
|
||
* {
|
||
* "materialCode": "物料编码",
|
||
* "bipMaterialId": "物料主键",
|
||
* "materialName": "物料名称",
|
||
* "materialCategory": "物料分类",
|
||
* "specification": "规格例如 台 件",
|
||
* "intervalModel": "产品类型 例如35-KV-断路器(物料自定义字段)",
|
||
* "primaryUnit": "主单位例如 1 ",
|
||
* "materialUnit": "单位例如 1 ",
|
||
* "drawingNumber": "图号编码 如 X56434 高压",
|
||
* "enableStatus": "启用状态 0 未启动 1 已启动 2 已停用",
|
||
* "materialType": "物料类型编码",
|
||
* "batchManagement": "批次管理",
|
||
* "outboundPriority": "出库优先级 按BIP类型",
|
||
* "mainWarehouse": "主仓库 按BIP类型",
|
||
* "reservation": "预留 按BIP类型",
|
||
* "virtualItem": "虚项 按BIP类型",
|
||
* "productionDepartment": "生产部门编码 按BIP类型",
|
||
* "matchingComponents": "配套子件 按BIP类型",
|
||
* "issuance": "发料 按BIP类型",
|
||
* "backflushMethod": "倒冲方式 按BIP类型",
|
||
* "postCompletionProcessing": "完工后续处理 按BIP类型",
|
||
* "bipCreateBy": "创建人"
|
||
* }
|
||
* ]
|
||
* }
|
||
*/
|
||
}
|
||
if (!dataArr.isEmpty()) {
|
||
JSONObject jsonObj = new JSONObject();
|
||
jsonObj.put("list", dataArr);
|
||
pushData(jsonObj);
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 推送同步数据
|
||
*/
|
||
private void pushData(JSONObject param) throws BusinessException {
|
||
// String jsonString = param.toJSONString();
|
||
// 转json字符串的时候保留null值
|
||
String jsonStr = JSON.toJSONString(param,
|
||
SerializerFeature.WriteMapNullValue,
|
||
SerializerFeature.WriteNullStringAsEmpty
|
||
);
|
||
logger.error("gymes-Material-param = " + jsonStr);
|
||
// NCCForUAPLogger.debug("gymes-Material-param = " + jsonStr);
|
||
String baseUrl = configParams.get("mesBaseUrl");
|
||
String requestUrl = baseUrl + updateUrl;
|
||
logger.error("gymes-Material-url = " + requestUrl);
|
||
String result = ThirdPartyPostRequestUtil.sendPostRequest(requestUrl, jsonStr);
|
||
JSONObject resultObj = JSONObject.parseObject(result);
|
||
logger.error("gymes-Material-res = " + result);
|
||
|
||
if (!"200".equals(resultObj.getString("code"))) {
|
||
logger.error("gymes-Material-error,result[" + resultObj.toJSONString() + "]");
|
||
throw new BusinessException("gymes-Material-error:" + resultObj.getString("msg"));
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 处理物料VO的通用方法
|
||
*/
|
||
private void processMaterialChange(Object[] objs, List<MaterialVO> list) throws BusinessException {
|
||
HYPubBO hyPub = new HYPubBO();
|
||
for (Object newVO : objs) {
|
||
String pk_material = getPkMaterialFromVO(newVO);
|
||
if (MMValueCheck.isEmpty(pk_material)) {
|
||
continue;
|
||
}
|
||
String pkOrg = getPkOrgFromVO(newVO);
|
||
// 判断各个页签修改信息是否属于高压生产中心
|
||
if (MMValueCheck.isNotEmpty(orgId) && !orgId.equals(pkOrg)) {
|
||
continue;
|
||
}
|
||
MaterialVO materialVO = (MaterialVO) hyPub.queryByPrimaryKey(MaterialVO.class, pk_material);
|
||
list.add(materialVO);
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 从不同类型的VO中获取pk_material
|
||
*/
|
||
private String getPkMaterialFromVO(Object vo) {
|
||
if (vo instanceof MaterialStockVO newVo) {
|
||
return newVo.getPk_material();
|
||
} else if (vo instanceof MaterialPlanVO newVo) {
|
||
return newVo.getPk_material();
|
||
} else if (vo instanceof MaterialProdVO) {
|
||
return ((MaterialProdVO) vo).getPk_material();
|
||
}
|
||
return null;
|
||
}
|
||
|
||
/**
|
||
* 从不同类型的VO中获取pk_org
|
||
*/
|
||
private String getPkOrgFromVO(Object vo) {
|
||
if (vo instanceof MaterialStockVO) {
|
||
return ((MaterialStockVO) vo).getPk_org();
|
||
} else if (vo instanceof MaterialPlanVO) {
|
||
return ((MaterialPlanVO) vo).getPk_org();
|
||
} else if (vo instanceof MaterialProdVO) {
|
||
return ((MaterialProdVO) vo).getPk_org();
|
||
}
|
||
return null;
|
||
}
|
||
|
||
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 Map getStockInfo(String pkMaterial) throws BusinessException {
|
||
String sql = " select pk_suplystockorg, martype, wholemanaflag, outpriority, remain,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 = '" + orgId + "' ";
|
||
Map map = (Map) new BaseDAO().executeQuery(sql, new MapProcessor());
|
||
return map;
|
||
}
|
||
|
||
private Map getProdInfo(String pkMaterial) throws BusinessException {
|
||
String sql = "SELECT b.code deptcode, a.iscreatesonprodorder, a.issend, a.conversemethod, a.converstime, a.wghxcl" +
|
||
" FROM bd_materialprod a" +
|
||
" left join org_dept b on a.pk_prodeptdoc = b.pk_dept " +
|
||
" where a.pk_material = '" + pkMaterial + "' " + " and a.pk_org = '" + orgId + "' ";
|
||
Map map = (Map) new BaseDAO().executeQuery(sql, new MapProcessor());
|
||
return map;
|
||
}
|
||
|
||
private boolean checkIfOrg(String code, Map<String, String> configParams) throws BusinessException {
|
||
String targetCode = configParams.get("mesBaseUrl");
|
||
if (targetCode == null || StringUtils.isEmpty(targetCode)) {
|
||
throw new BusinessException("未配置组织参数");
|
||
}
|
||
String[] orgItem = targetCode.split(",");
|
||
for (String orgCode : orgItem) {
|
||
if (!orgCode.isEmpty() && orgCode.equals(code)) {
|
||
return false;
|
||
}
|
||
}
|
||
return true;
|
||
}
|
||
|
||
public static Map<String, MaterialPlanVO> queryMaterialPlanInfoByPks(String[] pks, String pk_stockorg,
|
||
String[] fields) throws BusinessException {
|
||
Map<String, MaterialPlanVO> map = null;
|
||
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);
|
||
}
|
||
|
||
return map;
|
||
}
|
||
}
|