From 6e1dfc930fd9bc17d7a2c96ed008bafe0b2fdfae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E8=B4=B5=E5=BC=BA?= Date: Tue, 18 Mar 2025 17:02:16 +0800 Subject: [PATCH] =?UTF-8?q?=E7=89=A9=E6=96=99=E5=AF=BC=E5=85=A5=E4=BC=98?= =?UTF-8?q?=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ContractMaterialImportPlugin.java | 123 ++++++++++++------ 1 file changed, 83 insertions(+), 40 deletions(-) diff --git a/main/java/shkd/repc/recon/formplugin/ContractMaterialImportPlugin.java b/main/java/shkd/repc/recon/formplugin/ContractMaterialImportPlugin.java index cd9dd58..1ecf6a3 100644 --- a/main/java/shkd/repc/recon/formplugin/ContractMaterialImportPlugin.java +++ b/main/java/shkd/repc/recon/formplugin/ContractMaterialImportPlugin.java @@ -2,6 +2,7 @@ package shkd.repc.recon.formplugin; import com.alibaba.dubbo.common.utils.CollectionUtils; import com.alibaba.fastjson.JSONObject; +import kd.bos.coderule.api.CodeRuleInfo; import kd.bos.dataentity.entity.DynamicObject; import kd.bos.dataentity.entity.DynamicObjectCollection; import kd.bos.dataentity.metadata.IDataEntityType; @@ -17,6 +18,7 @@ import kd.bos.form.plugin.impt.BatchImportPlugin; import kd.bos.orm.query.QCP; import kd.bos.orm.query.QFilter; import kd.bos.servicehelper.BusinessDataServiceHelper; +import kd.bos.servicehelper.coderule.CodeRuleServiceHelper; import kd.bos.servicehelper.operation.SaveServiceHelper; import kd.bos.servicehelper.org.OrgUnitServiceHelper; import shkd.repc.recon.formplugin.dynamic.ImportMaterial; @@ -52,8 +54,6 @@ public class ContractMaterialImportPlugin extends AbstractFormPlugin { //只针对物料 if ("bd_material".equals(entityNumber)){ Long materialId = newMaterial(entry.getKey().getSearchValue(),materialList); - //存物料分类 - this.newMaterialType(materialId); basedata.add(materialId); } } else { @@ -71,48 +71,64 @@ public class ContractMaterialImportPlugin extends AbstractFormPlugin { } private Long newMaterial(String number,Set set){ DynamicObject bd_material = null; + Long id = null; try { ImportMaterial materialByNumber = getMaterialByNumber(number, set); bd_material = BusinessDataServiceHelper.newDynamicObject(BD_MATERIAL); //物料编码 - bd_material.set("number",number); + bd_material.set("number", number); //物料名称 - bd_material.set("name",materialByNumber.getMaterialName()); + bd_material.set("name", materialByNumber.getMaterialName()); //所属组织 long rootOrgId = OrgUnitServiceHelper.getRootOrgId(); DynamicObject org = BusinessDataServiceHelper.loadSingle(rootOrgId, BOS_ORG); if (null != org) { - bd_material.set("createorg",org); + bd_material.set("createorg", org); } //物料单位 QFilter unit = new QFilter("name", QCP.equals, materialByNumber.getMaterialUnit()); - DynamicObject measureUnits = BusinessDataServiceHelper.loadSingle("bd_measureunits",unit.toArray()); - if (null!=measureUnits){ - bd_material.set("baseunit",measureUnits); + DynamicObject measureUnits = BusinessDataServiceHelper.loadSingle("bd_measureunits", unit.toArray()); + if (null != measureUnits) { + bd_material.set("baseunit", measureUnits); + } + bd_material.set("modelnum", materialByNumber.getMaterialModel());//规格 + bd_material.set("materialtype", "1");//物料类型:物资 + //业务属性 + DynamicObjectCollection collection = bd_material.getDynamicObjectCollection("serviceattribute"); + DynamicObject serviceAttribute = BusinessDataServiceHelper.loadSingle("bd_serviceattribute", (new QFilter("number", QCP.equals, "1001")).toArray()); + if (serviceAttribute!=null){ + DynamicObject dynamicObject = new DynamicObject(collection.getDynamicObjectType()); + dynamicObject.set("fbasedataId",serviceAttribute); + collection.add(dynamicObject); } - bd_material.set("modelnum",materialByNumber.getMaterialModel());//规格 - bd_material.set("materialtype","1");//物料类型:物资 bd_material.set("enable", "1");//使用状态:可用 bd_material.set("status", "C");//数据状态:审核 bd_material.set("ctrlstrategy", "5");//控制策略:全局共享 bd_material.set("enablepur",true);//可采购 bd_material.set("enablesale",true);//可销售 bd_material.set("enableinv",true);//可库存 + SaveServiceHelper.save(new DynamicObject[]{bd_material}); + //物料分类映射 + DynamicObject group = BusinessDataServiceHelper.loadSingle("bd_materialgroup", (new QFilter("name", QCP.equals, materialByNumber.getMaterialType())).toArray()); + if (group==null) { + //待分类 + group = BusinessDataServiceHelper.loadSingle("bd_materialgroup", (new QFilter("number", QCP.equals, "waitgroup")).toArray()); + } + id = bd_material.getLong("id"); + this.newMaterialType(id,group); } catch (Exception e) { this.getView().showMessage(e.getMessage()); } - return bd_material.getLong("id"); + return id; } - private void newMaterialType(Long materialId){ + private void newMaterialType(Long materialId,DynamicObject group){ DynamicObject materialType = BusinessDataServiceHelper.newDynamicObject("bd_materialgroupdetail"); //分类标准 DynamicObject standard = BusinessDataServiceHelper.loadSingle("bd_materialgroupstandard", (new QFilter("number", QCP.equals, "JBFLBZ")).toArray()); materialType.set("standard",standard); - //分类 - DynamicObject group=BusinessDataServiceHelper.loadSingle("bd_materialgroup",(new QFilter("number", QCP.equals, "waitgroup")).toArray()); materialType.set("group",group); //物料 materialType.set("material",materialId); @@ -123,6 +139,13 @@ public class ContractMaterialImportPlugin extends AbstractFormPlugin { public void beforeImportEntry(BeforeImportEntryEventArgs e) { super.beforeImportEntry(e); try { + // 提前加载所有物料类型 + Set validMaterialTypes = new HashSet<>(); + DynamicObject[] typeList = BusinessDataServiceHelper.load("bd_materialgroup", "id,name", null); + for (DynamicObject obj : typeList) { + validMaterialTypes.add(obj.getString("name")); + } + materialList.clear(); Set existCodes = new HashSet<>(); @@ -144,16 +167,30 @@ public class ContractMaterialImportPlugin extends AbstractFormPlugin { ImportEntryData importData = (ImportEntryData) iterator.next(); JSONObject data = importData.getData(); Map map = data.toJavaObject(Map.class); - JSONObject material = (JSONObject) map.get("qeug_material"); - - // 如果 material 为空,则直接移除该行数据 - if (material == null) { - iterator.remove(); - continue; - } - + JSONObject material = (JSONObject) map.get("qeug_material");//物料编码 + String number=null; ImportMaterial importMaterial = new ImportMaterial(); - importMaterial.setMaterialNumber(material.getString("number")); + + // 如果 material 为空 + if (material == null) { + DynamicObject dynamicObject = BusinessDataServiceHelper.newDynamicObject("bd_material"); + CodeRuleInfo codeRule = CodeRuleServiceHelper.getCodeRule(dynamicObject.getDataEntityType().getName(), + dynamicObject, null); + number = CodeRuleServiceHelper.getNumber(codeRule, dynamicObject); + // 构造一个新的 JSONObject 来保存生成的物料编码 + JSONObject materialJson = new JSONObject(); + materialJson.put("importprop","number"); + materialJson.put("number", number); + map.put("qeug_material", materialJson); // 注意这里要传递完整对象 + importMaterial.setMaterialNumber(number); + }else { + importMaterial.setMaterialNumber(material.getString("number")); + } + //物料类型校验 + String impMaterialType = (String) map.get("qeug_importmaterialtype"); + if (!validMaterialTypes.contains(impMaterialType)) { + map.replace("qeug_importmaterialtype", "待分类"); + } importMaterial.setMaterialName((String) map.get("qeug_materialnames")); importMaterial.setMaterialType((String) map.get("qeug_importmaterialtype")); importMaterial.setMaterialModel((String) map.get("qeug_importmodel")); @@ -190,7 +227,7 @@ public class ContractMaterialImportPlugin extends AbstractFormPlugin { ImportEntryData entryData = iterator2.next(); Integer rowNum = (Integer) entryData.getData().get("rowNum"); if (indexSet.contains(rowNum)) { - iterator.remove(); + iterator2.remove(); } } } @@ -200,9 +237,15 @@ public class ContractMaterialImportPlugin extends AbstractFormPlugin { } + /** + * 校验物料名称、物料单位 + * @param entryDataList + * @param existingMaterialCodes + * @return + */ private Map validateEntry(List entryDataList,Set existingMaterialCodes) { Map indexVsMsgMap = new HashMap<>(); - Set encounteredMaterialCodes = new HashSet<>(); // 用于记录当前批次中的物料编码 +// Set encounteredMaterialCodes = new HashSet<>(); // 用于记录当前批次中的物料编码 for (int i = 0; i < entryDataList.size(); i++) { ImportEntryData entryData = entryDataList.get(i); @@ -210,33 +253,33 @@ public class ContractMaterialImportPlugin extends AbstractFormPlugin { // 获取当前行的 JSONObject 数据 JSONObject data = entryData.getData(); Map map = data.toJavaObject(Map.class); - JSONObject material = (JSONObject) map.get("qeug_material"); + //JSONObject material = (JSONObject) map.get("qeug_material"); // 获取物料编码和物料单位 - String materialCode = material.getString("number"); // 物料编码 + //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 (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) + "行:物料编码已存在,不能重复"); - } +// if (encounteredMaterialCodes.contains(materialCode)) { +// indexVsMsgMap.put(rowNum, "第" + (i + 1) + "行:物料编码重复"); +// } else { +// encounteredMaterialCodes.add(materialCode); +// } +// +// // 校验物料编码是否已经存在于当前页面的表格中 +// if (existingMaterialCodes.contains(materialCode)) { +// indexVsMsgMap.put(rowNum, "第" + (i + 1) + "行:物料编码已存在,不能重复"); +// } } return indexVsMsgMap;