Merge remote-tracking branch 'origin/master'
This commit is contained in:
@@ -326,6 +326,50 @@ RETURNING id
|
||||
return newID, true, nil
|
||||
}
|
||||
|
||||
func FindActivePricingParameterByScope(ctx context.Context, tx *sql.Tx, row pricingParameterRow) (int64, error) {
|
||||
row.AskiliYan = strings.TrimSpace(row.AskiliYan)
|
||||
row.Kategori = strings.TrimSpace(row.Kategori)
|
||||
row.UrunIlkGrubu = strings.TrimSpace(row.UrunIlkGrubu)
|
||||
row.UrunAnaGrubu = strings.TrimSpace(row.UrunAnaGrubu)
|
||||
row.UrunAltGrubu = strings.TrimSpace(row.UrunAltGrubu)
|
||||
row.Icerik = strings.TrimSpace(row.Icerik)
|
||||
row.Marka = strings.TrimSpace(row.Marka)
|
||||
row.BrandCode = strings.TrimSpace(row.BrandCode)
|
||||
row.BrandGroupSec = strings.TrimSpace(row.BrandGroupSec)
|
||||
|
||||
var id int64
|
||||
err := tx.QueryRowContext(ctx, `
|
||||
SELECT id
|
||||
FROM mk_urunpricingprmtr
|
||||
WHERE askili_yan=$1
|
||||
AND kategori=$2
|
||||
AND urun_ilk_grubu=$3
|
||||
AND urun_ana_grubu=$4
|
||||
AND urun_alt_grubu=$5
|
||||
AND icerik=$6
|
||||
AND marka=$7
|
||||
AND brand_code=$8
|
||||
AND brand_group_sec=$9
|
||||
AND is_active=TRUE
|
||||
ORDER BY last_seen_at DESC, id DESC
|
||||
LIMIT 1
|
||||
`,
|
||||
row.AskiliYan,
|
||||
row.Kategori,
|
||||
row.UrunIlkGrubu,
|
||||
row.UrunAnaGrubu,
|
||||
row.UrunAltGrubu,
|
||||
row.Icerik,
|
||||
row.Marka,
|
||||
row.BrandCode,
|
||||
row.BrandGroupSec,
|
||||
).Scan(&id)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return id, nil
|
||||
}
|
||||
|
||||
func SyncPricingParametersFromMSSQL(ctx context.Context, mssql *sql.DB, pg *sql.DB) (PricingParameterSyncResult, error) {
|
||||
out := PricingParameterSyncResult{}
|
||||
startedAt := time.Now()
|
||||
|
||||
@@ -70,6 +70,8 @@ type PricingRuleImportPayload struct {
|
||||
type PricingRuleImportResult struct {
|
||||
Success bool `json:"success"`
|
||||
Processed int `json:"processed"`
|
||||
Matched int `json:"matched"`
|
||||
Skipped int `json:"skipped"`
|
||||
Updated int `json:"updated"`
|
||||
ActivatedScopeCount int `json:"activated_scope_count"`
|
||||
ErrorCount int `json:"error_count"`
|
||||
@@ -162,14 +164,15 @@ func ImportPricingRulesHandler(pg *sql.DB) http.HandlerFunc {
|
||||
defer tx.Rollback()
|
||||
|
||||
updated := 0
|
||||
activatedScopeCount := 0
|
||||
matched := 0
|
||||
skipped := 0
|
||||
for _, raw := range payload.Items {
|
||||
if raw.TryWholesaleStep < 0 || raw.TryRetailStep < 0 || raw.UsdWholesaleStep < 0 || raw.UsdRetailStep < 0 || raw.EurWholesaleStep < 0 || raw.EurRetailStep < 0 {
|
||||
http.Error(w, "invalid rounding step", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
pricingParameterID, activated, err := queries.EnsureActivePricingParameterByScope(ctx, tx, queries.PricingParameterRowForImport(
|
||||
pricingParameterID, err := queries.FindActivePricingParameterByScope(ctx, tx, queries.PricingParameterRowForImport(
|
||||
raw.AskiliYan,
|
||||
raw.Kategori,
|
||||
raw.UrunIlkGrubu,
|
||||
@@ -180,13 +183,15 @@ func ImportPricingRulesHandler(pg *sql.DB) http.HandlerFunc {
|
||||
raw.BrandCode,
|
||||
raw.BrandGroupSec,
|
||||
))
|
||||
if err == sql.ErrNoRows {
|
||||
skipped++
|
||||
continue
|
||||
}
|
||||
if err != nil {
|
||||
http.Error(w, "pricing parameter resolve error", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
if activated {
|
||||
activatedScopeCount++
|
||||
}
|
||||
matched++
|
||||
|
||||
_, err = queries.UpsertPricingRule(ctx, tx, queries.PricingRuleSaveItem{
|
||||
PricingParameterID: pricingParameterID,
|
||||
@@ -233,8 +238,10 @@ func ImportPricingRulesHandler(pg *sql.DB) http.HandlerFunc {
|
||||
_ = json.NewEncoder(w).Encode(PricingRuleImportResult{
|
||||
Success: true,
|
||||
Processed: len(payload.Items),
|
||||
Matched: matched,
|
||||
Skipped: skipped,
|
||||
Updated: updated,
|
||||
ActivatedScopeCount: activatedScopeCount,
|
||||
ActivatedScopeCount: 0,
|
||||
ErrorCount: 0,
|
||||
})
|
||||
}
|
||||
@@ -545,7 +552,7 @@ func buildPricingRuleCSV(rows []queries.PricingParameterRuleRow) string {
|
||||
row.UrunAltGrubu,
|
||||
row.Icerik,
|
||||
row.Marka,
|
||||
row.BrandCode,
|
||||
csvExcelTextValue(row.BrandCode),
|
||||
row.BrandGroupSec,
|
||||
fmt.Sprintf("%.2f", pricingRuleNumericValue(row, "try_wholesale_step")),
|
||||
fmt.Sprintf("%.2f", pricingRuleNumericValue(row, "try_retail_step")),
|
||||
@@ -595,6 +602,14 @@ func csvEscapeValue(value string) string {
|
||||
return text
|
||||
}
|
||||
|
||||
func csvExcelTextValue(value string) string {
|
||||
text := strings.TrimSpace(value)
|
||||
if text == "" {
|
||||
return ""
|
||||
}
|
||||
return `="` + strings.ReplaceAll(text, `"`, `""`) + `"`
|
||||
}
|
||||
|
||||
func splitCSV(raw string) []string {
|
||||
raw = strings.TrimSpace(raw)
|
||||
if raw == "" {
|
||||
|
||||
Reference in New Issue
Block a user