liufumingren e6e65d9d65 1
2026-06-06 16:06:31 +08:00

63 lines
1.6 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

'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;