add:如果本地无用料清单数据则在线同步数据

This commit is contained in:
2024-12-16 23:08:51 +08:00
parent 7d414eaf9f
commit 6b1a3d18cc
3 changed files with 81 additions and 39 deletions

View File

@@ -170,7 +170,7 @@ public class SyncReceiptProcessHandler implements ClassProcess {
JSONObject fStockId = new JSONObject();
//tofix 目前都入料箱库
fStockId.put("FNumber", "25");
linkedMap.put("FStockId", fStockId);
tempMap.put("FStockId", fStockId);
}
}
fEntity.clear();

View File

@@ -1,8 +1,10 @@
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.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
@@ -26,15 +28,16 @@ import org.nl.wms.stor_manage.struct.service.IStIvtStructattrService;
import org.nl.wms.stor_manage.struct.service.dao.StructAssignQty;
import org.nl.wms.sync_manage.service.form_mapping.ISyncFormMappingService;
import org.nl.wms.sync_manage.service.form_mapping.dao.SyncFormMapping;
import org.nl.wms.system_manage.service.quartz.task.SyncErpBillsScheduleService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
import java.math.BigDecimal;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
/**
@@ -61,11 +64,17 @@ public class PmFormDataServiceImpl extends ServiceImpl<PmFormDataMapper, PmFormD
@Autowired
private IBmFormStrucService formStrucService;
@Lazy
@Autowired
private ISyncFormMappingService iSyncFormMappingService;
@Autowired
private IStIvtStructattrService iStIvtStructattrService;
@Lazy
@Autowired
private SyncErpBillsScheduleService syncErpBillsScheduleService;
@Override
public Integer syncFormData(String form_type, String dataString) {
@@ -183,23 +192,23 @@ public class PmFormDataServiceImpl extends ServiceImpl<PmFormDataMapper, PmFormD
Page page = PageHelper.startPage(pageQuery.getPage()+1, pageQuery.getSize());
page.setOrderBy("id DESC");
List<PmFormDataDto> pmFormDataDtos = this.baseMapper.queryTree2(query);
if (!CollectionUtils.isEmpty(pmFormDataDtos)){
List<String> parents = pmFormDataDtos.stream().map(PmFormDataDto::getId).collect(Collectors.toList());
List<PmFormDataDto> childs = this.baseMapper.selectChilds(parents);
if (!CollectionUtils.isEmpty(childs)){
Map<String, List<PmFormDataDto>> childMap = childs.stream().collect(Collectors.groupingBy(PmFormDataDto::getParent_id));
for (PmFormDataDto dataDto : pmFormDataDtos) {
String productArea = dataDto.getForm_data().getString("product_area");
dataDto.setProduct_area(productArea);
List<PmFormDataDto> children = childMap.get(dataDto.getId());
if (children!=null){
for (PmFormDataDto child : children) {
String childProductArea = child.getForm_data().getString("product_area");
child.setProduct_area(childProductArea);
}
}
dataDto.setChildren(children);
if (!CollectionUtils.isEmpty(pmFormDataDtos)) {
handleFormDatas(pmFormDataDtos);
} else {
//根据单据编号同步ERP用料清单单据
if ("PRD_PPBOM".equals(query.getForm_type())) {
List<SyncFormMapping> list = iSyncFormMappingService.list(new LambdaQueryWrapper<SyncFormMapping>().eq(SyncFormMapping::getForm_type, "PRD_PPBOM"));
JSONArray mappingJson = list.get(0).getMapping_json();
String mappingString = JSON.toJSONString(mappingJson);
syncErpBillsScheduleService.syncData(mappingString, list.get(0).getForm_type(), list.get(0).getDtl_split(), query.getCode());
List<PmFormDataDto> pmFormData = this.baseMapper.queryTree2(query);
if (!CollectionUtils.isEmpty(pmFormData)) {
handleFormDatas(pmFormData);
}
com.baomidou.mybatisplus.extension.plugins.pagination.Page<PmFormDataDto> dtoPage = new com.baomidou.mybatisplus.extension.plugins.pagination.Page<>(page.getPages(), page.getPageSize(), page.getTotal());
dtoPage.setRecords(pmFormData);
dtoPage.setTotal(pmFormData.size());
return dtoPage;
}
}
com.baomidou.mybatisplus.extension.plugins.pagination.Page<PmFormDataDto> dtoPage = new com.baomidou.mybatisplus.extension.plugins.pagination.Page<>(page.getPages(), page.getPageSize(), page.getTotal());
@@ -207,14 +216,34 @@ public class PmFormDataServiceImpl extends ServiceImpl<PmFormDataMapper, PmFormD
return dtoPage;
}
private void handleFormDatas(List<PmFormDataDto> pmFormDataDtos) {
List<String> parents = pmFormDataDtos.stream().map(PmFormDataDto::getId).collect(Collectors.toList());
List<PmFormDataDto> childs = this.baseMapper.selectChilds(parents);
if (!CollectionUtils.isEmpty(childs)) {
Map<String, List<PmFormDataDto>> childMap = childs.stream().collect(Collectors.groupingBy(PmFormDataDto::getParent_id));
for (PmFormDataDto dataDto : pmFormDataDtos) {
String productArea = dataDto.getForm_data().getString("product_area");
dataDto.setProduct_area(productArea);
List<PmFormDataDto> children = childMap.get(dataDto.getId());
if (children != null) {
for (PmFormDataDto child : children) {
String childProductArea = child.getForm_data().getString("product_area");
child.setProduct_area(childProductArea);
}
}
dataDto.setChildren(children);
}
}
}
@Override
public PmFormDataDto queryErpCode(String code,String stor_code) {
public PmFormDataDto queryErpCode(String code, String stor_code) {
FormDataQuery query = new FormDataQuery();
query.setCode(code);
query.setStatus(new String[]{StatusEnum.FORM_STATUS.code("生成"),StatusEnum.FORM_STATUS.code("执行中")});
query.setStatus(new String[]{StatusEnum.FORM_STATUS.code("生成"), StatusEnum.FORM_STATUS.code("执行中")});
List<PmFormDataDto> pmFormDataDtos = this.baseMapper.queryTree2(query);
if (CollectionUtils.isEmpty(pmFormDataDtos)){
throw new BadRequestException(code+"单据编码不存在或已经完成入库");
if (CollectionUtils.isEmpty(pmFormDataDtos)) {
throw new BadRequestException(code + "单据编码不存在或已经完成入库");
}
PmFormDataDto pmFormDataDto = pmFormDataDtos.get(0);
List<PmFormDataDto> childs = this.baseMapper.selectChilds(ListOf.of(pmFormDataDto.getId()));
@@ -228,21 +257,23 @@ public class PmFormDataServiceImpl extends ServiceImpl<PmFormDataMapper, PmFormD
//TODO:指定仓库
List<StructAssignQty> structMaterials = iStIvtStructattrService.getStructIvtAssign(collect, stor_code);
Map<String, List<StructAssignQty>> materialMap = structMaterials.stream().collect(Collectors.groupingBy(StructAssignQty::getMaterial_id));
child:
for (PmFormDataDto child : childs) {
String childProductArea = child.getForm_data().getString("product_area");
child.setProduct_area(childProductArea);
List<StructAssignQty> maps = materialMap.get(child.getMaterial_id());
if (CollectionUtils.isEmpty(maps)){
if (CollectionUtils.isEmpty(maps)) {
child.setSto_qty(new BigDecimal(0));
child.setPcsn("");
}else {
} else {
for (StructAssignQty assignQty : maps) {
if (child.getPcsn().equals(assignQty.getPcsn())){
if (assignQty.getPcsn().equals(child.getPcsn())) {
child.setSto_qty(assignQty.getSto_qty());
child.setPcsn(assignQty.getPcsn());
continue child;
}
}
child.setSto_qty(BigDecimal.valueOf(maps.stream().mapToInt(a->a.getSto_qty().intValue()).sum()));
child.setSto_qty(BigDecimal.valueOf(maps.stream().mapToInt(a -> a.getSto_qty().intValue()).sum()));
child.setPcsn("");
}
}

View File

@@ -31,6 +31,7 @@ import org.nl.wms.sync_manage.service.form_mapping.impl.SyncFormMappingServiceIm
import org.slf4j.MDC;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
@@ -60,6 +61,7 @@ public class SyncErpBillsScheduleService {
@Autowired
private IMdMeMaterialbaseService iMdMeMaterialbaseService;
@Lazy
@Autowired
private IPmFormDataService formDataService;
@@ -111,7 +113,7 @@ public class SyncErpBillsScheduleService {
for (SyncFormMapping m : list) {
JSONArray mappingJson = m.getMapping_json();
String mappingString = JSON.toJSONString(mappingJson);
syncData(mappingString, m.getForm_type(), m.getDtl_split());
syncData(mappingString, m.getForm_type(), m.getDtl_split(), "");
}
} finally {
LuceneAppender.traceIdTL.remove();
@@ -121,10 +123,11 @@ public class SyncErpBillsScheduleService {
/**
* 自动同步
*/
public void syncData(String mappingJson, String formType, Boolean dtlSplit) {
public void syncData(String mappingJson, String formType, Boolean dtlSplit, String billCode) {
boolean islock = lock.tryLock();
try {
if (islock) {
ErpQuery query = new ErpQuery();
JSONArray result;
Map errorMsg = new HashMap<>();
// 获取当前日期
@@ -132,15 +135,21 @@ public class SyncErpBillsScheduleService {
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.DAY_OF_MONTH, -10);
String sevenDaysAgo = new SimpleDateFormat("yyyy-MM-dd").format(calendar.getTime());
String filterString = getFilterString(formType, "", sevenDaysAgo, today);
ErpQuery query = new ErpQuery();
String filterString;
if (StringUtils.isNotBlank(billCode)) {
filterString = "FBillNo = '" + billCode + "'";
} else {
filterString = getFilterString(formType, "", sevenDaysAgo, today);
}
query.setFilterString(filterString);
query.setFormId(formType);
query.setFieldKeys("FID");
query.setLimit(0);
result = erpServiceUtils.queryBills(query);
K3CloudApi k3CloudApi = erpServiceUtils.getCloudApi();
handleBills(mappingJson, formType, dtlSplit, errorMsg, result, k3CloudApi);
if (ObjectUtils.isNotEmpty(result)) {
K3CloudApi k3CloudApi = erpServiceUtils.getCloudApi();
handleBills(mappingJson, formType, dtlSplit, errorMsg, result, k3CloudApi);
}
}
} catch (Exception ex) {
log.error("同步数据时出现异常: {}", ex.getMessage());
@@ -192,12 +201,14 @@ public class SyncErpBillsScheduleService {
query.setFieldKeys("FID");
query.setLimit(0);
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 {
return "同步成功,共计"+result.size()+"";
if (ObjectUtils.isNotEmpty(result)) {
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 {
return "同步成功,共计" + result.size() + "";
}
}else {
throw new BadRequestException("当前同步操作正在执行,稍后再试");