feat(backend): 新增商品分类/品牌/单位字典与属性菜单
- 按主体隔离的分类/品牌/单位 CRUD 与 /select 下拉接口 - 商品模型增加 brand_id / unit_id,属性被商品引用时禁止删除(attributeUsage) - 迁移:建三张字典表、商品表加字段、种子属性菜单、is_system 保护内置项 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
parent
406e5f3b58
commit
d22f3e1f0a
@ -61,6 +61,8 @@ class ProductController extends Controller {
|
||||
* @request query integer principal_id 所属主体ID(必填,需当前账号已绑定该主体)
|
||||
* @request query string keyword 关键词,匹配商品编码或名称
|
||||
* @request query string category_name 分类名称,精确匹配
|
||||
* @request query string brand_name 品牌名称,精确匹配
|
||||
* @request query string stock_unit 库存单位,精确匹配
|
||||
* @request query integer status 状态:1上架 0下架,不传则不过滤
|
||||
* @response 200 productListResponse success
|
||||
*/
|
||||
@ -68,7 +70,7 @@ class ProductController extends Controller {
|
||||
const { ctx } = this;
|
||||
const Op = this.app.Sequelize.Op;
|
||||
const { page, pageSize, offset, limit } = parsePagination(ctx.query);
|
||||
const { keyword, category_name: categoryName, status } = ctx.query;
|
||||
const { keyword, category_name: categoryName, brand_name: brandName, stock_unit: stockUnit, status } = ctx.query;
|
||||
const principalId = ctx.state.principalId;
|
||||
|
||||
const where = {};
|
||||
@ -82,6 +84,8 @@ class ProductController extends Controller {
|
||||
];
|
||||
}
|
||||
if (categoryName) where.category_name = categoryName;
|
||||
if (brandName) where.brand_name = brandName;
|
||||
if (stockUnit) where.stock_unit = stockUnit;
|
||||
if (status !== undefined && status !== '') where.status = Number(status);
|
||||
|
||||
const { count, rows } = await ctx.model.Product.findAndCountAll({
|
||||
|
||||
219
backend/app/controller/productBrand.js
Normal file
219
backend/app/controller/productBrand.js
Normal file
@ -0,0 +1,219 @@
|
||||
'use strict';
|
||||
|
||||
const { Controller } = require('egg');
|
||||
const { parsePagination, buildPageResult } = require('../lib/pagination');
|
||||
const { attachUsedCount } = require('../lib/attributeUsage');
|
||||
|
||||
/** 商品表引用品牌主键的字段名 */
|
||||
const COLUMN = 'brand_id';
|
||||
|
||||
/**
|
||||
* @controller ProductBrandController 商品品牌管理
|
||||
* 按主体隔离(principal_id);同一主体下品牌名称唯一;被商品引用时禁止删除。
|
||||
*/
|
||||
class ProductBrandController extends Controller {
|
||||
/**
|
||||
* @summary 商品品牌列表
|
||||
* @description 分页查询,支持按关键词(名称/编码)与状态过滤;附带 used_count(被商品使用次数)
|
||||
* @router get /api/product-brands
|
||||
* @apikey
|
||||
* @request query integer page 页码,默认 1
|
||||
* @request query integer pageSize 每页条数,默认 20
|
||||
* @request query string keyword 关键词,匹配品牌名称或编码
|
||||
* @request query integer status 状态:1启用 0禁用
|
||||
* @response 200 productBrandListResponse success
|
||||
*/
|
||||
async index() {
|
||||
const { ctx } = this;
|
||||
const Op = this.app.Sequelize.Op;
|
||||
const { page, pageSize, offset, limit } = parsePagination(ctx.query);
|
||||
const { keyword, status } = ctx.query;
|
||||
const principalId = ctx.state.principalId;
|
||||
|
||||
const where = {
|
||||
[Op.or]: [
|
||||
{ principal_id: principalId },
|
||||
{ is_system: 1 },
|
||||
],
|
||||
};
|
||||
if (keyword) {
|
||||
where[Op.or] = [
|
||||
{ name: { [Op.like]: `%${keyword}%` } },
|
||||
{ code: { [Op.like]: `%${keyword}%` } },
|
||||
];
|
||||
}
|
||||
if (status !== undefined && status !== '') where.status = Number(status);
|
||||
|
||||
const { count, rows } = await ctx.model.ProductBrand.findAndCountAll({
|
||||
where,
|
||||
offset,
|
||||
limit,
|
||||
order: [[ 'sort', 'ASC' ], [ 'id', 'ASC' ]],
|
||||
});
|
||||
|
||||
const list = await attachUsedCount(ctx, COLUMN, rows);
|
||||
ctx.body = { code: 0, message: 'success', data: buildPageResult(list, count, { page, pageSize }) };
|
||||
}
|
||||
|
||||
/**
|
||||
* @summary 商品品牌下拉(仅启用)
|
||||
* @description 供商品表单选择品牌,按 sort 升序返回
|
||||
* @router get /api/product-brands/select
|
||||
* @apikey
|
||||
* @response 200 baseResponse success
|
||||
*/
|
||||
async select() {
|
||||
const { ctx } = this;
|
||||
const Op = this.app.Sequelize.Op;
|
||||
const principalId = ctx.state.principalId;
|
||||
const rows = await ctx.model.ProductBrand.findAll({
|
||||
where: {
|
||||
status: 1,
|
||||
[Op.or]: [
|
||||
{ principal_id: principalId },
|
||||
{ is_system: 1 },
|
||||
],
|
||||
},
|
||||
attributes: [ 'id', 'name', 'code' ],
|
||||
order: [[ 'sort', 'ASC' ], [ 'id', 'ASC' ]],
|
||||
});
|
||||
ctx.body = { code: 0, message: 'success', data: rows };
|
||||
}
|
||||
|
||||
/**
|
||||
* @summary 商品品牌详情
|
||||
* @router get /api/product-brands/{id}
|
||||
* @apikey
|
||||
* @request path integer *id 品牌主键ID
|
||||
* @response 200 baseResponse success
|
||||
*/
|
||||
async show() {
|
||||
const { ctx } = this;
|
||||
const item = await ctx.model.ProductBrand.findByPk(ctx.params.id);
|
||||
if (!item) {
|
||||
ctx.status = 404;
|
||||
ctx.body = { code: 404, message: '商品品牌不存在' };
|
||||
return;
|
||||
}
|
||||
ctx.body = { code: 0, message: 'success', data: item };
|
||||
}
|
||||
|
||||
/**
|
||||
* @summary 新增商品品牌
|
||||
* @description 同一主体下品牌名称唯一
|
||||
* @router post /api/product-brands
|
||||
* @apikey
|
||||
* @request body createProductBrandRequest *body
|
||||
* @response 200 baseResponse success
|
||||
*/
|
||||
async create() {
|
||||
const { ctx } = this;
|
||||
const body = ctx.request.body || {};
|
||||
const principalId = ctx.state.principalId;
|
||||
const name = String(body.name || '').trim();
|
||||
|
||||
if (!name) {
|
||||
ctx.status = 400;
|
||||
ctx.body = { code: 400, message: '品牌名称为必填项' };
|
||||
return;
|
||||
}
|
||||
|
||||
const Op = this.app.Sequelize.Op;
|
||||
// 自定义不可与系统/本主体已有的重名:若已存在(系统或本主体)则直接复用,不再新增
|
||||
const exist = await ctx.model.ProductBrand.findOne({
|
||||
where: {
|
||||
name,
|
||||
[Op.or]: [ { principal_id: principalId }, { is_system: 1 } ],
|
||||
},
|
||||
});
|
||||
if (exist) {
|
||||
ctx.body = { code: 0, message: 'success', data: { id: exist.id } };
|
||||
return;
|
||||
}
|
||||
|
||||
const item = await ctx.model.ProductBrand.create({ ...body, principal_id: principalId, name, is_system: 0 });
|
||||
ctx.body = { code: 0, message: 'success', data: { id: item.id } };
|
||||
}
|
||||
|
||||
/**
|
||||
* @summary 更新商品品牌
|
||||
* @description 未传入的字段保持原值;同一主体下品牌名称唯一
|
||||
* @router put /api/product-brands/{id}
|
||||
* @apikey
|
||||
* @request path integer *id 品牌主键ID
|
||||
* @request body updateProductBrandRequest *body
|
||||
* @response 200 baseResponse success
|
||||
*/
|
||||
async update() {
|
||||
const { ctx } = this;
|
||||
const item = await ctx.model.ProductBrand.findByPk(ctx.params.id);
|
||||
if (!item) {
|
||||
ctx.status = 404;
|
||||
ctx.body = { code: 404, message: '商品品牌不存在' };
|
||||
return;
|
||||
}
|
||||
|
||||
if (item.is_system) {
|
||||
ctx.status = 400;
|
||||
ctx.body = { code: 400, message: '系统默认数据不可修改' };
|
||||
return;
|
||||
}
|
||||
|
||||
const body = { ...(ctx.request.body || {}) };
|
||||
delete body.is_system;
|
||||
if (body.name !== undefined) {
|
||||
const name = String(body.name).trim();
|
||||
if (!name) {
|
||||
ctx.status = 400;
|
||||
ctx.body = { code: 400, message: '品牌名称不能为空' };
|
||||
return;
|
||||
}
|
||||
const exist = await ctx.model.ProductBrand.findOne({ where: { principal_id: item.principal_id, name } });
|
||||
if (exist && Number(exist.id) !== Number(item.id)) {
|
||||
ctx.status = 400;
|
||||
ctx.body = { code: 400, message: `品牌「${name}」已存在` };
|
||||
return;
|
||||
}
|
||||
body.name = name;
|
||||
}
|
||||
|
||||
await item.update(body);
|
||||
ctx.body = { code: 0, message: 'success', data: { id: item.id } };
|
||||
}
|
||||
|
||||
/**
|
||||
* @summary 删除商品品牌
|
||||
* @description 若该品牌已被商品引用(products.brand_id),禁止删除;否则物理删除
|
||||
* @router delete /api/product-brands/{id}
|
||||
* @apikey
|
||||
* @request path integer *id 品牌主键ID
|
||||
* @response 200 baseResponse success
|
||||
*/
|
||||
async destroy() {
|
||||
const { ctx } = this;
|
||||
const item = await ctx.model.ProductBrand.findByPk(ctx.params.id);
|
||||
if (!item) {
|
||||
ctx.status = 404;
|
||||
ctx.body = { code: 404, message: '商品品牌不存在' };
|
||||
return;
|
||||
}
|
||||
|
||||
if (item.is_system) {
|
||||
ctx.status = 400;
|
||||
ctx.body = { code: 400, message: '系统默认数据不可删除' };
|
||||
return;
|
||||
}
|
||||
|
||||
const used = await ctx.model.Product.count({ where: { principal_id: item.principal_id, brand_id: item.id } });
|
||||
if (used > 0) {
|
||||
ctx.status = 400;
|
||||
ctx.body = { code: 400, message: `该品牌已被 ${used} 个商品使用,禁止删除` };
|
||||
return;
|
||||
}
|
||||
|
||||
await item.destroy();
|
||||
ctx.body = { code: 0, message: 'success', data: { id: Number(ctx.params.id) } };
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = ProductBrandController;
|
||||
219
backend/app/controller/productCategory.js
Normal file
219
backend/app/controller/productCategory.js
Normal file
@ -0,0 +1,219 @@
|
||||
'use strict';
|
||||
|
||||
const { Controller } = require('egg');
|
||||
const { parsePagination, buildPageResult } = require('../lib/pagination');
|
||||
const { attachUsedCount } = require('../lib/attributeUsage');
|
||||
|
||||
/** 商品表引用分类主键的字段名 */
|
||||
const COLUMN = 'category_id';
|
||||
|
||||
/**
|
||||
* @controller ProductCategoryController 商品分类管理
|
||||
* 按主体隔离(principal_id);同一主体下分类名称唯一;被商品引用时禁止删除。
|
||||
*/
|
||||
class ProductCategoryController extends Controller {
|
||||
/**
|
||||
* @summary 商品分类列表
|
||||
* @description 分页查询,支持按关键词(名称/编码)与状态过滤;附带 used_count(被商品使用次数)
|
||||
* @router get /api/product-categories
|
||||
* @apikey
|
||||
* @request query integer page 页码,默认 1
|
||||
* @request query integer pageSize 每页条数,默认 20
|
||||
* @request query string keyword 关键词,匹配分类名称或编码
|
||||
* @request query integer status 状态:1启用 0禁用
|
||||
* @response 200 productCategoryListResponse success
|
||||
*/
|
||||
async index() {
|
||||
const { ctx } = this;
|
||||
const Op = this.app.Sequelize.Op;
|
||||
const { page, pageSize, offset, limit } = parsePagination(ctx.query);
|
||||
const { keyword, status } = ctx.query;
|
||||
const principalId = ctx.state.principalId;
|
||||
|
||||
const where = {
|
||||
[Op.or]: [
|
||||
{ principal_id: principalId },
|
||||
{ is_system: 1 },
|
||||
],
|
||||
};
|
||||
if (keyword) {
|
||||
where[Op.or] = [
|
||||
{ name: { [Op.like]: `%${keyword}%` } },
|
||||
{ code: { [Op.like]: `%${keyword}%` } },
|
||||
];
|
||||
}
|
||||
if (status !== undefined && status !== '') where.status = Number(status);
|
||||
|
||||
const { count, rows } = await ctx.model.ProductCategory.findAndCountAll({
|
||||
where,
|
||||
offset,
|
||||
limit,
|
||||
order: [[ 'sort', 'ASC' ], [ 'id', 'ASC' ]],
|
||||
});
|
||||
|
||||
const list = await attachUsedCount(ctx, COLUMN, rows);
|
||||
ctx.body = { code: 0, message: 'success', data: buildPageResult(list, count, { page, pageSize }) };
|
||||
}
|
||||
|
||||
/**
|
||||
* @summary 商品分类下拉(仅启用)
|
||||
* @description 供商品表单选择分类,按 sort 升序返回
|
||||
* @router get /api/product-categories/select
|
||||
* @apikey
|
||||
* @response 200 baseResponse success
|
||||
*/
|
||||
async select() {
|
||||
const { ctx } = this;
|
||||
const Op = this.app.Sequelize.Op;
|
||||
const principalId = ctx.state.principalId;
|
||||
const rows = await ctx.model.ProductCategory.findAll({
|
||||
where: {
|
||||
status: 1,
|
||||
[Op.or]: [
|
||||
{ principal_id: principalId },
|
||||
{ is_system: 1 },
|
||||
],
|
||||
},
|
||||
attributes: [ 'id', 'name', 'code' ],
|
||||
order: [[ 'sort', 'ASC' ], [ 'id', 'ASC' ]],
|
||||
});
|
||||
ctx.body = { code: 0, message: 'success', data: rows };
|
||||
}
|
||||
|
||||
/**
|
||||
* @summary 商品分类详情
|
||||
* @router get /api/product-categories/{id}
|
||||
* @apikey
|
||||
* @request path integer *id 分类主键ID
|
||||
* @response 200 baseResponse success
|
||||
*/
|
||||
async show() {
|
||||
const { ctx } = this;
|
||||
const item = await ctx.model.ProductCategory.findByPk(ctx.params.id);
|
||||
if (!item) {
|
||||
ctx.status = 404;
|
||||
ctx.body = { code: 404, message: '商品分类不存在' };
|
||||
return;
|
||||
}
|
||||
ctx.body = { code: 0, message: 'success', data: item };
|
||||
}
|
||||
|
||||
/**
|
||||
* @summary 新增商品分类
|
||||
* @description 同一主体下分类名称唯一
|
||||
* @router post /api/product-categories
|
||||
* @apikey
|
||||
* @request body createProductCategoryRequest *body
|
||||
* @response 200 baseResponse success
|
||||
*/
|
||||
async create() {
|
||||
const { ctx } = this;
|
||||
const body = ctx.request.body || {};
|
||||
const principalId = ctx.state.principalId;
|
||||
const name = String(body.name || '').trim();
|
||||
|
||||
if (!name) {
|
||||
ctx.status = 400;
|
||||
ctx.body = { code: 400, message: '分类名称为必填项' };
|
||||
return;
|
||||
}
|
||||
|
||||
const Op = this.app.Sequelize.Op;
|
||||
// 自定义不可与系统/本主体已有的重名:若已存在(系统或本主体)则直接复用,不再新增
|
||||
const exist = await ctx.model.ProductCategory.findOne({
|
||||
where: {
|
||||
name,
|
||||
[Op.or]: [ { principal_id: principalId }, { is_system: 1 } ],
|
||||
},
|
||||
});
|
||||
if (exist) {
|
||||
ctx.body = { code: 0, message: 'success', data: { id: exist.id } };
|
||||
return;
|
||||
}
|
||||
|
||||
const item = await ctx.model.ProductCategory.create({ ...body, principal_id: principalId, name, is_system: 0 });
|
||||
ctx.body = { code: 0, message: 'success', data: { id: item.id } };
|
||||
}
|
||||
|
||||
/**
|
||||
* @summary 更新商品分类
|
||||
* @description 未传入的字段保持原值;同一主体下分类名称唯一
|
||||
* @router put /api/product-categories/{id}
|
||||
* @apikey
|
||||
* @request path integer *id 分类主键ID
|
||||
* @request body updateProductCategoryRequest *body
|
||||
* @response 200 baseResponse success
|
||||
*/
|
||||
async update() {
|
||||
const { ctx } = this;
|
||||
const item = await ctx.model.ProductCategory.findByPk(ctx.params.id);
|
||||
if (!item) {
|
||||
ctx.status = 404;
|
||||
ctx.body = { code: 404, message: '商品分类不存在' };
|
||||
return;
|
||||
}
|
||||
|
||||
if (item.is_system) {
|
||||
ctx.status = 400;
|
||||
ctx.body = { code: 400, message: '系统默认数据不可修改' };
|
||||
return;
|
||||
}
|
||||
|
||||
const body = { ...(ctx.request.body || {}) };
|
||||
delete body.is_system;
|
||||
if (body.name !== undefined) {
|
||||
const name = String(body.name).trim();
|
||||
if (!name) {
|
||||
ctx.status = 400;
|
||||
ctx.body = { code: 400, message: '分类名称不能为空' };
|
||||
return;
|
||||
}
|
||||
const exist = await ctx.model.ProductCategory.findOne({ where: { principal_id: item.principal_id, name } });
|
||||
if (exist && Number(exist.id) !== Number(item.id)) {
|
||||
ctx.status = 400;
|
||||
ctx.body = { code: 400, message: `分类「${name}」已存在` };
|
||||
return;
|
||||
}
|
||||
body.name = name;
|
||||
}
|
||||
|
||||
await item.update(body);
|
||||
ctx.body = { code: 0, message: 'success', data: { id: item.id } };
|
||||
}
|
||||
|
||||
/**
|
||||
* @summary 删除商品分类
|
||||
* @description 若该分类已被商品引用(products.category_id),禁止删除;否则物理删除
|
||||
* @router delete /api/product-categories/{id}
|
||||
* @apikey
|
||||
* @request path integer *id 分类主键ID
|
||||
* @response 200 baseResponse success
|
||||
*/
|
||||
async destroy() {
|
||||
const { ctx } = this;
|
||||
const item = await ctx.model.ProductCategory.findByPk(ctx.params.id);
|
||||
if (!item) {
|
||||
ctx.status = 404;
|
||||
ctx.body = { code: 404, message: '商品分类不存在' };
|
||||
return;
|
||||
}
|
||||
|
||||
if (item.is_system) {
|
||||
ctx.status = 400;
|
||||
ctx.body = { code: 400, message: '系统默认数据不可删除' };
|
||||
return;
|
||||
}
|
||||
|
||||
const used = await ctx.model.Product.count({ where: { principal_id: item.principal_id, category_id: item.id } });
|
||||
if (used > 0) {
|
||||
ctx.status = 400;
|
||||
ctx.body = { code: 400, message: `该分类已被 ${used} 个商品使用,禁止删除` };
|
||||
return;
|
||||
}
|
||||
|
||||
await item.destroy();
|
||||
ctx.body = { code: 0, message: 'success', data: { id: Number(ctx.params.id) } };
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = ProductCategoryController;
|
||||
219
backend/app/controller/productUnit.js
Normal file
219
backend/app/controller/productUnit.js
Normal file
@ -0,0 +1,219 @@
|
||||
'use strict';
|
||||
|
||||
const { Controller } = require('egg');
|
||||
const { parsePagination, buildPageResult } = require('../lib/pagination');
|
||||
const { attachUsedCount } = require('../lib/attributeUsage');
|
||||
|
||||
/** 商品表引用单位主键的字段名 */
|
||||
const COLUMN = 'unit_id';
|
||||
|
||||
/**
|
||||
* @controller ProductUnitController 商品单位管理
|
||||
* 按主体隔离(principal_id);同一主体下单位名称唯一;被商品引用时禁止删除。
|
||||
*/
|
||||
class ProductUnitController extends Controller {
|
||||
/**
|
||||
* @summary 商品单位列表
|
||||
* @description 分页查询,支持按关键词(名称/编码)与状态过滤;附带 used_count(被商品使用次数)
|
||||
* @router get /api/product-units
|
||||
* @apikey
|
||||
* @request query integer page 页码,默认 1
|
||||
* @request query integer pageSize 每页条数,默认 20
|
||||
* @request query string keyword 关键词,匹配单位名称或编码
|
||||
* @request query integer status 状态:1启用 0禁用
|
||||
* @response 200 productUnitListResponse success
|
||||
*/
|
||||
async index() {
|
||||
const { ctx } = this;
|
||||
const Op = this.app.Sequelize.Op;
|
||||
const { page, pageSize, offset, limit } = parsePagination(ctx.query);
|
||||
const { keyword, status } = ctx.query;
|
||||
const principalId = ctx.state.principalId;
|
||||
|
||||
const where = {
|
||||
[Op.or]: [
|
||||
{ principal_id: principalId },
|
||||
{ is_system: 1 },
|
||||
],
|
||||
};
|
||||
if (keyword) {
|
||||
where[Op.or] = [
|
||||
{ name: { [Op.like]: `%${keyword}%` } },
|
||||
{ code: { [Op.like]: `%${keyword}%` } },
|
||||
];
|
||||
}
|
||||
if (status !== undefined && status !== '') where.status = Number(status);
|
||||
|
||||
const { count, rows } = await ctx.model.ProductUnit.findAndCountAll({
|
||||
where,
|
||||
offset,
|
||||
limit,
|
||||
order: [[ 'sort', 'ASC' ], [ 'id', 'ASC' ]],
|
||||
});
|
||||
|
||||
const list = await attachUsedCount(ctx, COLUMN, rows);
|
||||
ctx.body = { code: 0, message: 'success', data: buildPageResult(list, count, { page, pageSize }) };
|
||||
}
|
||||
|
||||
/**
|
||||
* @summary 商品单位下拉(仅启用)
|
||||
* @description 供商品表单选择单位,按 sort 升序返回
|
||||
* @router get /api/product-units/select
|
||||
* @apikey
|
||||
* @response 200 baseResponse success
|
||||
*/
|
||||
async select() {
|
||||
const { ctx } = this;
|
||||
const Op = this.app.Sequelize.Op;
|
||||
const principalId = ctx.state.principalId;
|
||||
const rows = await ctx.model.ProductUnit.findAll({
|
||||
where: {
|
||||
status: 1,
|
||||
[Op.or]: [
|
||||
{ principal_id: principalId },
|
||||
{ is_system: 1 },
|
||||
],
|
||||
},
|
||||
attributes: [ 'id', 'name', 'code' ],
|
||||
order: [[ 'sort', 'ASC' ], [ 'id', 'ASC' ]],
|
||||
});
|
||||
ctx.body = { code: 0, message: 'success', data: rows };
|
||||
}
|
||||
|
||||
/**
|
||||
* @summary 商品单位详情
|
||||
* @router get /api/product-units/{id}
|
||||
* @apikey
|
||||
* @request path integer *id 单位主键ID
|
||||
* @response 200 baseResponse success
|
||||
*/
|
||||
async show() {
|
||||
const { ctx } = this;
|
||||
const item = await ctx.model.ProductUnit.findByPk(ctx.params.id);
|
||||
if (!item) {
|
||||
ctx.status = 404;
|
||||
ctx.body = { code: 404, message: '商品单位不存在' };
|
||||
return;
|
||||
}
|
||||
ctx.body = { code: 0, message: 'success', data: item };
|
||||
}
|
||||
|
||||
/**
|
||||
* @summary 新增商品单位
|
||||
* @description 同一主体下单位名称唯一
|
||||
* @router post /api/product-units
|
||||
* @apikey
|
||||
* @request body createProductUnitRequest *body
|
||||
* @response 200 baseResponse success
|
||||
*/
|
||||
async create() {
|
||||
const { ctx } = this;
|
||||
const body = ctx.request.body || {};
|
||||
const principalId = ctx.state.principalId;
|
||||
const name = String(body.name || '').trim();
|
||||
|
||||
if (!name) {
|
||||
ctx.status = 400;
|
||||
ctx.body = { code: 400, message: '单位名称为必填项' };
|
||||
return;
|
||||
}
|
||||
|
||||
const Op = this.app.Sequelize.Op;
|
||||
// 自定义不可与系统/本主体已有的重名:若已存在(系统或本主体)则直接复用,不再新增
|
||||
const exist = await ctx.model.ProductUnit.findOne({
|
||||
where: {
|
||||
name,
|
||||
[Op.or]: [ { principal_id: principalId }, { is_system: 1 } ],
|
||||
},
|
||||
});
|
||||
if (exist) {
|
||||
ctx.body = { code: 0, message: 'success', data: { id: exist.id } };
|
||||
return;
|
||||
}
|
||||
|
||||
const item = await ctx.model.ProductUnit.create({ ...body, principal_id: principalId, name, is_system: 0 });
|
||||
ctx.body = { code: 0, message: 'success', data: { id: item.id } };
|
||||
}
|
||||
|
||||
/**
|
||||
* @summary 更新商品单位
|
||||
* @description 未传入的字段保持原值;同一主体下单位名称唯一
|
||||
* @router put /api/product-units/{id}
|
||||
* @apikey
|
||||
* @request path integer *id 单位主键ID
|
||||
* @request body updateProductUnitRequest *body
|
||||
* @response 200 baseResponse success
|
||||
*/
|
||||
async update() {
|
||||
const { ctx } = this;
|
||||
const item = await ctx.model.ProductUnit.findByPk(ctx.params.id);
|
||||
if (!item) {
|
||||
ctx.status = 404;
|
||||
ctx.body = { code: 404, message: '商品单位不存在' };
|
||||
return;
|
||||
}
|
||||
|
||||
if (item.is_system) {
|
||||
ctx.status = 400;
|
||||
ctx.body = { code: 400, message: '系统默认数据不可修改' };
|
||||
return;
|
||||
}
|
||||
|
||||
const body = { ...(ctx.request.body || {}) };
|
||||
delete body.is_system;
|
||||
if (body.name !== undefined) {
|
||||
const name = String(body.name).trim();
|
||||
if (!name) {
|
||||
ctx.status = 400;
|
||||
ctx.body = { code: 400, message: '单位名称不能为空' };
|
||||
return;
|
||||
}
|
||||
const exist = await ctx.model.ProductUnit.findOne({ where: { principal_id: item.principal_id, name } });
|
||||
if (exist && Number(exist.id) !== Number(item.id)) {
|
||||
ctx.status = 400;
|
||||
ctx.body = { code: 400, message: `单位「${name}」已存在` };
|
||||
return;
|
||||
}
|
||||
body.name = name;
|
||||
}
|
||||
|
||||
await item.update(body);
|
||||
ctx.body = { code: 0, message: 'success', data: { id: item.id } };
|
||||
}
|
||||
|
||||
/**
|
||||
* @summary 删除商品单位
|
||||
* @description 若该单位已被商品引用(products.unit_id),禁止删除;否则物理删除
|
||||
* @router delete /api/product-units/{id}
|
||||
* @apikey
|
||||
* @request path integer *id 单位主键ID
|
||||
* @response 200 baseResponse success
|
||||
*/
|
||||
async destroy() {
|
||||
const { ctx } = this;
|
||||
const item = await ctx.model.ProductUnit.findByPk(ctx.params.id);
|
||||
if (!item) {
|
||||
ctx.status = 404;
|
||||
ctx.body = { code: 404, message: '商品单位不存在' };
|
||||
return;
|
||||
}
|
||||
|
||||
if (item.is_system) {
|
||||
ctx.status = 400;
|
||||
ctx.body = { code: 400, message: '系统默认数据不可删除' };
|
||||
return;
|
||||
}
|
||||
|
||||
const used = await ctx.model.Product.count({ where: { principal_id: item.principal_id, unit_id: item.id } });
|
||||
if (used > 0) {
|
||||
ctx.status = 400;
|
||||
ctx.body = { code: 400, message: `该单位已被 ${used} 个商品使用,禁止删除` };
|
||||
return;
|
||||
}
|
||||
|
||||
await item.destroy();
|
||||
ctx.body = { code: 0, message: 'success', data: { id: Number(ctx.params.id) } };
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = ProductUnitController;
|
||||
41
backend/app/lib/attributeUsage.js
Normal file
41
backend/app/lib/attributeUsage.js
Normal file
@ -0,0 +1,41 @@
|
||||
'use strict';
|
||||
|
||||
/**
|
||||
* 为商品属性(分类 / 品牌 / 单位)列表行附加「被商品使用次数(used_count)」。
|
||||
* 用于前端判断该属性是否已被商品引用:被引用(used_count > 0)时禁止删除。
|
||||
*
|
||||
* 单次按 id 分组聚合(而非逐行查询),避免 N+1:
|
||||
* 以商品表上引用该属性主键的字段(category_id / brand_id / unit_id)做 IN 聚合。
|
||||
*
|
||||
* 注意:按属性主键全局统计(不限定 principal_id)。原因:
|
||||
* - 属性主键全局唯一,按 id 聚合天然准确;
|
||||
* - 系统默认属性(is_system=1)可被任意主体商品引用,按当前主体统计会漏算。
|
||||
*
|
||||
* @param {Egg.Context} ctx egg context(需 principalCheck 已写入 ctx.state.principalId)
|
||||
* @param {string} column 商品表上引用该属性主键的字段名
|
||||
* @param {Array} rows 商品属性模型实例数组(findAndCountAll 的 rows)
|
||||
* @return {Promise<Array>} 附加 used_count 的 plain 对象数组
|
||||
*/
|
||||
async function attachUsedCount(ctx, column, rows) {
|
||||
const Op = ctx.app.Sequelize.Op;
|
||||
|
||||
const plain = rows.map(r => (r && r.toJSON ? r.toJSON() : r));
|
||||
const ids = plain.map(r => Number(r.id)).filter(Boolean);
|
||||
const usedMap = new Map();
|
||||
|
||||
if (ids.length) {
|
||||
const records = await ctx.model.Product.findAll({
|
||||
where: { [column]: { [Op.in]: ids } },
|
||||
attributes: [ column ],
|
||||
raw: true,
|
||||
});
|
||||
for (const rec of records) {
|
||||
const key = Number(rec[column]);
|
||||
usedMap.set(key, (usedMap.get(key) || 0) + 1);
|
||||
}
|
||||
}
|
||||
|
||||
return plain.map(r => ({ ...r, used_count: usedMap.get(Number(r.id)) || 0 }));
|
||||
}
|
||||
|
||||
module.exports = { attachUsedCount };
|
||||
@ -22,8 +22,11 @@ module.exports = app => {
|
||||
volume: { type: DECIMAL(10, 2), defaultValue: 0, comment: '体积(cm³)' },
|
||||
gross_weight: { type: INTEGER, defaultValue: 0, comment: '毛重(g)' },
|
||||
net_weight: { type: INTEGER, defaultValue: 0, comment: '净重(g)' },
|
||||
category_id: { type: INTEGER, comment: '分类ID' },
|
||||
category_name: { type: STRING(100), comment: '分类名称' },
|
||||
category_id: { type: INTEGER, comment: '分类ID(关联 product_categories)' },
|
||||
category_name: { type: STRING(100), comment: '分类名称(冗余文本)' },
|
||||
brand_id: { type: INTEGER, comment: '品牌ID(关联 product_brands)' },
|
||||
brand_name: { type: STRING(100), comment: '品牌名称(冗余文本)' },
|
||||
unit_id: { type: INTEGER, comment: '单位ID(关联 product_units)' },
|
||||
item_type: { type: TINYINT, allowNull: false, defaultValue: 1, comment: '商品类型' },
|
||||
tag_price: { type: DECIMAL(10, 2), defaultValue: 0, comment: '标价' },
|
||||
retail_price: { type: DECIMAL(12, 2), allowNull: false, defaultValue: 0, comment: '零售价' },
|
||||
|
||||
33
backend/app/model/productBrand.js
Normal file
33
backend/app/model/productBrand.js
Normal file
@ -0,0 +1,33 @@
|
||||
'use strict';
|
||||
|
||||
/**
|
||||
* 商品品牌模型,对应 product_brands 表(按主体隔离)
|
||||
* 供商品选择品牌:落 brand_id(本表主键)+ brand_name(名称文本,冗余存储)。
|
||||
* @param {Egg.Application} app egg application
|
||||
* @return {object} productBrand model
|
||||
*/
|
||||
module.exports = app => {
|
||||
const { BIGINT, STRING, TINYINT, INTEGER, DATE } = app.Sequelize;
|
||||
|
||||
const ProductBrand = app.model.define('product_brand', {
|
||||
id: { type: BIGINT.UNSIGNED, primaryKey: true, autoIncrement: true, comment: '主键' },
|
||||
principal_id: { type: BIGINT.UNSIGNED, allowNull: false, comment: '所属主体ID' },
|
||||
name: { type: STRING(100), allowNull: false, comment: '品牌名称' },
|
||||
code: { type: STRING(50), comment: '品牌编码' },
|
||||
status: { type: TINYINT, allowNull: false, defaultValue: 1, comment: '状态:1启用 0禁用' },
|
||||
sort: { type: INTEGER, allowNull: false, defaultValue: 0, comment: '排序(下拉展示顺序,越小越靠前)' },
|
||||
remark: { type: STRING(255), comment: '备注' },
|
||||
is_system: { type: TINYINT, allowNull: false, defaultValue: 0, comment: '是否系统默认:1系统数据(不可改/删,全部主体可见) 0自定义(按主体隔离)' },
|
||||
created_at: { type: DATE, comment: '创建时间' },
|
||||
updated_at: { type: DATE, comment: '更新时间' },
|
||||
}, {
|
||||
tableName: 'product_brands',
|
||||
comment: '商品品牌',
|
||||
});
|
||||
|
||||
ProductBrand.associate = () => {
|
||||
app.model.ProductBrand.belongsTo(app.model.Principal, { foreignKey: 'principal_id', as: 'principal' });
|
||||
};
|
||||
|
||||
return ProductBrand;
|
||||
};
|
||||
33
backend/app/model/productCategory.js
Normal file
33
backend/app/model/productCategory.js
Normal file
@ -0,0 +1,33 @@
|
||||
'use strict';
|
||||
|
||||
/**
|
||||
* 商品分类模型,对应 product_categories 表(按主体隔离)
|
||||
* 供商品选择分类:落 category_id(本表主键)+ category_name(名称文本,冗余存储)。
|
||||
* @param {Egg.Application} app egg application
|
||||
* @return {object} productCategory model
|
||||
*/
|
||||
module.exports = app => {
|
||||
const { BIGINT, STRING, TINYINT, INTEGER, DATE } = app.Sequelize;
|
||||
|
||||
const ProductCategory = app.model.define('product_category', {
|
||||
id: { type: BIGINT.UNSIGNED, primaryKey: true, autoIncrement: true, comment: '主键' },
|
||||
principal_id: { type: BIGINT.UNSIGNED, allowNull: false, comment: '所属主体ID' },
|
||||
name: { type: STRING(100), allowNull: false, comment: '分类名称' },
|
||||
code: { type: STRING(50), comment: '分类编码' },
|
||||
status: { type: TINYINT, allowNull: false, defaultValue: 1, comment: '状态:1启用 0禁用' },
|
||||
sort: { type: INTEGER, allowNull: false, defaultValue: 0, comment: '排序(下拉展示顺序,越小越靠前)' },
|
||||
remark: { type: STRING(255), comment: '备注' },
|
||||
is_system: { type: TINYINT, allowNull: false, defaultValue: 0, comment: '是否系统默认:1系统数据(不可改/删,全部主体可见) 0自定义(按主体隔离)' },
|
||||
created_at: { type: DATE, comment: '创建时间' },
|
||||
updated_at: { type: DATE, comment: '更新时间' },
|
||||
}, {
|
||||
tableName: 'product_categories',
|
||||
comment: '商品分类',
|
||||
});
|
||||
|
||||
ProductCategory.associate = () => {
|
||||
app.model.ProductCategory.belongsTo(app.model.Principal, { foreignKey: 'principal_id', as: 'principal' });
|
||||
};
|
||||
|
||||
return ProductCategory;
|
||||
};
|
||||
33
backend/app/model/productUnit.js
Normal file
33
backend/app/model/productUnit.js
Normal file
@ -0,0 +1,33 @@
|
||||
'use strict';
|
||||
|
||||
/**
|
||||
* 商品单位模型,对应 product_units 表(按主体隔离)
|
||||
* 供商品选择单位:落 unit_id(本表主键)+ stock_unit(单位名称文本,冗余存储)。
|
||||
* @param {Egg.Application} app egg application
|
||||
* @return {object} productUnit model
|
||||
*/
|
||||
module.exports = app => {
|
||||
const { BIGINT, STRING, TINYINT, INTEGER, DATE } = app.Sequelize;
|
||||
|
||||
const ProductUnit = app.model.define('product_unit', {
|
||||
id: { type: BIGINT.UNSIGNED, primaryKey: true, autoIncrement: true, comment: '主键' },
|
||||
principal_id: { type: BIGINT.UNSIGNED, allowNull: false, comment: '所属主体ID' },
|
||||
name: { type: STRING(100), allowNull: false, comment: '单位名称' },
|
||||
code: { type: STRING(50), comment: '单位编码' },
|
||||
status: { type: TINYINT, allowNull: false, defaultValue: 1, comment: '状态:1启用 0禁用' },
|
||||
sort: { type: INTEGER, allowNull: false, defaultValue: 0, comment: '排序(下拉展示顺序,越小越靠前)' },
|
||||
remark: { type: STRING(255), comment: '备注' },
|
||||
is_system: { type: TINYINT, allowNull: false, defaultValue: 0, comment: '是否系统默认:1系统数据(不可改/删,全部主体可见) 0自定义(按主体隔离)' },
|
||||
created_at: { type: DATE, comment: '创建时间' },
|
||||
updated_at: { type: DATE, comment: '更新时间' },
|
||||
}, {
|
||||
tableName: 'product_units',
|
||||
comment: '商品单位',
|
||||
});
|
||||
|
||||
ProductUnit.associate = () => {
|
||||
app.model.ProductUnit.belongsTo(app.model.Principal, { foreignKey: 'principal_id', as: 'principal' });
|
||||
};
|
||||
|
||||
return ProductUnit;
|
||||
};
|
||||
@ -0,0 +1,45 @@
|
||||
'use strict';
|
||||
|
||||
/**
|
||||
* 商品分类字典表:product_categories
|
||||
* 定位:按主体隔离(principal_id),供商品选择「分类」时落 category_id + category_name。
|
||||
* 同一主体下分类名称唯一(uk_product_categories_principal_name),名称即展示文本。
|
||||
*/
|
||||
module.exports = {
|
||||
async up(queryInterface, Sequelize) {
|
||||
await queryInterface.createTable(
|
||||
'product_categories',
|
||||
{
|
||||
id: { type: Sequelize.BIGINT.UNSIGNED, allowNull: false, autoIncrement: true, primaryKey: true },
|
||||
principal_id: { type: Sequelize.BIGINT.UNSIGNED, allowNull: false, comment: '所属主体ID' },
|
||||
name: { type: Sequelize.STRING(100), allowNull: false, comment: '分类名称' },
|
||||
code: { type: Sequelize.STRING(50), allowNull: true, comment: '分类编码' },
|
||||
status: { type: Sequelize.TINYINT, allowNull: false, defaultValue: 1, comment: '状态:1启用 0禁用' },
|
||||
sort: { type: Sequelize.INTEGER, allowNull: false, defaultValue: 0, comment: '排序(下拉展示顺序,越小越靠前)' },
|
||||
remark: { type: Sequelize.STRING(255), allowNull: true, comment: '备注' },
|
||||
created_at: {
|
||||
type: Sequelize.DATE,
|
||||
allowNull: false,
|
||||
defaultValue: Sequelize.literal('CURRENT_TIMESTAMP'),
|
||||
},
|
||||
updated_at: {
|
||||
type: Sequelize.DATE,
|
||||
allowNull: false,
|
||||
defaultValue: Sequelize.literal('CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP'),
|
||||
},
|
||||
},
|
||||
{ charset: 'utf8mb4', collate: 'utf8mb4_unicode_ci', comment: '商品分类' }
|
||||
);
|
||||
|
||||
await queryInterface.addIndex('product_categories', {
|
||||
fields: [ 'principal_id', 'name' ],
|
||||
unique: true,
|
||||
name: 'uk_product_categories_principal_name',
|
||||
});
|
||||
await queryInterface.addIndex('product_categories', { fields: [ 'principal_id', 'status', 'sort' ], name: 'idx_product_categories_p_s_sort' });
|
||||
},
|
||||
|
||||
async down(queryInterface) {
|
||||
await queryInterface.dropTable('product_categories');
|
||||
},
|
||||
};
|
||||
@ -0,0 +1,45 @@
|
||||
'use strict';
|
||||
|
||||
/**
|
||||
* 商品品牌字典表:product_brands
|
||||
* 定位:按主体隔离(principal_id),供商品选择「品牌」时落 brand_id + brand_name。
|
||||
* 同一主体下品牌名称唯一(uk_product_brands_principal_name),名称即展示文本。
|
||||
*/
|
||||
module.exports = {
|
||||
async up(queryInterface, Sequelize) {
|
||||
await queryInterface.createTable(
|
||||
'product_brands',
|
||||
{
|
||||
id: { type: Sequelize.BIGINT.UNSIGNED, allowNull: false, autoIncrement: true, primaryKey: true },
|
||||
principal_id: { type: Sequelize.BIGINT.UNSIGNED, allowNull: false, comment: '所属主体ID' },
|
||||
name: { type: Sequelize.STRING(100), allowNull: false, comment: '品牌名称' },
|
||||
code: { type: Sequelize.STRING(50), allowNull: true, comment: '品牌编码' },
|
||||
status: { type: Sequelize.TINYINT, allowNull: false, defaultValue: 1, comment: '状态:1启用 0禁用' },
|
||||
sort: { type: Sequelize.INTEGER, allowNull: false, defaultValue: 0, comment: '排序(下拉展示顺序,越小越靠前)' },
|
||||
remark: { type: Sequelize.STRING(255), allowNull: true, comment: '备注' },
|
||||
created_at: {
|
||||
type: Sequelize.DATE,
|
||||
allowNull: false,
|
||||
defaultValue: Sequelize.literal('CURRENT_TIMESTAMP'),
|
||||
},
|
||||
updated_at: {
|
||||
type: Sequelize.DATE,
|
||||
allowNull: false,
|
||||
defaultValue: Sequelize.literal('CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP'),
|
||||
},
|
||||
},
|
||||
{ charset: 'utf8mb4', collate: 'utf8mb4_unicode_ci', comment: '商品品牌' }
|
||||
);
|
||||
|
||||
await queryInterface.addIndex('product_brands', {
|
||||
fields: [ 'principal_id', 'name' ],
|
||||
unique: true,
|
||||
name: 'uk_product_brands_principal_name',
|
||||
});
|
||||
await queryInterface.addIndex('product_brands', { fields: [ 'principal_id', 'status', 'sort' ], name: 'idx_product_brands_p_s_sort' });
|
||||
},
|
||||
|
||||
async down(queryInterface) {
|
||||
await queryInterface.dropTable('product_brands');
|
||||
},
|
||||
};
|
||||
@ -0,0 +1,45 @@
|
||||
'use strict';
|
||||
|
||||
/**
|
||||
* 商品单位字典表:product_units
|
||||
* 定位:按主体隔离(principal_id),供商品选择「单位」时落 unit_id + stock_unit(单位文本)。
|
||||
* 同一主体下单位名称唯一(uk_product_units_principal_name),名称即展示文本。
|
||||
*/
|
||||
module.exports = {
|
||||
async up(queryInterface, Sequelize) {
|
||||
await queryInterface.createTable(
|
||||
'product_units',
|
||||
{
|
||||
id: { type: Sequelize.BIGINT.UNSIGNED, allowNull: false, autoIncrement: true, primaryKey: true },
|
||||
principal_id: { type: Sequelize.BIGINT.UNSIGNED, allowNull: false, comment: '所属主体ID' },
|
||||
name: { type: Sequelize.STRING(100), allowNull: false, comment: '单位名称' },
|
||||
code: { type: Sequelize.STRING(50), allowNull: true, comment: '单位编码' },
|
||||
status: { type: Sequelize.TINYINT, allowNull: false, defaultValue: 1, comment: '状态:1启用 0禁用' },
|
||||
sort: { type: Sequelize.INTEGER, allowNull: false, defaultValue: 0, comment: '排序(下拉展示顺序,越小越靠前)' },
|
||||
remark: { type: Sequelize.STRING(255), allowNull: true, comment: '备注' },
|
||||
created_at: {
|
||||
type: Sequelize.DATE,
|
||||
allowNull: false,
|
||||
defaultValue: Sequelize.literal('CURRENT_TIMESTAMP'),
|
||||
},
|
||||
updated_at: {
|
||||
type: Sequelize.DATE,
|
||||
allowNull: false,
|
||||
defaultValue: Sequelize.literal('CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP'),
|
||||
},
|
||||
},
|
||||
{ charset: 'utf8mb4', collate: 'utf8mb4_unicode_ci', comment: '商品单位' }
|
||||
);
|
||||
|
||||
await queryInterface.addIndex('product_units', {
|
||||
fields: [ 'principal_id', 'name' ],
|
||||
unique: true,
|
||||
name: 'uk_product_units_principal_name',
|
||||
});
|
||||
await queryInterface.addIndex('product_units', { fields: [ 'principal_id', 'status', 'sort' ], name: 'idx_product_units_p_s_sort' });
|
||||
},
|
||||
|
||||
async down(queryInterface) {
|
||||
await queryInterface.dropTable('product_units');
|
||||
},
|
||||
};
|
||||
@ -0,0 +1,28 @@
|
||||
'use strict';
|
||||
|
||||
/**
|
||||
* 商品表新增 brand_id / unit_id:分别关联 product_brands / product_units,
|
||||
* 与已有的 category_id 一同用于存放属性的 id;属性文本仍用既有字段
|
||||
* (brand_name / stock_unit)冗余存储,便于列表直接展示、兼容外部系统。
|
||||
*/
|
||||
module.exports = {
|
||||
async up(queryInterface, Sequelize) {
|
||||
await queryInterface.addColumn('products', 'brand_id', {
|
||||
type: Sequelize.INTEGER,
|
||||
allowNull: true,
|
||||
comment: '品牌ID(关联 product_brands)',
|
||||
});
|
||||
await queryInterface.addColumn('products', 'unit_id', {
|
||||
type: Sequelize.INTEGER,
|
||||
allowNull: true,
|
||||
comment: '单位ID(关联 product_units)',
|
||||
});
|
||||
await queryInterface.addIndex('products', { fields: [ 'brand_id' ], name: 'idx_products_brand_id' });
|
||||
await queryInterface.addIndex('products', { fields: [ 'unit_id' ], name: 'idx_products_unit_id' });
|
||||
},
|
||||
|
||||
async down(queryInterface) {
|
||||
await queryInterface.removeColumn('products', 'brand_id');
|
||||
await queryInterface.removeColumn('products', 'unit_id');
|
||||
},
|
||||
};
|
||||
@ -0,0 +1,45 @@
|
||||
'use strict';
|
||||
|
||||
/**
|
||||
* 新增「商品属性」菜单,挂在「商品管理」下,指向真实页面 views/ProductAttribute/Index.vue。
|
||||
* 该页面左侧以 tab 切换「分类 / 品牌 / 单位」三类属性,各自独立增删查改。
|
||||
* 幂等:按 name 定位,已存在则跳过。
|
||||
*/
|
||||
module.exports = {
|
||||
async up(queryInterface) {
|
||||
const sequelize = queryInterface.sequelize;
|
||||
|
||||
const [ goods ] = await sequelize.query(
|
||||
"SELECT id FROM menus WHERE name = '商品管理' AND parent_id = 0 LIMIT 1",
|
||||
{ type: sequelize.QueryTypes.SELECT }
|
||||
);
|
||||
if (!goods) return; // 商品管理节点不存在则跳过
|
||||
const goodsId = Number(goods.id);
|
||||
|
||||
const [ exist ] = await sequelize.query(
|
||||
"SELECT id FROM menus WHERE name = '商品属性' LIMIT 1",
|
||||
{ type: sequelize.QueryTypes.SELECT }
|
||||
);
|
||||
if (exist) return; // 已存在,跳过
|
||||
|
||||
await sequelize.query(
|
||||
`INSERT INTO menus
|
||||
(parent_id, name, type, path, icon, permission, component, sort, status, created_at, updated_at)
|
||||
VALUES (?, '商品属性', 2, '/product-attribute', 'Collection', 'productAttribute:list', 'views/ProductAttribute/Index.vue', 90, 1, NOW(), NOW())`,
|
||||
{ replacements: [ goodsId ] }
|
||||
);
|
||||
},
|
||||
|
||||
async down(queryInterface) {
|
||||
const sequelize = queryInterface.sequelize;
|
||||
const [ goods ] = await sequelize.query(
|
||||
"SELECT id FROM menus WHERE name = '商品管理' AND parent_id = 0 LIMIT 1",
|
||||
{ type: sequelize.QueryTypes.SELECT }
|
||||
);
|
||||
if (!goods) return;
|
||||
await sequelize.query(
|
||||
"DELETE FROM menus WHERE name = '商品属性' AND parent_id = ?",
|
||||
{ replacements: [ Number(goods.id) ] }
|
||||
);
|
||||
},
|
||||
};
|
||||
@ -0,0 +1,21 @@
|
||||
'use strict';
|
||||
|
||||
/**
|
||||
* 商品分类表增加 is_system 字段(是否系统默认)。
|
||||
* 系统数据:全部主体可见、不可改/删;自定义数据:按主体隔离(principal_id)。
|
||||
* 说明:分类不做预设系统数据;若后续需要系统级分类,须通过迁移脚本写入。
|
||||
*/
|
||||
module.exports = {
|
||||
async up(queryInterface, Sequelize) {
|
||||
await queryInterface.addColumn('product_categories', 'is_system', {
|
||||
type: Sequelize.TINYINT,
|
||||
allowNull: false,
|
||||
defaultValue: 0,
|
||||
comment: '是否系统默认:1系统数据(不可改/删,全部主体可见) 0自定义(按主体隔离)',
|
||||
});
|
||||
},
|
||||
|
||||
async down(queryInterface) {
|
||||
await queryInterface.removeColumn('product_categories', 'is_system');
|
||||
},
|
||||
};
|
||||
@ -0,0 +1,21 @@
|
||||
'use strict';
|
||||
|
||||
/**
|
||||
* 商品品牌表增加 is_system 字段(是否系统默认)。
|
||||
* 系统数据:全部主体可见、不可改/删;自定义数据:按主体隔离(principal_id)。
|
||||
* 说明:品牌不做预设系统数据;若后续需要系统级品牌,须通过迁移脚本写入。
|
||||
*/
|
||||
module.exports = {
|
||||
async up(queryInterface, Sequelize) {
|
||||
await queryInterface.addColumn('product_brands', 'is_system', {
|
||||
type: Sequelize.TINYINT,
|
||||
allowNull: false,
|
||||
defaultValue: 0,
|
||||
comment: '是否系统默认:1系统数据(不可改/删,全部主体可见) 0自定义(按主体隔离)',
|
||||
});
|
||||
},
|
||||
|
||||
async down(queryInterface) {
|
||||
await queryInterface.removeColumn('product_brands', 'is_system');
|
||||
},
|
||||
};
|
||||
@ -0,0 +1,40 @@
|
||||
'use strict';
|
||||
|
||||
/**
|
||||
* 商品单位表增加 is_system 字段(是否系统默认)。
|
||||
* 系统数据:全部主体可见、不可改/删;自定义数据:按主体隔离(principal_id)。
|
||||
* 同时种子通用系统默认单位(principal_id=0 作为系统占位主体)。
|
||||
*/
|
||||
module.exports = {
|
||||
async up(queryInterface, Sequelize) {
|
||||
await queryInterface.addColumn('product_units', 'is_system', {
|
||||
type: Sequelize.TINYINT,
|
||||
allowNull: false,
|
||||
defaultValue: 0,
|
||||
comment: '是否系统默认:1系统数据(不可改/删,全部主体可见) 0自定义(按主体隔离)',
|
||||
});
|
||||
|
||||
const now = new Date();
|
||||
const systems = ['件', '箱', '千克', '克', '台', '个', '套', '包', '卷', '组', '条', '双', '米', '升', '瓶', '袋'];
|
||||
await queryInterface.bulkInsert(
|
||||
'product_units',
|
||||
systems.map((name, i) => ({
|
||||
principal_id: 0,
|
||||
name,
|
||||
code: null,
|
||||
status: 1,
|
||||
sort: 100 + i,
|
||||
remark: '系统默认单位',
|
||||
is_system: 1,
|
||||
created_at: now,
|
||||
updated_at: now,
|
||||
})),
|
||||
{}
|
||||
);
|
||||
},
|
||||
|
||||
async down(queryInterface) {
|
||||
await queryInterface.sequelize.query('DELETE FROM product_units WHERE is_system = 1');
|
||||
await queryInterface.removeColumn('product_units', 'is_system');
|
||||
},
|
||||
};
|
||||
Loading…
x
Reference in New Issue
Block a user