- 新增 groupScope 中间件:接口配置、字段、快递映射、调用日志仅集团权限可用,仓库维护只读放行 - 仓库列表/下拉/详情限定为自己所属公司(货主),越权访问返回 403 - 仓库商品与库存仅展示、推送、同步该仓库所属货主的商品,越界商品直接拒绝 - 「外部仓库」页在所在公司范围下收起新增/导出/接口配置/操作列与无关标签页,默认进入商品页 - 所属公司文案统一为「货主」 - 库存同步结果按商品统计(skipped/skipped_kinds/invalid_count),提示改为「N 个商品数量一致无需同步」 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
304 lines
11 KiB
JavaScript
304 lines
11 KiB
JavaScript
'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 subsidiary_id 所属公司(下属公司ID)
|
||
* @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, subsidiary_id: subsidiaryId } = 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);
|
||
if (subsidiaryId !== undefined && subsidiaryId !== '') where.subsidiary_id = Number(subsidiaryId);
|
||
|
||
// 所在公司权限:仓库列表仅展示自己所属公司(货主)的仓库
|
||
const access = await ctx.service.access.resolve(ctx.userId, principalId);
|
||
if (!access.isGroup) {
|
||
if (where.subsidiary_id !== undefined) {
|
||
if (!access.ownCompanyIds.includes(Number(where.subsidiary_id))) {
|
||
ctx.body = { code: 0, message: 'success', data: buildPageResult([], 0, { page, pageSize }) };
|
||
return;
|
||
}
|
||
} else {
|
||
where.subsidiary_id = { [Op.in]: access.ownCompanyIds };
|
||
}
|
||
}
|
||
|
||
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' ],
|
||
},
|
||
{
|
||
model: ctx.model.PrincipalSubsidiary,
|
||
as: 'subsidiary',
|
||
attributes: [ 'id', 'name' ],
|
||
required: false,
|
||
},
|
||
],
|
||
});
|
||
|
||
const list = rows.map(row => this.withSubsidiaryName(row));
|
||
ctx.body = { code: 0, message: 'success', data: buildPageResult(list, count, { page, pageSize }) };
|
||
}
|
||
|
||
/**
|
||
* 附加扁平的 subsidiary_name(列表/详情展示用)
|
||
* @param {Object} row 仓库 model 实例
|
||
* @return {Object} 仓库 plain 对象(含 subsidiary_name)
|
||
*/
|
||
withSubsidiaryName(row) {
|
||
const plain = row && row.toJSON ? row.toJSON() : { ...row };
|
||
plain.subsidiary_name = plain.subsidiary ? plain.subsidiary.name : '';
|
||
return plain;
|
||
}
|
||
|
||
/**
|
||
* 校验「所属公司」:必须是当前主体下的下属公司(必填)
|
||
* @param {number|string|null|undefined} value 前端传入的 subsidiary_id
|
||
* @param {number|string} principalId 当前主体ID
|
||
* @return {Promise<{ok: boolean, value?: number, message?: string}>} 校验结果
|
||
*/
|
||
async resolveSubsidiaryId(value, principalId) {
|
||
if (value === undefined || value === null || value === '') {
|
||
return { ok: false, message: '请选择所属公司(下属公司)' };
|
||
}
|
||
const id = Number(value);
|
||
if (!Number.isInteger(id) || id <= 0) return { ok: false, message: '所属公司不合法' };
|
||
|
||
const subsidiary = await this.ctx.model.PrincipalSubsidiary.findOne({
|
||
where: { id, principal_id: principalId },
|
||
attributes: [ 'id' ],
|
||
});
|
||
if (!subsidiary) return { ok: false, message: '所属公司不存在或不属于当前集团' };
|
||
return { ok: true, value: Number(subsidiary.id) };
|
||
}
|
||
|
||
/**
|
||
* @summary 仓库下拉(仅启用)
|
||
* @router get /api/warehouses/select
|
||
* @apikey
|
||
*/
|
||
async select() {
|
||
const { ctx } = this;
|
||
const Op = this.app.Sequelize.Op;
|
||
const where = { principal_id: ctx.state.principalId, status: 1, deleted_at: null };
|
||
// 所在公司权限:下拉仅展示自己所属公司的仓库
|
||
const access = await ctx.service.access.resolve(ctx.userId, ctx.state.principalId);
|
||
if (!access.isGroup) where.subsidiary_id = { [Op.in]: access.ownCompanyIds };
|
||
|
||
const rows = await ctx.model.Warehouse.findAll({
|
||
where,
|
||
attributes: [ 'id', 'code', 'name', 'subsidiary_id' ],
|
||
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, {
|
||
include: [
|
||
{ model: ctx.model.PrincipalSubsidiary, as: 'subsidiary', attributes: [ 'id', 'name' ], required: false },
|
||
],
|
||
});
|
||
if (!w) {
|
||
ctx.status = 404;
|
||
ctx.body = { code: 404, message: '仓库不存在' };
|
||
return;
|
||
}
|
||
// 所在公司权限:只能查看自己所属公司的仓库详情
|
||
const access = await ctx.service.access.resolve(ctx.userId, ctx.state.principalId);
|
||
if (!access.isGroup && !access.ownCompanyIds.includes(Number(w.subsidiary_id))) {
|
||
ctx.status = 403;
|
||
ctx.body = { code: 403, 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: {
|
||
...this.withSubsidiaryName(w),
|
||
stats: {
|
||
api_count: apiCount,
|
||
api_enabled_count: apiEnabledCount,
|
||
inbound_order_count: inboundCount,
|
||
outbound_order_count: outboundCount,
|
||
},
|
||
},
|
||
};
|
||
}
|
||
|
||
/**
|
||
* @summary 新增仓库
|
||
* @description 仅创建仓库基础信息,接口配置请在仓库创建后单独维护;
|
||
* 仓库编码由系统自动生成(WH 前缀),无需且不接受前端传入;所属公司 subsidiary_id 必填
|
||
* @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 subsidiary = await this.resolveSubsidiaryId(ctx.request.body.subsidiary_id, principalId);
|
||
if (!subsidiary.ok) {
|
||
ctx.status = 400;
|
||
ctx.body = { code: 400, message: subsidiary.message };
|
||
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,
|
||
subsidiary_id: subsidiary.value,
|
||
code,
|
||
name,
|
||
type,
|
||
status,
|
||
remark,
|
||
});
|
||
ctx.body = { code: 0, message: 'success', data: { id: w.id, code: w.code } };
|
||
}
|
||
|
||
/**
|
||
* 生成同主体下唯一的仓库编码
|
||
* @param {number} principalId 主体ID
|
||
* @return {Promise<string|null>} 编码,重试多次仍冲突时返回 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 仅更新仓库基础信息(编码自动生成、不可修改);所属公司 subsidiary_id 必填
|
||
* @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;
|
||
|
||
const subsidiary = await this.resolveSubsidiaryId(ctx.request.body.subsidiary_id, w.principal_id);
|
||
if (!subsidiary.ok) {
|
||
ctx.status = 400;
|
||
ctx.body = { code: 400, message: subsidiary.message };
|
||
return;
|
||
}
|
||
|
||
await w.update({ name, type, status, remark, subsidiary_id: subsidiary.value });
|
||
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;
|