- 商品/商品图片、店铺、店铺商品、组合商品及明细 - 仓库、仓库商品(WMS itemId 映射)、仓库接口配置与字段定义(warehouseApi/warehouseApiField) - 库存:stock_items、product_stocks(ZC/CC 双维度)与库存重算工具(utils/recalcStock.js) - 库存单据与对接日志:stock orders、api_outbound_logs - 相关迁移:shops/shop_products/stock_orders/warehouses/warehouse_apis/warehouse_products/product_stocks/files-images 等表与字段
248 lines
8.0 KiB
JavaScript
248 lines
8.0 KiB
JavaScript
'use strict';
|
||
|
||
const { Controller } = require('egg');
|
||
const { parsePagination, buildPageResult } = require('../lib/pagination');
|
||
|
||
/** 明细里冗余返回的系统商品可选字段,便于前端直接展示 */
|
||
const PRODUCT_ATTRS = [ 'id', 'item_code', 'item_name', 'sku_property', 'stock_unit', 'retail_price' ];
|
||
|
||
/**
|
||
* 归一化组合明细:过滤非法行、同一商品去重合并为一条(数量取整且大于 0)。
|
||
* @param {Array} items 原始明细 [{ product_id, quantity }]
|
||
* @return {Array} 归一化后的明细
|
||
*/
|
||
function normalizeItems(items) {
|
||
if (!Array.isArray(items)) return [];
|
||
const map = new Map();
|
||
for (const it of items) {
|
||
const productId = Number(it && it.product_id);
|
||
const quantity = Number(it && it.quantity);
|
||
if (!productId || !quantity || quantity <= 0) continue;
|
||
map.set(productId, Math.floor(quantity));
|
||
}
|
||
return Array.from(map, ([ product_id, quantity ]) => ({ product_id, quantity }));
|
||
}
|
||
|
||
/**
|
||
* 用给定明细覆盖组合商品的全部明细关系(先清空再写入)。
|
||
* @param {Object} ctx egg context
|
||
* @param {number} comboProductId 组合商品ID
|
||
* @param {number} principalId 所属主体ID
|
||
* @param {Array} itemList 归一化后的明细
|
||
* @param {Object} transaction 事务
|
||
*/
|
||
async function replaceComboItems(ctx, comboProductId, principalId, itemList, transaction) {
|
||
await ctx.model.ComboProductItem.destroy({ where: { combo_product_id: comboProductId }, transaction });
|
||
if (!itemList.length) return;
|
||
await ctx.model.ComboProductItem.bulkCreate(
|
||
itemList.map(it => ({
|
||
principal_id: principalId,
|
||
combo_product_id: comboProductId,
|
||
product_id: it.product_id,
|
||
quantity: it.quantity,
|
||
})),
|
||
{ transaction }
|
||
);
|
||
}
|
||
|
||
/**
|
||
* 组合商品详情/列表统一的明细关联(含系统商品信息)
|
||
* @param {Object} ctx egg context
|
||
* @return {Object} sequelize include 配置
|
||
*/
|
||
function itemsInclude(ctx) {
|
||
return {
|
||
model: ctx.model.ComboProductItem,
|
||
as: 'items',
|
||
include: [{ model: ctx.model.Product, as: 'product', attributes: PRODUCT_ATTRS }],
|
||
};
|
||
}
|
||
|
||
/**
|
||
* @controller ComboProductController 组合商品管理
|
||
*/
|
||
class ComboProductController extends Controller {
|
||
/**
|
||
* @summary 组合商品列表
|
||
* @description 分页查询组合商品,支持按主体、关键词与状态过滤;items 为组合明细关系
|
||
* @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, pageSize, offset, limit } = parsePagination(ctx.query);
|
||
const { keyword, status } = ctx.query;
|
||
const principalId = ctx.state.principalId;
|
||
|
||
const 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,
|
||
limit,
|
||
order: [[ 'id', 'DESC' ]],
|
||
distinct: true,
|
||
include: [ itemsInclude(ctx) ],
|
||
});
|
||
|
||
ctx.body = { code: 0, message: 'success', data: buildPageResult(rows, count, { page, pageSize }) };
|
||
}
|
||
|
||
/**
|
||
* @summary 组合商品详情
|
||
* @description 按主键查询组合商品,items 为组合明细关系(系统商品ID + 数量)
|
||
* @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, {
|
||
include: [ itemsInclude(ctx) ],
|
||
});
|
||
|
||
if (!combo) {
|
||
ctx.status = 404;
|
||
ctx.body = { code: 404, message: '组合商品不存在' };
|
||
return;
|
||
}
|
||
|
||
ctx.body = { code: 0, message: 'success', data: combo };
|
||
}
|
||
|
||
/**
|
||
* @summary 新增组合商品
|
||
* @description 同一主体下组合商品编码 code 唯一;items 为组合明细 [{ product_id, quantity }],写入关系表
|
||
* @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, items } = 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 itemList = normalizeItems(items);
|
||
const transaction = await ctx.model.transaction();
|
||
try {
|
||
const combo = await ctx.model.ComboProduct.create(
|
||
{
|
||
principal_id: principalId,
|
||
code,
|
||
name,
|
||
price,
|
||
status: ctx.request.body.status,
|
||
remark: ctx.request.body.remark,
|
||
},
|
||
{ transaction }
|
||
);
|
||
await replaceComboItems(ctx, combo.id, principalId, itemList, transaction);
|
||
await transaction.commit();
|
||
ctx.body = { code: 0, message: 'success', data: { id: combo.id } };
|
||
} catch (err) {
|
||
await transaction.rollback();
|
||
throw err;
|
||
}
|
||
}
|
||
|
||
/**
|
||
* @summary 更新组合商品
|
||
* @description 未传入的字段保持原值;传入 items 时以之覆盖关系表中的组合明细
|
||
* @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;
|
||
}
|
||
|
||
const body = { ...ctx.request.body };
|
||
// 主体归属不可通过更新篡改,防止把资源移动到其它主体
|
||
delete body.principal_id;
|
||
const hasItems = Object.prototype.hasOwnProperty.call(body, 'items');
|
||
const itemList = normalizeItems(body.items);
|
||
delete body.items;
|
||
|
||
const transaction = await ctx.model.transaction();
|
||
try {
|
||
await combo.update(body, { transaction });
|
||
if (hasItems) {
|
||
await replaceComboItems(ctx, combo.id, combo.principal_id, itemList, transaction);
|
||
}
|
||
await transaction.commit();
|
||
ctx.body = { code: 0, message: 'success', data: { id: combo.id } };
|
||
} catch (err) {
|
||
await transaction.rollback();
|
||
throw err;
|
||
}
|
||
}
|
||
|
||
/**
|
||
* @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;
|
||
}
|
||
|
||
const transaction = await ctx.model.transaction();
|
||
try {
|
||
await ctx.model.ComboProductItem.destroy({ where: { combo_product_id: combo.id }, transaction });
|
||
await combo.destroy({ transaction });
|
||
await transaction.commit();
|
||
ctx.body = { code: 0, message: 'success', data: { id: Number(ctx.params.id) } };
|
||
} catch (err) {
|
||
await transaction.rollback();
|
||
throw err;
|
||
}
|
||
}
|
||
}
|
||
|
||
module.exports = ComboProductController;
|