|
|
@@ -0,0 +1,113 @@
|
|
|
+package com.tuoren.web.utils;
|
|
|
+
|
|
|
+import com.tuoren.web.layer.d0.ModificationComparison;
|
|
|
+import com.tuoren.web.layer.entity.BusReceiveRecordUpdateEntity;
|
|
|
+
|
|
|
+import java.math.BigDecimal;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @author wangzl
|
|
|
+ * @description: TODO
|
|
|
+ * @date 2025/8/14 15:00
|
|
|
+ */
|
|
|
+public class RecordUpdateUtil {
|
|
|
+ //从第二个开始对比
|
|
|
+ public static List<ModificationComparison> buildComparisons(BusReceiveRecordUpdateEntity prevRecord, BusReceiveRecordUpdateEntity currentRecord) {
|
|
|
+ List<ModificationComparison> diffs = new ArrayList<>();
|
|
|
+ // 动态对比字段
|
|
|
+ addIfChanged(diffs, "总量", prevRecord.getTotalQuantity(), currentRecord.getTotalQuantity());
|
|
|
+ addIfChanged(diffs, "首次量", prevRecord.getFirstQuantity(), currentRecord.getFirstQuantity());
|
|
|
+ addIfChanged(diffs, "持续量", prevRecord.getContinueQuantity(), currentRecord.getContinueQuantity());
|
|
|
+ addIfChanged(diffs, "极限量", prevRecord.getMaxQuantity(), currentRecord.getMaxQuantity());
|
|
|
+ addIfChanged(diffs, "追加量", prevRecord.getSingleQuantity(), currentRecord.getSingleQuantity());
|
|
|
+
|
|
|
+ return diffs;
|
|
|
+ }
|
|
|
+
|
|
|
+ //首次对比
|
|
|
+ public static List<ModificationComparison> buildInitialComparisons(BusReceiveRecordUpdateEntity currentRecord) {
|
|
|
+ List<ModificationComparison> comparisons = new ArrayList<>();
|
|
|
+ comparisons.add(buildComparison("总量", null, currentRecord.getTotalQuantity()));
|
|
|
+ comparisons.add(buildComparison("首次量", null, currentRecord.getFirstQuantity()));
|
|
|
+ comparisons.add(buildComparison("持续量", null, currentRecord.getContinueQuantity()));
|
|
|
+ comparisons.add(buildComparison("极限量", null, currentRecord.getMaxQuantity()));
|
|
|
+ comparisons.add(buildComparison("追加量", null, currentRecord.getSingleQuantity()));
|
|
|
+
|
|
|
+ return comparisons;
|
|
|
+ }
|
|
|
+ private static void addIfChanged(
|
|
|
+ List<ModificationComparison> list,
|
|
|
+ String fieldName,
|
|
|
+ Object oldVal,
|
|
|
+ Object newVal
|
|
|
+ ) {
|
|
|
+ if (!isEqual(oldVal, newVal)) {
|
|
|
+ list.add(buildComparison(fieldName, oldVal, newVal));
|
|
|
+ } else {
|
|
|
+ // 值相等时也记录,但 result=0
|
|
|
+ list.add(buildComparison(fieldName, oldVal, newVal));
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 值等值判断
|
|
|
+ private static boolean isEqual(Object a, Object b) {
|
|
|
+ if (a == null && b == null) return true;
|
|
|
+ if (a == null || b == null) return false;
|
|
|
+
|
|
|
+ if (a instanceof BigDecimal && b instanceof BigDecimal) {
|
|
|
+ return ((BigDecimal) a).compareTo((BigDecimal) b) == 0;
|
|
|
+ }
|
|
|
+ return a.equals(b);
|
|
|
+ }
|
|
|
+ private static ModificationComparison buildComparison(String fieldName, Object oldVal, Object newVal) {
|
|
|
+ int comparisonResult = calculateComparisonResult(oldVal, newVal);
|
|
|
+ return ModificationComparison.builder()
|
|
|
+ .fieldName(fieldName)
|
|
|
+ .oldValue(formatValue(oldVal))
|
|
|
+ .newValue(formatValue(newVal))
|
|
|
+ .result(comparisonResult)
|
|
|
+ .build();
|
|
|
+ }
|
|
|
+ // 值格式化方法
|
|
|
+ private static String formatValue(Object value) {
|
|
|
+ if (value == null) return "0";
|
|
|
+ if (value instanceof BigDecimal) {
|
|
|
+ return ((BigDecimal) value).stripTrailingZeros().toPlainString();
|
|
|
+ }
|
|
|
+ return value.toString();
|
|
|
+ }
|
|
|
+ /***
|
|
|
+ * @description: 对比输入参数
|
|
|
+ * @author wangzl
|
|
|
+ * @date 2025/8/14
|
|
|
+ */
|
|
|
+ private static int calculateComparisonResult(Object oldVal, Object newVal) {
|
|
|
+ if (oldVal == null && newVal == null) return 0;
|
|
|
+ if (oldVal == null) return 1; // 新增记录视为新值更大
|
|
|
+ if (newVal == null) return -1; // 删除记录视为旧值更大
|
|
|
+
|
|
|
+ // 处理 BigDecimal 类型
|
|
|
+ if (oldVal instanceof BigDecimal && newVal instanceof BigDecimal) {
|
|
|
+ return ((BigDecimal) newVal).compareTo((BigDecimal) oldVal);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 处理数值类型(Integer/Long/Double等)
|
|
|
+ if (oldVal instanceof Number && newVal instanceof Number) {
|
|
|
+ double oldNum = ((Number) oldVal).doubleValue();
|
|
|
+ double newNum = ((Number) newVal).doubleValue();
|
|
|
+ return Double.compare(newNum, oldNum);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 默认比较(按字符串或对象比较)
|
|
|
+ try {
|
|
|
+ Comparable oldComp = (Comparable) oldVal;
|
|
|
+ Comparable newComp = (Comparable) newVal;
|
|
|
+ return newComp.compareTo(oldComp);
|
|
|
+ } catch (ClassCastException e) {
|
|
|
+ // 无法比较时视为相等
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|