oms/backend/app/controller/principal.js

145 lines
4.2 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 PrincipalController 主体管理
*/
class PrincipalController extends Controller {
/**
* @summary 主体列表
* @description 分页查询主体,支持名称/编码模糊搜索、类型与状态过滤
* @router get /api/principals
* @apikey
* @request query integer page 页码,默认 1
* @request query integer pageSize 每页条数,默认 20
* @request query string keyword 关键词,匹配主体编码或名称
* @request query integer type 主体类型:1自营 2加盟 3外部,不传则不过滤
* @request query integer status 状态:1启用 0禁用,不传则不过滤
* @response 200 principalListResponse 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, type, status } = ctx.query;
const where = {};
if (keyword) {
where[Op.or] = [
{ code: { [Op.like]: `%${keyword}%` } },
{ name: { [Op.like]: `%${keyword}%` } },
];
}
if (type !== undefined && type !== '') where.type = Number(type);
if (status !== undefined && status !== '') where.status = Number(status);
const { count, rows } = await ctx.model.Principal.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/principals/{id}
* @apikey
* @request path integer *id 主体主键ID
* @response 200 baseResponse success
*/
async show() {
const { ctx } = this;
const principal = await ctx.model.Principal.findByPk(ctx.params.id);
if (!principal) {
ctx.status = 404;
ctx.body = { code: 404, message: '主体不存在' };
return;
}
ctx.body = { code: 0, message: 'success', data: principal };
}
/**
* @summary 新增主体
* @description 主体编码 code 全局唯一
* @router post /api/principals
* @apikey
* @request body createPrincipalRequest *body
* @response 200 baseResponse success
*/
async create() {
const { ctx } = this;
const { code, name } = ctx.request.body;
if (!code || !name) {
ctx.status = 400;
ctx.body = { code: 400, message: 'code 与 name 为必填项' };
return;
}
const exist = await ctx.model.Principal.findOne({ where: { code } });
if (exist) {
ctx.status = 400;
ctx.body = { code: 400, message: '主体编码已存在' };
return;
}
const principal = await ctx.model.Principal.create(ctx.request.body);
ctx.body = { code: 0, message: 'success', data: { id: principal.id } };
}
/**
* @summary 更新主体
* @description 未传入的字段保持原值
* @router put /api/principals/{id}
* @apikey
* @request path integer *id 主体主键ID
* @request body updatePrincipalRequest *body
* @response 200 baseResponse success
*/
async update() {
const { ctx } = this;
const principal = await ctx.model.Principal.findByPk(ctx.params.id);
if (!principal) {
ctx.status = 404;
ctx.body = { code: 404, message: '主体不存在' };
return;
}
await principal.update(ctx.request.body);
ctx.body = { code: 0, message: 'success', data: { id: principal.id } };
}
/**
* @summary 删除主体
* @description 物理删除主体;存在关联商品/订单时需先清理关联数据
* @router delete /api/principals/{id}
* @apikey
* @request path integer *id 主体主键ID
* @response 200 baseResponse success
*/
async destroy() {
const { ctx } = this;
const principal = await ctx.model.Principal.findByPk(ctx.params.id);
if (!principal) {
ctx.status = 404;
ctx.body = { code: 404, message: '主体不存在' };
return;
}
await principal.destroy();
ctx.body = { code: 0, message: 'success', data: { id: Number(ctx.params.id) } };
}
}
module.exports = PrincipalController;