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). // ProductRecord is the persistence model for product.products (SPU).
type ProductRecord struct { type ProductRecord struct {
ID string ID string
SPUNo string SPUNo string
Title string Title string
SellingPoint string SellingPoint string
BrandID string BrandID string
SupplierID string SupplierID string
ImageIDs []string ImageIDs []string
DetailHTML string DetailHTML string
MemberDiscountEnabled bool MemberDiscountEnabled bool
IsDistributionProduct bool IsDistributionProduct bool
GoodsType int32 DistributionProductDetailJSON string
ServiceGuarantees []string GoodsType int32
Status productv1.ProductStatus ServiceGuarantees []string
Attrs map[string]any Status productv1.ProductStatus
CrossBorderJSON string Attrs map[string]any
CategoryPropertiesJSON string CrossBorderJSON string
FreightChargeType int32 CategoryPropertiesJSON string
FreightFixedAmount int64 FreightChargeType int32
FreightTemplateID string FreightFixedAmount int64
SaleTimeType int32 FreightTemplateID string
ScheduledSaleTime *time.Time SaleTimeType int32
PresaleType int32 ScheduledSaleTime *time.Time
DeliveryTimeType int32 PresaleType int32
DeliveryStartAt *time.Time DeliveryTimeType int32
DeliveryAfterPaidDays int32 DeliveryStartAt *time.Time
PurchaseLimitQuantity int32 DeliveryAfterPaidDays int32
LeafCategoryIDs []string PurchaseLimitQuantity int32
CategoryBindingIDs []string LeafCategoryIDs []string
GroupIDs []string CategoryBindingIDs []string
GroupBindingIDs []string GroupIDs []string
ProductCategoryIDs []string GroupBindingIDs []string
ProductCategoryBindingIDs []string ProductCategoryIDs []string
ProductCategories []ProductCategorySummaryRecord ProductCategoryBindingIDs []string
Version int32 ProductCategories []ProductCategorySummaryRecord
DeletedAt *time.Time Version int32
CreatedAt time.Time DeletedAt *time.Time
UpdatedAt time.Time CreatedAt time.Time
Scope StoreScope UpdatedAt time.Time
Scope StoreScope
} }
// ProductFreightChargeUpdate is the persisted freight projection returned by // 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 tenant_id BIGINT;
ALTER TABLE product.products ADD COLUMN IF NOT EXISTS store_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 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 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 presale_type SMALLINT NOT NULL DEFAULT 0;
ALTER TABLE product.products ADD COLUMN IF NOT EXISTS delivery_time_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, detail_html = $7,
member_discount_enabled = $8, member_discount_enabled = $8,
is_distribution_product = $9, is_distribution_product = $9,
goods_type = $10, distribution_product_detail = $10::jsonb,
service_guarantees = $11, goods_type = $11,
cross_border = $12::jsonb, service_guarantees = $12,
category_properties = $13::jsonb, cross_border = $13::jsonb,
freight_charge_type = $14, category_properties = $14::jsonb,
freight_fixed_amount = $15, freight_charge_type = $15,
freight_template_id = $16, freight_fixed_amount = $16,
sale_time_type = $17, freight_template_id = $17,
scheduled_sale_time = $18, sale_time_type = $18,
presale_type = $19, scheduled_sale_time = $19,
delivery_time_type = $20, presale_type = $20,
delivery_start_at = $21, delivery_time_type = $21,
delivery_after_paid_days = $22, delivery_start_at = $22,
purchase_limit_quantity = $23, delivery_after_paid_days = $23,
purchase_limit_quantity = $24,
version = version + 1, version = version + 1,
updated_at = NOW() 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, `, productID, input.Product.Title, input.Product.SellingPoint, brandID, supplierID,
stringSliceOrEmpty(input.Product.ImageIDs), input.Product.DetailHTML, input.Product.MemberDiscountEnabled, input.Product.IsDistributionProduct, 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.FreightChargeType), input.Product.FreightFixedAmount, freightTemplateID,
int16(input.Product.SaleTimeType), input.Product.ScheduledSaleTime, int16(input.Product.SaleTimeType), input.Product.ScheduledSaleTime,
int16(input.Product.PresaleType), int16(input.Product.DeliveryTimeType), deliveryStartAtArgument(input.Product), deliveryAfterPaidDaysArgument(input.Product), input.Product.PurchaseLimitQuantity, 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, ''), COALESCE(detail_html, ''),
member_discount_enabled, member_discount_enabled,
is_distribution_product, is_distribution_product,
COALESCE(distribution_product_detail, '{}'::jsonb)::text,
goods_type, goods_type,
COALESCE(to_json(service_guarantees)::text, '[]'), COALESCE(to_json(service_guarantees)::text, '[]'),
status, status,
@ -4740,6 +4744,7 @@ INSERT INTO product.products (
detail_html, detail_html,
member_discount_enabled, member_discount_enabled,
is_distribution_product, is_distribution_product,
distribution_product_detail,
goods_type, goods_type,
service_guarantees, service_guarantees,
status, status,
@ -4756,7 +4761,7 @@ INSERT INTO product.products (
delivery_after_paid_days, delivery_after_paid_days,
purchase_limit_quantity 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, RETURNING id,
spu_no, spu_no,
title, title,
@ -4767,6 +4772,7 @@ RETURNING id,
COALESCE(detail_html, ''), COALESCE(detail_html, ''),
member_discount_enabled, member_discount_enabled,
is_distribution_product, is_distribution_product,
COALESCE(distribution_product_detail, '{}'::jsonb)::text,
goods_type, goods_type,
COALESCE(to_json(service_guarantees)::text, '[]'), COALESCE(to_json(service_guarantees)::text, '[]'),
status, status,
@ -4787,7 +4793,7 @@ RETURNING id,
created_at, created_at,
updated_at updated_at
`, id, product.Scope.TenantID, product.Scope.StoreID, product.SPUNo, product.Title, product.SellingPoint, brandID, supplierID, stringSliceOrEmpty(product.ImageIDs), `, 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, statusToDB(product.Status), attrs, categoryProperties, int16(product.FreightChargeType), product.FreightFixedAmount, freightTemplateID,
int16(product.SaleTimeType), product.ScheduledSaleTime, int16(product.SaleTimeType), product.ScheduledSaleTime,
int16(product.PresaleType), int16(product.DeliveryTimeType), deliveryStartAtArgument(product), deliveryAfterPaidDaysArgument(product), product.PurchaseLimitQuantity) 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.DetailHTML,
&rec.MemberDiscountEnabled, &rec.MemberDiscountEnabled,
&rec.IsDistributionProduct, &rec.IsDistributionProduct,
&rec.DistributionProductDetailJSON,
&goodsType, &goodsType,
&serviceGuaranteesJSON, &serviceGuaranteesJSON,
&status, &status,

View File

@ -2268,32 +2268,37 @@ func productFromCreateRequest(req *productv1.CreateProductRequest) (repository.P
if err != nil { if err != nil {
return repository.ProductRecord{}, nil, err return repository.ProductRecord{}, nil, err
} }
distributionProductDetailJSON, err := normalizeDistributionProductDetailJSON(req.GetDistributionProductDetail())
if err != nil {
return repository.ProductRecord{}, nil, err
}
record := repository.ProductRecord{ record := repository.ProductRecord{
SPUNo: spuNo, SPUNo: spuNo,
Title: title, Title: title,
SellingPoint: strings.TrimSpace(req.GetSellingPoint()), SellingPoint: strings.TrimSpace(req.GetSellingPoint()),
BrandID: strings.TrimSpace(req.GetBrandId()), BrandID: strings.TrimSpace(req.GetBrandId()),
SupplierID: strings.TrimSpace(req.GetSupplierId()), SupplierID: strings.TrimSpace(req.GetSupplierId()),
ImageIDs: normalizeStringList(req.GetImageIds()), ImageIDs: normalizeStringList(req.GetImageIds()),
DetailHTML: "", DetailHTML: "",
MemberDiscountEnabled: req.GetMemberDiscountEnabled(), MemberDiscountEnabled: req.GetMemberDiscountEnabled(),
GoodsType: defaultInt32(req.GetGoodsType(), 1), GoodsType: defaultInt32(req.GetGoodsType(), 1),
ServiceGuarantees: normalizeStringList(req.GetServiceGuarantees()), ServiceGuarantees: normalizeStringList(req.GetServiceGuarantees()),
Status: statusValue, Status: statusValue,
CrossBorderJSON: crossBorderJSON, CrossBorderJSON: crossBorderJSON,
CategoryPropertiesJSON: categoryPropertiesJSON, CategoryPropertiesJSON: categoryPropertiesJSON,
FreightChargeType: defaultInt32(req.GetFreightChargeType(), 1), FreightChargeType: defaultInt32(req.GetFreightChargeType(), 1),
FreightFixedAmount: req.GetFreightFixedAmount(), FreightFixedAmount: req.GetFreightFixedAmount(),
FreightTemplateID: strings.TrimSpace(req.GetFreightTemplateId()), FreightTemplateID: strings.TrimSpace(req.GetFreightTemplateId()),
SaleTimeType: defaultInt32(req.GetSaleTimeType(), 1), SaleTimeType: defaultInt32(req.GetSaleTimeType(), 1),
ScheduledSaleTime: timestampToTime(req.GetScheduledSaleTime()), ScheduledSaleTime: timestampToTime(req.GetScheduledSaleTime()),
PresaleType: req.GetPresaleType(), PresaleType: req.GetPresaleType(),
DeliveryTimeType: req.GetDeliveryTimeType(), DeliveryTimeType: req.GetDeliveryTimeType(),
DeliveryStartAt: timestampToTime(req.GetDeliveryStartAt()), DeliveryStartAt: timestampToTime(req.GetDeliveryStartAt()),
DeliveryAfterPaidDays: req.GetDeliveryAfterPaidDays(), DeliveryAfterPaidDays: req.GetDeliveryAfterPaidDays(),
PurchaseLimitQuantity: req.GetPurchaseLimitQuantity(), PurchaseLimitQuantity: req.GetPurchaseLimitQuantity(),
IsDistributionProduct: req.GetIsDistributionProduct(), IsDistributionProduct: req.GetIsDistributionProduct(),
DistributionProductDetailJSON: distributionProductDetailJSON,
} }
detailHTML, err := sanitizeDetailHTML(req.GetDetailHtml()) detailHTML, err := sanitizeDetailHTML(req.GetDetailHtml())
if err != nil { if err != nil {
@ -2421,6 +2426,12 @@ func mergeProductUpdate(current *repository.ProductRecord, update *productv1.Pro
merged.PurchaseLimitQuantity = update.GetPurchaseLimitQuantity() merged.PurchaseLimitQuantity = update.GetPurchaseLimitQuantity()
case "is_distribution_product": case "is_distribution_product":
merged.IsDistributionProduct = update.GetIsDistributionProduct() 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": case "leaf_catalog_category_ids":
leafCategoryIDs, err := normalizeLeafCategoryIDs(update.GetLeafCatalogCategoryIds()) leafCategoryIDs, err := normalizeLeafCategoryIDs(update.GetLeafCatalogCategoryIds())
if err != nil { if err != nil {
@ -2808,6 +2819,17 @@ func categoryPropertiesJSONFromProto(properties *structpb.Struct) (string, error
return string(raw), nil 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 { func categoryPropertiesProto(raw string) *structpb.Struct {
properties := make(map[string]any) properties := make(map[string]any)
if strings.TrimSpace(raw) != "" { if strings.TrimSpace(raw) != "" {
@ -3021,40 +3043,41 @@ func toProtoProduct(product *repository.ProductRecord, skus []repository.SKUReco
} }
result := &productv1.Product{ result := &productv1.Product{
ProductId: product.ID, ProductId: product.ID,
SpuNo: product.SPUNo, SpuNo: product.SPUNo,
Title: product.Title, Title: product.Title,
SellingPoint: product.SellingPoint, SellingPoint: product.SellingPoint,
Status: product.Status, Status: product.Status,
Skus: protoSKUs, Skus: protoSKUs,
CreatedAt: timeToMillisString(product.CreatedAt), CreatedAt: timeToMillisString(product.CreatedAt),
UpdatedAt: timeToMillisString(product.UpdatedAt), UpdatedAt: timeToMillisString(product.UpdatedAt),
ImageIds: product.ImageIDs, ImageIds: product.ImageIDs,
BrandId: product.BrandID, BrandId: product.BrandID,
SupplierId: product.SupplierID, SupplierId: product.SupplierID,
DetailHtml: product.DetailHTML, DetailHtml: product.DetailHTML,
MemberDiscountEnabled: product.MemberDiscountEnabled, MemberDiscountEnabled: product.MemberDiscountEnabled,
GoodsType: product.GoodsType, GoodsType: product.GoodsType,
ServiceGuarantees: product.ServiceGuarantees, ServiceGuarantees: product.ServiceGuarantees,
CrossBorderJson: product.CrossBorderJSON, CrossBorderJson: product.CrossBorderJSON,
CrossBorderInfo: toProtoCrossBorderInfo(product.CrossBorderJSON), CrossBorderInfo: toProtoCrossBorderInfo(product.CrossBorderJSON),
CategoryProperties: categoryPropertiesProto(product.CategoryPropertiesJSON), CategoryProperties: categoryPropertiesProto(product.CategoryPropertiesJSON),
FreightChargeType: product.FreightChargeType, FreightChargeType: product.FreightChargeType,
FreightFixedAmount: product.FreightFixedAmount, FreightFixedAmount: product.FreightFixedAmount,
FreightTemplateId: product.FreightTemplateID, FreightTemplateId: product.FreightTemplateID,
SaleTimeType: product.SaleTimeType, SaleTimeType: product.SaleTimeType,
ScheduledSaleTime: timePtrToMillisString(product.ScheduledSaleTime), ScheduledSaleTime: timePtrToMillisString(product.ScheduledSaleTime),
PresaleType: product.PresaleType, PresaleType: product.PresaleType,
DeliveryTimeType: product.DeliveryTimeType, DeliveryTimeType: product.DeliveryTimeType,
DeliveryStartAt: timePtrToTimestamp(product.DeliveryStartAt), DeliveryStartAt: timePtrToTimestamp(product.DeliveryStartAt),
DeliveryAfterPaidDays: product.DeliveryAfterPaidDays, DeliveryAfterPaidDays: product.DeliveryAfterPaidDays,
PurchaseLimitQuantity: product.PurchaseLimitQuantity, PurchaseLimitQuantity: product.PurchaseLimitQuantity,
IsDistributionProduct: product.IsDistributionProduct, IsDistributionProduct: product.IsDistributionProduct,
Version: product.Version, DistributionProductDetail: product.DistributionProductDetailJSON,
DeletedAt: timePtrToMillisString(product.DeletedAt), Version: product.Version,
LeafCatalogCategoryIds: append([]string(nil), product.LeafCategoryIDs...), DeletedAt: timePtrToMillisString(product.DeletedAt),
GroupIds: append([]string(nil), product.GroupIDs...), LeafCatalogCategoryIds: append([]string(nil), product.LeafCategoryIDs...),
ProductCategoryIds: append([]string(nil), product.ProductCategoryIDs...), GroupIds: append([]string(nil), product.GroupIDs...),
ProductCategoryIds: append([]string(nil), product.ProductCategoryIDs...),
} }
for _, category := range product.ProductCategories { for _, category := range product.ProductCategories {
item := &productv1.ProductCategorySummary{ 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, detail_html TEXT,
member_discount_enabled BOOLEAN NOT NULL DEFAULT TRUE, member_discount_enabled BOOLEAN NOT NULL DEFAULT TRUE,
is_distribution_product BOOLEAN NOT NULL DEFAULT FALSE, is_distribution_product BOOLEAN NOT NULL DEFAULT FALSE,
distribution_product_detail JSONB NOT NULL DEFAULT '{}'::JSONB,
goods_type SMALLINT NOT NULL DEFAULT 1, goods_type SMALLINT NOT NULL DEFAULT 1,
service_guarantees VARCHAR[] NOT NULL DEFAULT '{}'::VARCHAR[], service_guarantees VARCHAR[] NOT NULL DEFAULT '{}'::VARCHAR[],
status SMALLINT NOT NULL DEFAULT 1, 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.service_guarantees IS '服务保障展示快照;服务保障主数据变更时需通过业务流程刷新';
COMMENT ON COLUMN product.products.detail_html IS '商品详情 HTML;写入或渲染前必须做 HTML Sanitization,列表接口不得读取该字段'; 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.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.version IS '乐观锁版本号,用于后台并发编辑冲突检测';
COMMENT ON COLUMN product.products.deleted_at IS '软删除时间;NULL 表示未删除,商品禁止物理删除以保护订单和财务引用'; 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, detail_html TEXT,
member_discount_enabled BOOLEAN NOT NULL DEFAULT TRUE, member_discount_enabled BOOLEAN NOT NULL DEFAULT TRUE,
is_distribution_product BOOLEAN NOT NULL DEFAULT FALSE, is_distribution_product BOOLEAN NOT NULL DEFAULT FALSE,
distribution_product_detail JSONB NOT NULL DEFAULT '{}'::JSONB,
goods_type SMALLINT NOT NULL DEFAULT 1, goods_type SMALLINT NOT NULL DEFAULT 1,
service_guarantees VARCHAR[] NOT NULL DEFAULT '{}'::VARCHAR[], service_guarantees VARCHAR[] NOT NULL DEFAULT '{}'::VARCHAR[],
status SMALLINT NOT NULL DEFAULT 1, status SMALLINT NOT NULL DEFAULT 1,