7906-optimizeDeleteZone #3067
|
@ -6,16 +6,16 @@ module.exports = Self => {
|
||||||
arg: 'id',
|
arg: 'id',
|
||||||
type: 'number',
|
type: 'number',
|
||||||
description: 'The zone id',
|
description: 'The zone id',
|
||||||
http: {source: 'path'}
|
http: {source: 'path'},
|
||||||
},
|
},
|
||||||
returns: {
|
returns: {
|
||||||
type: 'object',
|
type: 'object',
|
||||||
root: true
|
root: true,
|
||||||
},
|
},
|
||||||
http: {
|
http: {
|
||||||
path: `/:id/deleteZone`,
|
path: `/:id/deleteZone`,
|
||||||
verb: 'POST'
|
verb: 'POST',
|
||||||
}
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
Self.deleteZone = async(ctx, id, options) => {
|
Self.deleteZone = async(ctx, id, options) => {
|
||||||
|
@ -28,8 +28,7 @@ module.exports = Self => {
|
||||||
let tx;
|
let tx;
|
||||||
const myOptions = {userId};
|
const myOptions = {userId};
|
||||||
|
|
||||||
if (typeof options == 'object')
|
if (typeof options == 'object') Object.assign(myOptions, options);
|
||||||
Object.assign(myOptions, options);
|
|
||||||
|
|
||||||
if (!myOptions.transaction) {
|
if (!myOptions.transaction) {
|
||||||
tx = await Self.beginTransaction({});
|
tx = await Self.beginTransaction({});
|
||||||
|
@ -38,40 +37,55 @@ module.exports = Self => {
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const filter = {
|
const filter = {
|
||||||
where: {
|
where: {zoneFk: id, shipped: {gte: today}},
|
||||||
zoneFk: id,
|
|
||||||
shipped: {gte: today}
|
|
||||||
},
|
|
||||||
include: {
|
include: {
|
||||||
relation: 'ticketState',
|
relation: 'ticketState',
|
||||||
scope: {
|
scope: {
|
||||||
fields: ['id', 'alertLevel', 'code']
|
fields: ['id', 'alertLevel', 'code'],
|
||||||
}
|
},
|
||||||
}
|
},
|
||||||
};
|
};
|
||||||
|
console.time('finds');
|
||||||
|
const [ticketList, fixingState, worker] = await Promise.all([
|
||||||
|
models.Ticket.find(filter, myOptions),
|
||||||
|
models.State.findOne({where: {code: 'FIXING'}}, myOptions),
|
||||||
|
models.Worker.findOne({where: {id: userId}}, myOptions)
|
||||||
|
]);
|
||||||
|
console.timeEnd('finds');
|
||||||
|
|
||||||
|
console.time('ticket update');
|
||||||
|
console.log('ticketList: ', ticketList.length);
|
||||||
|
// await models.Ticket.rawSql(
|
||||||
|
// 'UPDATE ticket SET zoneFk = NULL WHERE zoneFk = ? LIMIT 2000',
|
||||||
|
// [id],
|
||||||
|
// myOptions
|
||||||
|
// );
|
||||||
const promises = [];
|
const promises = [];
|
||||||
|
// OFFSET no li mola a mariadb
|
||||||
|
for (let i = 0; i <= 30000; i += 2000) {
|
||||||
|
promises.push(models.Ticket.rawSql(
|
||||||
|
`UPDATE ticket SET zoneFk = NULL WHERE zoneFk = ? LIMIT 2000 OFFSET ${i};`,
|
||||||
|
[id],
|
||||||
|
myOptions
|
||||||
|
));
|
||||||
|
}
|
||||||
|
await Promise.all(promises);
|
||||||
|
console.timeEnd('ticket update');
|
||||||
|
/*
|
||||||
|
|
||||||
const ticketList = await models.Ticket.find(filter, myOptions);
|
const updatePromises = ticketList.map(ticket => {
|
||||||
const fixingState = await models.State.findOne({where: {code: 'FIXING'}}, myOptions);
|
if (ticket.ticketState().alertLevel === 0) {
|
||||||
const worker = await models.Worker.findOne({
|
return models.Ticket.state(ctx, {
|
||||||
where: {id: userId}
|
|
||||||
}, myOptions);
|
|
||||||
|
|
||||||
await models.Ticket.rawSql('UPDATE ticket SET zoneFk = NULL WHERE zoneFk = ?', [id], myOptions);
|
|
||||||
|
|
||||||
for (ticket of ticketList) {
|
|
||||||
if (ticket.ticketState().alertLevel == 0) {
|
|
||||||
promises.push(models.Ticket.state(ctx, {
|
|
||||||
ticketFk: ticket.id,
|
ticketFk: ticket.id,
|
||||||
stateFk: fixingState.id,
|
stateFk: fixingState.id,
|
||||||
userFk: worker.id
|
userFk: worker.id
|
||||||
}, myOptions));
|
}, myOptions);
|
||||||
}
|
}
|
||||||
}
|
});
|
||||||
await Promise.all(promises);
|
await Promise.all(updatePromises);
|
||||||
await models.Zone.destroyById(id, myOptions);
|
await models.Zone.destroyById(id, myOptions);
|
||||||
|
*/
|
||||||
if (tx) await tx.commit();
|
if (tx) await tx.rollback();
|
||||||
|
|
||||||
return id;
|
return id;
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
|
|
Loading…
Reference in New Issue