'use strict'; const MainService = require('../main'); class WarehouseStoreService extends MainService { /** * 查询当前仓库下已授权的货主列表。 * 返回货主基本信息(id、公司名、联系人、联系电话、授权状态)。 * @param {Object} options 请求参数。 * @param {Object} options.body 请求体。 * @return {Promise} 货主列表。 */ async list({ body = {} } = {}) { const { ctx } = this; const { warehouseId } = this.getInfo(); const page = Math.max(1, Number(body.page) || 1); const pageSize = Math.min(100, Math.max(1, Number(body.pageSize) || 20)); // 通过 store_warehouse 关联查询已授权的货主 const { StoreWarehouse, Store } = ctx.model; const where = { warehouse_id: warehouseId, auth_status: 2, // 已通过 warehouse_use_status: 1, // 仓库侧可用 }; const { count: total, rows } = await StoreWarehouse.findAndCountAll({ attributes: [ 'id', 'store_id', 'auth_status', 'auth_start', 'auth_end', 'store_remask', 'warehouse_remask', ], where, include: [ { model: Store, as: 'store', attributes: [ 'id', 'company_name', 'contact_person', 'contact_phone', 'status', ], required: true, where: { status: 1, // 启用 }, }, ], offset: (page - 1) * pageSize, limit: pageSize, order: [[ 'id', 'DESC' ]], raw: true, }); const list = rows.map(row => ({ store_id: row['store.id'], company_name: row['store.company_name'], contact_person: row['store.contact_person'], contact_phone: row['store.contact_phone'], contract_id: row.id, auth_status: row.auth_status, auth_start: row.auth_start, auth_end: row.auth_end, store_remask: row.store_remask, warehouse_remask: row.warehouse_remask, })); return { list, pagination: { page, pageSize, total, totalPages: Math.ceil(total / pageSize), }, }; } } module.exports = WarehouseStoreService;