From 06fbee35821e014aeaa3918b0ea22913c3e09448 Mon Sep 17 00:00:00 2001 From: WendyYang Date: Fri, 9 Aug 2024 10:47:44 +0800 Subject: [PATCH] =?UTF-8?q?modify:=201.=20=E5=AF=B9=E8=B1=A1=E5=B1=9E?= =?UTF-8?q?=E6=80=A7=E6=AF=94=E8=BE=83=E4=BF=AE=E6=94=B9=EF=BC=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../api/projectdeclared/model/vo/PurchaseVO.java | 2 + .../pm/api/projectdeclared/utils/BeanDiffUtil.java | 98 +++++++++------------- .../api/projectlib/manage/ProjectChangeManage.java | 12 +-- 3 files changed, 46 insertions(+), 66 deletions(-) diff --git a/hz-pm-api/src/main/java/com/hz/pm/api/projectdeclared/model/vo/PurchaseVO.java b/hz-pm-api/src/main/java/com/hz/pm/api/projectdeclared/model/vo/PurchaseVO.java index 7366912..bf92149 100644 --- a/hz-pm-api/src/main/java/com/hz/pm/api/projectdeclared/model/vo/PurchaseVO.java +++ b/hz-pm-api/src/main/java/com/hz/pm/api/projectdeclared/model/vo/PurchaseVO.java @@ -5,6 +5,7 @@ import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; import lombok.AllArgsConstructor; import lombok.Data; +import lombok.NoArgsConstructor; import javax.validation.constraints.NotBlank; import java.math.BigDecimal; @@ -19,6 +20,7 @@ import java.time.LocalDateTime; */ @Data @ApiModel(value = "采购对象") +@NoArgsConstructor @AllArgsConstructor public class PurchaseVO { diff --git a/hz-pm-api/src/main/java/com/hz/pm/api/projectdeclared/utils/BeanDiffUtil.java b/hz-pm-api/src/main/java/com/hz/pm/api/projectdeclared/utils/BeanDiffUtil.java index 0147c81..d078926 100644 --- a/hz-pm-api/src/main/java/com/hz/pm/api/projectdeclared/utils/BeanDiffUtil.java +++ b/hz-pm-api/src/main/java/com/hz/pm/api/projectdeclared/utils/BeanDiffUtil.java @@ -2,12 +2,12 @@ package com.hz.pm.api.projectdeclared.utils; import cn.hutool.core.bean.BeanUtil; import cn.hutool.core.collection.CollUtil; +import cn.hutool.core.map.MapUtil; import cn.hutool.core.util.ArrayUtil; import cn.hutool.core.util.ObjUtil; import lombok.Builder; import lombok.Data; -import java.io.Serializable; import java.util.*; /** @@ -18,20 +18,18 @@ import java.util.*; * @author WendyYang * @since 10:27 2024/8/6 */ -public class BeanDiffUtil { +public final class BeanDiffUtil { private BeanDiffUtil() { } @Data @Builder - public static class FieldDiff implements Serializable { - - private static final long serialVersionUID = -9003132377177378460L; + public static class FieldDiff { private String fieldName; - private String fileType; + private String fieldType; private Object oldValue; @@ -42,88 +40,68 @@ public class BeanDiffUtil { /** * 比较两个对象的不同属性值,包括嵌套的对象属性。 * - * @param obj1 第一个对象 - * @param obj2 第二个对象 - * @return 返回一个 Map,其中 key 是不同属性的名称,value 是一个包含两个值的数组,第一个值来自 obj1,第二个值来自 obj2 - */ - private static List diff(Object obj1, Object obj2, boolean diffSameField, String... ignoreFields) { - Map map1 = BeanUtil.beanToMap(obj1); - Map map2 = BeanUtil.beanToMap(obj2); - if (ArrayUtil.isNotEmpty(ignoreFields)) { - for (String ignoreField : ignoreFields) { - map2.remove(ignoreField); - map1.remove(ignoreField); + * @param oldObj oldObj + * @param newObj newObj + * @param diffSame 是否只对比相同属性 + * @param ignores 忽略比较的字段 + * @return 对比结果 + **/ + public static List diff(Object oldObj, Object newObj, boolean diffSame, String... ignores) { + Map oldObjMap = BeanUtil.beanToMap(oldObj); + Map newObjMap = BeanUtil.beanToMap(newObj); + if (ArrayUtil.isNotEmpty(ignores)) { + for (String ignore : ignores) { + newObjMap.remove(ignore); + oldObjMap.remove(ignore); } } // 创建结果 Map List result = new ArrayList<>(); // 获取两个 Map 的 keySet - Set keys1 = map1.keySet(); - Set keys2 = map2.keySet(); + Set oldObjKeys = oldObjMap.keySet(); + Set newObjKeys = newObjMap.keySet(); // 计算不同的属性 - Collection diffKeys = CollUtil.disjunction(keys1, keys2); + Collection diffKeys = CollUtil.disjunction(oldObjKeys, newObjKeys); - if (!diffSameField) { + if (!diffSame) { // 输出不同的属性及其值 for (String key : diffKeys) { - Object oldValue = map1.getOrDefault(key, null); - Object newValue = map2.getOrDefault(key, null); + Object oldVal = oldObjMap.get(key); + Object newVal = newObjMap.get(key); result.add(FieldDiff.builder() .fieldName(key) - .fileType((oldValue == null ? newValue.getClass() : oldValue.getClass()).getTypeName()) - .oldValue(oldValue) - .newValue(newValue) + .fieldType((oldObj == null ? newVal.getClass() : oldVal.getClass()).getTypeName()) + .oldValue(oldVal) + .newValue(newVal) .build()); } } // 输出相同的属性,但是值不同 - Collection commonKeys = CollUtil.intersection(keys1, keys2); - for (String key : commonKeys) { - Object value1 = map1.get(key); - Object value2 = map2.get(key); - if (value1 == null && value2 == null) { + Collection sameKeys = CollUtil.intersection(oldObjKeys, newObjKeys); + for (String key : sameKeys) { + Object oldVal = oldObjMap.get(key); + Object newVal = newObjMap.get(key); + if (oldVal == null && newVal == null) { continue; } - Class valueClass = value1 == null ? value2.getClass() : value1.getClass(); + Class clazz = oldVal == null ? newVal.getClass() : oldVal.getClass(); + oldVal = MapUtil.get(oldObjMap, key, clazz); + newVal = MapUtil.get(newObjMap, key, clazz); // 如果是嵌套对象,则递归比较 - if (!BeanUtil.isBean(valueClass) && !ObjUtil.equals(value1, value2)) { - Object oldValue = map1.getOrDefault(key, null); - Object newValue = map2.getOrDefault(key, null); + if (!BeanUtil.isBean(clazz) && ObjUtil.notEqual(oldVal, newVal)) { result.add(FieldDiff.builder() .fieldName(key) - .fileType((oldValue == null ? newValue.getClass() : oldValue.getClass()).getTypeName()) - .oldValue(oldValue) - .newValue(newValue) + .fieldType(clazz.getTypeName()) + .oldValue(oldVal) + .newValue(newVal) .build()); } } - return result; } - /** - * 比较两个对象的不同属性值,包括嵌套的对象属性。 - * - * @param obj1 第一个对象 - * @param obj2 第二个对象 - * @return 返回一个 Map,其中 key 是不同属性的名称,value 是一个包含两个值的数组,第一个值来自 obj1,第二个值来自 obj2 - */ - public static List diffSameField(Object obj1, Object obj2, String... ignoreFields) { - return diff(obj1, obj2, true, ignoreFields); - } - - /** - * 比较两个对象的不同属性值,包括嵌套的对象属性。 - * - * @param obj1 第一个对象 - * @param obj2 第二个对象 - * @return 返回一个 Map,其中 key 是不同属性的名称,value 是一个包含两个值的数组,第一个值来自 obj1,第二个值来自 obj2 - */ - public static List diff(Object obj1, Object obj2, String... ignoreFields) { - return diff(obj1, obj2, false, ignoreFields); - } } diff --git a/hz-pm-api/src/main/java/com/hz/pm/api/projectlib/manage/ProjectChangeManage.java b/hz-pm-api/src/main/java/com/hz/pm/api/projectlib/manage/ProjectChangeManage.java index 447900a..c0c4248 100644 --- a/hz-pm-api/src/main/java/com/hz/pm/api/projectlib/manage/ProjectChangeManage.java +++ b/hz-pm-api/src/main/java/com/hz/pm/api/projectlib/manage/ProjectChangeManage.java @@ -92,19 +92,19 @@ public class ProjectChangeManage { } ProjectDetailVO projDetailOld = projectLibManage.getProjectDetail(project); - List projDiffs = BeanDiffUtil.diffSameField(projDetailOld, projJsonObj, + List projDiffs = BeanDiffUtil.diff(projDetailOld, projJsonObj, true, EditProjectDTO.SYSTEM_REPLACE_INFOS, "id", "projectCode"); List systemReplaceInfos = reqProj.getSystemReplaceInfos(); List systemReplaceInfosOld = projDetailOld.getSystemReplaceInfos(); if (!CollUtil.isEqualList(systemReplaceInfos, systemReplaceInfosOld)) { projDiffs.add(FieldDiff.builder() .fieldName("systemReplaceInfos") - .fileType(List.class.getTypeName()) + .fieldType(List.class.getTypeName()) .oldValue(systemReplaceInfosOld) .newValue(systemReplaceInfos) .build()); } - projJsonObj.set(EditProjectDetailDTO.FIELD_PROJECT_DIFFS, projDiffs); + projectEditDetail.set(EditProjectDetailDTO.FIELD_PROJECT_DIFFS, projDiffs); List purchaseJsonObjs = projectEditDetail.getBeanList(EditProjectDetailDTO.FIELD_PURCHASES, JSONObject.class); if (CollUtil.isNotEmpty(purchaseJsonObjs)) { List purchaseDetails = projDetailOld.getPurchaseDetails(); @@ -121,9 +121,9 @@ public class ProjectChangeManage { JSONObject reqOperation = reqPurchaseDetail.getJSONObject(EditPurchaseDetailDTO.FIELD_OPERATION); JSONObject reqContract = reqPurchaseDetail.getJSONObject(EditPurchaseDetailDTO.FIELD_CONTRACT); - List purchaseDiffs = BeanDiffUtil.diffSameField(purchase, reqPurchase, "id"); - List operationDiffs = BeanDiffUtil.diffSameField(operation, reqOperation, "id"); - List contractDiffs = BeanDiffUtil.diffSameField(contract, reqContract, "id"); + List purchaseDiffs = BeanDiffUtil.diff(purchase, reqPurchase, true, "id"); + List operationDiffs = BeanDiffUtil.diff(operation, reqOperation, true, "id"); + List contractDiffs = BeanDiffUtil.diff(contract, reqContract, true, "id"); reqPurchaseDetail.set(EditPurchaseDetailDTO.FIELD_PURCHASE_DIFFS, purchaseDiffs); reqPurchaseDetail.set(EditPurchaseDetailDTO.FIELD_CONTRACT_DIFFS, contractDiffs); reqPurchaseDetail.set(EditPurchaseDetailDTO.FIELD_OPERATION_DIFFS, operationDiffs);