Merge remote-tracking branch 'origin/master'
This commit is contained in:
@@ -582,25 +582,38 @@ func productSeriesApplyVariant(ctx context.Context, pg *sql.DB, v productSeriesA
|
|||||||
}
|
}
|
||||||
if fallbackID > 0 {
|
if fallbackID > 0 {
|
||||||
if !productSeriesFallbackOverrideExisting() {
|
if !productSeriesFallbackOverrideExisting() {
|
||||||
// Only apply fallback when the variant has no assignments yet.
|
// By default we don't override existing assignments. However, if the existing assignment(s)
|
||||||
var exists int
|
// are no longer compatible with current stock (e.g., stock sold out), keeping them causes
|
||||||
checkErr := pg.QueryRowContext(ctx, `
|
// stale/wrong series to stick forever (already_exists). In that case, we should replace
|
||||||
SELECT 1
|
// them with fallback=1.
|
||||||
FROM zbggseri
|
existingIDs, err := productSeriesLoadExistingSeriesIDs(ctx, pg, mmitemID, dim1ID, dim3ID)
|
||||||
WHERE mmitem_id=$1
|
if err != nil {
|
||||||
AND dim1=$2
|
return 0, 1, err
|
||||||
AND (($3::bigint IS NULL AND dim3 IS NULL) OR dim3=$3::bigint)
|
|
||||||
LIMIT 1
|
|
||||||
`, mmitemID, dim1ID, nullableInt64ForAuto(dim3ID)).Scan(&exists)
|
|
||||||
if checkErr == nil {
|
|
||||||
if productSeriesFallbackLogEnabled() {
|
|
||||||
log.Printf("[ProductSeriesFallback] already_exists product=%s color=%s dim3=%s fallback=%s(%d)", strings.TrimSpace(v.ProductCode), strings.TrimSpace(v.ColorCode), strings.TrimSpace(v.Dim3Code), strings.TrimSpace(fallbackCode), fallbackID)
|
|
||||||
}
|
|
||||||
// keep existing manual/previous assignment; nothing to do
|
|
||||||
return 0, 0, nil
|
|
||||||
}
|
}
|
||||||
if checkErr != nil && checkErr != sql.ErrNoRows {
|
if len(existingIDs) > 0 {
|
||||||
return 0, 1, checkErr
|
anyStillValid := false
|
||||||
|
for _, sid := range existingIDs {
|
||||||
|
// If we can't find the rule definition in memory, assume it's a manual/unknown mapping and keep it.
|
||||||
|
rule, ok := productSeriesFindRuleByID(rules, sid)
|
||||||
|
if !ok {
|
||||||
|
anyStillValid = true
|
||||||
|
break
|
||||||
|
}
|
||||||
|
if productSeriesCanConsume(v.SizeQty, rule) {
|
||||||
|
anyStillValid = true
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if anyStillValid {
|
||||||
|
if productSeriesFallbackLogEnabled() {
|
||||||
|
log.Printf("[ProductSeriesFallback] already_exists product=%s color=%s dim3=%s fallback=%s(%d)", strings.TrimSpace(v.ProductCode), strings.TrimSpace(v.ColorCode), strings.TrimSpace(v.Dim3Code), strings.TrimSpace(fallbackCode), fallbackID)
|
||||||
|
}
|
||||||
|
// keep existing manual/previous assignment; nothing to do
|
||||||
|
return 0, 0, nil
|
||||||
|
}
|
||||||
|
if productSeriesFallbackLogEnabled() {
|
||||||
|
log.Printf("[ProductSeriesFallback] existing_invalid_apply product=%s color=%s dim3=%s fallback=%s(%d)", strings.TrimSpace(v.ProductCode), strings.TrimSpace(v.ColorCode), strings.TrimSpace(v.Dim3Code), strings.TrimSpace(fallbackCode), fallbackID)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if productSeriesFallbackLogEnabled() {
|
if productSeriesFallbackLogEnabled() {
|
||||||
@@ -650,6 +663,41 @@ VALUES ($1, $2, $3, $4)
|
|||||||
return len(selected), 0, nil
|
return len(selected), 0, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func productSeriesFindRuleByID(rules []productSeriesAutoRule, seriesID int64) (productSeriesAutoRule, bool) {
|
||||||
|
for _, r := range rules {
|
||||||
|
if r.SeriesID == seriesID {
|
||||||
|
return r, true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return productSeriesAutoRule{}, false
|
||||||
|
}
|
||||||
|
|
||||||
|
func productSeriesLoadExistingSeriesIDs(ctx context.Context, pg *sql.DB, mmitemID int64, dim1ID int64, dim3ID sql.NullInt64) ([]int64, error) {
|
||||||
|
rows, err := pg.QueryContext(ctx, `
|
||||||
|
SELECT seri_id
|
||||||
|
FROM zbggseri
|
||||||
|
WHERE mmitem_id=$1
|
||||||
|
AND dim1=$2
|
||||||
|
AND (($3::bigint IS NULL AND dim3 IS NULL) OR dim3=$3::bigint)
|
||||||
|
ORDER BY seri_id
|
||||||
|
`, mmitemID, dim1ID, nullableInt64ForAuto(dim3ID))
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer rows.Close()
|
||||||
|
out := []int64{}
|
||||||
|
for rows.Next() {
|
||||||
|
var id int64
|
||||||
|
if err := rows.Scan(&id); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if id > 0 {
|
||||||
|
out = append(out, id)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return out, rows.Err()
|
||||||
|
}
|
||||||
|
|
||||||
func productSeriesLoadMSSQLStockVariants(ctx context.Context) (map[string]productSeriesAutoVariant, int, error) {
|
func productSeriesLoadMSSQLStockVariants(ctx context.Context) (map[string]productSeriesAutoVariant, int, error) {
|
||||||
rows, err := db.MssqlDB.QueryContext(ctx, queries.GetProductSeriesStockRowsQuery,
|
rows, err := db.MssqlDB.QueryContext(ctx, queries.GetProductSeriesStockRowsQuery,
|
||||||
"", "", "", "", "", "", "", "", "", "0", "",
|
"", "", "", "", "", "", "", "", "", "0", "",
|
||||||
|
|||||||
Reference in New Issue
Block a user