lc/lc123/cloud/app/api/controller/WMSPurInController.java

422 lines
26 KiB
Java

package tqq9.lc123.cloud.app.api.controller;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.drew.lang.annotations.NotNull;
import kd.bos.dataentity.OperateOption;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.utils.StringUtils;
import kd.bos.entity.botp.runtime.ConvertOperationResult;
import kd.bos.entity.botp.runtime.PushArgs;
import kd.bos.entity.botp.runtime.SourceBillReport;
import kd.bos.entity.operate.result.IOperateInfo;
import kd.bos.entity.operate.result.OperationResult;
import kd.bos.openapi.common.custom.annotation.ApiController;
import kd.bos.openapi.common.custom.annotation.ApiParam;
import kd.bos.openapi.common.custom.annotation.ApiPostMapping;
import kd.bos.openapi.common.result.CustomApiResult;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.bos.servicehelper.botp.ConvertServiceHelper;
import kd.bos.servicehelper.operation.OperationServiceHelper;
import tqq9.lc123.cloud.app.api.utils.ApiResultExt;
import tqq9.lc123.cloud.app.api.utils.Constants;
import tqq9.lc123.cloud.app.plugin.utils.BotpParamUtils;
import java.math.BigDecimal;
import java.util.*;
/**
* 入库回传java接口插件
*/
@ApiController(value = "AbstractFormPlugin", desc = "WMS-入库回传j")
public class WMSPurInController {
private static String RECEIPTNOTICE = "pm_receiptnotice";//收货通知单
private static String PURINBILL = "im_purinbill";//采购入库单
private static String BILLENTRY = "billentry";//收货通知单分录,采购退货申请单分录
private static String PM_PURREFUNDAPPLYBILL = "pm_purrefundapplybill";//采购退货申请单
private static String TQQ9_OTHERINAPPLY = "tqq9_otherinapply";//其他入库申请
private static String IM_OTHERINBILL = "im_otherinbill";//其他入库
@ApiPostMapping(value = "/WMSPushPurIn", desc = "WMS-入库回传")
public CustomApiResult<ApiResultExt> WMS_PurInPush
(@NotNull @ApiParam(value = "入参", example = "") HashMap<String, Object> data) {
ApiResultExt resultExt = new ApiResultExt();
List<ApiResultExt.ResultBean> results = new ArrayList<>();
String VoucherType = (String) data.get("VoucherType");//单据类型
int index = 0;
if (StringUtils.equals("pm_receiptnotice", VoucherType)) {
String billno = (String) data.get("erpOrderCode");//收货通知单编号
//收货通知单
DynamicObject receiptnotice = BusinessDataServiceHelper.loadSingle(RECEIPTNOTICE, new QFilter[]{new QFilter("billno", QCP.equals, billno)});
JSONObject keys = new JSONObject();
keys.put("billno", billno);
ApiResultExt.ResultBean resultBean = new ApiResultExt.ResultBean();
ApiResultExt.Error error = new ApiResultExt.Error();
error.setRowMsg(new ArrayList<String>());
resultBean.setBillIndex(0);
resultBean.setKeys(keys);
resultBean.setNumber(billno);
resultBean.setType(Constants.TYPE_PUSH);
if (receiptnotice == null) {
index = 1;
List<String> rowMsg = error.getRowMsg();
rowMsg.add("根据传入编号:" + billno + "未找到对应收货通知单");
error.setRowMsg(rowMsg);
error.setEntityKey(Constants.IM_PURINBILL);
error.setKeys(keys);
resultBean.setId("");
resultBean.setBillStatus(false);
resultBean.setErrors(error);
results.add(resultBean);
} else {
Long id = receiptnotice.getLong("id");
Map<String, Object> param = new HashMap<>();//传入转换规则处理参数
HashMap<Long, Set<Long>> entitypkMap = new HashMap<>();//上下游id映射Map
JSONObject detailObject = new JSONObject();
JSONArray details = (JSONArray) data.get("details");
for (Object detail : details) {
JSONObject detail1 = (JSONObject) detail;
String entryId = detail1.getString("erpDetailId");//金蝶源头单据细单号
BigDecimal quantity = detail1.getBigDecimal("quantity");//入库数量
String uniqueCode = detail1.getString("uniqueCode");//商品编码
Date manufactureDate = detail1.getDate("manufactureDate");//生产日期
Date expirationDate = detail1.getDate("expirationDate");//失效期
String batch = detail1.getString("batch");//批次
String registrationCode = detail1.getString("registrationCode");//注册证号
String licenceCode = detail1.getString("licenceCode");//生产许可证
String producer = detail1.getString("producer");//生产厂商
detailObject.put("entryId", entryId);
detailObject.put("uniqueCode", uniqueCode);
detailObject.put("quantity", quantity);
detailObject.put("manufactureDate", manufactureDate);
detailObject.put("expirationDate", expirationDate);
detailObject.put("batch", batch);
detailObject.put("registrationCode", registrationCode);
detailObject.put("licenceCode", licenceCode);
detailObject.put("producer", producer);
detailObject.put("VoucherType", VoucherType);
entitypkMap.computeIfAbsent(id, k -> new HashSet<>()).add(detail1.getLong("erpDetailId"));
param.put(entryId, detailObject);
}
//开始下推
String rule = BotpParamUtils.getBotpRuleId(RECEIPTNOTICE, PURINBILL, "收货通知单_采购入库单_转换规则");
PushArgs pushArgs = BotpParamUtils.getPushArgs(RECEIPTNOTICE, PURINBILL, BILLENTRY, param, entitypkMap, rule);
ConvertOperationResult pushResult = ConvertServiceHelper.pushAndSave(pushArgs);
List<SourceBillReport> billReports = pushResult.getBillReports();
if (!pushResult.isSuccess()) {
for (SourceBillReport billReport : billReports) {
String billMessage = billReport.getFailMessage();
List<String> rowMsg = error.getRowMsg();
rowMsg.add(billMessage);
error.setRowMsg(rowMsg);
error.setEntityKey(PURINBILL);
error.setKeys(keys);
resultBean.setId("");
resultBean.setBillStatus(false);
resultBean.setErrors(error);
results.add(resultBean);
}
} else {
results.add(resultBean);
Set<Object> targetBillIds = pushResult.getTargetBillIds();
DynamicObject[] load = new DynamicObject[targetBillIds.size()];
HashMap<Object, String> reMap = new HashMap<Object, String>();
Iterator<Object> iterator = targetBillIds.iterator();
for (int i = 0; iterator.hasNext(); i++) {
Object next = iterator.next();
DynamicObject dynamicObject = BusinessDataServiceHelper.loadSingle(PURINBILL, new QFilter[]{new QFilter("id", QCP.equals, next)});
load[i] = dynamicObject;
reMap.put(dynamicObject.get("id"), dynamicObject.getString("billno"));
resultBean.setId(next.toString());
resultBean.setBillStatus(true);
}
//开始提交
OperateOption operateOption = OperateOption.create();
OperationResult sumbitResult = OperationServiceHelper.executeOperate(Constants.TYPE_SUBMIT, RECEIPTNOTICE, load, operateOption);
if (!sumbitResult.isSuccess()) {
List<IOperateInfo> allErrorOrValidateInfo = sumbitResult.getAllErrorOrValidateInfo();
if (allErrorOrValidateInfo.size() != 0) {
for (IOperateInfo iOperateInfo : allErrorOrValidateInfo) {
HashMap<String, Object> returnMap = resultExt.addErrorToResultBeanByNumber(results, iOperateInfo, Constants.AR_SETTLERECORD);
results = (List<ApiResultExt.ResultBean>) returnMap.get("ResultBeanList");
}
} else {
HashMap<String, Object> returnMap = resultExt.addOneErrorToResultBeanByNumber(results, sumbitResult.getMessage(), Constants.AR_SETTLERECORD);
results = (List<ApiResultExt.ResultBean>) returnMap.get("ResultBeanList");
}
} else {
//开始审核
OperationResult auditResult = OperationServiceHelper.executeOperate(Constants.TYPE_AUDIT, RECEIPTNOTICE, load, operateOption);
if (!auditResult.isSuccess()) {
List<IOperateInfo> allErrorOrValidateInfo1 = auditResult.getAllErrorOrValidateInfo();
if (allErrorOrValidateInfo1.size() != 0) {
for (IOperateInfo iOperateInfo : allErrorOrValidateInfo1) {
HashMap<String, Object> returnMap = resultExt.addErrorToResultBeanByNumber(results, iOperateInfo, Constants.AR_SETTLERECORD);
results = (List<ApiResultExt.ResultBean>) returnMap.get("ResultBeanList");
}
} else {
HashMap<String, Object> returnMap = resultExt.addOneErrorToResultBeanByNumber(results, auditResult.getMessage(), Constants.AR_SETTLERECORD);
results = (List<ApiResultExt.ResultBean>) returnMap.get("ResultBeanList");
}
}
}
}
}
} else if (StringUtils.equals("pm_purrefundapplybill", VoucherType)) {
String billno = (String) data.get("erpOrderCode");//采购退货单编号
//采购退货单
DynamicObject pm_purrefundapplybill = BusinessDataServiceHelper.loadSingle(PM_PURREFUNDAPPLYBILL, new QFilter[]{new QFilter("billno", QCP.equals, billno)});
JSONObject keys = new JSONObject();
keys.put("billno", billno);
ApiResultExt.ResultBean resultBean = new ApiResultExt.ResultBean();
ApiResultExt.Error error = new ApiResultExt.Error();
error.setRowMsg(new ArrayList<String>());
resultBean.setBillIndex(0);
resultBean.setKeys(keys);
resultBean.setNumber(billno);
resultBean.setType(Constants.TYPE_PUSH);
if (pm_purrefundapplybill == null) {
index = 1;
List<String> rowMsg = error.getRowMsg();
rowMsg.add("根据传入编号:" + billno + "未找到对应采购退货单");
error.setRowMsg(rowMsg);
error.setEntityKey(PURINBILL);
error.setKeys(keys);
resultBean.setId("");
resultBean.setBillStatus(false);
resultBean.setErrors(error);
results.add(resultBean);
} else {
Long id = pm_purrefundapplybill.getLong("id");
Map<String, Object> param = new HashMap<>();//传入转换规则处理参数
HashMap<Long, Set<Long>> entitypkMap = new HashMap<>();//上下游id映射Map
JSONObject detailObject = new JSONObject();
JSONArray details = (JSONArray) data.get("details");
for (Object detail : details) {
JSONObject detail1 = (JSONObject) detail;
String entryId = detail1.getString("erpDetailId");//金蝶源头单据细单号
BigDecimal quantity = detail1.getBigDecimal("quantity");//入库数量
String uniqueCode = detail1.getString("uniqueCode");//商品编码
Date manufactureDate = detail1.getDate("manufactureDate");//生产日期
Date expirationDate = detail1.getDate("expirationDate");//失效期
String batch = detail1.getString("batch");//批次
String registrationCode = detail1.getString("registrationCode");//注册证号
String licenceCode = detail1.getString("licenceCode");//生产许可证
String producer = detail1.getString("producer");//生产厂商
detailObject.put("entryId", entryId);
detailObject.put("uniqueCode", uniqueCode);
detailObject.put("quantity", quantity);
detailObject.put("manufactureDate", manufactureDate);
detailObject.put("expirationDate", expirationDate);
detailObject.put("batch", batch);
detailObject.put("registrationCode", registrationCode);
detailObject.put("licenceCode", licenceCode);
detailObject.put("producer", producer);
detailObject.put("VoucherType", VoucherType);
entitypkMap.computeIfAbsent(id, k -> new HashSet<>()).add(detail1.getLong("erpDetailId"));
param.put(entryId, detailObject);
}
//开始下推
String rule = BotpParamUtils.getBotpRuleId(PM_PURREFUNDAPPLYBILL, PURINBILL, "采购退货申请_采购入库_转换规则_扩展");
PushArgs pushArgs = BotpParamUtils.getPushArgs(PM_PURREFUNDAPPLYBILL, PURINBILL, BILLENTRY, param, entitypkMap, rule);
ConvertOperationResult pushResult = ConvertServiceHelper.pushAndSave(pushArgs);
List<SourceBillReport> billReports = pushResult.getBillReports();
if (!pushResult.isSuccess()) {
for (SourceBillReport billReport : billReports) {
String billMessage = billReport.getFailMessage();
List<String> rowMsg = error.getRowMsg();
rowMsg.add(billMessage);
error.setRowMsg(rowMsg);
error.setEntityKey(PURINBILL);
error.setKeys(keys);
resultBean.setId("");
resultBean.setBillStatus(false);
resultBean.setErrors(error);
results.add(resultBean);
}
} else {
results.add(resultBean);
Set<Object> targetBillIds = pushResult.getTargetBillIds();
DynamicObject[] load = new DynamicObject[targetBillIds.size()];
HashMap<Object, String> reMap = new HashMap<Object, String>();
Iterator<Object> iterator = targetBillIds.iterator();
for (int i = 0; iterator.hasNext(); i++) {
Object next = iterator.next();
DynamicObject dynamicObject = BusinessDataServiceHelper.loadSingle(PURINBILL, new QFilter[]{new QFilter("id", QCP.equals, next)});
load[i] = dynamicObject;
reMap.put(dynamicObject.get("id"), dynamicObject.getString("billno"));
resultBean.setId(next.toString());
resultBean.setBillStatus(true);
}
//开始提交
OperateOption operateOption = OperateOption.create();
OperationResult sumbitResult = OperationServiceHelper.executeOperate(Constants.TYPE_SUBMIT, PM_PURREFUNDAPPLYBILL, load, operateOption);
if (!sumbitResult.isSuccess()) {
List<IOperateInfo> allErrorOrValidateInfo = sumbitResult.getAllErrorOrValidateInfo();
if (allErrorOrValidateInfo.size() != 0) {
for (IOperateInfo iOperateInfo : allErrorOrValidateInfo) {
HashMap<String, Object> returnMap = resultExt.addErrorToResultBeanByNumber(results, iOperateInfo, Constants.AR_SETTLERECORD);
results = (List<ApiResultExt.ResultBean>) returnMap.get("ResultBeanList");
}
} else {
HashMap<String, Object> returnMap = resultExt.addOneErrorToResultBeanByNumber(results, sumbitResult.getMessage(), Constants.AR_SETTLERECORD);
results = (List<ApiResultExt.ResultBean>) returnMap.get("ResultBeanList");
}
} else {
//开始审核
OperationResult auditResult = OperationServiceHelper.executeOperate(Constants.TYPE_AUDIT, PM_PURREFUNDAPPLYBILL, load, operateOption);
if (!auditResult.isSuccess()) {
List<IOperateInfo> allErrorOrValidateInfo1 = auditResult.getAllErrorOrValidateInfo();
if (allErrorOrValidateInfo1.size() != 0) {
for (IOperateInfo iOperateInfo : allErrorOrValidateInfo1) {
HashMap<String, Object> returnMap = resultExt.addErrorToResultBeanByNumber(results, iOperateInfo, Constants.AR_SETTLERECORD);
results = (List<ApiResultExt.ResultBean>) returnMap.get("ResultBeanList");
}
} else {
HashMap<String, Object> returnMap = resultExt.addOneErrorToResultBeanByNumber(results, auditResult.getMessage(), Constants.AR_SETTLERECORD);
results = (List<ApiResultExt.ResultBean>) returnMap.get("ResultBeanList");
}
}
}
}
}
} else if (StringUtils.equals("tqq9_otherinapply", VoucherType)) {
String billno = (String) data.get("erpOrderCode");//其他入库申请单编号
//采购退货单
DynamicObject pm_purrefundapplybill = BusinessDataServiceHelper.loadSingle(TQQ9_OTHERINAPPLY, new QFilter[]{new QFilter("billno", QCP.equals, billno)});
JSONObject keys = new JSONObject();
keys.put("billno", billno);
ApiResultExt.ResultBean resultBean = new ApiResultExt.ResultBean();
ApiResultExt.Error error = new ApiResultExt.Error();
error.setRowMsg(new ArrayList<String>());
resultBean.setBillIndex(0);
resultBean.setKeys(keys);
resultBean.setNumber(billno);
resultBean.setType(Constants.TYPE_PUSH);
if (pm_purrefundapplybill == null) {
index = 1;
List<String> rowMsg = error.getRowMsg();
rowMsg.add("根据传入编号:" + billno + "未找到对应其他入库申请单");
error.setRowMsg(rowMsg);
error.setEntityKey(IM_OTHERINBILL);
error.setKeys(keys);
resultBean.setId("");
resultBean.setBillStatus(false);
resultBean.setErrors(error);
results.add(resultBean);
} else {
Long id = pm_purrefundapplybill.getLong("id");
Map<String, Object> param = new HashMap<>();//传入转换规则处理参数
HashMap<Long, Set<Long>> entitypkMap = new HashMap<>();//上下游id映射Map
JSONObject detailObject = new JSONObject();
JSONArray details = (JSONArray) data.get("details");
for (Object detail : details) {
JSONObject detail1 = (JSONObject) detail;
String entryId = detail1.getString("erpDetailId");//金蝶源头单据细单号
BigDecimal quantity = detail1.getBigDecimal("quantity");//入库数量
String uniqueCode = detail1.getString("uniqueCode");//商品编码
Date manufactureDate = detail1.getDate("manufactureDate");//生产日期
Date expirationDate = detail1.getDate("expirationDate");//失效期
String batch = detail1.getString("batch");//批次
String registrationCode = detail1.getString("registrationCode");//注册证号
String licenceCode = detail1.getString("licenceCode");//生产许可证
String producer = detail1.getString("producer");//生产厂商
detailObject.put("entryId", entryId);
detailObject.put("uniqueCode", uniqueCode);
detailObject.put("quantity", quantity);
detailObject.put("manufactureDate", manufactureDate);
detailObject.put("expirationDate", expirationDate);
detailObject.put("batch", batch);
detailObject.put("registrationCode", registrationCode);
detailObject.put("licenceCode", licenceCode);
detailObject.put("producer", producer);
detailObject.put("VoucherType", VoucherType);
entitypkMap.computeIfAbsent(id, k -> new HashSet<>()).add(detail1.getLong("erpDetailId"));
param.put(entryId, detailObject);
}
//开始下推
String rule = BotpParamUtils.getBotpRuleId(TQQ9_OTHERINAPPLY, IM_OTHERINBILL, "其他入库申请_其他入库_转换规则");
PushArgs pushArgs = BotpParamUtils.getPushArgs(TQQ9_OTHERINAPPLY, IM_OTHERINBILL, BILLENTRY, param, entitypkMap, rule);
ConvertOperationResult pushResult = ConvertServiceHelper.pushAndSave(pushArgs);
List<SourceBillReport> billReports = pushResult.getBillReports();
if (!pushResult.isSuccess()) {
for (SourceBillReport billReport : billReports) {
String billMessage = billReport.getFailMessage();
List<String> rowMsg = error.getRowMsg();
rowMsg.add(billMessage);
error.setRowMsg(rowMsg);
error.setEntityKey(IM_OTHERINBILL);
error.setKeys(keys);
resultBean.setId("");
resultBean.setBillStatus(false);
resultBean.setErrors(error);
results.add(resultBean);
}
} else {
results.add(resultBean);
Set<Object> targetBillIds = pushResult.getTargetBillIds();
DynamicObject[] load = new DynamicObject[targetBillIds.size()];
HashMap<Object, String> reMap = new HashMap<Object, String>();
Iterator<Object> iterator = targetBillIds.iterator();
for (int i = 0; iterator.hasNext(); i++) {
Object next = iterator.next();
DynamicObject dynamicObject = BusinessDataServiceHelper.loadSingle(IM_OTHERINBILL, new QFilter[]{new QFilter("id", QCP.equals, next)});
load[i] = dynamicObject;
reMap.put(dynamicObject.get("id"), dynamicObject.getString("billno"));
resultBean.setId(next.toString());
resultBean.setBillStatus(true);
}
//开始提交
OperateOption operateOption = OperateOption.create();
OperationResult sumbitResult = OperationServiceHelper.executeOperate(Constants.TYPE_SUBMIT, TQQ9_OTHERINAPPLY, load, operateOption);
if (!sumbitResult.isSuccess()) {
List<IOperateInfo> allErrorOrValidateInfo = sumbitResult.getAllErrorOrValidateInfo();
if (allErrorOrValidateInfo.size() != 0) {
for (IOperateInfo iOperateInfo : allErrorOrValidateInfo) {
HashMap<String, Object> returnMap = resultExt.addErrorToResultBeanByNumber(results, iOperateInfo, Constants.AR_SETTLERECORD);
results = (List<ApiResultExt.ResultBean>) returnMap.get("ResultBeanList");
}
} else {
HashMap<String, Object> returnMap = resultExt.addOneErrorToResultBeanByNumber(results, sumbitResult.getMessage(), Constants.AR_SETTLERECORD);
results = (List<ApiResultExt.ResultBean>) returnMap.get("ResultBeanList");
}
} else {
//开始审核
OperationResult auditResult = OperationServiceHelper.executeOperate(Constants.TYPE_AUDIT, TQQ9_OTHERINAPPLY, load, operateOption);
if (!auditResult.isSuccess()) {
List<IOperateInfo> allErrorOrValidateInfo1 = auditResult.getAllErrorOrValidateInfo();
if (allErrorOrValidateInfo1.size() != 0) {
for (IOperateInfo iOperateInfo : allErrorOrValidateInfo1) {
HashMap<String, Object> returnMap = resultExt.addErrorToResultBeanByNumber(results, iOperateInfo, Constants.AR_SETTLERECORD);
results = (List<ApiResultExt.ResultBean>) returnMap.get("ResultBeanList");
}
} else {
HashMap<String, Object> returnMap = resultExt.addOneErrorToResultBeanByNumber(results, auditResult.getMessage(), Constants.AR_SETTLERECORD);
results = (List<ApiResultExt.ResultBean>) returnMap.get("ResultBeanList");
}
}
}
}
}
}
int failCount = index;//失败数量
int successCount = 1 - index;//成功数量
resultExt.setFailCount(failCount);
resultExt.setSuccessCount(successCount);
resultExt.setResult(results);
return CustomApiResult.success(resultExt);
}
}