mirror of
https://ids-dev.idsbuybuybuy.com/ids-ecommerce/ids-svc-product.git
synced 2026-11-29 16:57:41 +08:00
feat: add CampaignID to CategoryRecord and update related methods for category handling
This commit is contained in:
parent
4f8a99010a
commit
8643103846
@ -255,15 +255,16 @@ type CategoryProductRecord struct {
|
|||||||
|
|
||||||
// CategoryRecord is the persistence model for product.product_category.
|
// CategoryRecord is the persistence model for product.product_category.
|
||||||
type CategoryRecord struct {
|
type CategoryRecord struct {
|
||||||
ID string
|
ID string
|
||||||
NodeType int32
|
NodeType int32
|
||||||
Name string
|
Name string
|
||||||
ParentID string
|
ParentID string
|
||||||
Level int32
|
Level int32
|
||||||
ImageIDs []string
|
ImageIDs []string
|
||||||
SortNum int32
|
SortNum int32
|
||||||
Status int32
|
Status int32
|
||||||
Version int32
|
CampaignID string
|
||||||
|
Version int32
|
||||||
// ProductCount is populated when listing product groups.
|
// ProductCount is populated when listing product groups.
|
||||||
ProductCount int64
|
ProductCount int64
|
||||||
}
|
}
|
||||||
@ -276,6 +277,7 @@ type CategoryFieldUpdate struct {
|
|||||||
ImageIDs *[]string
|
ImageIDs *[]string
|
||||||
SortNum *int32
|
SortNum *int32
|
||||||
Status *int32
|
Status *int32
|
||||||
|
CampaignID *string
|
||||||
}
|
}
|
||||||
|
|
||||||
// CategoryTreeUpdate groups an optional root update with direct-child updates.
|
// CategoryTreeUpdate groups an optional root update with direct-child updates.
|
||||||
@ -405,12 +407,15 @@ 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.product_category ADD COLUMN IF NOT EXISTS tenant_id BIGINT;
|
ALTER TABLE product.product_category ADD COLUMN IF NOT EXISTS tenant_id BIGINT;
|
||||||
ALTER TABLE product.product_category ADD COLUMN IF NOT EXISTS store_id BIGINT;
|
ALTER TABLE product.product_category ADD COLUMN IF NOT EXISTS store_id BIGINT;
|
||||||
|
ALTER TABLE product.product_category ADD COLUMN IF NOT EXISTS campaign_id BIGINT;
|
||||||
ALTER TABLE product.product_items ADD COLUMN IF NOT EXISTS tenant_id BIGINT;
|
ALTER TABLE product.product_items ADD COLUMN IF NOT EXISTS tenant_id BIGINT;
|
||||||
ALTER TABLE product.product_items ADD COLUMN IF NOT EXISTS store_id BIGINT;
|
ALTER TABLE product.product_items ADD COLUMN IF NOT EXISTS store_id BIGINT;
|
||||||
DROP INDEX IF EXISTS product.uq_products_spu_no;
|
DROP INDEX IF EXISTS product.uq_products_spu_no;
|
||||||
CREATE UNIQUE INDEX IF NOT EXISTS uq_products_store_spu_no ON product.products (store_id, spu_no) WHERE deleted_at IS NULL;
|
CREATE UNIQUE INDEX IF NOT EXISTS uq_products_store_spu_no ON product.products (store_id, spu_no) WHERE deleted_at IS NULL;
|
||||||
CREATE INDEX IF NOT EXISTS idx_products_store_id ON product.products (store_id) WHERE deleted_at IS NULL;
|
CREATE INDEX IF NOT EXISTS idx_products_store_id ON product.products (store_id) WHERE deleted_at IS NULL;
|
||||||
CREATE INDEX IF NOT EXISTS idx_product_category_store_id ON product.product_category (store_id);
|
CREATE INDEX IF NOT EXISTS idx_product_category_store_id ON product.product_category (store_id);
|
||||||
|
CREATE INDEX IF NOT EXISTS idx_product_category_campaign_id
|
||||||
|
ON product.product_category (campaign_id) WHERE campaign_id IS NOT NULL;
|
||||||
DROP INDEX IF EXISTS product.uq_product_items_sku_no;
|
DROP INDEX IF EXISTS product.uq_product_items_sku_no;
|
||||||
DROP INDEX IF EXISTS product.uq_product_items_sku_no_active;
|
DROP INDEX IF EXISTS product.uq_product_items_sku_no_active;
|
||||||
CREATE UNIQUE INDEX IF NOT EXISTS uq_product_items_store_sku_no_active
|
CREATE UNIQUE INDEX IF NOT EXISTS uq_product_items_store_sku_no_active
|
||||||
@ -644,7 +649,7 @@ func (r *ProductRepository) DeleteCategory(ctx context.Context, categoryID strin
|
|||||||
if target.Level == 1 {
|
if target.Level == 1 {
|
||||||
rows, err := tx.QueryContext(ctx, `
|
rows, err := tx.QueryContext(ctx, `
|
||||||
SELECT id, node_type, name, parent_id, level,
|
SELECT id, node_type, name, parent_id, level,
|
||||||
COALESCE(to_json(image_ids)::text, '[]'), sort_num, status, version
|
COALESCE(to_json(image_ids)::text, '[]'), sort_num, status, campaign_id, version
|
||||||
FROM product.product_category
|
FROM product.product_category
|
||||||
WHERE parent_id = $1 AND store_id = $2
|
WHERE parent_id = $1 AND store_id = $2
|
||||||
FOR UPDATE
|
FOR UPDATE
|
||||||
@ -728,7 +733,7 @@ func loadCategoryForUpdate(ctx context.Context, tx *sql.Tx, categoryID string, s
|
|||||||
}
|
}
|
||||||
row := tx.QueryRowContext(ctx, `
|
row := tx.QueryRowContext(ctx, `
|
||||||
SELECT id, node_type, name, parent_id, level,
|
SELECT id, node_type, name, parent_id, level,
|
||||||
COALESCE(to_json(image_ids)::text, '[]'), sort_num, status, version
|
COALESCE(to_json(image_ids)::text, '[]'), sort_num, status, campaign_id, version
|
||||||
FROM product.product_category
|
FROM product.product_category
|
||||||
WHERE id = $1 AND store_id = $2
|
WHERE id = $1 AND store_id = $2
|
||||||
FOR UPDATE`, id, scope.StoreID)
|
FOR UPDATE`, id, scope.StoreID)
|
||||||
@ -743,28 +748,34 @@ FOR UPDATE`, id, scope.StoreID)
|
|||||||
}
|
}
|
||||||
|
|
||||||
func updateCategoryFields(ctx context.Context, tx *sql.Tx, update CategoryFieldUpdate) (*CategoryRecord, error) {
|
func updateCategoryFields(ctx context.Context, tx *sql.Tx, update CategoryFieldUpdate) (*CategoryRecord, error) {
|
||||||
if update.Name == nil && update.ImageIDs == nil && update.SortNum == nil && update.Status == nil {
|
if update.Name == nil && update.ImageIDs == nil && update.SortNum == nil && update.Status == nil && update.CampaignID == nil {
|
||||||
return nil, errors.New("category update has no fields")
|
return nil, errors.New("category update has no fields")
|
||||||
}
|
}
|
||||||
id, err := parseID(update.ID, "category_id")
|
id, err := parseID(update.ID, "category_id")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
campaignID, err := nullableCategoryCampaignID(update.CampaignID)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
row := tx.QueryRowContext(ctx, `
|
row := tx.QueryRowContext(ctx, `
|
||||||
UPDATE product.product_category
|
UPDATE product.product_category
|
||||||
SET name = CASE WHEN $3 THEN $4 ELSE name END,
|
SET name = CASE WHEN $3 THEN $4 ELSE name END,
|
||||||
image_ids = CASE WHEN $5 THEN $6 ELSE image_ids END,
|
image_ids = CASE WHEN $5 THEN $6 ELSE image_ids END,
|
||||||
sort_num = CASE WHEN $7 THEN $8 ELSE sort_num END,
|
sort_num = CASE WHEN $7 THEN $8 ELSE sort_num END,
|
||||||
status = CASE WHEN $9 THEN $10 ELSE status END,
|
status = CASE WHEN $9 THEN $10 ELSE status END,
|
||||||
|
campaign_id = CASE WHEN $11 THEN $12 ELSE campaign_id END,
|
||||||
version = version + 1
|
version = version + 1
|
||||||
WHERE id = $1 AND version = $2
|
WHERE id = $1 AND version = $2
|
||||||
RETURNING id, node_type, name, parent_id, level,
|
RETURNING id, node_type, name, parent_id, level,
|
||||||
COALESCE(to_json(image_ids)::text, '[]'), sort_num, status, version`,
|
COALESCE(to_json(image_ids)::text, '[]'), sort_num, status, campaign_id, version`,
|
||||||
id, update.ExpectedVersion,
|
id, update.ExpectedVersion,
|
||||||
update.Name != nil, stringValue(update.Name),
|
update.Name != nil, stringValue(update.Name),
|
||||||
update.ImageIDs != nil, stringSliceOrEmpty(pointerStringSlice(update.ImageIDs)),
|
update.ImageIDs != nil, stringSliceOrEmpty(pointerStringSlice(update.ImageIDs)),
|
||||||
update.SortNum != nil, int32Value(update.SortNum),
|
update.SortNum != nil, int32Value(update.SortNum),
|
||||||
update.Status != nil, int32Value(update.Status),
|
update.Status != nil, int32Value(update.Status),
|
||||||
|
update.CampaignID != nil, campaignID,
|
||||||
)
|
)
|
||||||
category, err := scanCategory(row)
|
category, err := scanCategory(row)
|
||||||
if errors.Is(err, sql.ErrNoRows) {
|
if errors.Is(err, sql.ErrNoRows) {
|
||||||
@ -776,6 +787,17 @@ RETURNING id, node_type, name, parent_id, level,
|
|||||||
return category, nil
|
return category, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func nullableCategoryCampaignID(value *string) (sql.NullInt64, error) {
|
||||||
|
if value == nil || strings.TrimSpace(*value) == "" {
|
||||||
|
return sql.NullInt64{}, nil
|
||||||
|
}
|
||||||
|
id, err := parseID(strings.TrimSpace(*value), "campaign_id")
|
||||||
|
if err != nil {
|
||||||
|
return sql.NullInt64{}, err
|
||||||
|
}
|
||||||
|
return sql.NullInt64{Int64: id, Valid: true}, nil
|
||||||
|
}
|
||||||
|
|
||||||
func stringValue(value *string) string {
|
func stringValue(value *string) string {
|
||||||
if value == nil {
|
if value == nil {
|
||||||
return ""
|
return ""
|
||||||
@ -2645,6 +2667,7 @@ SELECT id,
|
|||||||
COALESCE(to_json(image_ids)::text, '[]'),
|
COALESCE(to_json(image_ids)::text, '[]'),
|
||||||
sort_num,
|
sort_num,
|
||||||
status,
|
status,
|
||||||
|
campaign_id,
|
||||||
version
|
version
|
||||||
FROM product.product_category
|
FROM product.product_category
|
||||||
WHERE node_type = 1
|
WHERE node_type = 1
|
||||||
@ -2686,6 +2709,7 @@ SELECT c.id,
|
|||||||
COALESCE(to_json(c.image_ids)::text, '[]'),
|
COALESCE(to_json(c.image_ids)::text, '[]'),
|
||||||
c.sort_num,
|
c.sort_num,
|
||||||
c.status,
|
c.status,
|
||||||
|
c.campaign_id,
|
||||||
c.version,
|
c.version,
|
||||||
COUNT(p.id) AS product_count
|
COUNT(p.id) AS product_count
|
||||||
FROM product.product_category AS c
|
FROM product.product_category AS c
|
||||||
@ -3046,6 +3070,10 @@ func insertCategoryNode(ctx context.Context, tx *sql.Tx, category CategoryRecord
|
|||||||
}
|
}
|
||||||
parentID = sql.NullInt64{Int64: parsedParentID, Valid: true}
|
parentID = sql.NullInt64{Int64: parsedParentID, Valid: true}
|
||||||
}
|
}
|
||||||
|
campaignID, err := nullableCategoryCampaignID(&category.CampaignID)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
row := tx.QueryRowContext(ctx, `
|
row := tx.QueryRowContext(ctx, `
|
||||||
INSERT INTO product.product_category (
|
INSERT INTO product.product_category (
|
||||||
@ -3058,9 +3086,10 @@ INSERT INTO product.product_category (
|
|||||||
level,
|
level,
|
||||||
image_ids,
|
image_ids,
|
||||||
sort_num,
|
sort_num,
|
||||||
status
|
status,
|
||||||
|
campaign_id
|
||||||
)
|
)
|
||||||
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)
|
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11)
|
||||||
RETURNING id,
|
RETURNING id,
|
||||||
node_type,
|
node_type,
|
||||||
name,
|
name,
|
||||||
@ -3069,8 +3098,9 @@ RETURNING id,
|
|||||||
COALESCE(to_json(image_ids)::text, '[]'),
|
COALESCE(to_json(image_ids)::text, '[]'),
|
||||||
sort_num,
|
sort_num,
|
||||||
status,
|
status,
|
||||||
|
campaign_id,
|
||||||
version
|
version
|
||||||
`, id, scope.TenantID, scope.StoreID, int16(category.NodeType), category.Name, parentID, int16(category.Level), stringSliceOrEmpty(category.ImageIDs), category.SortNum, int16(category.Status))
|
`, id, scope.TenantID, scope.StoreID, int16(category.NodeType), category.Name, parentID, int16(category.Level), stringSliceOrEmpty(category.ImageIDs), category.SortNum, int16(category.Status), campaignID)
|
||||||
|
|
||||||
createdCategory, err := scanCategory(row)
|
createdCategory, err := scanCategory(row)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -3469,9 +3499,10 @@ func scanGroup(scanner rowScanner) (*CategoryRecord, error) {
|
|||||||
nodeType int16
|
nodeType int16
|
||||||
level int16
|
level int16
|
||||||
status int16
|
status int16
|
||||||
|
campaignID sql.NullInt64
|
||||||
)
|
)
|
||||||
if err := scanner.Scan(
|
if err := scanner.Scan(
|
||||||
&id, &nodeType, &rec.Name, &parentID, &level, &imageIDsJSON, &rec.SortNum, &status, &rec.Version, &rec.ProductCount,
|
&id, &nodeType, &rec.Name, &parentID, &level, &imageIDsJSON, &rec.SortNum, &status, &campaignID, &rec.Version, &rec.ProductCount,
|
||||||
); err != nil {
|
); err != nil {
|
||||||
return nil, fmt.Errorf("scan group: %w", err)
|
return nil, fmt.Errorf("scan group: %w", err)
|
||||||
}
|
}
|
||||||
@ -3488,6 +3519,9 @@ func scanGroup(scanner rowScanner) (*CategoryRecord, error) {
|
|||||||
}
|
}
|
||||||
rec.ImageIDs = imageIDs
|
rec.ImageIDs = imageIDs
|
||||||
rec.Status = int32(status)
|
rec.Status = int32(status)
|
||||||
|
if campaignID.Valid {
|
||||||
|
rec.CampaignID = formatID(campaignID.Int64)
|
||||||
|
}
|
||||||
return &rec, nil
|
return &rec, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -3500,9 +3534,10 @@ func scanCategory(scanner rowScanner) (*CategoryRecord, error) {
|
|||||||
nodeType int16
|
nodeType int16
|
||||||
level int16
|
level int16
|
||||||
status int16
|
status int16
|
||||||
|
campaignID sql.NullInt64
|
||||||
)
|
)
|
||||||
if err := scanner.Scan(
|
if err := scanner.Scan(
|
||||||
&id, &nodeType, &rec.Name, &parentID, &level, &imageIDsJSON, &rec.SortNum, &status, &rec.Version,
|
&id, &nodeType, &rec.Name, &parentID, &level, &imageIDsJSON, &rec.SortNum, &status, &campaignID, &rec.Version,
|
||||||
); err != nil {
|
); err != nil {
|
||||||
return nil, fmt.Errorf("scan category: %w", err)
|
return nil, fmt.Errorf("scan category: %w", err)
|
||||||
}
|
}
|
||||||
@ -3519,6 +3554,9 @@ func scanCategory(scanner rowScanner) (*CategoryRecord, error) {
|
|||||||
}
|
}
|
||||||
rec.ImageIDs = imageIDs
|
rec.ImageIDs = imageIDs
|
||||||
rec.Status = int32(status)
|
rec.Status = int32(status)
|
||||||
|
if campaignID.Valid {
|
||||||
|
rec.CampaignID = formatID(campaignID.Int64)
|
||||||
|
}
|
||||||
return &rec, nil
|
return &rec, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -1166,12 +1166,13 @@ func categoryTreeFromCreateRequest(req *productv1.CreateCategoryTreeRequest) (re
|
|||||||
}
|
}
|
||||||
|
|
||||||
parent := repository.CategoryRecord{
|
parent := repository.CategoryRecord{
|
||||||
NodeType: categoryNodeType,
|
NodeType: categoryNodeType,
|
||||||
Name: name,
|
Name: name,
|
||||||
Level: 1,
|
Level: 1,
|
||||||
ImageIDs: imageIDs,
|
ImageIDs: imageIDs,
|
||||||
SortNum: req.GetSortNum(),
|
SortNum: req.GetSortNum(),
|
||||||
Status: statusValue,
|
Status: statusValue,
|
||||||
|
CampaignID: req.GetCampaignId(),
|
||||||
}
|
}
|
||||||
|
|
||||||
children := make([]repository.CategoryRecord, 0, len(req.GetChildren()))
|
children := make([]repository.CategoryRecord, 0, len(req.GetChildren()))
|
||||||
@ -1199,12 +1200,13 @@ func categoryTreeFromCreateRequest(req *productv1.CreateCategoryTreeRequest) (re
|
|||||||
}
|
}
|
||||||
|
|
||||||
children = append(children, repository.CategoryRecord{
|
children = append(children, repository.CategoryRecord{
|
||||||
NodeType: categoryNodeType,
|
NodeType: categoryNodeType,
|
||||||
Name: childName,
|
Name: childName,
|
||||||
Level: 2,
|
Level: 2,
|
||||||
ImageIDs: childImageIDs,
|
ImageIDs: childImageIDs,
|
||||||
SortNum: child.GetSortNum(),
|
SortNum: child.GetSortNum(),
|
||||||
Status: childStatus,
|
Status: childStatus,
|
||||||
|
CampaignID: child.GetCampaignId(),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1298,11 +1300,12 @@ func newChildCategoryFromProto(child *productv1.CreateChildCategory) (repository
|
|||||||
return repository.CategoryRecord{}, err
|
return repository.CategoryRecord{}, err
|
||||||
}
|
}
|
||||||
return repository.CategoryRecord{
|
return repository.CategoryRecord{
|
||||||
NodeType: categoryNodeType,
|
NodeType: categoryNodeType,
|
||||||
Name: name,
|
Name: name,
|
||||||
ImageIDs: imageIDs,
|
ImageIDs: imageIDs,
|
||||||
SortNum: child.GetSortNum(),
|
SortNum: child.GetSortNum(),
|
||||||
Status: statusValue,
|
Status: statusValue,
|
||||||
|
CampaignID: child.GetCampaignId(),
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1338,6 +1341,14 @@ func categoryFieldUpdateFromProto(id string, patch *productv1.CategoryFieldPatch
|
|||||||
return nil, fmt.Errorf("%s.status must be 0 or 1", field)
|
return nil, fmt.Errorf("%s.status must be 0 or 1", field)
|
||||||
}
|
}
|
||||||
update.Status = &value
|
update.Status = &value
|
||||||
|
case "campaign_id":
|
||||||
|
value := strings.TrimSpace(patch.GetCampaignId())
|
||||||
|
if value != "" {
|
||||||
|
if _, err := normalizeCategoryID(value, field+".campaign_id"); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
update.CampaignID = &value
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return update, nil
|
return update, nil
|
||||||
@ -1347,7 +1358,7 @@ func categoryUpdatePaths(mask *fieldmaskpb.FieldMask) ([]string, error) {
|
|||||||
if mask == nil || len(mask.GetPaths()) == 0 {
|
if mask == nil || len(mask.GetPaths()) == 0 {
|
||||||
return nil, errors.New("update_mask is required")
|
return nil, errors.New("update_mask is required")
|
||||||
}
|
}
|
||||||
allowed := map[string]struct{}{"name": {}, "image_ids": {}, "sort_num": {}, "status": {}}
|
allowed := map[string]struct{}{"name": {}, "image_ids": {}, "sort_num": {}, "status": {}, "campaign_id": {}}
|
||||||
paths := make([]string, 0, len(mask.GetPaths()))
|
paths := make([]string, 0, len(mask.GetPaths()))
|
||||||
seen := make(map[string]struct{}, len(mask.GetPaths()))
|
seen := make(map[string]struct{}, len(mask.GetPaths()))
|
||||||
for _, path := range mask.GetPaths() {
|
for _, path := range mask.GetPaths() {
|
||||||
@ -1387,12 +1398,13 @@ func groupFromCreateRequest(req *productv1.CreateGroupRequest) (repository.Categ
|
|||||||
}
|
}
|
||||||
|
|
||||||
return repository.CategoryRecord{
|
return repository.CategoryRecord{
|
||||||
NodeType: groupNodeType,
|
NodeType: groupNodeType,
|
||||||
Name: name,
|
Name: name,
|
||||||
Level: 1,
|
Level: 1,
|
||||||
ImageIDs: imageIDs,
|
ImageIDs: imageIDs,
|
||||||
SortNum: req.GetSortNum(),
|
SortNum: req.GetSortNum(),
|
||||||
Status: statusValue,
|
Status: statusValue,
|
||||||
|
CampaignID: req.GetCampaignId(),
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -2161,6 +2173,7 @@ func toProtoCategoryNode(category *repository.CategoryRecord) *productv1.Categor
|
|||||||
ImageJson: categoryImageIDsJSON(category.ImageIDs),
|
ImageJson: categoryImageIDsJSON(category.ImageIDs),
|
||||||
SortNum: category.SortNum,
|
SortNum: category.SortNum,
|
||||||
Status: category.Status,
|
Status: category.Status,
|
||||||
|
CampaignId: category.CampaignID,
|
||||||
Version: category.Version,
|
Version: category.Version,
|
||||||
Children: []*productv1.CategoryNode{},
|
Children: []*productv1.CategoryNode{},
|
||||||
}
|
}
|
||||||
@ -2180,6 +2193,7 @@ func toProtoGroupNode(group *repository.CategoryRecord) *productv1.GroupNode {
|
|||||||
SortNum: group.SortNum,
|
SortNum: group.SortNum,
|
||||||
Status: group.Status,
|
Status: group.Status,
|
||||||
ProductCount: group.ProductCount,
|
ProductCount: group.ProductCount,
|
||||||
|
CampaignId: group.CampaignID,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user