#1705 item.index optimizar velocidad

This commit is contained in:
Carlos Jimenez Ruiz 2019-10-04 10:07:45 +02:00
parent c6e8498281
commit da2c763573
1 changed files with 17 additions and 6 deletions

View File

@ -62,13 +62,26 @@ module.exports = Self => {
Self.filter = async(ctx, filter) => { Self.filter = async(ctx, filter) => {
let conn = Self.dataSource.connector; let conn = Self.dataSource.connector;
let codeWhere;
if (ctx.args.search) {
let items = await Self.app.models.ItemBarcode.find({
where: {code: ctx.args.search},
fields: ['itemFk']
});
let itemIds = [];
for (const item of items)
itemIds.push(item.itemFk);
codeWhere = {'i.id': {inq: itemIds}};
}
let where = buildFilter(ctx.args, (param, value) => { let where = buildFilter(ctx.args, (param, value) => {
switch (param) { switch (param) {
case 'search': case 'search':
return /^\d+$/.test(value) return /^\d+$/.test(value)
? {or: [{'i.id': value}, {'ib.code': value}]} ? {or: [{'i.id': value}, codeWhere]}
: {or: [{'i.name': {like: `%${value}%`}}, {'ib.code': value}]}; : {or: [{'i.name': {like: `%${value}%`}}, codeWhere]};
case 'id': case 'id':
return {'i.id': value}; return {'i.id': value};
case 'description': case 'description':
@ -83,8 +96,8 @@ module.exports = Self => {
return {'i.isActive': value}; return {'i.isActive': value};
} }
}); });
filter = mergeFilters(filter, {where}); filter = mergeFilters(filter, {where});
let stmts = []; let stmts = [];
let stmt; let stmt;
@ -123,8 +136,7 @@ module.exports = Self => {
LEFT JOIN origin ori ON ori.id = i.originFk LEFT JOIN origin ori ON ori.id = i.originFk
LEFT JOIN cache.last_buy lb ON lb.item_id = i.id AND lb.warehouse_id = t.warehouseFk LEFT JOIN cache.last_buy lb ON lb.item_id = i.id AND lb.warehouse_id = t.warehouseFk
LEFT JOIN vn.buy b ON b.id = lb.buy_id LEFT JOIN vn.buy b ON b.id = lb.buy_id
LEFT JOIN itemPlacement itn ON itn.itemFk = i.id AND itn.warehouseFk = t.warehouseFk LEFT JOIN itemPlacement itn ON itn.itemFk = i.id AND itn.warehouseFk = t.warehouseFk`
LEFT JOIN itemBarcode ib ON ib.itemFk = i.id`
); );
if (ctx.args.tags) { if (ctx.args.tags) {
@ -150,7 +162,6 @@ module.exports = Self => {
} }
stmt.merge(conn.makeWhere(filter.where)); stmt.merge(conn.makeWhere(filter.where));
stmt.merge(`GROUP BY i.id`);
stmt.merge(conn.makePagination(filter)); stmt.merge(conn.makePagination(filter));
let itemsIndex = stmts.push(stmt) - 1; let itemsIndex = stmts.push(stmt) - 1;