From f93fce450c949a71decd961425fcaf9c2078b681 Mon Sep 17 00:00:00 2001 From: BXY <103352887+BXY0414@users.noreply.github.com> Date: Fri, 30 Oct 2026 14:51:16 +0800 Subject: [PATCH] feat: add is_distribution_product flag to product schema and related logic --- internal/repository/product_repository.go | 48 ++++++++++++------- internal/server/product_server.go | 31 +++++++----- ...1_add_products_is_distribution_product.sql | 7 +++ scripts/sql/02-product-schema.sql | 2 + scripts/sql/recreate-product-tables.sql | 1 + 5 files changed, 58 insertions(+), 31 deletions(-) create mode 100644 scripts/sql/011_add_products_is_distribution_product.sql diff --git a/internal/repository/product_repository.go b/internal/repository/product_repository.go index efaf29a..7bfc4c5 100644 --- a/internal/repository/product_repository.go +++ b/internal/repository/product_repository.go @@ -89,6 +89,7 @@ type ProductRecord struct { ImageIDs []string DetailHTML string MemberDiscountEnabled bool + IsDistributionProduct bool GoodsType int32 ServiceGuarantees []string Status productv1.ProductStatus @@ -312,6 +313,7 @@ type CategoryProductRecord struct { StockQuantity int64 LockedQuantity int64 CategoryPropertiesJSON string + IsDistributionProduct bool PresaleType int32 ProductCategories []ProductCategorySummaryRecord Groups []ProductGroupSummaryRecord @@ -716,6 +718,7 @@ CREATE INDEX IF NOT EXISTS idx_product_items_price ON product.product_items(pric CREATE INDEX IF NOT EXISTS idx_products_active_created_id ON product.products(created_at DESC, id DESC) WHERE deleted_at IS NULL; ALTER TABLE product.products ADD COLUMN IF NOT EXISTS tenant_id BIGINT; ALTER TABLE product.products ADD COLUMN IF NOT EXISTS store_id BIGINT; +ALTER TABLE product.products ADD COLUMN IF NOT EXISTS is_distribution_product BOOLEAN NOT NULL DEFAULT FALSE; ALTER TABLE product.products ADD COLUMN IF NOT EXISTS category_properties JSONB NOT NULL DEFAULT '{}'::JSONB; ALTER TABLE product.products ADD COLUMN IF NOT EXISTS presale_type SMALLINT NOT NULL DEFAULT 0; ALTER TABLE product.products ADD COLUMN IF NOT EXISTS delivery_time_type SMALLINT NOT NULL DEFAULT 0; @@ -1843,25 +1846,26 @@ SET title = $2, image_ids = $6, detail_html = $7, member_discount_enabled = $8, - goods_type = $9, - service_guarantees = $10, - cross_border = $11::jsonb, - category_properties = $12::jsonb, - freight_charge_type = $13, - freight_fixed_amount = $14, - freight_template_id = $15, - sale_time_type = $16, - scheduled_sale_time = $17, - presale_type = $18, - delivery_time_type = $19, - delivery_start_at = $20, - delivery_after_paid_days = $21, - purchase_limit_quantity = $22, + is_distribution_product = $9, + goods_type = $10, + service_guarantees = $11, + cross_border = $12::jsonb, + category_properties = $13::jsonb, + freight_charge_type = $14, + freight_fixed_amount = $15, + freight_template_id = $16, + sale_time_type = $17, + scheduled_sale_time = $18, + presale_type = $19, + delivery_time_type = $20, + delivery_start_at = $21, + delivery_after_paid_days = $22, + purchase_limit_quantity = $23, version = version + 1, updated_at = NOW() -WHERE id = $1 AND version = $23 AND deleted_at IS NULL AND store_id = $24 +WHERE id = $1 AND version = $24 AND deleted_at IS NULL AND store_id = $25 `, productID, input.Product.Title, input.Product.SellingPoint, brandID, supplierID, - stringSliceOrEmpty(input.Product.ImageIDs), input.Product.DetailHTML, input.Product.MemberDiscountEnabled, + stringSliceOrEmpty(input.Product.ImageIDs), input.Product.DetailHTML, input.Product.MemberDiscountEnabled, input.Product.IsDistributionProduct, int16(input.Product.GoodsType), stringSliceOrEmpty(input.Product.ServiceGuarantees), attrs, categoryProperties, int16(input.Product.FreightChargeType), input.Product.FreightFixedAmount, freightTemplateID, int16(input.Product.SaleTimeType), input.Product.ScheduledSaleTime, @@ -3593,6 +3597,7 @@ SELECT p.updated_at, p.status, COALESCE(to_json(p.image_ids)::text, '[]'), COALESCE(p.category_properties, '{}'::jsonb)::text, + p.is_distribution_product, p.presale_type, COALESCE(sku_summary.price, 0), COALESCE(sku_summary.sold_quantity, 0), @@ -3985,6 +3990,7 @@ SELECT p.updated_at, p.status, COALESCE(to_json(p.image_ids)::text, '[]'), COALESCE(p.category_properties, '{}'::jsonb)::text, + p.is_distribution_product, p.presale_type, COALESCE(sku_summary.price, 0), COALESCE(sku_summary.sold_quantity, 0), @@ -4188,6 +4194,7 @@ SELECT DISTINCT p.updated_at, p.status, COALESCE(to_json(p.image_ids)::text, '[]'), COALESCE(p.category_properties, '{}'::jsonb)::text, + p.is_distribution_product, p.presale_type, COALESCE(sku_summary.price, 0), COALESCE(sku_summary.sold_quantity, 0), @@ -4649,6 +4656,7 @@ SELECT id, COALESCE(to_json(image_ids)::text, '[]'), COALESCE(detail_html, ''), member_discount_enabled, + is_distribution_product, goods_type, COALESCE(to_json(service_guarantees)::text, '[]'), status, @@ -4731,6 +4739,7 @@ INSERT INTO product.products ( image_ids, detail_html, member_discount_enabled, + is_distribution_product, goods_type, service_guarantees, status, @@ -4747,7 +4756,7 @@ INSERT INTO product.products ( delivery_after_paid_days, purchase_limit_quantity ) -VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15::jsonb, $16::jsonb, $17, $18, $19, $20, $21, $22, $23, $24, $25, $26) +VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16::jsonb, $17::jsonb, $18, $19, $20, $21, $22, $23, $24, $25, $26, $27) RETURNING id, spu_no, title, @@ -4757,6 +4766,7 @@ RETURNING id, COALESCE(to_json(image_ids)::text, '[]'), COALESCE(detail_html, ''), member_discount_enabled, + is_distribution_product, goods_type, COALESCE(to_json(service_guarantees)::text, '[]'), status, @@ -4777,7 +4787,7 @@ RETURNING id, created_at, updated_at `, id, product.Scope.TenantID, product.Scope.StoreID, product.SPUNo, product.Title, product.SellingPoint, brandID, supplierID, stringSliceOrEmpty(product.ImageIDs), - product.DetailHTML, product.MemberDiscountEnabled, int16(product.GoodsType), stringSliceOrEmpty(product.ServiceGuarantees), + product.DetailHTML, product.MemberDiscountEnabled, product.IsDistributionProduct, int16(product.GoodsType), stringSliceOrEmpty(product.ServiceGuarantees), statusToDB(product.Status), attrs, categoryProperties, int16(product.FreightChargeType), product.FreightFixedAmount, freightTemplateID, int16(product.SaleTimeType), product.ScheduledSaleTime, int16(product.PresaleType), int16(product.DeliveryTimeType), deliveryStartAtArgument(product), deliveryAfterPaidDaysArgument(product), product.PurchaseLimitQuantity) @@ -5174,6 +5184,7 @@ func scanCategoryProduct(scanner rowScanner) (*CategoryProductRecord, error) { &status, &imageIDsJSON, &categoryPropertiesJSON, + &rec.IsDistributionProduct, &rec.PresaleType, &rec.Price, &rec.SoldQuantity, @@ -5229,6 +5240,7 @@ func scanProduct(scanner rowScanner) (*ProductRecord, error) { &imageIDsJSON, &rec.DetailHTML, &rec.MemberDiscountEnabled, + &rec.IsDistributionProduct, &goodsType, &serviceGuaranteesJSON, &status, diff --git a/internal/server/product_server.go b/internal/server/product_server.go index 8e3270c..6585fda 100644 --- a/internal/server/product_server.go +++ b/internal/server/product_server.go @@ -2293,6 +2293,7 @@ func productFromCreateRequest(req *productv1.CreateProductRequest) (repository.P DeliveryStartAt: timestampToTime(req.GetDeliveryStartAt()), DeliveryAfterPaidDays: req.GetDeliveryAfterPaidDays(), PurchaseLimitQuantity: req.GetPurchaseLimitQuantity(), + IsDistributionProduct: req.GetIsDistributionProduct(), } detailHTML, err := sanitizeDetailHTML(req.GetDetailHtml()) if err != nil { @@ -2418,6 +2419,8 @@ func mergeProductUpdate(current *repository.ProductRecord, update *productv1.Pro merged.DeliveryAfterPaidDays = update.GetDeliveryAfterPaidDays() case "purchase_limit_quantity": merged.PurchaseLimitQuantity = update.GetPurchaseLimitQuantity() + case "is_distribution_product": + merged.IsDistributionProduct = update.GetIsDistributionProduct() case "leaf_catalog_category_ids": leafCategoryIDs, err := normalizeLeafCategoryIDs(update.GetLeafCatalogCategoryIds()) if err != nil { @@ -3046,6 +3049,7 @@ func toProtoProduct(product *repository.ProductRecord, skus []repository.SKUReco DeliveryStartAt: timePtrToTimestamp(product.DeliveryStartAt), DeliveryAfterPaidDays: product.DeliveryAfterPaidDays, PurchaseLimitQuantity: product.PurchaseLimitQuantity, + IsDistributionProduct: product.IsDistributionProduct, Version: product.Version, DeletedAt: timePtrToMillisString(product.DeletedAt), LeafCatalogCategoryIds: append([]string(nil), product.LeafCategoryIDs...), @@ -3113,19 +3117,20 @@ func toProtoCategoryProduct(product *repository.CategoryProductRecord) *productv return nil } result := &productv1.CategoryProduct{ - UpdatedAt: timeToMillisString(product.UpdatedAt), - CreatedAt: timeToMillisString(product.CreatedAt), - Title: product.Title, - ProductId: product.ID, - SpuNo: product.SPUNo, - Status: product.Status, - ImageIds: product.ImageIDs, - Price: product.Price, - SoldQuantity: product.SoldQuantity, - StockQuantity: product.StockQuantity, - LockedQuantity: product.LockedQuantity, - CategoryProperties: categoryPropertiesProto(product.CategoryPropertiesJSON), - PresaleType: product.PresaleType, + UpdatedAt: timeToMillisString(product.UpdatedAt), + CreatedAt: timeToMillisString(product.CreatedAt), + Title: product.Title, + ProductId: product.ID, + SpuNo: product.SPUNo, + Status: product.Status, + ImageIds: product.ImageIDs, + Price: product.Price, + SoldQuantity: product.SoldQuantity, + StockQuantity: product.StockQuantity, + LockedQuantity: product.LockedQuantity, + CategoryProperties: categoryPropertiesProto(product.CategoryPropertiesJSON), + PresaleType: product.PresaleType, + IsDistributionProduct: product.IsDistributionProduct, } for _, category := range product.ProductCategories { item := &productv1.ProductCategorySummary{ diff --git a/scripts/sql/011_add_products_is_distribution_product.sql b/scripts/sql/011_add_products_is_distribution_product.sql new file mode 100644 index 0000000..8ac311f --- /dev/null +++ b/scripts/sql/011_add_products_is_distribution_product.sql @@ -0,0 +1,7 @@ +-- Adds an explicit SPU-level distribution flag. Existing products remain +-- non-distribution products by default. +ALTER TABLE product.products + ADD COLUMN IF NOT EXISTS is_distribution_product BOOLEAN NOT NULL DEFAULT FALSE; + +COMMENT ON COLUMN product.products.is_distribution_product + IS '是否为分销商品;FALSE 表示非分销商品'; diff --git a/scripts/sql/02-product-schema.sql b/scripts/sql/02-product-schema.sql index fdf570c..a87d7f3 100644 --- a/scripts/sql/02-product-schema.sql +++ b/scripts/sql/02-product-schema.sql @@ -25,6 +25,7 @@ CREATE TABLE IF NOT EXISTS product.products ( image_ids VARCHAR[] NOT NULL DEFAULT '{}'::VARCHAR[], detail_html TEXT, member_discount_enabled BOOLEAN NOT NULL DEFAULT TRUE, + is_distribution_product BOOLEAN NOT NULL DEFAULT FALSE, goods_type SMALLINT NOT NULL DEFAULT 1, service_guarantees VARCHAR[] NOT NULL DEFAULT '{}'::VARCHAR[], status SMALLINT NOT NULL DEFAULT 1, @@ -79,6 +80,7 @@ COMMENT ON COLUMN product.products.brand_id IS '品牌 ID,对应 product.brand COMMENT ON COLUMN product.products.supplier_id IS '供应商 ID,对应 product.suppliers.id;由应用层校验所属店铺及可用状态'; COMMENT ON COLUMN product.products.service_guarantees IS '服务保障展示快照;服务保障主数据变更时需通过业务流程刷新'; COMMENT ON COLUMN product.products.detail_html IS '商品详情 HTML;写入或渲染前必须做 HTML Sanitization,列表接口不得读取该字段'; +COMMENT ON COLUMN product.products.is_distribution_product IS '是否为分销商品;FALSE 表示非分销商品'; COMMENT ON COLUMN product.products.version IS '乐观锁版本号,用于后台并发编辑冲突检测'; COMMENT ON COLUMN product.products.deleted_at IS '软删除时间;NULL 表示未删除,商品禁止物理删除以保护订单和财务引用'; diff --git a/scripts/sql/recreate-product-tables.sql b/scripts/sql/recreate-product-tables.sql index 5f25d14..f0900a1 100644 --- a/scripts/sql/recreate-product-tables.sql +++ b/scripts/sql/recreate-product-tables.sql @@ -48,6 +48,7 @@ CREATE TABLE IF NOT EXISTS product.products ( image_ids VARCHAR[] NOT NULL DEFAULT '{}'::VARCHAR[], detail_html TEXT, member_discount_enabled BOOLEAN NOT NULL DEFAULT TRUE, + is_distribution_product BOOLEAN NOT NULL DEFAULT FALSE, goods_type SMALLINT NOT NULL DEFAULT 1, service_guarantees VARCHAR[] NOT NULL DEFAULT '{}'::VARCHAR[], status SMALLINT NOT NULL DEFAULT 1,