20260808172946
This commit is contained in:
1 parent
4fbfea82f2
commit
d72ca4193d
293 files changed
+37523
-73467
No files matched your search
@@ -378,6 +378,66 @@ export function diffData(
|
||||
) {
|
||||
const ignoreSet = new Set(ignoreFields);
|
||||
|
||||
const versionFields = new Set(["b_version", "mx_version"]);
|
||||
|
||||
|
||||
/**
|
||||
* 判断是否为数字类型
|
||||
*
|
||||
* 支持:
|
||||
* 40
|
||||
* "40"
|
||||
* "40.00"
|
||||
*/
|
||||
const isNumberValue = (value) => {
|
||||
if (typeof value === "number") {
|
||||
return !Number.isNaN(value);
|
||||
}
|
||||
|
||||
if (typeof value === "string") {
|
||||
if (value.trim() === "") {
|
||||
return false;
|
||||
}
|
||||
|
||||
return !Number.isNaN(Number(value));
|
||||
}
|
||||
|
||||
return false;
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* 数据值比较
|
||||
*
|
||||
* 数字:
|
||||
* 40 === "40.00"
|
||||
*
|
||||
* 非数字:
|
||||
* 保持原 JSON 比较逻辑
|
||||
*/
|
||||
const isEqualValue = (a, b) => {
|
||||
|
||||
// null undefined 空字符串统一
|
||||
if (a == null || a === "") {
|
||||
a = "";
|
||||
}
|
||||
|
||||
if (b == null || b === "") {
|
||||
b = "";
|
||||
}
|
||||
|
||||
|
||||
// 两边都是数字,按数值比较
|
||||
if (isNumberValue(a) && isNumberValue(b)) {
|
||||
return Math.abs(Number(a) - Number(b)) < 0.000001;
|
||||
}
|
||||
|
||||
|
||||
// 其他类型保持原来的比较方式
|
||||
return JSON.stringify(a) === JSON.stringify(b);
|
||||
};
|
||||
|
||||
|
||||
const result = {
|
||||
hasChanges: false,
|
||||
insertdata: [],
|
||||
@@ -385,106 +445,239 @@ export function diffData(
|
||||
deletedata: [],
|
||||
};
|
||||
|
||||
// 过滤字段
|
||||
|
||||
const filterFields = (row) => {
|
||||
return Object.fromEntries(
|
||||
Object.entries(row).filter(([key]) => {
|
||||
return !key.startsWith("_") && !key.startsWith("v_") && !ignoreSet.has(key);
|
||||
return (
|
||||
!key.startsWith("_") &&
|
||||
!key.startsWith("v_") &&
|
||||
!ignoreSet.has(key)
|
||||
);
|
||||
}),
|
||||
);
|
||||
};
|
||||
|
||||
|
||||
// 获取版本字段
|
||||
const getVersionData = (row) => {
|
||||
const data = {};
|
||||
|
||||
Object.keys(row).forEach((key) => {
|
||||
if (versionFields.has(key)) {
|
||||
data[key] = row[key];
|
||||
}
|
||||
});
|
||||
|
||||
return data;
|
||||
};
|
||||
|
||||
|
||||
// ===========================
|
||||
// 单表处理
|
||||
// 单表
|
||||
// ===========================
|
||||
if (isSingle) {
|
||||
const isNewRecord = !original || Object.keys(original).length === 0 || !original[primaryKey];
|
||||
|
||||
// 新增
|
||||
const isNewRecord =
|
||||
!original ||
|
||||
Object.keys(original).length === 0 ||
|
||||
!original[primaryKey];
|
||||
|
||||
|
||||
if (isNewRecord) {
|
||||
|
||||
result.hasChanges = true;
|
||||
|
||||
result.insertdata.push(filterFields(current));
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
// 修改
|
||||
|
||||
const changes = {};
|
||||
|
||||
|
||||
Object.keys(current).forEach((key) => {
|
||||
if (key.startsWith("_") || key.startsWith("v_") || ignoreSet.has(key)) return;
|
||||
|
||||
// 版本字段不参与比较
|
||||
if (versionFields.has(key)) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
if (
|
||||
key.startsWith("_") ||
|
||||
key.startsWith("v_") ||
|
||||
ignoreSet.has(key)
|
||||
) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
const cur = current[key] ?? "";
|
||||
const org = original[key] ?? "";
|
||||
|
||||
if (JSON.stringify(cur) !== JSON.stringify(org)) {
|
||||
|
||||
if (!isEqualValue(cur, org)) {
|
||||
changes[key] = cur;
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
|
||||
if (Object.keys(changes).length > 0) {
|
||||
|
||||
result.hasChanges = true;
|
||||
|
||||
|
||||
result.updatedata.push({
|
||||
|
||||
[primaryKey]: current[primaryKey],
|
||||
|
||||
// 修改字段
|
||||
...changes,
|
||||
|
||||
// 版本
|
||||
...getVersionData(current),
|
||||
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
// ===========================
|
||||
// 子表处理
|
||||
// ===========================
|
||||
const pk = Array.isArray(primaryKey) ? primaryKey[0] : primaryKey;
|
||||
|
||||
const originalMap = new Map(original.map((item) => [item[pk], item]));
|
||||
|
||||
// ===========================
|
||||
// 子表
|
||||
// ===========================
|
||||
|
||||
const pk = Array.isArray(primaryKey)
|
||||
? primaryKey[0]
|
||||
: primaryKey;
|
||||
|
||||
|
||||
const originalMap = new Map(
|
||||
original.map((item) => [item[pk], item]),
|
||||
);
|
||||
|
||||
|
||||
|
||||
// 新增
|
||||
|
||||
result.insertdata = current
|
||||
.filter((row) => !row[pk] || !originalMap.has(row[pk]))
|
||||
|
||||
.filter(
|
||||
(row) =>
|
||||
!row[pk] ||
|
||||
!originalMap.has(row[pk]),
|
||||
)
|
||||
|
||||
.map(filterFields);
|
||||
|
||||
|
||||
|
||||
// 修改
|
||||
|
||||
current.forEach((row) => {
|
||||
|
||||
const id = row[pk];
|
||||
|
||||
if (!id) return;
|
||||
|
||||
if (!id) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
const oldRow = originalMap.get(id);
|
||||
|
||||
if (!oldRow) return;
|
||||
|
||||
if (!oldRow) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
const changes = {};
|
||||
|
||||
|
||||
Object.keys(row).forEach((key) => {
|
||||
if (key === pk || key.startsWith("_") || key.startsWith("v_") || ignoreSet.has(key)) {
|
||||
|
||||
|
||||
// 版本字段不比较
|
||||
if (versionFields.has(key)) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
if (
|
||||
key === pk ||
|
||||
key.startsWith("_") ||
|
||||
key.startsWith("v_") ||
|
||||
ignoreSet.has(key)
|
||||
) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
const cur = row[key] ?? "";
|
||||
const org = oldRow[key] ?? "";
|
||||
|
||||
if (JSON.stringify(cur) !== JSON.stringify(org)) {
|
||||
|
||||
if (!isEqualValue(cur, org)) {
|
||||
changes[key] = cur;
|
||||
}
|
||||
|
||||
|
||||
});
|
||||
|
||||
|
||||
|
||||
if (Object.keys(changes).length > 0) {
|
||||
|
||||
|
||||
result.updatedata.push({
|
||||
|
||||
[pk]: id,
|
||||
|
||||
...changes,
|
||||
|
||||
// 版本
|
||||
...getVersionData(row),
|
||||
|
||||
});
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
});
|
||||
|
||||
|
||||
|
||||
// 删除
|
||||
|
||||
result.deletedata = original
|
||||
.filter((o) => !current.some((c) => c[pk] === o[pk]))
|
||||
|
||||
.filter(
|
||||
(o) =>
|
||||
!current.some(
|
||||
(c) => c[pk] === o[pk],
|
||||
),
|
||||
)
|
||||
|
||||
.map((o) => ({
|
||||
[pk]: o[pk],
|
||||
// 删除带版本
|
||||
...getVersionData(o),
|
||||
}));
|
||||
|
||||
|
||||
result.hasChanges =
|
||||
result.insertdata.length > 0 || result.updatedata.length > 0 || result.deletedata.length > 0;
|
||||
result.insertdata.length > 0 ||
|
||||
result.updatedata.length > 0 ||
|
||||
result.deletedata.length > 0;
|
||||
|
||||
return result;
|
||||
}
|
||||
@@ -497,4 +690,4 @@ export default {
|
||||
fillDefaultArray,
|
||||
diffData,
|
||||
fillSerialNoAndDefaults,
|
||||
};
|
||||
};
|
||||
Reference in new issue
Block a user