From 839b81f66116e88739e64fb2f222c82d403752ae Mon Sep 17 00:00:00 2001 From: liufumingren Date: Thu, 5 Nov 2026 00:50:24 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=95=86=E5=93=81=E4=B8=8E=E5=A4=96?= =?UTF-8?q?=E9=83=A8=E4=BB=93=E5=BA=93=E5=85=B3=E8=81=94=E6=89=80=E5=B1=9E?= =?UTF-8?q?=E8=B4=A7=E4=B8=BB=EF=BC=88=E4=B8=8B=E5=B1=9E=E5=85=AC=E5=8F=B8?= =?UTF-8?q?=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - products、warehouses 新增 subsidiary_id(下属公司),创建/更新必填并校验归属当前主体 - 商品批量导入新增「所属货主」列,按下属公司名称匹配,缺失时报错 - 商品、外部仓库列表与导出新增所属货主列、筛选与下拉控件 - 仓库下拉与详情接口返回 subsidiary_id / subsidiary_name Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- backend/app/contract/index.js | 14 ++ backend/app/controller/product.js | 138 ++++++++++++++++-- backend/app/controller/warehouse.js | 87 +++++++++-- backend/app/lib/productImport.js | 10 +- backend/app/model/product.js | 3 + backend/app/model/warehouse.js | 3 + backend/app/utils/exportDefs.js | 14 ++ ...261104070700-add-subsidiary-to-products.js | 27 ++++ ...1104070900-add-subsidiary-to-warehouses.js | 26 ++++ frontend/src/api/exportTask.ts | 3 +- frontend/src/api/product.ts | 8 + frontend/src/api/warehouse.ts | 9 ++ frontend/src/components/ExportFilterField.vue | 15 +- frontend/src/views/ExportTask/Index.vue | 11 +- frontend/src/views/Product/Index.vue | 57 +++++++- 15 files changed, 397 insertions(+), 28 deletions(-) create mode 100644 backend/database/migrations/20261104070700-add-subsidiary-to-products.js create mode 100644 backend/database/migrations/20261104070900-add-subsidiary-to-warehouses.js diff --git a/backend/app/contract/index.js b/backend/app/contract/index.js index 77b8977..f8072a2 100644 --- a/backend/app/contract/index.js +++ b/backend/app/contract/index.js @@ -74,6 +74,9 @@ const warehouseBaseItem = { principal_id: { type: 'integer', description: '所属主体ID' }, code: { type: 'string', description: '仓库编码(系统自动生成,WH 前缀)' }, name: { type: 'string', description: '仓库名称' }, + subsidiary_id: { type: 'integer', description: '所属公司(下属公司ID)' }, + subsidiary_name: { type: 'string', description: '所属公司名称' }, + subsidiary: { type: 'principalSubsidiaryRef', description: '所属公司(下属公司)' }, type: { type: 'string', description: '仓库类型:wms/erp/other' }, status: { type: 'integer', description: '状态:1启用 0禁用' }, remark: { type: 'string', description: '备注' }, @@ -451,6 +454,9 @@ module.exports = { productItem: { id: { type: 'integer', description: '主键' }, principal_id: { type: 'integer', description: '所属主体ID' }, + subsidiary_id: { type: 'integer', description: '所属货主(下属公司ID)' }, + subsidiary_name: { type: 'string', description: '所属货主(下属公司名称)' }, + subsidiary: { type: 'principalSubsidiaryRef', description: '所属货主(下属公司)' }, item_code: { type: 'string', description: '商品编码' }, goods_code: { type: 'string', description: '货物编码' }, item_name: { type: 'string', description: '商品名称' }, @@ -491,6 +497,10 @@ module.exports = { }, productPage: buildPage('productItem', '商品列表'), productListResponse: buildListResponse('productPage'), + principalSubsidiaryRef: { + id: { type: 'integer', description: '下属公司ID' }, + name: { type: 'string', description: '下属公司名称' }, + }, /** 商品批量导入结果 */ productImportResult: { total: { type: 'integer', description: '文件中的数据行数(不含表头与空行)' }, @@ -509,6 +519,7 @@ module.exports = { }, createProductRequest: { principal_id: { type: 'integer', required: true, description: '所属主体ID' }, + subsidiary_id: { type: 'integer', required: true, description: '所属货主(下属公司ID),必填,需属于当前主体' }, item_code: { type: 'string', required: true, description: '商品编码,主体内唯一' }, item_name: { type: 'string', required: true, description: '商品名称' }, goods_code: { type: 'string', description: '货物编码' }, @@ -539,6 +550,7 @@ module.exports = { }, updateProductRequest: { principal_id: { type: 'integer', description: '所属主体ID' }, + subsidiary_id: { type: 'integer', description: '所属货主(下属公司ID),必填(不可清空)' }, item_code: { type: 'string', description: '商品编码' }, item_name: { type: 'string', description: '商品名称' }, goods_code: { type: 'string', description: '货物编码' }, @@ -964,12 +976,14 @@ module.exports = { createWarehouseRequest: { principal_id: { type: 'integer', required: true, description: '所属主体ID,由请求拦截器注入' }, name: { type: 'string', required: true, description: '仓库名称' }, + subsidiary_id: { type: 'integer', required: true, description: '所属公司(下属公司ID),必填' }, type: { type: 'string', description: '仓库类型:wms/erp/other' }, status: { type: 'integer', description: '状态:1启用 0禁用,默认1' }, remark: { type: 'string', description: '备注' }, }, updateWarehouseRequest: { name: { type: 'string', description: '仓库名称' }, + subsidiary_id: { type: 'integer', required: true, description: '所属公司(下属公司ID),必填' }, type: { type: 'string', description: '仓库类型' }, status: { type: 'integer', description: '状态:1启用 0禁用' }, remark: { type: 'string', description: '备注' }, diff --git a/backend/app/controller/product.js b/backend/app/controller/product.js index 58db34b..1f2cf57 100644 --- a/backend/app/controller/product.js +++ b/backend/app/controller/product.js @@ -65,6 +65,7 @@ class ProductController extends Controller { * @request query string category_name 分类名称,精确匹配 * @request query string brand_name 品牌名称,精确匹配 * @request query string stock_unit 库存单位,精确匹配 + * @request query integer subsidiary_id 所属货主(下属公司ID) * @request query integer status 状态:1上架 0下架,不传则不过滤 * @response 200 productListResponse success */ @@ -89,17 +90,25 @@ class ProductController extends Controller { if (brandName) where.brand_name = brandName; if (stockUnit) where.stock_unit = stockUnit; if (status !== undefined && status !== '') where.status = Number(status); + if (ctx.query.subsidiary_id !== undefined && ctx.query.subsidiary_id !== '') { + where.subsidiary_id = Number(ctx.query.subsidiary_id); + } const { count, rows } = await ctx.model.Product.findAndCountAll({ where, offset, limit, order: [[ 'id', 'DESC' ]], + include: this.subsidiaryInclude(), }); // 附带库存字段(正品/次品的可用与锁定数量),取自 product_stocks const list = await attachStockFields(ctx, principalId, rows); - ctx.body = { code: 0, message: 'success', data: buildPageResult(list, count, { page, pageSize }) }; + ctx.body = { + code: 0, + message: 'success', + data: buildPageResult(list.map(row => this.withSubsidiaryName(row)), count, { page, pageSize }), + }; } /** @@ -112,7 +121,7 @@ class ProductController extends Controller { */ async show() { const { ctx } = this; - const product = await ctx.model.Product.findByPk(ctx.params.id); + const product = await ctx.model.Product.findByPk(ctx.params.id, { include: this.subsidiaryInclude() }); if (!product) { ctx.status = 404; @@ -121,12 +130,55 @@ class ProductController extends Controller { } const [ data ] = await attachStockFields(ctx, product.principal_id, [ product ]); - ctx.body = { code: 0, message: 'success', data }; + ctx.body = { code: 0, message: 'success', data: this.withSubsidiaryName(data) }; + } + + /** + * 所属货主(下属公司)关联:仅取 id 与名称 + * @return {Object} Sequelize include 配置 + */ + subsidiaryInclude() { + return { + model: this.ctx.model.PrincipalSubsidiary, + as: 'subsidiary', + attributes: [ 'id', 'name' ], + required: false, + }; + } + + /** + * 附加扁平的 subsidiary_name(列表/详情展示用,避免前端层层取值) + * @param {Object} row 商品 plain 对象 + * @return {Object} 处理后的商品对象 + */ + withSubsidiaryName(row) { + return { ...row, subsidiary_name: row.subsidiary ? row.subsidiary.name : '' }; + } + + /** + * 校验并归一化「所属货主」:必须是当前主体下的下属公司;空值表示不指定 + * @param {number|string|null|undefined} value 前端传入的 subsidiary_id + * @param {number|string} principalId 当前主体ID + * @return {Promise<{ok: boolean, value?: number|null, message?: string}>} 校验结果 + */ + async resolveSubsidiaryId(value, principalId) { + if (value === undefined) return { ok: true, value: undefined }; + if (value === null || value === '') return { ok: true, value: null }; + + 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 新增商品 - * @description 同一主体下商品编码 item_code 唯一 + * @description 同一主体下商品编码 item_code 唯一;所属货主(下属公司)subsidiary_id 为必填项 * @router post /api/products * @apikey * @request body createProductRequest *body @@ -147,6 +199,14 @@ class ProductController extends Controller { return; } + if (ctx.request.body.subsidiary_id === undefined + || ctx.request.body.subsidiary_id === null + || ctx.request.body.subsidiary_id === '') { + ctx.status = 400; + ctx.body = { code: 400, message: '所属货主(下属公司)为必填项' }; + return; + } + const exist = await ctx.model.Product.findOne({ where: { principal_id: principalId, item_code: itemCode } }); if (exist) { ctx.status = 400; @@ -154,7 +214,17 @@ class ProductController extends Controller { return; } - const product = await ctx.model.Product.create(ctx.request.body); + 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 product = await ctx.model.Product.create({ + ...ctx.request.body, + subsidiary_id: subsidiary.value, + }); await ctx.service.operationLog.record({ targetType: 'product', targetId: product.id, @@ -197,8 +267,24 @@ class ProductController extends Controller { sku_property: product.sku_property, retail_price: product.retail_price, status: product.status, + subsidiary_id: product.subsidiary_id, }; delete ctx.request.body.principal_id; + + const subsidiary = await this.resolveSubsidiaryId(ctx.request.body.subsidiary_id, product.principal_id); + if (!subsidiary.ok) { + ctx.status = 400; + ctx.body = { code: 400, message: subsidiary.message }; + return; + } + // 所属货主为必填:显式传空表示清空时要拒绝,历史无货主商品编辑时需重新选择 + if (subsidiary.value === null) { + ctx.status = 400; + ctx.body = { code: 400, message: '所属货主(下属公司)为必填项' }; + return; + } + if (subsidiary.value !== undefined) ctx.request.body.subsidiary_id = subsidiary.value; + await product.update(ctx.request.body); // 操作记录:写清改了哪些字段(前后值) @@ -237,7 +323,7 @@ class ProductController extends Controller { /** * @summary 下载商品批量导入模板 - * @description 生成仅含表头的 xlsx 模板(商品编码 / 商品名称 / 货物编码 / SKU规格 / 分类 / 品牌 / 条码 / 单位 / 零售价 / 成本价 / 标签价 / 备注) + * @description 生成仅含表头的 xlsx 模板(商品编码 / 商品名称 / 货物编码 / SKU规格 / 分类 / 品牌 / 条码 / 单位 / 零售价 / 成本价 / 标签价 / 所属货主 / 备注) * @router get /api/products/import-template * @apikey * @request query integer principal_id 所属主体ID(必填,需当前账号已绑定该主体) @@ -265,9 +351,10 @@ class ProductController extends Controller { /** * @summary 批量导入商品 - * @description 上传 xlsx(表单字段 file,第一行为表头)。只接受 12 个字段: - * 商品编码 / 商品名称 / 货物编码 / SKU规格 / 分类 / 品牌 / 条码 / 单位 / 零售价 / 成本价 / 标签价 / 备注; - * 同一主体下商品编码已存在则更新(空单元格不覆盖原值,软删除的会恢复),否则新建 + * @description 上传 xlsx(表单字段 file,第一行为表头)。只接受这些字段: + * 商品编码 / 商品名称 / 货物编码 / SKU规格 / 分类 / 品牌 / 条码 / 单位 / 零售价 / 成本价 / 标签价 / 所属货主 / 备注; + * 「所属货主」填下属公司名称(需已在组织架构中创建),新增商品时必填; + * 匹配不到该行报错;同一主体下商品编码已存在则更新(空单元格不覆盖原值,软删除的会恢复),否则新建 * @router post /api/products/import * @apikey * @request formData file *file xlsx 文件 @@ -358,6 +445,18 @@ class ProductController extends Controller { for (const row of rows) existMap.set(row.item_code, row); } + // 2.1) 所属货主按下属公司名称解析(仅当前主体下的公司;空单元格不覆盖原值) + const subsidiaryNames = [ ...new Set(validRows.map(item => item.data.subsidiary_name).filter(Boolean)) ]; + const subsidiaryMap = new Map(); + if (subsidiaryNames.length) { + const rows = await ctx.model.PrincipalSubsidiary.findAll({ + where: { principal_id: principalId, name: { [Op.in]: subsidiaryNames } }, + attributes: [ 'id', 'name' ], + raw: true, + }); + for (const row of rows) subsidiaryMap.set(row.name, Number(row.id)); + } + // 3) 逐行写入(同一事务,任一行异常回滚该行前的内容不做半途提交) const transaction = await ctx.model.transaction(); let created = 0; @@ -365,7 +464,22 @@ class ProductController extends Controller { let restored = 0; const logRows = []; try { - for (const { rowNumber, data } of validRows) { + for (const { rowNumber, data: rawData } of validRows) { + let subsidiaryId; + if (rawData.subsidiary_name && !subsidiaryMap.has(rawData.subsidiary_name)) { + errors.push({ + row: rowNumber, + item_code: rawData.item_code, + message: `所属货主「${rawData.subsidiary_name}」不存在,请先在组织架构 → 下属公司中创建`, + }); + continue; + } + if (rawData.subsidiary_name) subsidiaryId = subsidiaryMap.get(rawData.subsidiary_name); + + const data = { ...rawData }; + delete data.subsidiary_name; + if (subsidiaryId) data.subsidiary_id = subsidiaryId; + const exist = existMap.get(data.item_code); try { if (exist) { @@ -394,6 +508,10 @@ class ProductController extends Controller { errors.push({ row: rowNumber, item_code: data.item_code, message: '商品名称不能为空(新增商品必填)' }); continue; } + if (!data.subsidiary_id) { + errors.push({ row: rowNumber, item_code: data.item_code, message: '所属货主不能为空(新增商品必填)' }); + continue; + } const product = await ctx.model.Product.create({ ...data, principal_id: principalId, diff --git a/backend/app/controller/warehouse.js b/backend/app/controller/warehouse.js index c4fd658..ccb7e54 100644 --- a/backend/app/controller/warehouse.js +++ b/backend/app/controller/warehouse.js @@ -31,19 +31,21 @@ class WarehouseController extends Controller { * @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 } = 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 { count, rows } = await ctx.model.Warehouse.findAndCountAll({ where, @@ -56,10 +58,49 @@ class WarehouseController extends Controller { as: 'apis', attributes: [ 'id', 'name', 'type', 'base_url', 'app_key', 'status' ], }, + { + model: ctx.model.PrincipalSubsidiary, + as: 'subsidiary', + attributes: [ 'id', 'name' ], + required: false, + }, ], }); - ctx.body = { code: 0, message: 'success', data: buildPageResult(rows, count, { page, pageSize }) }; + 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) }; } /** @@ -71,7 +112,7 @@ class WarehouseController extends Controller { 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' ], + attributes: [ 'id', 'code', 'name', 'subsidiary_id' ], order: [[ 'id', 'DESC' ]], }); ctx.body = { code: 0, message: 'success', data: rows }; @@ -87,13 +128,16 @@ class WarehouseController extends Controller { */ async show() { const { ctx } = this; - const w = await ctx.model.Warehouse.findByPk(ctx.params.id); + 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 warehouseId = w.id; const [ apiCount, apiEnabledCount, inboundCount, outboundCount ] = await Promise.all([ ctx.model.WarehouseApi.count({ where: { warehouse_id: warehouseId } }), @@ -106,7 +150,7 @@ class WarehouseController extends Controller { code: 0, message: 'success', data: { - ...w.get({ plain: true }), + ...this.withSubsidiaryName(w), stats: { api_count: apiCount, api_enabled_count: apiEnabledCount, @@ -120,7 +164,7 @@ class WarehouseController extends Controller { /** * @summary 新增仓库 * @description 仅创建仓库基础信息,接口配置请在仓库创建后单独维护; - * 仓库编码由系统自动生成(WH 前缀),无需且不接受前端传入 + * 仓库编码由系统自动生成(WH 前缀),无需且不接受前端传入;所属公司 subsidiary_id 必填 * @router post /api/warehouses * @apikey * @request body createWarehouseRequest *body @@ -135,6 +179,13 @@ class WarehouseController extends Controller { 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; @@ -142,7 +193,15 @@ class WarehouseController extends Controller { return; } - const w = await ctx.model.Warehouse.create({ principal_id: principalId, code, name, type, status, remark }); + 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 } }; } @@ -163,7 +222,7 @@ class WarehouseController extends Controller { /** * @summary 更新仓库 - * @description 仅更新仓库基础信息(编码自动生成、不可修改),接口配置请在接口配置中维护 + * @description 仅更新仓库基础信息(编码自动生成、不可修改);所属公司 subsidiary_id 必填 * @router put /api/warehouses/{id} * @apikey * @request path integer *id 仓库主键ID @@ -179,7 +238,15 @@ class WarehouseController extends Controller { return; } const { name, type, status, remark } = ctx.request.body; - await w.update({ name, type, status, remark }); + + 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 } }; } diff --git a/backend/app/lib/productImport.js b/backend/app/lib/productImport.js index c2611d0..3bac1ae 100644 --- a/backend/app/lib/productImport.js +++ b/backend/app/lib/productImport.js @@ -6,8 +6,9 @@ const fs = require('fs'); /** * 商品批量导入的列定义与解析工具。 * - * 只接受下面 12 个字段,表头按中文名匹配(允许少量别名); + * 只接受下面这些字段,表头按中文名匹配(允许少量别名); * 未识别的列会在结果里以 ignored_columns 返回,不参与导入。 + * 「所属货主」按下属公司名称匹配(subsidiary_name),由控制器解析为 subsidiary_id。 */ const IMPORT_COLUMNS = [ { field: 'item_code', label: '商品编码', required: true, type: 'text', max: 50 }, @@ -21,6 +22,13 @@ const IMPORT_COLUMNS = [ { field: 'retail_price', label: '零售价', type: 'money' }, { field: 'cost_price', label: '成本价', type: 'money' }, { field: 'tag_price', label: '标签价', type: 'money', aliases: [ '标价' ] }, + { + field: 'subsidiary_name', + label: '所属货主', + type: 'text', + max: 100, + aliases: [ '货主', '下属公司', '所属公司' ], + }, { field: 'remark', label: '备注', type: 'text', max: 255 }, ]; diff --git a/backend/app/model/product.js b/backend/app/model/product.js index c4453c1..45c0125 100644 --- a/backend/app/model/product.js +++ b/backend/app/model/product.js @@ -11,6 +11,7 @@ module.exports = app => { const Product = app.model.define('product', { id: { type: BIGINT.UNSIGNED, primaryKey: true, autoIncrement: true, comment: '主键' }, principal_id: { type: BIGINT.UNSIGNED, allowNull: false, comment: '所属主体ID' }, + subsidiary_id: { type: BIGINT.UNSIGNED, comment: '所属货主(下属公司ID)' }, item_code: { type: STRING(50), allowNull: false, comment: '商品编码' }, goods_code: { type: STRING(100), comment: '货物编码' }, item_name: { type: STRING(255), allowNull: false, comment: '商品名称' }, @@ -55,6 +56,8 @@ module.exports = app => { Product.associate = () => { app.model.Product.belongsTo(app.model.Principal, { foreignKey: 'principal_id', as: 'principal' }); + // 所属货主(下属公司):为空表示未指定 + app.model.Product.belongsTo(app.model.PrincipalSubsidiary, { foreignKey: 'subsidiary_id', as: 'subsidiary' }); // 反向关联:店铺商品单向绑定到该商品、订单明细解析到该商品 app.model.Product.hasMany(app.model.ShopProduct, { foreignKey: 'product_id', as: 'shopProducts' }); app.model.Product.hasMany(app.model.OrderItem, { foreignKey: 'product_id', as: 'orderItems' }); diff --git a/backend/app/model/warehouse.js b/backend/app/model/warehouse.js index 621687d..c99e0f1 100644 --- a/backend/app/model/warehouse.js +++ b/backend/app/model/warehouse.js @@ -12,6 +12,7 @@ module.exports = app => { const Warehouse = app.model.define('warehouse', { id: { type: BIGINT.UNSIGNED, primaryKey: true, autoIncrement: true, comment: '主键' }, principal_id: { type: BIGINT.UNSIGNED, allowNull: false, comment: '所属主体ID' }, + subsidiary_id: { type: BIGINT.UNSIGNED, comment: '所属公司(下属公司ID)' }, code: { type: STRING(50), allowNull: false, comment: '仓库编码(系统自动生成,WH 前缀)' }, name: { type: STRING(100), allowNull: false, comment: '仓库名称' }, type: { type: STRING(50), comment: '仓库类型:wms/erp/other' }, @@ -25,6 +26,8 @@ module.exports = app => { Warehouse.associate = () => { app.model.Warehouse.belongsTo(app.model.Principal, { foreignKey: 'principal_id', as: 'principal' }); + // 所属公司(下属公司):新建/编辑仓库时必选 + app.model.Warehouse.belongsTo(app.model.PrincipalSubsidiary, { foreignKey: 'subsidiary_id', as: 'subsidiary' }); // 一个仓库可配置多个接口 app.model.Warehouse.hasMany(app.model.WarehouseApi, { foreignKey: 'warehouse_id', as: 'apis' }); // 仓库-商品关联:记录商品是否已推送到该仓库 diff --git a/backend/app/utils/exportDefs.js b/backend/app/utils/exportDefs.js index 7fb3cb2..9cac8b8 100644 --- a/backend/app/utils/exportDefs.js +++ b/backend/app/utils/exportDefs.js @@ -360,6 +360,11 @@ const PRODUCT_COLUMNS = [ { key: 'brand_name', header: '品牌' }, { key: 'barcode', header: '条码' }, { key: 'stock_unit', header: '单位' }, + { + key: 'subsidiary_name', + header: '所属货主', + expr: '(SELECT s.name FROM principal_subsidiaries s WHERE s.id = t.subsidiary_id)', + }, { key: 'retail_price', header: '零售价' }, { key: 'cost_price', header: '成本价' }, { key: 'tag_price', header: '标签价' }, @@ -501,6 +506,11 @@ const WAREHOUSE_COLUMNS = [ { key: 'id', header: 'ID' }, { key: 'code', header: '仓库编码' }, { key: 'name', header: '仓库名称' }, + { + key: 'subsidiary_name', + header: '货主', + expr: '(SELECT s.name FROM principal_subsidiaries s WHERE s.id = t.subsidiary_id)', + }, { key: 'type', header: '仓库类型', format: labels(WAREHOUSE_TYPE_MAP) }, { key: 'status', header: '状态', format: labels(ENABLE_MAP) }, { key: 'remark', header: '备注' }, @@ -759,6 +769,7 @@ const EXPORT_TYPES = { { key: 'category_name', label: '分类', type: 'text', placeholder: '精确匹配(如 服装)' }, { key: 'brand_name', label: '品牌', type: 'text', placeholder: '精确匹配' }, { key: 'stock_unit', label: '单位', type: 'text', placeholder: '精确匹配(如 件)' }, + { key: 'subsidiary_id', label: '所属货主', type: 'subsidiary' }, { key: 'status', label: '状态', type: 'select', options: ENABLE_OPTIONS }, ], build: (f, ctx) => { @@ -769,6 +780,7 @@ const EXPORT_TYPES = { .eq('t.category_name', f.category_name) .eq('t.brand_name', f.brand_name) .eq('t.stock_unit', f.stock_unit) + .eq('t.subsidiary_id', f.subsidiary_id) .eq('t.status', f.status); return buildResult({ columns: PRODUCT_COLUMNS, from: 'products t', wh }); }, @@ -907,6 +919,7 @@ const EXPORT_TYPES = { label: '外部仓库', filters: [ { key: 'keyword', label: '关键词', type: 'text', placeholder: '仓库编码 / 名称' }, + { key: 'subsidiary_id', label: '货主', type: 'subsidiary' }, { key: 'type', label: '仓库类型', type: 'select', options: WAREHOUSE_TYPE_OPTIONS }, { key: 'status', label: '状态', type: 'select', options: ENABLE_OPTIONS }, ], @@ -915,6 +928,7 @@ const EXPORT_TYPES = { wh.eq('t.principal_id', ctx.principalId) .raw('t.deleted_at IS NULL') .kw([ 't.code', 't.name' ], f.keyword) + .eq('t.subsidiary_id', f.subsidiary_id) .eq('t.type', f.type) .eq('t.status', f.status); return buildResult({ columns: WAREHOUSE_COLUMNS, from: 'warehouses t', wh }); diff --git a/backend/database/migrations/20261104070700-add-subsidiary-to-products.js b/backend/database/migrations/20261104070700-add-subsidiary-to-products.js new file mode 100644 index 0000000..ccebbb4 --- /dev/null +++ b/backend/database/migrations/20261104070700-add-subsidiary-to-products.js @@ -0,0 +1,27 @@ +'use strict'; + +/** + * 商品增加「所属货主」:即所属下属公司(principal_subsidiaries.id)。 + * + * 可为空(历史商品与外部同步商品没有货主归属), + * 有值时必须是当前主体(集团)下的下属公司。 + */ +module.exports = { + async up(queryInterface, Sequelize) { + await queryInterface.addColumn('products', 'subsidiary_id', { + type: Sequelize.BIGINT.UNSIGNED, + allowNull: true, + comment: '所属货主(下属公司ID)', + references: { model: 'principal_subsidiaries', key: 'id' }, + after: 'principal_id', + }); + await queryInterface.addIndex('products', [ 'principal_id', 'subsidiary_id' ], { + name: 'idx_products_principal_subsidiary', + }); + }, + + async down(queryInterface) { + await queryInterface.removeIndex('products', 'idx_products_principal_subsidiary'); + await queryInterface.removeColumn('products', 'subsidiary_id'); + }, +}; diff --git a/backend/database/migrations/20261104070900-add-subsidiary-to-warehouses.js b/backend/database/migrations/20261104070900-add-subsidiary-to-warehouses.js new file mode 100644 index 0000000..3667de2 --- /dev/null +++ b/backend/database/migrations/20261104070900-add-subsidiary-to-warehouses.js @@ -0,0 +1,26 @@ +'use strict'; + +/** + * 外部仓库绑定下属公司:warehouses 增加 subsidiary_id(所属公司)。 + * + * 新建/编辑仓库时必须选择所属公司(后端校验),历史仓库可为空、编辑时需补齐。 + */ +module.exports = { + async up(queryInterface, Sequelize) { + await queryInterface.addColumn('warehouses', 'subsidiary_id', { + type: Sequelize.BIGINT.UNSIGNED, + allowNull: true, + comment: '所属公司(下属公司ID)', + references: { model: 'principal_subsidiaries', key: 'id' }, + after: 'principal_id', + }); + await queryInterface.addIndex('warehouses', [ 'principal_id', 'subsidiary_id' ], { + name: 'idx_warehouses_principal_subsidiary', + }); + }, + + async down(queryInterface) { + await queryInterface.removeIndex('warehouses', 'idx_warehouses_principal_subsidiary'); + await queryInterface.removeColumn('warehouses', 'subsidiary_id'); + }, +}; diff --git a/frontend/src/api/exportTask.ts b/frontend/src/api/exportTask.ts index f736053..c6a905e 100644 --- a/frontend/src/api/exportTask.ts +++ b/frontend/src/api/exportTask.ts @@ -7,7 +7,7 @@ import request from '@/utils/request' */ export type ExportType = string -/** 筛选字段控件类型:文本 / 下拉 / 日期 / 数字 / 商品选择器 / 店铺选择器 / 仓库选择器 */ +/** 筛选字段控件类型:文本 / 下拉 / 日期 / 数字 / 商品选择器 / 店铺选择器 / 仓库选择器 / 下属公司选择器 */ export type ExportFilterType = | 'text' | 'select' @@ -16,6 +16,7 @@ export type ExportFilterType = | 'product' | 'shop' | 'warehouse' + | 'subsidiary' /** 筛选字段下拉选项 */ export interface ExportFilterOption { diff --git a/frontend/src/api/product.ts b/frontend/src/api/product.ts index d7fa5c1..3d3b48f 100644 --- a/frontend/src/api/product.ts +++ b/frontend/src/api/product.ts @@ -4,6 +4,12 @@ import request from '@/utils/request' export interface ProductItem { id: number principal_id: number + /** 所属货主(下属公司ID),为空表示未指定 */ + subsidiary_id: number | null + /** 所属货主名称(列表/详情接口返回) */ + subsidiary_name?: string + /** 所属货主明细(列表/详情接口返回) */ + subsidiary?: { id: number; name: string } | null item_code: string goods_code: string | null barcode: string | null @@ -62,6 +68,8 @@ export interface ProductSearchItem { export interface ProductPayload { item_code: string item_name: string + /** 所属货主(下属公司ID),必填 */ + subsidiary_id: number goods_code?: string | null barcode?: string | null item_type?: number diff --git a/frontend/src/api/warehouse.ts b/frontend/src/api/warehouse.ts index b1b5efb..e9cb579 100644 --- a/frontend/src/api/warehouse.ts +++ b/frontend/src/api/warehouse.ts @@ -4,6 +4,12 @@ import type { WarehouseApiItem } from '@/api/warehouseApi' export interface WarehouseItem { id: number principal_id: number + /** 所属公司(下属公司ID) */ + subsidiary_id: number | null + /** 所属公司名称(列表/详情接口返回) */ + subsidiary_name?: string + /** 所属公司明细(列表/详情接口返回) */ + subsidiary?: { id: number; name: string } | null code: string name: string type: string | null @@ -19,6 +25,7 @@ export interface WarehouseOption { id: number code: string name: string + subsidiary_id?: number | null } /** 仓库关联统计(详情接口返回) */ @@ -38,6 +45,8 @@ export interface WarehousePayload { /** 仓库编码由后端自动生成(WH 前缀),无需传入 */ code?: string name: string + /** 所属公司(下属公司ID),必填 */ + subsidiary_id: number type?: string | null status?: number remark?: string | null diff --git a/frontend/src/components/ExportFilterField.vue b/frontend/src/components/ExportFilterField.vue index 2edd0ca..8ae0ce1 100644 --- a/frontend/src/components/ExportFilterField.vue +++ b/frontend/src/components/ExportFilterField.vue @@ -10,7 +10,7 @@ import type { ProductSearchItem } from '@/api/product' * * - text 文本框 / select 下拉 / date 日期 / number 数字 * - product 商品选择器(锁定 product_id,并通过 update:productLabel 回传展示标签) - * - shop / warehouse 店铺、仓库虚拟下拉(选项由父组件传入,避免各页重复请求) + * - shop / warehouse / subsidiary 店铺、仓库、下属公司(所属货主)虚拟下拉(选项由父组件传入,避免各页重复请求) */ const props = withDefaults( @@ -25,12 +25,15 @@ const props = withDefaults( shopOptions?: { label: string; value: number }[] /** 仓库下拉选项 */ warehouseOptions?: { label: string; value: number }[] + /** 下属公司(所属货主)下拉选项 */ + subsidiaryOptions?: { label: string; value: number }[] }>(), { modelValue: undefined, productLabel: '', shopOptions: () => [], warehouseOptions: () => [], + subsidiaryOptions: () => [], }, ) @@ -133,6 +136,16 @@ function clearProduct() { style="width: 100%" /> + + , }) -/** 店铺 / 仓库下拉选项(打开弹窗时按需加载) */ +/** 店铺 / 仓库 / 下属公司下拉选项(打开弹窗时按需加载) */ const shopOptions = ref<{ label: string; value: number }[]>([]) const warehouseOptions = ref<{ label: string; value: number }[]>([]) +const subsidiaryOptions = ref<{ label: string; value: number }[]>([]) async function loadOptions() { try { @@ -97,6 +99,12 @@ async function loadOptions() { } catch { warehouseOptions.value = [] } + try { + const list = await getPrincipalSubsidiaryOptions() + subsidiaryOptions.value = list.map(item => ({ label: item.name, value: item.id })) + } catch { + subsidiaryOptions.value = [] + } } function openDialog() { @@ -264,6 +272,7 @@ onUnmounted(() => stopPolling()) :field="fd" :shop-options="shopOptions" :warehouse-options="warehouseOptions" + :subsidiary-options="subsidiaryOptions" /> diff --git a/frontend/src/views/Product/Index.vue b/frontend/src/views/Product/Index.vue index 29319dc..4e5ae40 100644 --- a/frontend/src/views/Product/Index.vue +++ b/frontend/src/views/Product/Index.vue @@ -33,6 +33,7 @@ import DataTable from '@/components/DataTable.vue' import ListPager from '@/components/ListPager.vue' import OperationLogPanel from '@/components/OperationLogPanel.vue' import { DEFAULT_PAGE_SIZE, useListPage } from '@/composables/useListPage' +import { getPrincipalSubsidiaryOptions, type PrincipalSubsidiaryOption } from '@/api/principalSubsidiary' import { mediaUrl } from '@/utils/media' const route = useRoute() @@ -44,6 +45,7 @@ const query = reactive({ category_name: '', brand_name: '', stock_unit: '', + subsidiary_id: '' as string | number, status: '' as string | number, }) @@ -55,6 +57,7 @@ const { loading, list, total, elapsed, fetchList, search, handlePageChange, hand if (!params.category_name) delete params.category_name if (!params.brand_name) delete params.brand_name if (!params.stock_unit) delete params.stock_unit + if (params.subsidiary_id === '') delete params.subsidiary_id if (params.status === '') delete params.status return getProducts(params) }, @@ -65,6 +68,7 @@ function reset() { query.category_name = '' query.brand_name = '' query.stock_unit = '' + query.subsidiary_id = '' query.status = '' search() } @@ -75,6 +79,7 @@ const exportFilters = computed(() => ({ category_name: query.category_name, brand_name: query.brand_name, stock_unit: query.stock_unit, + subsidiary_id: query.subsidiary_id, status: query.status, })) @@ -91,6 +96,17 @@ const categoryOptions = ref([]) const brandOptions = ref([]) const unitOptions = ref([]) +/** 所属货主下拉(组织架构 → 下属公司,仅当前主体启用中的公司) */ +const subsidiaryOptions = ref([]) + +async function loadSubsidiaryOptions() { + try { + subsidiaryOptions.value = await getPrincipalSubsidiaryOptions() + } catch { + subsidiaryOptions.value = [] + } +} + async function loadAttributeOptions() { try { const [ c, b, u ] = await Promise.all([ @@ -175,6 +191,7 @@ const editId = ref(null) const form = reactive({ item_code: '', item_name: '', + subsidiary_id: null as number | null, goods_code: '', barcode: '', item_type: 1, @@ -206,6 +223,7 @@ const form = reactive({ const EMPTY_FORM = { item_code: '', item_name: '', + subsidiary_id: null as number | null, goods_code: '', barcode: '', item_type: 1, @@ -237,6 +255,7 @@ const EMPTY_FORM = { const rules: FormRules = { item_code: [{ required: true, message: '请输入商品编码', trigger: 'blur' }], item_name: [{ required: true, message: '请输入商品名称', trigger: 'blur' }], + subsidiary_id: [{ required: true, message: '请选择所属货主', trigger: 'change' }], retail_price: [{ required: true, message: '请输入零售价', trigger: 'blur' }], status: [{ required: true, message: '请选择状态', trigger: 'change' }], } @@ -254,6 +273,7 @@ function openEdit(row: ProductItem) { Object.assign(form, { item_code: row.item_code, item_name: row.item_name, + subsidiary_id: row.subsidiary_id ?? null, goods_code: row.goods_code || '', barcode: row.barcode || '', item_type: row.item_type, @@ -321,8 +341,12 @@ async function submitForm() { if (!valid) return submitting.value = true try { - // 主图冗余取图片组首张,继续兼容外部系统与历史单图字段 - const payload = { ...form, image: form.images[0] || '' } + // 主图冗余取图片组首张,继续兼容外部系统与历史单图字段;所属货主已由表单校验保证必选 + const payload = { + ...form, + subsidiary_id: Number(form.subsidiary_id), + image: form.images[0] || '', + } if (editId.value == null) { await createProduct(payload) ElMessage.success('新增成功') @@ -451,7 +475,7 @@ async function submitImport() { } onMounted(async () => { - await loadAttributeOptions() + await Promise.all([ loadAttributeOptions(), loadSubsidiaryOptions() ]) applyRouteQuery() await fetchList() }) @@ -512,6 +536,18 @@ onMounted(async () => { + + + + + @@ -530,6 +566,9 @@ onMounted(async () => { + + +