63 lines
1.6 KiB
JavaScript
63 lines
1.6 KiB
JavaScript
'use strict';
|
||
const Service = require('egg').Service;
|
||
|
||
class spaceService extends Service {
|
||
async searchSpace(data) {
|
||
const { ctx, app } = this;
|
||
const { keyword } = data;
|
||
const warehouseId = ctx.user.store_id;
|
||
|
||
// 构建基础查询条件:warehouse_id 为当前仓库或 null
|
||
const baseWhere = {
|
||
[app.Sequelize.Op.or]: [
|
||
{ warehouse_id: warehouseId },
|
||
{ warehouse_id: null },
|
||
],
|
||
};
|
||
|
||
let whereCondition = baseWhere;
|
||
|
||
// 如果有关键词,添加模糊查询
|
||
if (keyword && keyword.trim()) {
|
||
const searchKeyword = `%${keyword.trim()}%`;
|
||
whereCondition = {
|
||
[app.Sequelize.Op.and]: [
|
||
baseWhere, // 仓库条件
|
||
{
|
||
[app.Sequelize.Op.or]: [
|
||
{
|
||
space_code: {
|
||
[app.Sequelize.Op.like]: searchKeyword,
|
||
},
|
||
},
|
||
{
|
||
custom_barcode: {
|
||
[app.Sequelize.Op.like]: searchKeyword,
|
||
},
|
||
},
|
||
{
|
||
custom_name: {
|
||
[app.Sequelize.Op.like]: searchKeyword,
|
||
},
|
||
},
|
||
],
|
||
},
|
||
],
|
||
};
|
||
}
|
||
|
||
const results = await app.model.LocationSpace.findAll({
|
||
where: whereCondition,
|
||
attributes: [
|
||
'id', 'space_code', 'custom_barcode', 'custom_name',
|
||
],
|
||
order: [[ 'space_code', 'DESC' ]],
|
||
limit: 50,
|
||
subQuery: false,
|
||
});
|
||
|
||
return { code: 200, data: results, total: results.length };
|
||
}
|
||
}
|
||
module.exports = spaceService;
|