feat(完工入库报工): 新增物料消耗报工job

This commit is contained in:
chengzhaofeng 2025-01-08 19:41:52 +08:00
parent 4bfa3023c6
commit 25d8510266

View File

@ -0,0 +1,208 @@
package com.foreverwin.iep.integration.handler.def.job;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.foreverwin.iep.integration.domain.InvokeRequest;
import com.foreverwin.iep.integration.domain.InvokeResponse;
import com.foreverwin.iep.integration.handler.def.sap.GoodMvtCreateService;
import com.foreverwin.iep.integration.handler.def.sap.SapUtil;
import com.foreverwin.iep.integration.utils.JS;
import com.foreverwin.iep.prd.entity.ZLotMaterialCompleteTransaction;
import com.foreverwin.iep.prd.entity.ZLotMaterialConsumptionTransaction;
import com.foreverwin.iep.prd.mapper.ZLotMaterialCompleteTransactionMapper;
import com.foreverwin.iep.prd.mapper.ZLotMaterialConsumptionTransactionMapper;
import com.foreverwin.mom.common.core.exception.BusinessException;
import com.foreverwin.mom.common.data.tenant.TenantContextHolder;
import com.foreverwin.mom.common.security.util.SecurityUtils;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Component;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
/**
* 101完工入库JOB
*/
@Component
@AllArgsConstructor
public class SapMaterialConsumptionJob {
private final ZLotMaterialConsumptionTransactionMapper zLotMaterialConsumptionTransactionMapper;
private final GoodMvtCreateService goodMvtCreateService;
@XxlJob("sapMaterialConsumptionJobHandler")
public ReturnT<String> sapMaterialConsumptionJobHandler() {
try {
String site = null;
String startDate = null;
String endDate = null;
InvokeResponse invokeResponse = new InvokeResponse();
List<ZLotMaterialConsumptionTransaction> newList = new ArrayList<>();
String postingDate = "";
JSONArray itemList = new JSONArray();
List<ZLotMaterialConsumptionTransaction> srcList = new ArrayList<>();
String jobParam = XxlJobHelper.getJobParam();
if (StrUtil.isNotBlank(jobParam)) {
Map<String, String> paramMap = JS.parseMap(jobParam, "=", ";");
site = paramMap.get("site");
startDate = paramMap.get("startDate");
endDate = paramMap.get("endDate");
}
if (StrUtil.isBlank(site)) {
XxlJobHelper.log("ERROR:未维护站点[site]参数");
return ReturnT.FAIL;
} else {
TenantContextHolder.setTenantId(site);
if (StrUtil.isBlank(startDate) && StrUtil.isBlank(endDate)) {
// 没有昨天一天的数据,默认昨天
startDate = DateUtil.format(DateUtil.beginOfDay(DateUtil.yesterday()), "yyyy-MM-dd HH:mm:ss");
endDate = DateUtil.format(DateUtil.endOfDay(DateUtil.yesterday()), "yyyy-MM-dd HH:mm:ss");
}
List<ZLotMaterialConsumptionTransaction> zLotMaterialConsumptionTransactionList = zLotMaterialConsumptionTransactionMapper.selectList(
Wrappers.<ZLotMaterialConsumptionTransaction>lambdaQuery().eq(
ZLotMaterialConsumptionTransaction::getSite, site).ne(
ZLotMaterialConsumptionTransaction::getSyncStatus, "S")
.gt(ZLotMaterialConsumptionTransaction::getDateCreate, startDate)
.lt(ZLotMaterialConsumptionTransaction::getDateCreate, endDate));
if (CollectionUtils.isEmpty(zLotMaterialConsumptionTransactionList)) {
XxlJobHelper.log("WARN:没有需要同步的261物料消耗数据!!!");
return ReturnT.SUCCESS;
}
for (ZLotMaterialConsumptionTransaction consumptionTransaction : zLotMaterialConsumptionTransactionList) {
// 重新校验一遍是否已经报过工,防止重复报工
ZLotMaterialConsumptionTransaction targetObj = zLotMaterialConsumptionTransactionMapper.selectById(consumptionTransaction.getSid());
if ("S".equals(targetObj.getSyncStatus())) {
throw BusinessException.build("批次" + targetObj.getLotNo() + "消耗已经报工,不可重复报工");
} else {
postingDate = consumptionTransaction.getSyncPostingDate().format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
targetObj.setSyncPostingDate(consumptionTransaction.getSyncPostingDate());
srcList.add(targetObj);
JSONObject jsonObject = new JSONObject();
site = consumptionTransaction.getSite();
jsonObject.put("ZEILE", ""); // 物料凭证中的项目
jsonObject.put("MATNR", consumptionTransaction.getErpItem()); // 物料编号
jsonObject.put("MATKL", ""); // 物料组
jsonObject.put("WERKS", site); // 工厂
jsonObject.put("LGORT", consumptionTransaction.getErpInventory()); // 存储地点
jsonObject.put("CHARG", consumptionTransaction.getSapBatchNo()); // 批次号
jsonObject.put("BWART", "261"); // 移动类型
jsonObject.put("SOBKZ", ""); // 特殊库存标识
jsonObject.put("AUFNR", consumptionTransaction.getShopOrder()); // 订单号
jsonObject.put("EBELN", ""); // 采购订单号
jsonObject.put("EBELP", ""); // 采购凭证的项目编号
jsonObject.put("MENGE", consumptionTransaction.getMainQty()); // 数量
jsonObject.put("MEINS", consumptionTransaction.getMainUom()); // 基本计量单位
if(consumptionTransaction.getSubQty()!=null){
jsonObject.put("ERFMG", consumptionTransaction.getSubQty()); // 以录入项单位表示的数量
jsonObject.put("ERFME", consumptionTransaction.getSubUom()); // 录入单位
}else{
jsonObject.put("ERFMG", null); // 以录入项单位表示的数量
jsonObject.put("ERFME", ""); // 录入单位
}
jsonObject.put("MAT_KDAUF", ""); // 评估销售定单库存的销售定单号
jsonObject.put("MAT_KDPOS", ""); // 评估销售订单库存的销售订单项目
jsonObject.put("UMWRK", ""); // 收货工厂/发货工厂
jsonObject.put("UMLGO", ""); // 收货/发货库存地点
jsonObject.put("UMCHA", ""); // 收货/发货批量
jsonObject.put("RSNUM", consumptionTransaction.getReservedNo()); // 预留/相关需求的编号
jsonObject.put("RSPOS", consumptionTransaction.getReservedLine()); // 预留 / 相关需求的项目编号
itemList.add(jsonObject);
newList.add(targetObj);
}
}
if (!itemList.isEmpty()) {
InvokeRequest invokeRequest = new InvokeRequest();
// 传参对象
try {
LocalDateTime nowLocalDateTime = LocalDateTime.now();
String nowTime = nowLocalDateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
invokeRequest.setTransSid(SapUtil.generateReqId());
invokeRequest.setTransTime(nowTime);
invokeRequest.setFromSystem("IEP");
invokeRequest.setToSystem("ERP");
invokeRequest.setSite(site);
invokeRequest.setMessageId("ZMM_MES_GOODSMVT_CREATE");
// 组装抛送的报完呢
String jsonObjectStr = SapUtil.appendHead(assembleMaterialMsgBody(itemList, postingDate));
invokeRequest.setMessageContent(jsonObjectStr);
invokeRequest.setStart(nowTime);
invokeRequest.setKeyValue(nowTime);
invokeRequest.setRetry(false);
TenantContextHolder.setTenantId(site);
invokeResponse = goodMvtCreateService.call(invokeRequest);
// 处理ERP返回的报文
invokeResponse = updateMaterialConsumption(invokeResponse.getObjectResponse(), srcList, invokeResponse);
} catch (Exception e) {
// 不用回滚,将错误抛出
invokeResponse.setResult("E");
invokeResponse.setMessageContent(e.getMessage());
}
}
if ("E".equals(invokeResponse.getResult())) {
XxlJobHelper.log("ERROR:" + invokeResponse.getMessageContent());
return ReturnT.FAIL;
} else {
XxlJobHelper.log("SUCCESS:101完工入库同步ERP:" + DateUtil.now());
return ReturnT.SUCCESS;
}
}
} catch (Exception e) {
XxlJobHelper.handleFail("ERROR:101完工入库同步ERP失败:" + e);
return ReturnT.FAIL;
}
}
public InvokeResponse updateMaterialConsumption(JSONObject jsonObject, List<ZLotMaterialConsumptionTransaction> srcList, InvokeResponse invokeResponse) {
JSONObject bodyJson = jsonObject.getJSONObject("RESPONSE_BODY");
JSONObject outJson = bodyJson.getJSONObject("ES_OUTPUT");
String result = outJson.getString("MSGTY");
String message = outJson.getString("MSGTX");
String msgno = outJson.getString("MSGNO"); // 系统消息编号
String mblnr = outJson.getString("MBLNR"); // 物料凭证编号
for (ZLotMaterialConsumptionTransaction targetObj : srcList) {
targetObj.setSyncStatus(result);
targetObj.setSyncMsgText(message);
targetObj.setSyncMsgNo(msgno);
targetObj.setSyncSubmitter(SecurityUtils.getUserName());
targetObj.setSyncVoucherNo(mblnr);
targetObj.setCreator(SecurityUtils.getUserName());
targetObj.setDateCreate(LocalDateTime.now());
zLotMaterialConsumptionTransactionMapper.updateById(targetObj);
}
invokeResponse.setResult(result);
invokeResponse.setMessageContent(message);
return invokeResponse;
}
// 凭证中的过账日期 yyyy-MM-dd
private String assembleMaterialMsgBody(JSONArray itemList, String postingDate) {
JSONObject jsonObject = new JSONObject();
jsonObject.put("RFC_FUNCTION", "ZMM_MES_GOODSMVT_CREATE");
JSONObject headRe = new JSONObject();
headRe.put("EXCHANGE_SYSTEM", "MES");
headRe.put("EXCHANGE_ID", SapUtil.generateReqId());
headRe.put("EXCHANGE_DOC", "iMES_Call_SAP");
jsonObject.put("REQUEST_CONTEXT", headRe);
JSONObject bodyRe = new JSONObject();
JSONObject headBody = new JSONObject();
headBody.put("ZPARTNER", SecurityUtils.getUserName()); // 员工BP编号
headBody.put("BUDAT", postingDate); // 凭证中的过账日期
bodyRe.put("IS_HEAD", headBody);
bodyRe.put("IT_ITEM", itemList);
jsonObject.put("REQUEST_BODY", bodyRe);
return jsonObject.toJSONString();
}
}