feat: add distribution_product_detail field to product schema and related logic

This commit is contained in:
BXY 2026-10-30 15:14:45 +08:00
parent f93fce450c
commit 89bbce72dd
5 changed files with 153 additions and 114 deletions

View File

@ -80,44 +80,45 @@ var ErrSameStore = errors.New("target store is the current store")
// ProductRecord is the persistence model for product.products (SPU).
type ProductRecord struct {
ID string
SPUNo string
Title string
SellingPoint string
BrandID string
SupplierID string
ImageIDs []string
DetailHTML string
MemberDiscountEnabled bool
IsDistributionProduct bool
GoodsType int32
ServiceGuarantees []string
Status productv1.ProductStatus
Attrs map[string]any
CrossBorderJSON string
CategoryPropertiesJSON string
FreightChargeType int32
FreightFixedAmount int64
FreightTemplateID string
SaleTimeType int32
ScheduledSaleTime *time.Time
PresaleType int32
DeliveryTimeType int32
DeliveryStartAt *time.Time
DeliveryAfterPaidDays int32
PurchaseLimitQuantity int32
LeafCategoryIDs []string
CategoryBindingIDs []string
GroupIDs []string
GroupBindingIDs []string
ProductCategoryIDs []string
ProductCategoryBindingIDs []string
ProductCategories []ProductCategorySummaryRecord
Version int32
DeletedAt *time.Time
CreatedAt time.Time
UpdatedAt time.Time
Scope StoreScope
ID string
SPUNo string
Title string
SellingPoint string
BrandID string
SupplierID string
ImageIDs []string
DetailHTML string
MemberDiscountEnabled bool
IsDistributionProduct bool
DistributionProductDetailJSON string
GoodsType int32
ServiceGuarantees []string
Status productv1.ProductStatus
Attrs map[string]any
CrossBorderJSON string
CategoryPropertiesJSON string
FreightChargeType int32
FreightFixedAmount int64
FreightTemplateID string
SaleTimeType int32
ScheduledSaleTime *time.Time
PresaleType int32
DeliveryTimeType int32
DeliveryStartAt *time.Time
DeliveryAfterPaidDays int32
PurchaseLimitQuantity int32
LeafCategoryIDs []string
CategoryBindingIDs []string
GroupIDs []string
GroupBindingIDs []string
ProductCategoryIDs []string
ProductCategoryBindingIDs []string
ProductCategories []ProductCategorySummaryRecord
Version int32
DeletedAt *time.Time
CreatedAt time.Time
UpdatedAt time.Time
Scope StoreScope
}
// ProductFreightChargeUpdate is the persisted freight projection returned by
@ -719,6 +720,7 @@ CREATE INDEX IF NOT EXISTS idx_products_active_created_id ON product.products(cr
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 distribution_product_detail JSONB NOT NULL DEFAULT '{}'::JSONB;
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;
@ -1847,26 +1849,27 @@ SET title = $2,
detail_html = $7,
member_discount_enabled = $8,
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,
distribution_product_detail = $10::jsonb,
goods_type = $11,
service_guarantees = $12,
cross_border = $13::jsonb,
category_properties = $14::jsonb,
freight_charge_type = $15,
freight_fixed_amount = $16,
freight_template_id = $17,
sale_time_type = $18,
scheduled_sale_time = $19,
presale_type = $20,
delivery_time_type = $21,
delivery_start_at = $22,
delivery_after_paid_days = $23,
purchase_limit_quantity = $24,
version = version + 1,
updated_at = NOW()
WHERE id = $1 AND version = $24 AND deleted_at IS NULL AND store_id = $25
WHERE id = $1 AND version = $25 AND deleted_at IS NULL AND store_id = $26
`, productID, input.Product.Title, input.Product.SellingPoint, brandID, supplierID,
stringSliceOrEmpty(input.Product.ImageIDs), input.Product.DetailHTML, input.Product.MemberDiscountEnabled, input.Product.IsDistributionProduct,
int16(input.Product.GoodsType), stringSliceOrEmpty(input.Product.ServiceGuarantees), attrs, categoryProperties,
input.Product.DistributionProductDetailJSON, 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,
int16(input.Product.PresaleType), int16(input.Product.DeliveryTimeType), deliveryStartAtArgument(input.Product), deliveryAfterPaidDaysArgument(input.Product), input.Product.PurchaseLimitQuantity,
@ -4657,6 +4660,7 @@ SELECT id,
COALESCE(detail_html, ''),
member_discount_enabled,
is_distribution_product,
COALESCE(distribution_product_detail, '{}'::jsonb)::text,
goods_type,
COALESCE(to_json(service_guarantees)::text, '[]'),
status,
@ -4740,6 +4744,7 @@ INSERT INTO product.products (
detail_html,
member_discount_enabled,
is_distribution_product,
distribution_product_detail,
goods_type,
service_guarantees,
status,
@ -4756,7 +4761,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, $16::jsonb, $17::jsonb, $18, $19, $20, $21, $22, $23, $24, $25, $26, $27)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13::jsonb, $14, $15, $16, $17::jsonb, $18::jsonb, $19, $20, $21, $22, $23, $24, $25, $26, $27, $28)
RETURNING id,
spu_no,
title,
@ -4767,6 +4772,7 @@ RETURNING id,
COALESCE(detail_html, ''),
member_discount_enabled,
is_distribution_product,
COALESCE(distribution_product_detail, '{}'::jsonb)::text,
goods_type,
COALESCE(to_json(service_guarantees)::text, '[]'),
status,
@ -4787,7 +4793,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, product.IsDistributionProduct, int16(product.GoodsType), stringSliceOrEmpty(product.ServiceGuarantees),
product.DetailHTML, product.MemberDiscountEnabled, product.IsDistributionProduct, product.DistributionProductDetailJSON, 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)
@ -5241,6 +5247,7 @@ func scanProduct(scanner rowScanner) (*ProductRecord, error) {
&rec.DetailHTML,
&rec.MemberDiscountEnabled,
&rec.IsDistributionProduct,
&rec.DistributionProductDetailJSON,
&goodsType,
&serviceGuaranteesJSON,
&status,

View File

@ -2268,32 +2268,37 @@ func productFromCreateRequest(req *productv1.CreateProductRequest) (repository.P
if err != nil {
return repository.ProductRecord{}, nil, err
}
distributionProductDetailJSON, err := normalizeDistributionProductDetailJSON(req.GetDistributionProductDetail())
if err != nil {
return repository.ProductRecord{}, nil, err
}
record := repository.ProductRecord{
SPUNo: spuNo,
Title: title,
SellingPoint: strings.TrimSpace(req.GetSellingPoint()),
BrandID: strings.TrimSpace(req.GetBrandId()),
SupplierID: strings.TrimSpace(req.GetSupplierId()),
ImageIDs: normalizeStringList(req.GetImageIds()),
DetailHTML: "",
MemberDiscountEnabled: req.GetMemberDiscountEnabled(),
GoodsType: defaultInt32(req.GetGoodsType(), 1),
ServiceGuarantees: normalizeStringList(req.GetServiceGuarantees()),
Status: statusValue,
CrossBorderJSON: crossBorderJSON,
CategoryPropertiesJSON: categoryPropertiesJSON,
FreightChargeType: defaultInt32(req.GetFreightChargeType(), 1),
FreightFixedAmount: req.GetFreightFixedAmount(),
FreightTemplateID: strings.TrimSpace(req.GetFreightTemplateId()),
SaleTimeType: defaultInt32(req.GetSaleTimeType(), 1),
ScheduledSaleTime: timestampToTime(req.GetScheduledSaleTime()),
PresaleType: req.GetPresaleType(),
DeliveryTimeType: req.GetDeliveryTimeType(),
DeliveryStartAt: timestampToTime(req.GetDeliveryStartAt()),
DeliveryAfterPaidDays: req.GetDeliveryAfterPaidDays(),
PurchaseLimitQuantity: req.GetPurchaseLimitQuantity(),
IsDistributionProduct: req.GetIsDistributionProduct(),
SPUNo: spuNo,
Title: title,
SellingPoint: strings.TrimSpace(req.GetSellingPoint()),
BrandID: strings.TrimSpace(req.GetBrandId()),
SupplierID: strings.TrimSpace(req.GetSupplierId()),
ImageIDs: normalizeStringList(req.GetImageIds()),
DetailHTML: "",
MemberDiscountEnabled: req.GetMemberDiscountEnabled(),
GoodsType: defaultInt32(req.GetGoodsType(), 1),
ServiceGuarantees: normalizeStringList(req.GetServiceGuarantees()),
Status: statusValue,
CrossBorderJSON: crossBorderJSON,
CategoryPropertiesJSON: categoryPropertiesJSON,
FreightChargeType: defaultInt32(req.GetFreightChargeType(), 1),
FreightFixedAmount: req.GetFreightFixedAmount(),
FreightTemplateID: strings.TrimSpace(req.GetFreightTemplateId()),
SaleTimeType: defaultInt32(req.GetSaleTimeType(), 1),
ScheduledSaleTime: timestampToTime(req.GetScheduledSaleTime()),
PresaleType: req.GetPresaleType(),
DeliveryTimeType: req.GetDeliveryTimeType(),
DeliveryStartAt: timestampToTime(req.GetDeliveryStartAt()),
DeliveryAfterPaidDays: req.GetDeliveryAfterPaidDays(),
PurchaseLimitQuantity: req.GetPurchaseLimitQuantity(),
IsDistributionProduct: req.GetIsDistributionProduct(),
DistributionProductDetailJSON: distributionProductDetailJSON,
}
detailHTML, err := sanitizeDetailHTML(req.GetDetailHtml())
if err != nil {
@ -2421,6 +2426,12 @@ func mergeProductUpdate(current *repository.ProductRecord, update *productv1.Pro
merged.PurchaseLimitQuantity = update.GetPurchaseLimitQuantity()
case "is_distribution_product":
merged.IsDistributionProduct = update.GetIsDistributionProduct()
case "distribution_product_detail":
detailJSON, err := normalizeDistributionProductDetailJSON(update.GetDistributionProductDetail())
if err != nil {
return repository.ProductRecord{}, err
}
merged.DistributionProductDetailJSON = detailJSON
case "leaf_catalog_category_ids":
leafCategoryIDs, err := normalizeLeafCategoryIDs(update.GetLeafCatalogCategoryIds())
if err != nil {
@ -2808,6 +2819,17 @@ func categoryPropertiesJSONFromProto(properties *structpb.Struct) (string, error
return string(raw), nil
}
func normalizeDistributionProductDetailJSON(raw string) (string, error) {
trimmed := strings.TrimSpace(raw)
if trimmed == "" {
return "{}", nil
}
if !json.Valid([]byte(trimmed)) {
return "", errors.New("distribution_product_detail must be valid JSON")
}
return trimmed, nil
}
func categoryPropertiesProto(raw string) *structpb.Struct {
properties := make(map[string]any)
if strings.TrimSpace(raw) != "" {
@ -3021,40 +3043,41 @@ func toProtoProduct(product *repository.ProductRecord, skus []repository.SKUReco
}
result := &productv1.Product{
ProductId: product.ID,
SpuNo: product.SPUNo,
Title: product.Title,
SellingPoint: product.SellingPoint,
Status: product.Status,
Skus: protoSKUs,
CreatedAt: timeToMillisString(product.CreatedAt),
UpdatedAt: timeToMillisString(product.UpdatedAt),
ImageIds: product.ImageIDs,
BrandId: product.BrandID,
SupplierId: product.SupplierID,
DetailHtml: product.DetailHTML,
MemberDiscountEnabled: product.MemberDiscountEnabled,
GoodsType: product.GoodsType,
ServiceGuarantees: product.ServiceGuarantees,
CrossBorderJson: product.CrossBorderJSON,
CrossBorderInfo: toProtoCrossBorderInfo(product.CrossBorderJSON),
CategoryProperties: categoryPropertiesProto(product.CategoryPropertiesJSON),
FreightChargeType: product.FreightChargeType,
FreightFixedAmount: product.FreightFixedAmount,
FreightTemplateId: product.FreightTemplateID,
SaleTimeType: product.SaleTimeType,
ScheduledSaleTime: timePtrToMillisString(product.ScheduledSaleTime),
PresaleType: product.PresaleType,
DeliveryTimeType: product.DeliveryTimeType,
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...),
GroupIds: append([]string(nil), product.GroupIDs...),
ProductCategoryIds: append([]string(nil), product.ProductCategoryIDs...),
ProductId: product.ID,
SpuNo: product.SPUNo,
Title: product.Title,
SellingPoint: product.SellingPoint,
Status: product.Status,
Skus: protoSKUs,
CreatedAt: timeToMillisString(product.CreatedAt),
UpdatedAt: timeToMillisString(product.UpdatedAt),
ImageIds: product.ImageIDs,
BrandId: product.BrandID,
SupplierId: product.SupplierID,
DetailHtml: product.DetailHTML,
MemberDiscountEnabled: product.MemberDiscountEnabled,
GoodsType: product.GoodsType,
ServiceGuarantees: product.ServiceGuarantees,
CrossBorderJson: product.CrossBorderJSON,
CrossBorderInfo: toProtoCrossBorderInfo(product.CrossBorderJSON),
CategoryProperties: categoryPropertiesProto(product.CategoryPropertiesJSON),
FreightChargeType: product.FreightChargeType,
FreightFixedAmount: product.FreightFixedAmount,
FreightTemplateId: product.FreightTemplateID,
SaleTimeType: product.SaleTimeType,
ScheduledSaleTime: timePtrToMillisString(product.ScheduledSaleTime),
PresaleType: product.PresaleType,
DeliveryTimeType: product.DeliveryTimeType,
DeliveryStartAt: timePtrToTimestamp(product.DeliveryStartAt),
DeliveryAfterPaidDays: product.DeliveryAfterPaidDays,
PurchaseLimitQuantity: product.PurchaseLimitQuantity,
IsDistributionProduct: product.IsDistributionProduct,
DistributionProductDetail: product.DistributionProductDetailJSON,
Version: product.Version,
DeletedAt: timePtrToMillisString(product.DeletedAt),
LeafCatalogCategoryIds: append([]string(nil), product.LeafCategoryIDs...),
GroupIds: append([]string(nil), product.GroupIDs...),
ProductCategoryIds: append([]string(nil), product.ProductCategoryIDs...),
}
for _, category := range product.ProductCategories {
item := &productv1.ProductCategorySummary{

View File

@ -0,0 +1,6 @@
-- 为已部署环境补充分销商品详情;JSONB 由数据库保证传入内容是合法 JSON。
ALTER TABLE product.products
ADD COLUMN IF NOT EXISTS distribution_product_detail JSONB NOT NULL DEFAULT '{}'::JSONB;
COMMENT ON COLUMN product.products.distribution_product_detail
IS '分销商品详情 JSON;由后台原样保存业务提交的有效 JSON';

View File

@ -26,6 +26,7 @@ CREATE TABLE IF NOT EXISTS product.products (
detail_html TEXT,
member_discount_enabled BOOLEAN NOT NULL DEFAULT TRUE,
is_distribution_product BOOLEAN NOT NULL DEFAULT FALSE,
distribution_product_detail JSONB NOT NULL DEFAULT '{}'::JSONB,
goods_type SMALLINT NOT NULL DEFAULT 1,
service_guarantees VARCHAR[] NOT NULL DEFAULT '{}'::VARCHAR[],
status SMALLINT NOT NULL DEFAULT 1,
@ -81,6 +82,7 @@ COMMENT ON COLUMN product.products.supplier_id IS '供应商 ID,对应 product
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.distribution_product_detail IS '分销商品详情 JSON;由后台原样保存业务提交的有效 JSON';
COMMENT ON COLUMN product.products.version IS '乐观锁版本号,用于后台并发编辑冲突检测';
COMMENT ON COLUMN product.products.deleted_at IS '软删除时间;NULL 表示未删除,商品禁止物理删除以保护订单和财务引用';

View File

@ -49,6 +49,7 @@ CREATE TABLE IF NOT EXISTS product.products (
detail_html TEXT,
member_discount_enabled BOOLEAN NOT NULL DEFAULT TRUE,
is_distribution_product BOOLEAN NOT NULL DEFAULT FALSE,
distribution_product_detail JSONB NOT NULL DEFAULT '{}'::JSONB,
goods_type SMALLINT NOT NULL DEFAULT 1,
service_guarantees VARCHAR[] NOT NULL DEFAULT '{}'::VARCHAR[],
status SMALLINT NOT NULL DEFAULT 1,