- 按主体隔离的分类/品牌/单位 CRUD 与 /select 下拉接口 - 商品模型增加 brand_id / unit_id,属性被商品引用时禁止删除(attributeUsage) - 迁移:建三张字典表、商品表加字段、种子属性菜单、is_system 保护内置项 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
220 lines
6.8 KiB
JavaScript
220 lines
6.8 KiB
JavaScript
'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;
|