oms/backend/app/controller/shopProduct.js
liufumingren cbd16752a5 feat(backend): 商品、店铺、组合商品、仓库与库存模块
- 商品/商品图片、店铺、店铺商品、组合商品及明细
- 仓库、仓库商品(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 等表与字段
2026-09-13 02:23:39 +08:00

241 lines
8.4 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');
const { parsePagination, buildPageResult } = require('../lib/pagination');
/**
* @controller ShopProductController 店铺商品管理
* 采集自各平台店铺的商品,可单向绑定系统商品(products)或组合商品(combo_products)
*/
class ShopProductController extends Controller {
/**
* @summary 店铺商品列表
* @description 分页查询店铺商品,支持按店铺、绑定状态、关键词(标题/商家编码/平台商品ID)过滤
* @router get /api/shop-products
* @apikey
* @request query integer page 页码,默认 1
* @request query integer pageSize 每页条数,默认 20
* @request query integer principal_id 所属主体ID(必填,需当前账号已绑定该主体)
* @request query integer shop_id 店铺ID,不传则不过滤
* @request query integer bind_type 绑定类型:0未绑定 1系统商品 2组合商品
* @request query integer status 状态:1在售 0下架
* @request query string keyword 关键词,匹配标题、商家编码或平台商品ID
* @response 200 shopProductListResponse success
*/
async index() {
const { ctx } = this;
const Op = this.app.Sequelize.Op;
const { page, pageSize, offset, limit } = parsePagination(ctx.query);
const { shop_id: shopId, bind_type: bindType, status, keyword } = ctx.query;
const where = {};
where.principal_id = ctx.state.principalId;
if (shopId) where.shop_id = Number(shopId);
if (bindType !== undefined && bindType !== '') where.bind_type = Number(bindType);
if (status !== undefined && status !== '') where.status = Number(status);
if (keyword) {
where[Op.or] = [
{ title: { [Op.like]: `%${keyword}%` } },
{ outer_code: { [Op.like]: `%${keyword}%` } },
{ outer_item_id: { [Op.like]: `%${keyword}%` } },
];
}
const { count, rows } = await ctx.model.ShopProduct.findAndCountAll({
where,
offset,
limit,
order: [[ 'id', 'DESC' ]],
include: [
{ model: ctx.model.Shop, as: 'shop', attributes: [ 'id', 'name', 'platform' ] },
{ model: ctx.model.Product, as: 'product', attributes: [ 'id', 'item_code', 'item_name' ] },
{ model: ctx.model.ComboProduct, as: 'comboProduct', attributes: [ 'id', 'code', 'name' ] },
],
});
ctx.body = { code: 0, message: 'success', data: buildPageResult(rows, count, { page, pageSize }) };
}
/**
* @summary 店铺商品详情
* @description 按主键查询,带出店铺、绑定的系统商品与组合商品
* @router get /api/shop-products/{id}
* @apikey
* @request path integer *id 主键ID
* @response 200 baseResponse success
*/
async show() {
const { ctx } = this;
const item = await ctx.model.ShopProduct.findByPk(ctx.params.id, {
include: [
{ model: ctx.model.Shop, as: 'shop', attributes: [ 'id', 'name', 'platform' ] },
{ model: ctx.model.Product, as: 'product', attributes: [ 'id', 'item_code', 'item_name' ] },
{ model: ctx.model.ComboProduct, as: 'comboProduct', attributes: [ 'id', 'code', 'name' ] },
],
});
if (!item) {
ctx.status = 404;
ctx.body = { code: 404, message: '店铺商品不存在' };
return;
}
ctx.body = { code: 0, message: 'success', data: item };
}
/**
* @summary 录入/采集店铺商品
* @description shop_id 与 outer_item_id 必填;同一店铺下平台商品ID+规格ID唯一,重复录入时更新采集信息
* @router post /api/shop-products
* @apikey
* @request body createShopProductRequest *body
* @response 200 baseResponse success
*/
async create() {
const { ctx } = this;
const { shop_id: shopId, outer_item_id: outerItemId, outer_sku_id: outerSkuId, principal_id: principalId } = ctx.request.body;
if (!shopId || !outerItemId) {
ctx.status = 400;
ctx.body = { code: 400, message: 'shop_id 与 outer_item_id 为必填项' };
return;
}
// 店铺必须存在且属于当前主体,防止跨主体挂接
const shop = await ctx.model.Shop.findOne({ where: { id: shopId, principal_id: principalId } });
if (!shop) {
ctx.status = 404;
ctx.body = { code: 404, message: '店铺不存在或不属于当前主体' };
return;
}
const exist = await ctx.model.ShopProduct.findOne({
where: { shop_id: shopId, outer_item_id: outerItemId, outer_sku_id: outerSkuId || null },
});
if (exist) {
await exist.update({ ...ctx.request.body, synced_at: new Date() });
ctx.body = { code: 0, message: 'success', data: { id: exist.id, updated: true } };
return;
}
const item = await ctx.model.ShopProduct.create({
...ctx.request.body,
synced_at: new Date(),
});
ctx.body = { code: 0, message: 'success', data: { id: item.id } };
}
/**
* @summary 更新店铺商品
* @description 可修改采集信息;绑定关系建议使用专用绑定接口
* @router put /api/shop-products/{id}
* @apikey
* @request path integer *id 主键ID
* @request body updateShopProductRequest *body
* @response 200 baseResponse success
*/
async update() {
const { ctx } = this;
const item = await ctx.model.ShopProduct.findByPk(ctx.params.id);
if (!item) {
ctx.status = 404;
ctx.body = { code: 404, message: '店铺商品不存在' };
return;
}
delete ctx.request.body.principal_id;
await item.update(ctx.request.body);
ctx.body = { code: 0, message: 'success', data: { id: item.id } };
}
/**
* @summary 绑定系统商品或组合商品
* @description 单向绑定:bind_type=1 绑系统商品(传 product_id)、2 绑组合商品(传 combo_product_id)、0 解绑;
* 绑定的商品必须属于当前主体
* @router post /api/shop-products/{id}/bind
* @apikey
* @request path integer *id 店铺商品主键ID
* @request body bindShopProductRequest *body
* @response 200 baseResponse success
*/
async bind() {
const { ctx } = this;
const { bind_type: bindType, product_id: productId, combo_product_id: comboProductId } = ctx.request.body;
const principalId = ctx.state.principalId;
const item = await ctx.model.ShopProduct.findByPk(ctx.params.id);
if (!item) {
ctx.status = 404;
ctx.body = { code: 404, message: '店铺商品不存在' };
return;
}
const type = Number(bindType);
if (![ 0, 1, 2 ].includes(type)) {
ctx.status = 400;
ctx.body = { code: 400, message: 'bind_type 只能为 0未绑定 1系统商品 2组合商品' };
return;
}
const data = { bind_type: type, product_id: null, combo_product_id: null };
if (type === 1) {
if (!productId) {
ctx.status = 400;
ctx.body = { code: 400, message: '绑定系统商品时 product_id 为必填项' };
return;
}
const product = await ctx.model.Product.findOne({ where: { id: productId, principal_id: principalId } });
if (!product) {
ctx.status = 404;
ctx.body = { code: 404, message: '系统商品不存在或不属于当前主体' };
return;
}
data.product_id = Number(productId);
}
if (type === 2) {
if (!comboProductId) {
ctx.status = 400;
ctx.body = { code: 400, message: '绑定组合商品时 combo_product_id 为必填项' };
return;
}
const combo = await ctx.model.ComboProduct.findOne({ where: { id: comboProductId, principal_id: principalId } });
if (!combo) {
ctx.status = 404;
ctx.body = { code: 404, message: '组合商品不存在或不属于当前主体' };
return;
}
data.combo_product_id = Number(comboProductId);
}
await item.update(data);
ctx.body = { code: 0, message: 'success', data: { id: item.id } };
}
/**
* @summary 删除店铺商品
* @description 物理删除采集到的店铺商品
* @router delete /api/shop-products/{id}
* @apikey
* @request path integer *id 主键ID
* @response 200 baseResponse success
*/
async destroy() {
const { ctx } = this;
const item = await ctx.model.ShopProduct.findByPk(ctx.params.id);
if (!item) {
ctx.status = 404;
ctx.body = { code: 404, message: '店铺商品不存在' };
return;
}
await item.destroy();
ctx.body = { code: 0, message: 'success', data: { id: Number(ctx.params.id) } };
}
}
module.exports = ShopProductController;