oms/backend/app/controller/comboProduct.js

147 lines
4.5 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

'use strict';
const { Controller } = require('egg');
/**
* @controller ComboProductController 组合商品管理
*/
class ComboProductController extends Controller {
/**
* @summary 组合商品列表
* @description 分页查询组合商品,支持按主体、关键词与状态过滤
* @router get /api/combo-products
* @apikey
* @request query integer page 页码,默认 1
* @request query integer pageSize 每页条数,默认 20
* @request query integer principal_id 所属主体ID(必填,需当前账号已绑定该主体)
* @request query string keyword 关键词,匹配组合商品编码或名称
* @request query integer status 状态:1上架 0下架,不传则不过滤
* @response 200 comboProductListResponse 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, 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 (status !== undefined && status !== '') where.status = Number(status);
const { count, rows } = await ctx.model.ComboProduct.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 按主键查询组合商品,items 为组合明细 JSON
* @router get /api/combo-products/{id}
* @apikey
* @request path integer *id 组合商品主键ID
* @response 200 baseResponse success
*/
async show() {
const { ctx } = this;
const combo = await ctx.model.ComboProduct.findByPk(ctx.params.id);
if (!combo) {
ctx.status = 404;
ctx.body = { code: 404, message: '组合商品不存在' };
return;
}
ctx.body = { code: 0, message: 'success', data: combo };
}
/**
* @summary 新增组合商品
* @description 同一主体下组合商品编码 code 唯一;items 为组合明细
* @router post /api/combo-products
* @apikey
* @request body createComboProductRequest *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.ComboProduct.findOne({ where: { principal_id: principalId, code } });
if (exist) {
ctx.status = 400;
ctx.body = { code: 400, message: '该主体下组合商品编码已存在' };
return;
}
const combo = await ctx.model.ComboProduct.create(ctx.request.body);
ctx.body = { code: 0, message: 'success', data: { id: combo.id } };
}
/**
* @summary 更新组合商品
* @description 未传入的字段保持原值;items 传 JSON 数组覆盖原明细
* @router put /api/combo-products/{id}
* @apikey
* @request path integer *id 组合商品主键ID
* @request body updateComboProductRequest *body
* @response 200 baseResponse success
*/
async update() {
const { ctx } = this;
const combo = await ctx.model.ComboProduct.findByPk(ctx.params.id);
if (!combo) {
ctx.status = 404;
ctx.body = { code: 404, message: '组合商品不存在' };
return;
}
delete ctx.request.body.principal_id;
await combo.update(ctx.request.body);
ctx.body = { code: 0, message: 'success', data: { id: combo.id } };
}
/**
* @summary 删除组合商品
* @description 物理删除组合商品
* @router delete /api/combo-products/{id}
* @apikey
* @request path integer *id 组合商品主键ID
* @response 200 baseResponse success
*/
async destroy() {
const { ctx } = this;
const combo = await ctx.model.ComboProduct.findByPk(ctx.params.id);
if (!combo) {
ctx.status = 404;
ctx.body = { code: 404, message: '组合商品不存在' };
return;
}
await combo.destroy();
ctx.body = { code: 0, message: 'success', data: { id: Number(ctx.params.id) } };
}
}
module.exports = ComboProductController;