明树Git Lab

Commit f85145e3 authored by zhangqi's avatar zhangqi

zhushi

parent 1e45275f
...@@ -189,14 +189,12 @@ async function saveAllResourceInfo(req, res, next) { ...@@ -189,14 +189,12 @@ async function saveAllResourceInfo(req, res, next) {
} }
if (resource.type === 1) { if (resource.type === 1) {
const existingRecords = await DB.ResourcesInfo.findAll({ const existingRecords = await DB.ResourcesInfo.findAll({
where: { del: 0, resourceId: resourceId }, // ← 关键修复! where: { del: 0, resourceId: resourceId },
raw: true raw: true
}); });
console.log("existingRecords", existingRecords); console.log("existingRecords", existingRecords);
const existMap = new Map(); const existMap = new Map();
existingRecords.forEach(r => existMap.set(r.id, r)); existingRecords.forEach(r => existMap.set(r.id, r));
...@@ -205,7 +203,7 @@ async function saveAllResourceInfo(req, res, next) { ...@@ -205,7 +203,7 @@ async function saveAllResourceInfo(req, res, next) {
const incomingIds = new Set(); const incomingIds = new Set();
for (const item of data) { for (const item of data) {
if (item.resourceId != resourceId) { // ← 正确写法 if (item.resourceId != resourceId) {
console.log(item.value, "不属于当前资源,跳过"); console.log(item.value, "不属于当前资源,跳过");
continue; continue;
} }
...@@ -214,18 +212,21 @@ async function saveAllResourceInfo(req, res, next) { ...@@ -214,18 +212,21 @@ async function saveAllResourceInfo(req, res, next) {
const record = { const record = {
...item, ...item,
//绑定ID
resourceId: Number(resourceId), resourceId: Number(resourceId),
del: 0 del: 0
}; };
//传进来有id,数据库能查到的为更新
if (item.id != null && existMap.has(item.id)) { if (item.id != null && existMap.has(item.id)) {
toUpdate.push(record); toUpdate.push(record);
incomingIds.add(item.id); incomingIds.add(item.id);
} else { } else {
//没id的统一为新增
toCreate.push(record); toCreate.push(record);
} }
} }
// 数据库中的id,但是没传进来的自动为删除
const toDeleteIds = existingRecords const toDeleteIds = existingRecords
.filter(r => !incomingIds.has(r.id)) .filter(r => !incomingIds.has(r.id))
.map(r => r.id); .map(r => r.id);
...@@ -247,7 +248,7 @@ async function saveAllResourceInfo(req, res, next) { ...@@ -247,7 +248,7 @@ async function saveAllResourceInfo(req, res, next) {
} }
if (toDeleteIds.length > 0) { if (toDeleteIds.length > 0) {
await DB.ResourcesInfo.update( await DB.ResourcesInfo.destroy(
{ del: 1 }, { del: 1 },
{ where: { id: toDeleteIds } } { where: { id: toDeleteIds } }
); );
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment