opt:单据同步优化;

This commit is contained in:
2024-12-06 14:11:04 +08:00
parent 8b52373500
commit 10ca271230
6 changed files with 222 additions and 118 deletions

View File

@@ -11,7 +11,6 @@ import org.springframework.expression.spel.SpelEvaluationException;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.StandardEvaluationContext;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
@@ -77,10 +76,20 @@ public class SpelUtil {
}
//如果表达式为null,空则返回''
private static Map<String, String> Product_Area_Mapping = MapOf.of(
"827083", "A1", "827082", "A1", "827081", "A1", "827080", "A1", "1028689", "A1", "1028688", "A1", "827077", "A1", "827076", "A1"
, "827075", "A1", "827074", "A1", "827073"
, "827078", "A2", "827079", "A2"
, "", "A3");
"827083", "A1",
"827082", "A1",
"827081", "A1",
"827080", "A1",
"1028689", "A1",
"1028688", "A1",
"827077", "A1",
"827076", "A1",
"827075", "A1",
"827074", "A1",
"827073", "A1",
"827078", "A2",
"827079", "A2"
);
/**
*
*/
@@ -97,7 +106,7 @@ public class SpelUtil {
if (field.equals("product_area")){
String product_area = Product_Area_Mapping.get(value);
if (StringUtils.isEmpty(product_area)){
throw new BadRequestException("当前业务数据product_area"+value+" 对应的车间数据");
product_area = "A1";
}
value = product_area;
}

View File

@@ -78,7 +78,7 @@ public class HeartClientServer extends AbstraceServer {
@Override
public void doConnect() {
System.out.println("-------尝试连接服务器-------");
// System.out.println("-------尝试连接服务器-------");
try {
ChannelFuture connect = ((Bootstrap) server).connect(address);
connect.addListener((ChannelFutureListener) channelFuture -> {

View File

@@ -320,14 +320,14 @@ public class SyncErpService {
K3CloudApi cloudApi = new K3CloudApi(identifyInfo);
String today = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
//String filterString = "FUseOrgId='750572'";
//String filterString = "FBillNo ='PPBOM240800244'";
String filterString = "FBillNo ='XSDD241200129'";
//String filterString = "FCreateDate >= '" + today + " 00:00:00' and FCreateDate <= '" + today + " 23:59:59'";
ErpQuery query = new ErpQuery();
//query.setFilterString(filterString);
//query.setFormId(syncFormMapping.getForm_type());
query.setFormId(syncFormMapping.getForm_type());
query.setFieldKeys("FMATERIALID");
query.setLimit(1);
query.setFieldKeys("FID");
query.setLimit(0);
String jsonString = JSON.toJSONString(query);
List<List<Object>> lists = cloudApi.executeBillQuery(jsonString);
log.info("同步ERP结果" + lists.size());

View File

@@ -316,22 +316,30 @@ public class CockpitServiceImpl implements CockpitService {
r.setProduct_area(pointInfo.getProduct_area());
}
}
InteracteDto<Object> dto = InteracteDto.builder().service("wmsToAcsService")
InteracteDto<Object> dto = InteracteDto.builder()
.service("wmsToAcsService")
.trace_id(MDC.get("trace_id"))
.type("getWeight")
.data(new JSONObject().put("device_code", code))
.data(new JSONObject().fluentPut("device_code", code)) // 使用 fluentPut 提高代码简洁性
.build();
//查询称重重量
TableDataInfo result = InterationUtil.notifyExt("/api/wmsToAcs/apply", (JSONObject) JSON.toJSON(dto));
String weight;
//tofix
if (result.getCode().equals("200")) {
Object sd = result.getData();
JSONObject data = JSONObject.parseObject(sd.toString());
if (ObjectUtil.isNotEmpty(data)) {
weight = data.getString("weight");
BigDecimal theoryQty = BigDecimal.ZERO;
BigDecimal actual_weight = BigDecimal.ZERO;
if ("200".equals(result.getCode())) {
Object dataObj = result.getData();
if (dataObj != null) {
JSONObject data = JSONObject.parseObject(dataObj.toString());
if (data != null && data.containsKey("weight")) {
String weightStr = data.getString("weight");
if (ObjectUtil.isNotEmpty(weightStr)) {
// 转换为 BigDecimal
actual_weight = new BigDecimal(weightStr.trim());
}
}
}
}
BigDecimal theoryQty = BigDecimal.ZERO;
r.setActual_weight(actual_weight);
//根据称重信息计算理论数量
if (r.getActual_weight().compareTo(BigDecimal.ZERO) > 0) {
//计算理论重量

View File

@@ -146,9 +146,10 @@ public class PmFormDataServiceImpl extends ServiceImpl<PmFormDataMapper, PmFormD
}
return result;
} catch (Exception ex) {
ex.printStackTrace();
log.error("ERP单据解析异常" + ex.getMessage());
throw new BadRequestException(ex.getMessage());
// ex.printStackTrace();
// log.error("ERP单据解析异常" + ex.getMessage());
// throw new BadRequestException(ex.getMessage());
return null;
}
}

View File

@@ -59,9 +59,9 @@ public class SyncErpBillsScheduleService {
private static Map<String, String> BillOrg_Mapping = MapOf.of(
"PRD_PPBOM", "FPrdOrgId",
"PRD_MO", "FPrdOrgId",
"SAL_SaleOrder", "StockOrgId",
"PUR_ReceiveBill", "StockOrgId"
, "ka7c19edf9d4b4b39b8cc4a06802163b0", "F_PMSY_StockOrgId_Id");
"SAL_SaleOrder", "FStockOrgId",
"PUR_ReceiveBill", "FStockOrgId"
, "ka7c19edf9d4b4b39b8cc4a06802163b0", "F_PMSY_PrdOrgId");
public void run() {
try {
@@ -69,22 +69,176 @@ public class SyncErpBillsScheduleService {
MDC.put("requestIp", "127.0.0.1");
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, "BD_MATERIAL"));
List<SyncFormMapping> list = syncFormMappingServiceImpl.list(new LambdaQueryWrapper<SyncFormMapping>().in(SyncFormMapping::getForm_type, "ka7c19edf9d4b4b39b8cc4a06802163b0", "PRD_MO", "PUR_ReceiveBill", "PRD_PPBOM"));
for (SyncFormMapping m : list) {
JSONArray mappingJson = m.getMapping_json();
String mappingString = JSON.toJSONString(mappingJson);
syncData(mappingString, m.getForm_type(), m.getDtl_split());
}
// list.forEach(this::syncData);
} finally {
LuceneAppender.traceIdTL.remove();
}
}
/**
*
*/
public void syncData(String mappingJson, String formType, Boolean dtlSplit) {
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);
//构建查询条件
String today = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
String filterString = BillOrg_Mapping.get(formType) + " ='750572' AND FDocumentStatus='C' ";
if ("ka7c19edf9d4b4b39b8cc4a06802163b0".equals(formType)) {
filterString += " AND F_PMSY_BillStatus ='A' AND F_PMSY_CreateDate >= '" + today + " 00:00:00' and F_PMSY_CreateDate <= '" + today + " 23:59:59' ";
} else {
filterString += " AND FCreateDate >= '" + today + " 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'";
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);
}
}
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));
Set<String> materials = formDataList.stream().filter(rd -> StringUtils.isBlank(rd.getCode())).map(PmFormData::getMaterial_id).collect(Collectors.toSet());
int materialCount = iMdMeMaterialbaseService.count(new QueryWrapper<MdMeMaterialbase>().in("material_id", materials));
if (materialCount != materials.size()) {
log.error("保存数据 [{}] 时出现异常: {}", JSON.toJSONString(formDataList), "物料信息不存在" + materials.toString());
}
for (String id : formDataList.stream().map(PmFormData::getId).collect(Collectors.toList())) {
if (exitFormDataList.contains(id)) {
continue 外部;
}
}
for (PmFormData formData : formDataList) {
formDataService.save(formData);
}
} catch (Exception e) {
//log.error("解析数据 [{}] 时出现异常: {}", JSON.toJSONString(r), e.getMessage());
}
}
}
} catch (Exception ex) {
log.error("同步数据时出现异常: {}", ex.getMessage());
} finally {
if (lock.isLocked() && lock.isHeldByCurrentThread()) {
lock.unlock();
}
}
}
public void syncData1(String mappingJson, String formType, Boolean dtlSplit) {
boolean islock = lock.tryLock();
try {
if (islock) {
IdentifyInfo identifyInfo = new IdentifyInfo();
BeanUtils.copyProperties(erpSec, identifyInfo);
K3CloudApi cloudApi = new K3CloudApi(identifyInfo);
String today = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
String filterString = "FUseOrgId='750572'";
//String filterString = BillOrg + "='750572' AND FDocumentStatus='C' AND FCreateDate >= '" + today + " 00:00:00' and FCreateDate <= '" + today + " 23:59:59'";
//构建查询条件
// String BillOrg = BillOrg_Mapping.get(formType);
// String filterString = BillOrg + "='750572' AND FDocumentStatus='C' ";
// 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' ";
// }
ErpQuery query = new ErpQuery();
query.setFilterString(filterString);
query.setFormId(formType);
query.setFieldKeys("FMATERIALID");
query.setLimit(0);
String jsonString = JSON.toJSONString(query);
List<List<Object>> lists = cloudApi.executeBillQuery(jsonString);
// Set<String> exitFormDataList = pmFormDataMapper.existFormDataList();
for (List<Object> list : lists) {
for (Object r : list) {
String fid = r.toString();
if (StringUtils.isEmpty(fid)) {
log.error("单据同步失败,没有找到FID");
continue;
}
try {
OperateParam param = new OperateParam();
param.setId(fid);
OperatorResult view = cloudApi.view(formType, param);
RepoStatus status = view.getResult().getResponseStatus();
if (status.isIsSuccess()) {
List<Object> result = new ArrayList<>();
result.add(view.getResult().getResult());
getData(mappingJson, formType, dtlSplit, result);
} else {
ArrayList<RepoError> errors = status.getErrors();
String errorMsg = errors.stream().map(RepoError::getMessage).collect(Collectors.joining(","));
}
} catch (Exception e) {
log.error("处理ID [{}] 时出现异常: {}", fid, e.getMessage());
}
}
}
}
} catch (Exception ex) {
log.error("同步数据时出现异常: {}", ex.getMessage());
} finally {
if (lock.isLocked() && lock.isHeldByCurrentThread()) {
lock.unlock();
}
}
}
public void syncDataTest(String mappingJson, String formType, Boolean dtlSplit) {
boolean islock = lock.tryLock();
try {
if (islock) {
@@ -159,95 +313,27 @@ public class SyncErpBillsScheduleService {
}
}
public void syncData(String mappingJson, String formType, Boolean dtlSplit) {
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);
String today = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
String filterString = "FBillNo ='PPBOM240800244'";
//String filterString = BillOrg + "='750572' AND FDocumentStatus='C' AND FCreateDate >= '" + today + " 00:00:00' and FCreateDate <= '" + today + " 23:59:59'";
//构建查询条件
// String BillOrg = BillOrg_Mapping.get(formType);
// String filterString = BillOrg + "='750572' AND FDocumentStatus='C' ";
// 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'";
ErpQuery query = new ErpQuery();
query.setFilterString(filterString);
query.setFormId(formType);
query.setFieldKeys("FMATERIALID");
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);
private void getData(String mappingJson, String formType, Boolean dtlSplit, List<Object> result) {
String jsonString;
外部:
for (Object r : result) {
try {
JSONArray mappingJsonArray = JSONArray.parseArray(mappingJson);
List<PmFormData> formDataList = formDataService.syncAnalyse(mappingJsonArray, formType, dtlSplit, JSON.toJSONString(r));
// for (String id : formDataList.stream().map(PmFormData::getId).collect(Collectors.toList())) {
// if (exitFormDataList.contains(id)) {
// continue 外部;
// }
// }
if (formDataList != null) {
List<PmFormData> formDataFilterList = formDataList.stream().filter(rs1 -> "BD_MATERIAL_1".equals(rs1.getForm_type())).collect(Collectors.toList());
for (PmFormData formData : formDataFilterList) {
formDataService.save(formData);
}
}
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));
Set<String> materials = formDataList.stream().map(PmFormData::getMaterial_id).collect(Collectors.toSet());
int materialCount = iMdMeMaterialbaseService.count(new QueryWrapper<MdMeMaterialbase>().in("material_id", materials));
if (materialCount!=materials.size()){
log.error("保存数据 [{}] 时出现异常: {}", JSON.toJSONString(formDataList), "物料信息不存在"+materials.toString());
}
for (String id : formDataList.stream().map(PmFormData::getId).collect(Collectors.toList())) {
if (exitFormDataList.contains(id)) {
continue 外部;
}
}
for (PmFormData formData : formDataList) {
formDataService.save(formData);
}
} catch (Exception e) {
log.error("解析数据 [{}] 时出现异常: {}", JSON.toJSONString(r), e.getMessage());
}
}
}
} catch (Exception ex) {
log.error("同步数据时出现异常: {}", ex.getMessage());
} finally {
if (lock.isLocked() && lock.isHeldByCurrentThread()) {
lock.unlock();
} catch (Exception e) {
log.error("解析数据 [{}] 时出现异常: {}", JSON.toJSONString(r), e.getMessage());
}
}
}