'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;