合同物料非定额导入校验

This commit is contained in:
李贵强 2025-01-02 16:13:46 +08:00
parent d724f240f0
commit c1ef44e138
2 changed files with 104 additions and 30 deletions

View File

@ -1,11 +1,13 @@
package shkd.repc.recon.formplugin; package shkd.repc.recon.formplugin;
import com.alibaba.dubbo.common.utils.CollectionUtils;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import kd.bos.dataentity.entity.DynamicObject; import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection; import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.entity.datamodel.BasedataItem; import kd.bos.entity.datamodel.BasedataItem;
import kd.bos.entity.datamodel.events.BeforeImportEntryEventArgs; import kd.bos.entity.datamodel.events.BeforeImportEntryEventArgs;
import kd.bos.entity.datamodel.events.QueryImportBasedataEventArgs; import kd.bos.entity.datamodel.events.QueryImportBasedataEventArgs;
import kd.bos.entity.plugin.ImportLogger;
import kd.bos.form.plugin.AbstractFormPlugin; import kd.bos.form.plugin.AbstractFormPlugin;
import kd.bos.form.plugin.importentry.resolving.ImportEntryData; import kd.bos.form.plugin.importentry.resolving.ImportEntryData;
import kd.bos.form.plugin.impt.BatchImportPlugin; import kd.bos.form.plugin.impt.BatchImportPlugin;
@ -37,13 +39,8 @@ public class ContractMaterialImportPlugin extends AbstractFormPlugin {
for (Map.Entry<BasedataItem, List<Object>> entry : searchResult.entrySet()) { for (Map.Entry<BasedataItem, List<Object>> entry : searchResult.entrySet()) {
List<Object> basedata = entry.getValue(); List<Object> basedata = entry.getValue();
if (basedata.isEmpty()) { // 查不到基础资料数据 if (basedata.isEmpty()) { // 查不到基础资料数据
//数据去重
//List<ImportMaterial> list = getDistinctMaterialsByNumber();
Long materialId = newMaterial(entry.getKey().getSearchValue(),materialList); Long materialId = newMaterial(entry.getKey().getSearchValue(),materialList);
basedata.add(materialId); basedata.add(materialId);
//algo.storage.redis.ip_port
// System.getProperty("algo.storage.redis.ip_port");
} else { } else {
// 查到多个基础资料数据 // 查到多个基础资料数据
// 业务场景应根据需求剔除不合法的基础资料数据最终保留一个 // 业务场景应根据需求剔除不合法的基础资料数据最终保留一个
@ -70,9 +67,10 @@ public class ContractMaterialImportPlugin extends AbstractFormPlugin {
bd_material.set("createorg",org); bd_material.set("createorg",org);
} }
//物料分类 //物料分类
QFilter type = new QFilter("name", QCP.equals, materialByNumber.getMaterialType()); DynamicObject materialGroup = BusinessDataServiceHelper.loadSingle("bd_materialgroup",(new QFilter("name", QCP.equals, materialByNumber.getMaterialType())).toArray());
DynamicObject materialGroup = BusinessDataServiceHelper.loadSingle("bd_materialgroup",type.toArray()); if (null==materialGroup){
if (null!=materialGroup){ materialGroup=BusinessDataServiceHelper.loadSingle("bd_materialgroup",(new QFilter("number", QCP.equals, "waitgroup")).toArray());
}
//物料分组 //物料分组
bd_material.set("group",materialGroup); bd_material.set("group",materialGroup);
//分类标准 //分类标准
@ -85,7 +83,6 @@ public class ContractMaterialImportPlugin extends AbstractFormPlugin {
} }
//分类 //分类
newEntry.set("groupid",materialGroup); newEntry.set("groupid",materialGroup);
}
//物料单位 //物料单位
QFilter unit = new QFilter("name", QCP.equals, materialByNumber.getMaterialUnit()); QFilter unit = new QFilter("name", QCP.equals, materialByNumber.getMaterialUnit());
DynamicObject measureUnits = BusinessDataServiceHelper.loadSingle("bd_measureunits",unit.toArray()); DynamicObject measureUnits = BusinessDataServiceHelper.loadSingle("bd_measureunits",unit.toArray());
@ -108,6 +105,14 @@ public class ContractMaterialImportPlugin extends AbstractFormPlugin {
public void beforeImportEntry(BeforeImportEntryEventArgs e) { public void beforeImportEntry(BeforeImportEntryEventArgs e) {
super.beforeImportEntry(e); super.beforeImportEntry(e);
materialList.clear(); materialList.clear();
Set<String> existCodes=new HashSet<>();
DynamicObjectCollection orderFormEntry = (DynamicObjectCollection) this.getModel().getValue("qeug_orderformentry");
if (null!=orderFormEntry&&orderFormEntry.size()!=0){
for (DynamicObject dynamicObject : orderFormEntry) {
String number = dynamicObject.getString("qeug_material.number");
existCodes.add(number);
}
}
HashMap itemEntry = (HashMap) e.getSource(); HashMap itemEntry = (HashMap) e.getSource();
ArrayList list = (ArrayList) itemEntry.get("qeug_orderformentry"); ArrayList list = (ArrayList) itemEntry.get("qeug_orderformentry");
for (int i = 0; i < list.size(); i++) { for (int i = 0; i < list.size(); i++) {
@ -128,19 +133,76 @@ public class ContractMaterialImportPlugin extends AbstractFormPlugin {
importMaterial.setMaterialUnit((String) map.get("qeug_importunit")); importMaterial.setMaterialUnit((String) map.get("qeug_importunit"));
materialList.add(importMaterial); materialList.add(importMaterial);
} }
Set<Map.Entry<String, List<ImportEntryData>>> entries = itemEntry.entrySet();
for (Map.Entry<String, List<ImportEntryData>> entry : entries) {
String entryName = entry.getKey();
List<ImportEntryData> entryEntityImportDataList = entry.getValue();
if (CollectionUtils.isEmpty(entryEntityImportDataList)) {
return;
}
Map<Integer, String> indexVsMsgMap = validateEntry(entryEntityImportDataList,existCodes);
Set<Integer> indexSet = indexVsMsgMap.keySet();
Map<String, List<Object>> logMap = e.getEntryDataMap();
ImportLogger importLogger = (ImportLogger)logMap.get(entryName).get(0);
for (Map.Entry<Integer, String> indexVsMsgEntry : indexVsMsgMap.entrySet()) {
Integer index = indexVsMsgEntry.getKey();
importLogger.log(index, indexVsMsgEntry.getValue());
importLogger.fail();
importLogger.setTotal(importLogger.getTotal() + 1);
}
Iterator<ImportEntryData> iterator = entryEntityImportDataList.iterator();
while (iterator.hasNext()) {
ImportEntryData entryData = iterator.next();
Integer rowNum = (Integer) entryData.getData().get("rowNum");
if (indexSet.contains(rowNum)) {
iterator.remove();
}
}
}
} }
public List<ImportMaterial> getDistinctMaterialsByNumber() { private Map<Integer, String> validateEntry(List<ImportEntryData> entryDataList,Set<String> existingMaterialCodes) {
Map<Integer, String> indexVsMsgMap = new HashMap<>();
Set<String> encounteredMaterialCodes = new HashSet<>(); // 用于记录当前批次中的物料编码
for (int i = 0; i < entryDataList.size(); i++) {
ImportEntryData entryData = entryDataList.get(i);
return new ArrayList<>(materialList.stream() // 获取当前行的 JSONObject 数据
.collect(Collectors.toMap( JSONObject data = entryData.getData();
ImportMaterial::getMaterialNumber, // 根据 materialNumber 去重 Map<String, Object> map = data.toJavaObject(Map.class);
material -> material, // 保留第一个匹配的元素 JSONObject material = (JSONObject) map.get("qeug_material");
(existing, replacement) -> existing // 如果有重复的 materialNumber保留第一个
)) // 获取物料编码和物料单位
.values()); // 收集成 List String materialCode = material.getString("number"); // 物料编码
String materialUnit = (String) map.get("qeug_importunit"); // 物料单位
// 获取当前行号
Integer rowNum = (Integer) entryData.getData().get("rowNum");
// 校验物料编码和物料单位
if (materialCode == null || materialCode.trim().isEmpty()) {
indexVsMsgMap.put(rowNum, "" + (i + 1) + "行:物料编码不能为空或空字符串");
} }
if (materialUnit == null || materialUnit.trim().isEmpty()) {
indexVsMsgMap.put(rowNum, "" + (i + 1) + "行:物料单位不能为空或空字符串");
}
// 校验物料编码是否重复
if (encounteredMaterialCodes.contains(materialCode)) {
indexVsMsgMap.put(rowNum, "" + (i + 1) + "行:物料编码重复");
} else {
encounteredMaterialCodes.add(materialCode);
}
// 校验物料编码是否已经存在于当前页面的表格中
if (existingMaterialCodes.contains(materialCode)) {
indexVsMsgMap.put(rowNum, "" + (i + 1) + "行:物料编码已存在,不能重复");
}
}
return indexVsMsgMap;
}
public ImportMaterial getMaterialByNumber(String number, Set<ImportMaterial> set) { public ImportMaterial getMaterialByNumber(String number, Set<ImportMaterial> set) {
ImportMaterial result = null; ImportMaterial result = null;
@ -156,6 +218,4 @@ public class ContractMaterialImportPlugin extends AbstractFormPlugin {
} }
return result; return result;
} }
} }

View File

@ -62,4 +62,18 @@ public class ImportMaterial {
", materialUnit='" + materialUnit + '\'' + ", materialUnit='" + materialUnit + '\'' +
'}'; '}';
} }
public ImportMaterial() {
super();
}
@Override
public int hashCode() {
return super.hashCode();
}
@Override
public boolean equals(Object obj) {
return super.equals(obj);
}
} }