opt:优化ERP同步代码

This commit is contained in:
2024-12-14 09:49:38 +08:00
parent 57f0fcd1e7
commit 2f2d9b75e5
8 changed files with 167 additions and 226 deletions

View File

@@ -35,7 +35,7 @@ public class ErpServiceUtils {
/**
* 构建api
*/
private K3CloudApi getCloudApi() {
public K3CloudApi getCloudApi() {
IdentifyInfo identifyInfo = new IdentifyInfo();
BeanUtils.copyProperties(erpSec, identifyInfo);
return new K3CloudApi(identifyInfo);

View File

@@ -120,6 +120,12 @@ public class BmFormStruc implements Serializable {
*/
private String pcsn;
/**
* 是否自动同步
*/
private Boolean is_sync;
/**
* 自定义表单字段存json:{"dept":"部门","empp":"员工"}
*/

View File

@@ -117,6 +117,11 @@ public class BmFormStrucDto implements Serializable {
*/
private String pcsn;
/**
* 是否自动同步
*/
private Boolean is_sync;
/**
* 自定义表单字段存json:{"dept":"部门","empp":"员工"}
*/

View File

@@ -135,8 +135,7 @@ public class SyncErpService {
* 单据审核
*/
public JSONObject audit(JSONObject from) {
// return erpServiceUtils.audit(from);
return null;
return erpServiceUtils.audit(from);
}

View File

@@ -3,6 +3,7 @@ package org.nl.wms.flow_manage.flow.service.classprocessimpl;
import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.github.javaparser.utils.Log;
import com.kingdee.bos.webapi.entity.IdentifyInfo;
@@ -12,6 +13,8 @@ import org.apache.commons.lang3.StringUtils;
import org.nl.common.domain.exception.BadRequestException;
import org.nl.common.enums.StatusEnum;
import org.nl.common.utils.ErpServiceUtils;
import org.nl.wms.base_manage.material.service.IMdMeMaterialbaseService;
import org.nl.wms.base_manage.material.service.dao.MdMeMaterialbase;
import org.nl.wms.external_system.erp.dto.ErpQuery;
import org.nl.wms.external_system.erp.dto.ErpSec;
import org.nl.wms.flow_manage.flow.framework.process.nodeType.excess.impl.process.classprocess.ClassProcess;
@@ -22,6 +25,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;
/*
@@ -36,9 +40,11 @@ public class SyncReceiptProcessHandler implements ClassProcess {
IPmFormDataService iPmFormDataService;
@Autowired
ErpSec erpSec;
@Autowired
private IMdMeMaterialbaseService iMdMeMaterialbaseService;
@Resource
private ErpServiceUtils erpServiceUtils;
/**
* 回执单回传
*/
@@ -58,16 +64,30 @@ public class SyncReceiptProcessHandler implements ClassProcess {
throw new BadRequestException("回传失败:回传数据form_data中数据格式不是formid,data类型");
}
//判断生产入库的手工录入与MRP生成方式需回传不同的源单信息
if ("0".equals(formData.getString("FSrcEntryId")) && "PRD_INSTOCK".equals(formType)) {
//查询工序汇报单
ErpQuery reportQuery = new ErpQuery();
reportQuery.setFilterString("FMoNumber ='" + formData.getString("moNumber") + "'");
reportQuery.setOrderString("FId desc");
reportQuery.setFormId("SFC_OperationReport");
JSONArray jsonArray = erpServiceUtils.queryBills(reportQuery);
formData.remove("FSrcEntryId");
formData.put("FSrcBillNo", jsonArray.getJSONObject(0).getString("BillNo"));
}
// if ("0".equals(formData.getString("FSrcEntryId")) && "PRD_INSTOCK".equals(formType)) {
// //查询工序汇报单
// ErpQuery reportQuery = new ErpQuery();
// reportQuery.setFilterString("FMoNumber ='" + formData.getString("moNumber") + "'");
// reportQuery.setOrderString("FId desc");
// reportQuery.setFormId("SFC_OperationReport");
// JSONArray jsonArray = erpServiceUtils.queryBills(reportQuery);
// formData.remove("FSrcEntryId");
// formData.put("FSrcBillNo", jsonArray.getJSONObject(0).getString("BillNo"));
// }
// if ("PRD_PickMtrl".equals(formType)) {
// List<MdMeMaterialbase> materialList = iMdMeMaterialbaseService.list(new LambdaQueryWrapper<MdMeMaterialbase>().eq(MdMeMaterialbase::getMaterial_id, formData.getString("material_id")));
// if (ObjectUtils.isNotEmpty(materialList)) {
// MdMeMaterialbase materialInfo = materialList.get(0);
// //没有启用批次号
// if ("0".equals(materialInfo.getPrint_no())) {
// formData.remove("FLot");
// }
// //辅助单位
// if (!"0".equals(materialInfo.getAssist_unit_id())) {
// formData.put("FSecUnitId", materialInfo.getAssist_unit_id());
// }
// }
// }
String json = formData.toJSONString();
try {
IdentifyInfo identifyInfo = new IdentifyInfo();

View File

@@ -174,7 +174,7 @@ public class CockpitServiceImpl implements CockpitService {
obj4.put("total", total4);
obj4.put("box", box4);
obj4.put("time", time4);
List<TaskInfo> taskList = cockpitMapper.getTaskListByDays(0);
List<TaskInfo> taskList = cockpitMapper.getTaskListByDays(7);
if (ObjectUtil.isNotEmpty(taskList)) {
//料箱库入库
List<TaskInfo> stockList1 = taskList.stream()
@@ -227,7 +227,7 @@ public class CockpitServiceImpl implements CockpitService {
JSONObject obj3 = new JSONObject();
List<MdMeMaterialbase> materList = cockpitMapper.getMaterInfos();
if (ObjectUtil.isNotEmpty(materList)) {
List<MdMeMaterialbase> fstockList = materList.stream().filter(r -> "FStockId".equals(r.getProduct_area())).collect(Collectors.toList());
List<MdMeMaterialbase> fstockList = materList.stream().filter(r -> "FStockId".equals(r.getProduct_area())).collect(Collectors.toList());
List<MdMeMaterialbase> fstockPalletList = materList.stream().filter(r -> "FStockPallet".equals(r.getProduct_area())).collect(Collectors.toList());
if (ObjectUtil.isNotEmpty(fstockList)) {
getMaterialAnalysis(jsonArray1, obj0, obj1, fstockList);
@@ -384,6 +384,7 @@ public class CockpitServiceImpl implements CockpitService {
private static void getMaterialAnalysis(JSONArray jsonArray, JSONObject obj0, JSONObject obj1, List<MdMeMaterialbase> materList) {
//物料分组
Map<String, BigDecimal> groupedMaterials = materList.stream()
.filter(material -> material.getMaterial_name() != null)
.collect(Collectors.groupingBy(
MdMeMaterialbase::getMaterial_name,
Collectors.reducing(BigDecimal.ZERO, MdMeMaterialbase::getNet_weight, BigDecimal::add)

View File

@@ -1,7 +1,6 @@
package org.nl.wms.pm_manage.form_data.service.impl;
import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

View File

@@ -13,11 +13,14 @@ import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.nl.common.domain.exception.BadRequestException;
import org.nl.common.utils.BaseCode;
import org.nl.common.utils.ErpServiceUtils;
import org.nl.common.utils.IdUtil;
import org.nl.common.utils.MapOf;
import org.nl.config.lucene.LuceneAppender;
import org.nl.wms.base_manage.material.service.IMdMeMaterialbaseService;
import org.nl.wms.base_manage.material.service.dao.MdMeMaterialbase;
import org.nl.wms.config_manage.form_struc.service.dao.BmFormStruc;
import org.nl.wms.config_manage.form_struc.service.impl.BmFormStrucServiceImpl;
import org.nl.wms.external_system.erp.dto.ErpQuery;
import org.nl.wms.external_system.erp.dto.ErpSec;
import org.nl.wms.pm_manage.form_data.service.IPmFormDataService;
@@ -50,6 +53,10 @@ public class SyncErpBillsScheduleService {
@Autowired
private SyncFormMappingServiceImpl syncFormMappingServiceImpl;
@Autowired
private BmFormStrucServiceImpl bmFormStrucServiceImpl;
@Autowired
private IMdMeMaterialbaseService iMdMeMaterialbaseService;
@@ -58,6 +65,12 @@ public class SyncErpBillsScheduleService {
@Autowired
private PmFormDataMapper pmFormDataMapper;
@Autowired
private ErpServiceUtils erpServiceUtils;
//组织机构
private static final Map<String, String> BILL_ORG_MAPPING = MapOf.of(
"PRD_PPBOM", "FPrdOrgId",
@@ -92,7 +105,9 @@ public class SyncErpBillsScheduleService {
MDC.put("requestTime", DateUtil.now());
LuceneAppender.traceIdTL.set(BaseCode.intToChars(IdUtil.getLongId()));
//List<SyncFormMapping> list = syncFormMappingServiceImpl.list(new LambdaQueryWrapper<SyncFormMapping>().in(SyncFormMapping::getForm_type, "BD_MATERIAL"));
List<SyncFormMapping> list = syncFormMappingServiceImpl.list(new LambdaQueryWrapper<SyncFormMapping>().in(SyncFormMapping::getForm_type, "ka7c19edf9d4b4b39b8cc4a06802163b0", "PRD_PPBOM", "PRD_MO", "SAL_SaleOrder", "PUR_ReceiveBill", "PUR_MRAPP"));
List<BmFormStruc> strucList = bmFormStrucServiceImpl.list(new LambdaQueryWrapper<BmFormStruc>().eq(BmFormStruc::getIs_sync, "1"));
Set<String> structSet = strucList.stream().map(BmFormStruc::getForm_type).collect(Collectors.toSet());
List<SyncFormMapping> list = syncFormMappingServiceImpl.list(new LambdaQueryWrapper<SyncFormMapping>().in(SyncFormMapping::getForm_type, structSet));
for (SyncFormMapping m : list) {
JSONArray mappingJson = m.getMapping_json();
String mappingString = JSON.toJSONString(mappingJson);
@@ -110,107 +125,22 @@ public class SyncErpBillsScheduleService {
boolean islock = lock.tryLock();
try {
if (islock) {
List<Object> result = new ArrayList<>();
IdentifyInfo identifyInfo = new IdentifyInfo();
BeanUtils.copyProperties(erpSec, identifyInfo);
K3CloudApi cloudApi = new K3CloudApi(identifyInfo);
JSONArray result;
Map errorMsg = new HashMap<>();
// 获取当前日期
String today = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.DAY_OF_MONTH, -10);
String sevenDaysAgo = new SimpleDateFormat("yyyy-MM-dd").format(calendar.getTime());
String filterString = BILL_ORG_MAPPING.get(formType) + " = '750572' AND FDocumentStatus = 'C' ";
if ("ka7c19edf9d4b4b39b8cc4a06802163b0".equals(formType)) {
filterString += " AND F_PMSY_BillStatus = 'A' AND F_PMSY_CreateDate >= '" + sevenDaysAgo + " 00:00:00' AND F_PMSY_CreateDate <= '" + today + " 23:59:59' ";
} else if ("SAL_SaleOrder".equals(formType)) {
filterString += " AND FCloseStatus ='A' AND FCreateDate >= '" + sevenDaysAgo + " 00:00:00' and FCreateDate <= '" + today + " 23:59:59' ";
} else {
filterString += " AND FCreateDate >= '" + sevenDaysAgo + " 00:00:00' and FCreateDate <= '" + today + " 23:59:59' ";
}
// if ("ka7c19edf9d4b4b39b8cc4a06802163b0".equals(formType)) {
// filterString += " AND F_PMSY_StockOrgId='750572' AND F_PMSY_BillStatus ='A' AND F_PMSY_CreateDate >= '2024-11-01 00:00:00' and F_PMSY_CreateDate <= '2024-12-04 23:59:59' ";
// }else{
// filterString += " AND FCreateDate >= '2024-11-01 00:00:00' and FCreateDate <= '2024-12-04 23:59:59' ";
// }
//String filterString = "FUseOrgId='750572'";
String filterString = getFilterString(formType, "", sevenDaysAgo, today);
ErpQuery query = new ErpQuery();
query.setFilterString(filterString);
query.setFormId(formType);
query.setFieldKeys("FID");
query.setLimit(0);
String jsonString = JSON.toJSONString(query);
List<List<Object>> lists = cloudApi.executeBillQuery(jsonString);
Set<String> ids = new HashSet<>();
for (List<Object> list : lists) {
for (Object r : list) {
String fid = r.toString();
if (StringUtils.isEmpty(fid)) {
log.error("单据同步失败,没有找到FID");
continue;
}
ids.add(fid);
}
}
//物料同步
SyncFormMapping syncFormMapping = syncFormMappingServiceImpl.getOne(new LambdaQueryWrapper<SyncFormMapping>().in(SyncFormMapping::getForm_type, "BD_MATERIAL"));
String materialMappingString = JSON.toJSONString(syncFormMapping.getMapping_json());
OperateParam param = new OperateParam();
Map<String, String> error = new HashMap<>();
for (String id : ids) {
try {
param.setId(id);
OperatorResult view = cloudApi.view(formType, param);
RepoStatus status = view.getResult().getResponseStatus();
if (status.isIsSuccess()) {
result.add(view.getResult().getResult());
} else {
ArrayList<RepoError> errors = status.getErrors();
String errorMsg = errors.stream().map(RepoError::getMessage).collect(Collectors.joining(","));
error.put(id, errorMsg);
}
} catch (Exception e) {
log.error("处理ID [{}] 时出现异常: {}", id, e.getMessage());
}
}
if (!CollectionUtils.isEmpty(error)) {
log.error("ERP单据同步失败:{}", error);
}
Set<String> exitFormDataList = pmFormDataMapper.existFormDataList();
外部:
for (Object r : result) {
try {
jsonString = JSON.toJSONString(r);
JSONArray mappingJsonArray = JSONArray.parseArray(mappingJson);
List<PmFormData> formDataList = formDataService.syncAnalyse(mappingJsonArray, formType, dtlSplit, JSON.toJSONString(r));
//主单据
final String mainId = formDataList.stream().filter(rd -> StringUtils.isNotBlank(rd.getCode())).collect(Collectors.toList()).get(0).getId();
Set<String> materials = formDataList.stream().filter(rd -> StringUtils.isBlank(rd.getCode())).map(PmFormData::getMaterial_id).collect(Collectors.toSet());
List<MdMeMaterialbase> materialList = iMdMeMaterialbaseService.list(new QueryWrapper<MdMeMaterialbase>().select("material_id").in("material_id", materials));
Set<String> materialHas = materialList.stream().map(MdMeMaterialbase::getMaterial_id).collect(Collectors.toSet());
for (String m : materials) {
if (!materialHas.contains(m)) {
Boolean materialSync = queryMaterialInfo(materialMappingString, syncFormMapping.getForm_type(), syncFormMapping.getDtl_split(), m, cloudApi);
if (!materialSync) {
continue 外部;
}
}
}
for (PmFormData f : formDataList) {
if (exitFormDataList.contains(f.getId() + "$" + f.getForm_type())) {
continue 外部;
}
//单据明细
if (StringUtils.isBlank(f.getCode())) {
f.setParent_id(mainId + "$" + formType);
}
f.setId(f.getId() + "$" + f.getForm_type());
formDataService.save(f);
}
} catch (Exception e) {
log.error("解析数据 [{}]数据失败: {}", JSON.toJSONString(r), e.getMessage());
}
}
result = erpServiceUtils.queryBills(query);
K3CloudApi k3CloudApi = erpServiceUtils.getCloudApi();
handleBills(mappingJson, formType, dtlSplit, errorMsg, result, k3CloudApi);
}
} catch (Exception ex) {
log.error("同步数据时出现异常: {}", ex.getMessage());
@@ -245,7 +175,7 @@ public class SyncErpBillsScheduleService {
try {
if (islock) {
//单据同步总数
List<Object> result = new ArrayList<>();
JSONArray result;
// 获取当前日期
String timeEnd = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
Calendar calendar = Calendar.getInstance();
@@ -255,123 +185,15 @@ public class SyncErpBillsScheduleService {
timeStart = start;
timeEnd = end;
}
//String filterString = "FUseOrgId='750572'";
String filterString;
if (StringUtils.isNotBlank(code)) {
filterString = "FBillNo = '" + code + "'";
} else {
filterString = BILL_ORG_MAPPING.get(formType) + " = '750572' ";
if ("ka7c19edf9d4b4b39b8cc4a06802163b0".equals(formType)) {
filterString += "AND FDocumentStatus = 'C' AND F_PMSY_BillStatus = 'A' AND F_PMSY_CreateDate >= '" + timeStart + " 00:00:00' AND F_PMSY_CreateDate <= '" + timeEnd + " 23:59:59' ";
} else if ("SAL_SaleOrder".equals(formType)) {
filterString += " AND FDocumentStatus = 'C' AND FCloseStatus ='A' AND FCreateDate >= '" + timeStart + " 00:00:00' and FCreateDate <= '" + timeEnd + " 23:59:59' ";
} else if ("STK_TransferDirect".equals(formType)||"STK_MisDelivery".equals(formType)||"PUR_MRB".equals(formType)||"SAL_RETURNSTOCK".equals(formType)) {
filterString += " AND FDocumentStatus ='B' AND FCreateDate >= '" + timeStart + " 00:00:00' and FCreateDate <= '" + timeEnd + " 23:59:59' ";
} else {
filterString += " AND FDocumentStatus = 'C' AND FCreateDate >= '" + timeStart + " 00:00:00' and FCreateDate <= '" + timeEnd + " 23:59:59' ";
}
}
String filterString = getFilterString(formType, code, timeEnd, timeStart);
ErpQuery query = new ErpQuery();
query.setFilterString(filterString);
query.setFormId(formType);
query.setFieldKeys("FID");
query.setLimit(0);
String jsonString = JSON.toJSONString(query);
IdentifyInfo identifyInfo = new IdentifyInfo();
BeanUtils.copyProperties(erpSec, identifyInfo);
K3CloudApi cloudApi = new K3CloudApi(identifyInfo);
List<List<Object>> lists = cloudApi.executeBillQuery(jsonString);
Set<String> ids = new HashSet<>();
for (List<Object> list : lists) {
for (Object r : list) {
String fid = r.toString();
if (StringUtils.isEmpty(fid)) {
log.error("单据同步失败,没有找到FID");
continue;
}
ids.add(fid);
}
}
OperateParam param = new OperateParam();
for (String id : ids) {
try {
param.setId(id);
OperatorResult view = cloudApi.view(formType, param);
RepoStatus status = view.getResult().getResponseStatus();
if (status.isIsSuccess()) {
result.add(view.getResult().getResult());
} else {
errorMsg.put(id, errorMsg);
}
} catch (Exception e) {
errorMsg.put("id","处理ID异常"+e.getMessage());
}
}
//物料同步
SyncFormMapping syncFormMapping = syncFormMappingServiceImpl.getOne(new LambdaQueryWrapper<SyncFormMapping>().in(SyncFormMapping::getForm_type, "BD_MATERIAL"));
String materialMappingString = JSON.toJSONString(syncFormMapping.getMapping_json());
Set<String> exitFormDataList = pmFormDataMapper.existFormDataList();
外部:
for (int i = 0; i < result.size(); i++) {
Object r = result.get(i);
try {
JSONArray mappingJsonArray = JSONArray.parseArray(mappingJson);
List<PmFormData> formDataList = formDataService.syncAnalyse(mappingJsonArray, formType, dtlSplit, JSON.toJSONString(r));
//主单据
List<PmFormData> mainBill = formDataList.stream().filter(rd -> StringUtils.isNotBlank(rd.getCode())).collect(Collectors.toList());
//主单据Id
final String mainId = mainBill.get(0).getId();
//仓库Key
final String stockId = BILL_STOCK_ID_MAPPING.get(formType);
//除了合格证相关单据,其他都过滤非立库仓库组织
if (!"PRD_MO".equals(formType) && !"PUR_ReceiveBill".equals(formType)) {
//明细单据
List<PmFormData> detailBills = formDataList.stream()
.filter(rs -> StringUtils.isBlank(rs.getCode()))
.collect(Collectors.toList());
//仓库信息
List<PmFormData> detailWithStock = detailBills.stream()
.filter(rd -> StringUtils.isNotBlank(rd.getForm_data().getString(stockId)))
.collect(Collectors.toList());
//托盘1233925与料箱库1233926
List<PmFormData> filteredDetails = detailWithStock.stream()
.filter(rt -> new HashSet<>(Arrays.asList("1233925", "1233926")).contains(rt.getForm_data().getString(stockId)))
.collect(Collectors.toList());
if (ObjectUtils.isNotEmpty(filteredDetails)) {
mainBill.addAll(filteredDetails);
} else {
continue;
}
} else {
mainBill = formDataList;
}
//检查物料信息,如果不存在则新增物料
Set<String> materials = mainBill.stream().filter(rd -> StringUtils.isBlank(rd.getCode())).map(PmFormData::getMaterial_id).collect(Collectors.toSet());
List<MdMeMaterialbase> materialList = iMdMeMaterialbaseService.list(new QueryWrapper<MdMeMaterialbase>().select("material_id").in("material_id", materials));
Set<String> materialHas = materialList.stream().map(MdMeMaterialbase::getMaterial_id).collect(Collectors.toSet());
for (String m : materials) {
if (!materialHas.contains(m)) {
Boolean materialSync = queryMaterialInfo(materialMappingString, syncFormMapping.getForm_type(), syncFormMapping.getDtl_split(), m, cloudApi);
if (!materialSync) {
continue 外部;
}
}
}
for (PmFormData f : mainBill) {
if (exitFormDataList.contains(f.getId() + "$" + f.getForm_type())) {
continue 外部;
}
//单据明细
if (StringUtils.isBlank(f.getCode())) {
f.setParent_id(mainId + "$" + formType);
}
f.setId(f.getId() + "$" + f.getForm_type());
formDataService.save(f);
}
} catch (Exception e) {
errorMsg.put("result第"+i+1+"条解析异常:",e.getMessage());
}
}
result = erpServiceUtils.queryBills(query);
K3CloudApi k3CloudApi = erpServiceUtils.getCloudApi();
handleBills(mappingJson, formType, dtlSplit, errorMsg, result, k3CloudApi);
if (!CollectionUtils.isEmpty(errorMsg)){
throw new BadRequestException("同步成功"+(result.size()-errorMsg.size())+"条,失败信息:"+errorMsg.toString());
}else {
@@ -389,6 +211,94 @@ public class SyncErpBillsScheduleService {
}
}
private static String getFilterString(String formType, String code, String timeEnd, String timeStart) {
String filterString;
if (StringUtils.isNotBlank(code)) {
filterString = "FBillNo = '" + code + "'";
} else {
filterString = BILL_ORG_MAPPING.get(formType) + " = '750572' ";
if ("ka7c19edf9d4b4b39b8cc4a06802163b0".equals(formType)) {
filterString += "AND FDocumentStatus = 'C' AND F_PMSY_BillStatus = 'A' AND F_PMSY_CreateDate >= '" + timeStart + " 00:00:00' AND F_PMSY_CreateDate <= '" + timeEnd + " 23:59:59' ";
} else if ("SAL_SaleOrder".equals(formType)) {
filterString += " AND FDocumentStatus = 'C' AND FCloseStatus ='A' AND FCreateDate >= '" + timeStart + " 00:00:00' and FCreateDate <= '" + timeEnd + " 23:59:59' ";
} else if ("STK_TransferDirect".equals(formType)||"STK_MisDelivery".equals(formType)||"PUR_MRB".equals(formType)||"SAL_RETURNSTOCK".equals(formType)) {
filterString += " AND FDocumentStatus ='B' AND FCreateDate >= '" + timeStart + " 00:00:00' and FCreateDate <= '" + timeEnd + " 23:59:59' ";
} else {
filterString += " AND FDocumentStatus = 'C' AND FCreateDate >= '" + timeStart + " 00:00:00' and FCreateDate <= '" + timeEnd + " 23:59:59' ";
}
}
return filterString;
}
private void handleBills(String mappingJson, String formType, Boolean dtlSplit, Map errorMsg, JSONArray result, K3CloudApi cloudApi) {
//物料同步
SyncFormMapping syncFormMapping = syncFormMappingServiceImpl.getOne(new LambdaQueryWrapper<SyncFormMapping>().in(SyncFormMapping::getForm_type, "BD_MATERIAL"));
String materialMappingString = JSON.toJSONString(syncFormMapping.getMapping_json());
Set<String> exitFormDataList = pmFormDataMapper.existFormDataList();
外部:
for (int i = 0; i < result.size(); i++) {
Object r = result.get(i);
try {
JSONArray mappingJsonArray = JSONArray.parseArray(mappingJson);
List<PmFormData> formDataList = formDataService.syncAnalyse(mappingJsonArray, formType, dtlSplit, JSON.toJSONString(r));
//主单据
List<PmFormData> mainBill = formDataList.stream().filter(rd -> StringUtils.isNotBlank(rd.getCode())).collect(Collectors.toList());
//主单据Id
final String mainId = mainBill.get(0).getId();
//仓库Key
final String stockId = BILL_STOCK_ID_MAPPING.get(formType);
//除了合格证相关单据,其他都过滤非立库仓库组织
// if (!"PRD_MO".equals(formType) && !"PUR_ReceiveBill".equals(formType)) {
if (false) {
//明细单据
List<PmFormData> detailBills = formDataList.stream()
.filter(rs -> StringUtils.isBlank(rs.getCode()))
.collect(Collectors.toList());
//仓库信息
List<PmFormData> detailWithStock = detailBills.stream()
.filter(rd -> StringUtils.isNotBlank(rd.getForm_data().getString(stockId)))
.collect(Collectors.toList());
//托盘1233925与料箱库1233926
List<PmFormData> filteredDetails = detailWithStock.stream()
.filter(rt -> new HashSet<>(Arrays.asList("1233925", "1233926")).contains(rt.getForm_data().getString(stockId)))
.collect(Collectors.toList());
if (ObjectUtils.isNotEmpty(filteredDetails)) {
mainBill.addAll(filteredDetails);
} else {
continue;
}
} else {
mainBill = formDataList;
}
//检查物料信息,如果不存在则新增物料
Set<String> materials = mainBill.stream().filter(rd -> StringUtils.isBlank(rd.getCode())).map(PmFormData::getMaterial_id).collect(Collectors.toSet());
List<MdMeMaterialbase> materialList = iMdMeMaterialbaseService.list(new QueryWrapper<MdMeMaterialbase>().select("material_id").in("material_id", materials));
Set<String> materialHas = materialList.stream().map(MdMeMaterialbase::getMaterial_id).collect(Collectors.toSet());
for (String m : materials) {
if (!materialHas.contains(m)) {
Boolean materialSync = queryMaterialInfo(materialMappingString, syncFormMapping.getForm_type(), syncFormMapping.getDtl_split(), m, cloudApi);
if (!materialSync) {
continue 外部;
}
}
}
for (PmFormData f : mainBill) {
if (exitFormDataList.contains(f.getId() + "$" + f.getForm_type())) {
continue 外部;
}
//单据明细
if (StringUtils.isBlank(f.getCode())) {
f.setParent_id(mainId + "$" + formType);
}
f.setId(f.getId() + "$" + f.getForm_type());
formDataService.save(f);
}
} catch (Exception e) {
errorMsg.put("result第"+i+1+"条解析异常:",e.getMessage());
}
}
}
/**
* 查询物料信息
@@ -504,8 +414,8 @@ public class SyncErpBillsScheduleService {
}
OperateParam param = new OperateParam();
Map<String, String> error = new HashMap<>();
List<PmFormData> formList = formDataService.list(new LambdaQueryWrapper<PmFormData>().eq(PmFormData::getForm_type, "BD_MATERIAL_1"));
Set<String> idSet1 = formList.stream().map(PmFormData::getParent_id).collect(Collectors.toSet());
List<MdMeMaterialbase> formList = iMdMeMaterialbaseService.list();
Set<String> idSet1 = formList.stream().map(MdMeMaterialbase::getMaterial_id).collect(Collectors.toSet());
Set<String> differenceSet = new HashSet<>(ids);
differenceSet.removeAll(idSet1);
//数据库已存在的数据
@@ -517,8 +427,9 @@ public class SyncErpBillsScheduleService {
if (status.isIsSuccess()) {
JSONArray mappingJsonArray = JSONArray.parseArray(mappingJson);
List<PmFormData> formDataList = formDataService.syncAnalyse(mappingJsonArray, formType, dtlSplit, JSON.toJSONString(view.getResult().getResult()));
if (!formDataList.isEmpty()) {
formDataService.saveBatch(formDataList);
List<PmFormData> formData = formDataList.stream().filter(r -> StringUtils.isBlank(r.getCode())).collect(Collectors.toList());
if (!formData.isEmpty()) {
formDataService.saveBatch(formData);
}
} else {
ArrayList<RepoError> errors = status.getErrors();