Merge remote-tracking branch 'origin/master'
This commit is contained in:
@@ -22,6 +22,7 @@ type BrandGroupOption struct {
|
||||
Code string `json:"code"`
|
||||
Title string `json:"title"`
|
||||
Description string `json:"description"`
|
||||
AnchorMode string `json:"anchor_mode"`
|
||||
}
|
||||
|
||||
func EnsureBrandClassificationTables(pg *sql.DB) error {
|
||||
@@ -41,10 +42,15 @@ CREATE TABLE IF NOT EXISTS mk_brandgrp (
|
||||
code TEXT NOT NULL UNIQUE,
|
||||
title TEXT NOT NULL,
|
||||
description TEXT NOT NULL DEFAULT '',
|
||||
anchor_mode TEXT NOT NULL DEFAULT 'USD',
|
||||
sort_order SMALLINT NOT NULL DEFAULT 0,
|
||||
created_at TIMESTAMPTZ NOT NULL DEFAULT now()
|
||||
)`,
|
||||
`ALTER TABLE mk_brandgrp ADD COLUMN IF NOT EXISTS description TEXT NOT NULL DEFAULT ''`,
|
||||
`ALTER TABLE mk_brandgrp ADD COLUMN IF NOT EXISTS anchor_mode TEXT NOT NULL DEFAULT 'USD'`,
|
||||
`UPDATE mk_brandgrp SET anchor_mode='USD' WHERE COALESCE(NULLIF(BTRIM(anchor_mode), ''), '') = ''`,
|
||||
`ALTER TABLE mk_brandgrp DROP CONSTRAINT IF EXISTS ck_mk_brandgrp_anchor_mode`,
|
||||
`ALTER TABLE mk_brandgrp ADD CONSTRAINT ck_mk_brandgrp_anchor_mode CHECK (anchor_mode IN ('TRY','USD'))`,
|
||||
`
|
||||
INSERT INTO mk_brandgrp (id, code, title, description, sort_order)
|
||||
VALUES
|
||||
@@ -74,7 +80,7 @@ CREATE TABLE IF NOT EXISTS mk_brandgrpmatch (
|
||||
}
|
||||
|
||||
func ListBrandGroups(ctx context.Context, pg *sql.DB) ([]BrandGroupOption, error) {
|
||||
rows, err := pg.QueryContext(ctx, `SELECT id, code, title, description FROM mk_brandgrp ORDER BY sort_order, id`)
|
||||
rows, err := pg.QueryContext(ctx, `SELECT id, code, title, description, anchor_mode FROM mk_brandgrp ORDER BY sort_order, id`)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -82,17 +88,57 @@ func ListBrandGroups(ctx context.Context, pg *sql.DB) ([]BrandGroupOption, error
|
||||
out := make([]BrandGroupOption, 0, 8)
|
||||
for rows.Next() {
|
||||
var o BrandGroupOption
|
||||
if err := rows.Scan(&o.ID, &o.Code, &o.Title, &o.Description); err != nil {
|
||||
if err := rows.Scan(&o.ID, &o.Code, &o.Title, &o.Description, &o.AnchorMode); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
o.Code = strings.TrimSpace(o.Code)
|
||||
o.Title = strings.TrimSpace(o.Title)
|
||||
o.Description = strings.TrimSpace(o.Description)
|
||||
o.AnchorMode = strings.ToUpper(strings.TrimSpace(o.AnchorMode))
|
||||
if o.AnchorMode == "" {
|
||||
o.AnchorMode = "USD"
|
||||
}
|
||||
out = append(out, o)
|
||||
}
|
||||
return out, rows.Err()
|
||||
}
|
||||
|
||||
func SetBrandGroupAnchorMode(ctx context.Context, tx *sql.Tx, grpID int, anchorMode string) error {
|
||||
anchorMode = strings.ToUpper(strings.TrimSpace(anchorMode))
|
||||
if anchorMode == "" {
|
||||
anchorMode = "USD"
|
||||
}
|
||||
_, err := tx.ExecContext(ctx, `
|
||||
UPDATE mk_brandgrp
|
||||
SET anchor_mode=$2
|
||||
WHERE id=$1
|
||||
`, grpID, anchorMode)
|
||||
return err
|
||||
}
|
||||
|
||||
func SyncPricingRuleAnchorModesByGroup(ctx context.Context, tx *sql.Tx, grpID int, anchorMode string) error {
|
||||
anchorMode = strings.ToUpper(strings.TrimSpace(anchorMode))
|
||||
if anchorMode == "" {
|
||||
anchorMode = "USD"
|
||||
}
|
||||
_, err := tx.ExecContext(ctx, `
|
||||
UPDATE mk_pricing_rule r
|
||||
SET anchor_mode=$2,
|
||||
updated_at=now()
|
||||
WHERE EXISTS (
|
||||
SELECT 1
|
||||
FROM mk_brandgrp g
|
||||
JOIN LATERAL unnest(r.brand_group) bg(value) ON TRUE
|
||||
WHERE g.id=$1
|
||||
AND (
|
||||
UPPER(BTRIM(bg.value)) = UPPER(BTRIM(g.code))
|
||||
OR UPPER(BTRIM(bg.value)) = UPPER(BTRIM(g.title))
|
||||
)
|
||||
)
|
||||
`, grpID, anchorMode)
|
||||
return err
|
||||
}
|
||||
|
||||
func ListBrandsWithGroups(ctx context.Context, pg *sql.DB, q string, limit int) ([]BrandRow, error) {
|
||||
if limit <= 0 {
|
||||
limit = 5000
|
||||
|
||||
Reference in New Issue
Block a user