150 lines
4.5 KiB
JavaScript
150 lines
4.5 KiB
JavaScript
'use strict';
|
||
|
||
const { Controller } = require('egg');
|
||
|
||
/**
|
||
* @controller ProductController 商品管理
|
||
*/
|
||
class ProductController extends Controller {
|
||
/**
|
||
* @summary 商品列表
|
||
* @description 分页查询商品,支持按主体、关键词与状态过滤
|
||
* @router get /api/products
|
||
* @apikey
|
||
* @request query integer page 页码,默认 1
|
||
* @request query integer pageSize 每页条数,默认 20
|
||
* @request query integer principal_id 所属主体ID(必填,需当前账号已绑定该主体)
|
||
* @request query string keyword 关键词,匹配商品编码或名称
|
||
* @request query string category 分类,精确匹配
|
||
* @request query integer status 状态:1上架 0下架,不传则不过滤
|
||
* @response 200 productListResponse success
|
||
*/
|
||
async index() {
|
||
const { ctx } = this;
|
||
const Op = this.app.Sequelize.Op;
|
||
const page = Number(ctx.query.page || 1);
|
||
const pageSize = Number(ctx.query.pageSize || 20);
|
||
const { keyword, category, status } = ctx.query;
|
||
const principalId = ctx.state.principalId;
|
||
|
||
const where = {};
|
||
where.principal_id = principalId;
|
||
if (keyword) {
|
||
where[Op.or] = [
|
||
{ code: { [Op.like]: `%${keyword}%` } },
|
||
{ name: { [Op.like]: `%${keyword}%` } },
|
||
];
|
||
}
|
||
if (category) where.category = category;
|
||
if (status !== undefined && status !== '') where.status = Number(status);
|
||
|
||
const { count, rows } = await ctx.model.Product.findAndCountAll({
|
||
where,
|
||
offset: (page - 1) * pageSize,
|
||
limit: pageSize,
|
||
order: [[ 'id', 'DESC' ]],
|
||
});
|
||
|
||
ctx.body = { code: 0, message: 'success', data: { list: rows, total: count, page, pageSize } };
|
||
}
|
||
|
||
/**
|
||
* @summary 商品详情
|
||
* @description 按主键查询商品
|
||
* @router get /api/products/{id}
|
||
* @apikey
|
||
* @request path integer *id 商品主键ID
|
||
* @response 200 baseResponse success
|
||
*/
|
||
async show() {
|
||
const { ctx } = this;
|
||
const product = await ctx.model.Product.findByPk(ctx.params.id);
|
||
|
||
if (!product) {
|
||
ctx.status = 404;
|
||
ctx.body = { code: 404, message: '商品不存在' };
|
||
return;
|
||
}
|
||
|
||
ctx.body = { code: 0, message: 'success', data: product };
|
||
}
|
||
|
||
/**
|
||
* @summary 新增商品
|
||
* @description 同一主体下商品编码 code 唯一
|
||
* @router post /api/products
|
||
* @apikey
|
||
* @request body createProductRequest *body
|
||
* @response 200 baseResponse success
|
||
*/
|
||
async create() {
|
||
const { ctx } = this;
|
||
const { principal_id: principalId, code, name, price } = ctx.request.body;
|
||
|
||
if (!principalId || !code || !name || price === undefined) {
|
||
ctx.status = 400;
|
||
ctx.body = { code: 400, message: 'principal_id、code、name、price 为必填项' };
|
||
return;
|
||
}
|
||
|
||
const exist = await ctx.model.Product.findOne({ where: { principal_id: principalId, code } });
|
||
if (exist) {
|
||
ctx.status = 400;
|
||
ctx.body = { code: 400, message: '该主体下商品编码已存在' };
|
||
return;
|
||
}
|
||
|
||
const product = await ctx.model.Product.create(ctx.request.body);
|
||
ctx.body = { code: 0, message: 'success', data: { id: product.id } };
|
||
}
|
||
|
||
/**
|
||
* @summary 更新商品
|
||
* @description 未传入的字段保持原值
|
||
* @router put /api/products/{id}
|
||
* @apikey
|
||
* @request path integer *id 商品主键ID
|
||
* @request body updateProductRequest *body
|
||
* @response 200 baseResponse success
|
||
*/
|
||
async update() {
|
||
const { ctx } = this;
|
||
const product = await ctx.model.Product.findByPk(ctx.params.id);
|
||
|
||
if (!product) {
|
||
ctx.status = 404;
|
||
ctx.body = { code: 404, message: '商品不存在' };
|
||
return;
|
||
}
|
||
|
||
// 主体归属不可通过更新篡改,防止把资源移动到其它主体
|
||
delete ctx.request.body.principal_id;
|
||
await product.update(ctx.request.body);
|
||
ctx.body = { code: 0, message: 'success', data: { id: product.id } };
|
||
}
|
||
|
||
/**
|
||
* @summary 删除商品
|
||
* @description 物理删除商品;被组合商品引用时需先解除引用
|
||
* @router delete /api/products/{id}
|
||
* @apikey
|
||
* @request path integer *id 商品主键ID
|
||
* @response 200 baseResponse success
|
||
*/
|
||
async destroy() {
|
||
const { ctx } = this;
|
||
const product = await ctx.model.Product.findByPk(ctx.params.id);
|
||
|
||
if (!product) {
|
||
ctx.status = 404;
|
||
ctx.body = { code: 404, message: '商品不存在' };
|
||
return;
|
||
}
|
||
|
||
await product.destroy();
|
||
ctx.body = { code: 0, message: 'success', data: { id: Number(ctx.params.id) } };
|
||
}
|
||
}
|
||
|
||
module.exports = ProductController;
|