'use strict'; const { Controller } = require('egg'); const { parsePagination, buildPageResult } = require('../lib/pagination'); /** 仓库编码前缀:编码由系统自动生成,无需人工填写 */ const CODE_PREFIX = 'WH'; /** 生成仓库编码:WH + yyMMddHHmmss + 4 位随机数(如 WH2609111530123456) */ function genWarehouseCode() { const pad = num => String(num).padStart(2, '0'); const d = new Date(); const ts = `${pad(d.getFullYear() % 100)}${pad(d.getMonth() + 1)}${pad(d.getDate())}` + `${pad(d.getHours())}${pad(d.getMinutes())}${pad(d.getSeconds())}`; const rand = Math.floor(1000 + Math.random() * 9000); return `${CODE_PREFIX}${ts}${rand}`; } /** * @controller WarehouseController 仓库管理 * 仅管理仓库基础信息;接口配置见 WarehouseApiController */ class WarehouseController extends Controller { /** * @summary 仓库列表 * @description 分页查询仓库,并带出其接口配置(一个仓库可有多条接口配置) * @router get /api/warehouses * @apikey * @request query integer page 页码,默认 1 * @request query integer pageSize 每页条数,默认 20 * @request query string code 仓库编码,支持模糊匹配 * @request query string name 仓库名称,支持模糊匹配 * @request query integer status 状态:1启用 0禁用,不传则不过滤 */ async index() { const { ctx } = this; const Op = this.app.Sequelize.Op; const { page, pageSize, offset, limit } = parsePagination(ctx.query); const { code, name, status } = ctx.query; const principalId = ctx.state.principalId; const where = { principal_id: principalId }; if (code) where.code = { [Op.like]: `%${code}%` }; if (name) where.name = { [Op.like]: `%${name}%` }; if (status !== undefined && status !== '') where.status = Number(status); const { count, rows } = await ctx.model.Warehouse.findAndCountAll({ where, offset, limit, order: [[ 'id', 'DESC' ]], include: [ { model: ctx.model.WarehouseApi, as: 'apis', attributes: [ 'id', 'name', 'type', 'base_url', 'app_key', 'status' ], }, ], }); ctx.body = { code: 0, message: 'success', data: buildPageResult(rows, count, { page, pageSize }) }; } /** * @summary 仓库下拉(仅启用) * @router get /api/warehouses/select * @apikey */ async select() { const { ctx } = this; const rows = await ctx.model.Warehouse.findAll({ where: { principal_id: ctx.state.principalId, status: 1, deleted_at: null }, attributes: [ 'id', 'code', 'name' ], order: [[ 'id', 'DESC' ]], }); ctx.body = { code: 0, message: 'success', data: rows }; } /** * @summary 仓库详情 * @description 按主键查询仓库基本信息,并附带接口配置、入库/出库单的关联统计,供详情面板展示 * @router get /api/warehouses/{id} * @apikey * @request path integer *id 仓库主键ID * @response 200 warehouseDetailResponse success */ async show() { const { ctx } = this; const w = await ctx.model.Warehouse.findByPk(ctx.params.id); if (!w) { ctx.status = 404; ctx.body = { code: 404, message: '仓库不存在' }; return; } const warehouseId = w.id; const [ apiCount, apiEnabledCount, inboundCount, outboundCount ] = await Promise.all([ ctx.model.WarehouseApi.count({ where: { warehouse_id: warehouseId } }), ctx.model.WarehouseApi.count({ where: { warehouse_id: warehouseId, status: 1 } }), ctx.model.InboundOrder.count({ where: { warehouse_id: warehouseId } }), ctx.model.OutboundOrder.count({ where: { warehouse_id: warehouseId } }), ]); ctx.body = { code: 0, message: 'success', data: { ...w.get({ plain: true }), stats: { api_count: apiCount, api_enabled_count: apiEnabledCount, inbound_order_count: inboundCount, outbound_order_count: outboundCount, }, }, }; } /** * @summary 新增仓库 * @description 仅创建仓库基础信息,接口配置请在仓库创建后单独维护; * 仓库编码由系统自动生成(WH 前缀),无需且不接受前端传入 * @router post /api/warehouses * @apikey * @request body createWarehouseRequest *body * @response 200 baseResponse success */ async create() { const { ctx } = this; const { principal_id: principalId, name, type, status, remark } = ctx.request.body; if (!principalId || !name) { ctx.status = 400; ctx.body = { code: 400, message: 'principal_id、name 为必填项' }; return; } const code = await this.nextWarehouseCode(principalId); if (!code) { ctx.status = 500; ctx.body = { code: 500, message: '仓库编码生成失败,请重试' }; return; } const w = await ctx.model.Warehouse.create({ principal_id: principalId, code, name, type, status, remark }); ctx.body = { code: 0, message: 'success', data: { id: w.id, code: w.code } }; } /** * 生成同主体下唯一的仓库编码 * @param {number} principalId 主体ID * @return {Promise} 编码,重试多次仍冲突时返回 null */ async nextWarehouseCode(principalId) { const { ctx } = this; for (let i = 0; i < 5; i++) { const code = genWarehouseCode(); const exist = await ctx.model.Warehouse.findOne({ where: { principal_id: principalId, code } }); if (!exist) return code; } return null; } /** * @summary 更新仓库 * @description 仅更新仓库基础信息(编码自动生成、不可修改),接口配置请在接口配置中维护 * @router put /api/warehouses/{id} * @apikey * @request path integer *id 仓库主键ID * @request body updateWarehouseRequest *body * @response 200 baseResponse success */ async update() { const { ctx } = this; const w = await ctx.model.Warehouse.findByPk(ctx.params.id); if (!w) { ctx.status = 404; ctx.body = { code: 404, message: '仓库不存在' }; return; } const { name, type, status, remark } = ctx.request.body; await w.update({ name, type, status, remark }); ctx.body = { code: 0, message: 'success', data: { id: w.id } }; } /** * @summary 删除仓库(软删除) * @description 软删除仓库,同时清理其接口配置与商品推送绑定 * @router delete /api/warehouses/{id} * @apikey * @request path integer *id 仓库主键ID * @response 200 baseResponse success */ async destroy() { const { ctx } = this; const w = await ctx.model.Warehouse.findByPk(ctx.params.id); if (!w) { ctx.status = 404; ctx.body = { code: 404, message: '仓库不存在' }; return; } await ctx.model.WarehouseApi.destroy({ where: { warehouse_id: w.id } }); await ctx.model.WarehouseProduct.destroy({ where: { warehouse_id: w.id } }); await w.destroy(); ctx.body = { code: 0, message: 'success', data: { id: Number(ctx.params.id) } }; } } module.exports = WarehouseController;