明树Git Lab

Commit 8d249dc2 authored by zfp1's avatar zfp1

update

parent 813abdf1
...@@ -187,83 +187,27 @@ async function saveAllResourceInfo(req, res, next) { ...@@ -187,83 +187,27 @@ async function saveAllResourceInfo(req, res, next) {
if (!resource) { if (!resource) {
return res.sendError(errorMessage.resourceNotFound); return res.sendError(errorMessage.resourceNotFound);
} }
let toCreate = [], toKeep = [];
if (resource.type === 1) { if (data.length) {
const existingRecords = await DB.ResourcesInfo.findAll({ for (let index = 0; index < data.length; index++) {
where: { del: 0, resourceId: resourceId }, const element = data[index];
raw: true if (element.id) {
}); toKeep.push(element.id);
console.log("existingRecords", existingRecords);
const existMap = new Map();
existingRecords.forEach(r => existMap.set(r.id, r));
const toCreate = [];
const toUpdate = [];
const incomingIds = new Set();
for (const item of data) {
if (item.resourceId != resourceId) {
console.log(item.value, "不属于当前资源,跳过");
continue;
}
console.log("每条aaa记录", item);
const record = {
...item,
//绑定ID
resourceId: Number(resourceId),
del: 0
};
//传进来有id,数据库能查到的为更新
if (item.id != null && existMap.has(item.id)) {
toUpdate.push(record);
incomingIds.add(item.id);
} else { } else {
//没id的统一为新增 element.resourceId = resource.id;
toCreate.push(record); toCreate.push(element);
} }
} }
// 数据库中的id,但是没传进来的自动为删除
const toDeleteIds = existingRecords
.filter(r => !incomingIds.has(r.id))
.map(r => r.id);
console.log('本次新增:', toCreate.length);
console.log('本次更新:', toUpdate.length);
console.log('本次软删:', toDeleteIds);
if (toCreate.length > 0) {
await DB.ResourcesInfo.bulkCreate(toCreate);
} }
if(toKeep.length) {
if (toUpdate.length > 0) { await DB.ResourcesInfo.destory({
for (const item of toUpdate) { where: {[Op.nin]: toKeep}
await DB.ResourcesInfo.update(item, { })
where: { id: item.id }
});
} }
if(toCreate.length) {
await DB.ResourcesInfo.bulkCreate(toCreate);
} }
return res.sendData();
if (toDeleteIds.length > 0) {
await DB.ResourcesInfo.destroy(
{ del: 1 },
{ where: { id: toDeleteIds } }
);
}
const ret = await DB.ResourcesInfo.findAll({
where: { del: 0, resourceId: resourceId },
raw: true
});
return res.sendData(ret);
}
return res.sendData([]);
} catch (error) { } catch (error) {
console.error('saveAllResourceInfo error:', error); console.error('saveAllResourceInfo error:', error);
next(error); next(error);
...@@ -274,7 +218,7 @@ async function listResourceAll(req, res, next) { ...@@ -274,7 +218,7 @@ async function listResourceAll(req, res, next) {
try { try {
let datas = await DB.Resources.findAll( let datas = await DB.Resources.findAll(
{ {
where: {del: 0}, where: { del: 0 },
include: [ include: [
{ {
model: DB.ResourcesInfo, model: DB.ResourcesInfo,
......
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