Merge remote-tracking branch 'origin/master'
This commit is contained in:
@@ -180,16 +180,33 @@ func OrderProductionValidateRoute(mssql *sql.DB) http.Handler {
|
||||
log.Printf("[OrderProductionValidateRoute] rid=%s orderHeaderID=%s payload lineCount=%d insertMissing=%t cdItemCount=%d attributeCount=%d",
|
||||
rid, id, len(payload.Lines), payload.InsertMissing, len(payload.CdItems), len(payload.ProductAttributes))
|
||||
|
||||
newLines, existingLines := splitLinesByCdItemDraft(payload.Lines, payload.CdItems)
|
||||
newCodes := uniqueCodesFromLines(newLines)
|
||||
existingCodes := uniqueCodesFromLines(existingLines)
|
||||
missing := make([]models.OrderProductionMissingVariant, 0)
|
||||
targets := make([]models.OrderProductionMissingVariant, 0)
|
||||
stepStart := time.Now()
|
||||
missing, err := buildMissingVariants(mssql, id, payload.Lines)
|
||||
if err != nil {
|
||||
log.Printf("[OrderProductionValidateRoute] rid=%s orderHeaderID=%s step=build_missing failed duration_ms=%d err=%v",
|
||||
rid, id, time.Since(stepStart).Milliseconds(), err)
|
||||
writeDBError(w, http.StatusInternalServerError, "validate_missing_variants", id, "", len(payload.Lines), err)
|
||||
return
|
||||
if len(newLines) > 0 {
|
||||
err := runWithTransientMSSQLRetry("validate_build_targets_missing", 3, 500*time.Millisecond, func() error {
|
||||
var stepErr error
|
||||
targets, stepErr = buildTargetVariants(mssql, id, newLines)
|
||||
if stepErr != nil {
|
||||
return stepErr
|
||||
}
|
||||
missing, stepErr = buildMissingVariantsFromTargets(mssql, id, targets)
|
||||
return stepErr
|
||||
})
|
||||
if err != nil {
|
||||
log.Printf("[OrderProductionValidateRoute] rid=%s orderHeaderID=%s step=build_missing failed duration_ms=%d err=%v",
|
||||
rid, id, time.Since(stepStart).Milliseconds(), err)
|
||||
writeDBError(w, http.StatusInternalServerError, "validate_missing_variants", id, "", len(newLines), err)
|
||||
return
|
||||
}
|
||||
}
|
||||
log.Printf("[OrderProductionValidateRoute] rid=%s orderHeaderID=%s lineCount=%d missingCount=%d build_missing_ms=%d total_ms=%d",
|
||||
rid, id, len(payload.Lines), len(missing), time.Since(stepStart).Milliseconds(), time.Since(start).Milliseconds())
|
||||
log.Printf("[OrderProductionValidateRoute] rid=%s orderHeaderID=%s lineCount=%d newLineCount=%d existingLineCount=%d targetVariantCount=%d missingCount=%d build_missing_ms=%d total_ms=%d",
|
||||
rid, id, len(payload.Lines), len(newLines), len(existingLines), len(targets), len(missing), time.Since(stepStart).Milliseconds(), time.Since(start).Milliseconds())
|
||||
log.Printf("[OrderProductionValidateRoute] rid=%s orderHeaderID=%s scope newCodes=%v existingCodes=%v",
|
||||
rid, id, newCodes, existingCodes)
|
||||
|
||||
resp := map[string]any{
|
||||
"missingCount": len(missing),
|
||||
@@ -230,17 +247,57 @@ func OrderProductionApplyRoute(mssql *sql.DB, ml *mailer.GraphMailer) http.Handl
|
||||
}
|
||||
log.Printf("[OrderProductionApplyRoute] rid=%s orderHeaderID=%s payload lineCount=%d insertMissing=%t cdItemCount=%d attributeCount=%d",
|
||||
rid, id, len(payload.Lines), payload.InsertMissing, len(payload.CdItems), len(payload.ProductAttributes))
|
||||
|
||||
stepMissingStart := time.Now()
|
||||
missing, err := buildMissingVariants(mssql, id, payload.Lines)
|
||||
if err != nil {
|
||||
log.Printf("[OrderProductionApplyRoute] rid=%s orderHeaderID=%s step=build_missing failed duration_ms=%d err=%v",
|
||||
rid, id, time.Since(stepMissingStart).Milliseconds(), err)
|
||||
writeDBError(w, http.StatusInternalServerError, "apply_validate_missing_variants", id, "", len(payload.Lines), err)
|
||||
return
|
||||
if len(payload.Lines) > 0 {
|
||||
limit := 5
|
||||
if len(payload.Lines) < limit {
|
||||
limit = len(payload.Lines)
|
||||
}
|
||||
samples := make([]string, 0, limit)
|
||||
for i := 0; i < limit; i++ {
|
||||
ln := payload.Lines[i]
|
||||
dim1 := ""
|
||||
if ln.ItemDim1Code != nil {
|
||||
dim1 = strings.TrimSpace(*ln.ItemDim1Code)
|
||||
}
|
||||
samples = append(samples, fmt.Sprintf(
|
||||
"lineID=%s newItem=%s newColor=%s newDim1=%s newDim2=%s",
|
||||
strings.TrimSpace(ln.OrderLineID),
|
||||
strings.ToUpper(strings.TrimSpace(ln.NewItemCode)),
|
||||
strings.ToUpper(strings.TrimSpace(ln.NewColor)),
|
||||
strings.ToUpper(strings.TrimSpace(dim1)),
|
||||
strings.ToUpper(strings.TrimSpace(ln.NewDim2)),
|
||||
))
|
||||
}
|
||||
log.Printf("[OrderProductionApplyRoute] rid=%s orderHeaderID=%s payload lineSamples=%v", rid, id, samples)
|
||||
}
|
||||
log.Printf("[OrderProductionApplyRoute] rid=%s orderHeaderID=%s lineCount=%d missingCount=%d build_missing_ms=%d",
|
||||
rid, id, len(payload.Lines), len(missing), time.Since(stepMissingStart).Milliseconds())
|
||||
|
||||
newLines, existingLines := splitLinesByCdItemDraft(payload.Lines, payload.CdItems)
|
||||
newCodes := uniqueCodesFromLines(newLines)
|
||||
existingCodes := uniqueCodesFromLines(existingLines)
|
||||
stepMissingStart := time.Now()
|
||||
missing := make([]models.OrderProductionMissingVariant, 0)
|
||||
barcodeTargets := make([]models.OrderProductionMissingVariant, 0)
|
||||
if len(newLines) > 0 {
|
||||
err := runWithTransientMSSQLRetry("apply_build_targets_missing", 3, 500*time.Millisecond, func() error {
|
||||
var stepErr error
|
||||
barcodeTargets, stepErr = buildTargetVariants(mssql, id, newLines)
|
||||
if stepErr != nil {
|
||||
return stepErr
|
||||
}
|
||||
missing, stepErr = buildMissingVariantsFromTargets(mssql, id, barcodeTargets)
|
||||
return stepErr
|
||||
})
|
||||
if err != nil {
|
||||
log.Printf("[OrderProductionApplyRoute] rid=%s orderHeaderID=%s step=build_missing failed duration_ms=%d err=%v",
|
||||
rid, id, time.Since(stepMissingStart).Milliseconds(), err)
|
||||
writeDBError(w, http.StatusInternalServerError, "apply_validate_missing_variants", id, "", len(newLines), err)
|
||||
return
|
||||
}
|
||||
}
|
||||
log.Printf("[OrderProductionApplyRoute] rid=%s orderHeaderID=%s lineCount=%d newLineCount=%d existingLineCount=%d targetVariantCount=%d missingCount=%d build_missing_ms=%d",
|
||||
rid, id, len(payload.Lines), len(newLines), len(existingLines), len(barcodeTargets), len(missing), time.Since(stepMissingStart).Milliseconds())
|
||||
log.Printf("[OrderProductionApplyRoute] rid=%s orderHeaderID=%s scope newCodes=%v existingCodes=%v",
|
||||
rid, id, newCodes, existingCodes)
|
||||
|
||||
if len(missing) > 0 && !payload.InsertMissing {
|
||||
log.Printf("[OrderProductionApplyRoute] rid=%s orderHeaderID=%s early_exit=missing_variants total_ms=%d",
|
||||
@@ -269,30 +326,83 @@ func OrderProductionApplyRoute(mssql *sql.DB, ml *mailer.GraphMailer) http.Handl
|
||||
writeDBError(w, http.StatusInternalServerError, "begin_tx", id, username, len(payload.Lines), err)
|
||||
return
|
||||
}
|
||||
defer tx.Rollback()
|
||||
log.Printf("[OrderProductionApplyRoute] rid=%s orderHeaderID=%s step=begin_tx duration_ms=%d", rid, id, time.Since(stepBeginStart).Milliseconds())
|
||||
committed := false
|
||||
currentStep := "begin_tx"
|
||||
applyTxSettings := func(tx *sql.Tx) error {
|
||||
// XACT_ABORT OFF:
|
||||
// Barcode insert path intentionally tolerates duplicate-key errors (fallback/skip duplicate).
|
||||
// With XACT_ABORT ON, that expected error aborts the whole transaction and causes COMMIT 3902.
|
||||
_, execErr := tx.Exec(`SET XACT_ABORT OFF; SET LOCK_TIMEOUT 15000;`)
|
||||
return execErr
|
||||
}
|
||||
defer func() {
|
||||
if committed {
|
||||
return
|
||||
}
|
||||
rbStart := time.Now()
|
||||
if rbErr := tx.Rollback(); rbErr != nil && rbErr != sql.ErrTxDone {
|
||||
log.Printf("[OrderProductionApplyRoute] rid=%s orderHeaderID=%s rollback step=%s failed duration_ms=%d err=%v",
|
||||
rid, id, currentStep, time.Since(rbStart).Milliseconds(), rbErr)
|
||||
return
|
||||
}
|
||||
log.Printf("[OrderProductionApplyRoute] rid=%s orderHeaderID=%s rollback step=%s ok duration_ms=%d",
|
||||
rid, id, currentStep, time.Since(rbStart).Milliseconds())
|
||||
}()
|
||||
|
||||
stepTxSettingsStart := time.Now()
|
||||
if _, err := tx.Exec(`SET XACT_ABORT ON; SET LOCK_TIMEOUT 15000;`); err != nil {
|
||||
currentStep = "tx_settings"
|
||||
if err := applyTxSettings(tx); err != nil {
|
||||
writeDBError(w, http.StatusInternalServerError, "tx_settings", id, username, len(payload.Lines), err)
|
||||
return
|
||||
}
|
||||
log.Printf("[OrderProductionApplyRoute] rid=%s orderHeaderID=%s step=tx_settings duration_ms=%d", rid, id, time.Since(stepTxSettingsStart).Milliseconds())
|
||||
if err := ensureTxAlive(tx, "after_tx_settings"); err != nil {
|
||||
writeDBError(w, http.StatusInternalServerError, "tx_not_active_after_tx_settings", id, username, len(payload.Lines), err)
|
||||
return
|
||||
}
|
||||
|
||||
var inserted int64
|
||||
if payload.InsertMissing {
|
||||
if payload.InsertMissing && len(newLines) > 0 {
|
||||
currentStep = "insert_missing_variants"
|
||||
cdItemByCode := buildCdItemDraftMap(payload.CdItems)
|
||||
stepInsertMissingStart := time.Now()
|
||||
inserted, err = queries.InsertMissingVariantsTx(tx, missing, username, cdItemByCode)
|
||||
if err != nil && isTransientMSSQLNetworkErr(err) {
|
||||
log.Printf("[OrderProductionApplyRoute] rid=%s orderHeaderID=%s step=insert_missing transient_error retry=1 err=%v",
|
||||
rid, id, err)
|
||||
_ = tx.Rollback()
|
||||
tx, err = mssql.Begin()
|
||||
if err != nil {
|
||||
writeDBError(w, http.StatusInternalServerError, "begin_tx_retry_insert_missing", id, username, len(payload.Lines), err)
|
||||
return
|
||||
}
|
||||
currentStep = "tx_settings_retry_insert_missing"
|
||||
if err = applyTxSettings(tx); err != nil {
|
||||
writeDBError(w, http.StatusInternalServerError, "tx_settings_retry_insert_missing", id, username, len(payload.Lines), err)
|
||||
return
|
||||
}
|
||||
if err = ensureTxAlive(tx, "after_tx_settings_retry_insert_missing"); err != nil {
|
||||
writeDBError(w, http.StatusInternalServerError, "tx_not_active_after_tx_settings_retry_insert_missing", id, username, len(payload.Lines), err)
|
||||
return
|
||||
}
|
||||
currentStep = "insert_missing_variants_retry"
|
||||
inserted, err = queries.InsertMissingVariantsTx(tx, missing, username, cdItemByCode)
|
||||
}
|
||||
if err != nil {
|
||||
writeDBError(w, http.StatusInternalServerError, "insert_missing_variants", id, username, len(missing), err)
|
||||
return
|
||||
}
|
||||
if err := ensureTxAlive(tx, "after_insert_missing_variants"); err != nil {
|
||||
writeDBError(w, http.StatusInternalServerError, "tx_not_active_after_insert_missing_variants", id, username, len(missing), err)
|
||||
return
|
||||
}
|
||||
log.Printf("[OrderProductionApplyRoute] rid=%s orderHeaderID=%s step=insert_missing inserted=%d duration_ms=%d",
|
||||
rid, id, inserted, time.Since(stepInsertMissingStart).Milliseconds())
|
||||
}
|
||||
|
||||
stepValidateAttrStart := time.Now()
|
||||
currentStep = "validate_attributes"
|
||||
if err := validateProductAttributes(payload.ProductAttributes); err != nil {
|
||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
@@ -300,37 +410,8 @@ func OrderProductionApplyRoute(mssql *sql.DB, ml *mailer.GraphMailer) http.Handl
|
||||
log.Printf("[OrderProductionApplyRoute] rid=%s orderHeaderID=%s step=validate_attributes count=%d duration_ms=%d",
|
||||
rid, id, len(payload.ProductAttributes), time.Since(stepValidateAttrStart).Milliseconds())
|
||||
|
||||
stepUpdateHeaderStart := time.Now()
|
||||
if err := queries.UpdateOrderHeaderAverageDueDateTx(tx, id, payload.HeaderAverageDueDate, username); err != nil {
|
||||
writeDBError(w, http.StatusInternalServerError, "update_order_header_average_due_date", id, username, 0, err)
|
||||
return
|
||||
}
|
||||
log.Printf("[OrderProductionApplyRoute] rid=%s orderHeaderID=%s step=update_header_average_due_date changed=%t duration_ms=%d",
|
||||
rid, id, payload.HeaderAverageDueDate != nil, time.Since(stepUpdateHeaderStart).Milliseconds())
|
||||
|
||||
stepUpdateLinesStart := time.Now()
|
||||
updated, err := queries.UpdateOrderLinesTx(tx, id, payload.Lines, username)
|
||||
if err != nil {
|
||||
log.Printf("[OrderProductionApplyRoute] rid=%s orderHeaderID=%s step=update_lines failed duration_ms=%d err=%v",
|
||||
rid, id, time.Since(stepUpdateLinesStart).Milliseconds(), err)
|
||||
writeDBError(w, http.StatusInternalServerError, "update_order_lines", id, username, len(payload.Lines), err)
|
||||
return
|
||||
}
|
||||
log.Printf("[OrderProductionApplyRoute] rid=%s orderHeaderID=%s step=update_lines updated=%d duration_ms=%d",
|
||||
rid, id, updated, time.Since(stepUpdateLinesStart).Milliseconds())
|
||||
|
||||
stepUpsertBarcodeStart := time.Now()
|
||||
barcodeInserted, err := queries.InsertItemBarcodesTx(tx, id, payload.Lines, username)
|
||||
if err != nil {
|
||||
log.Printf("[OrderProductionApplyRoute] rid=%s orderHeaderID=%s step=upsert_barcodes failed duration_ms=%d err=%v",
|
||||
rid, id, time.Since(stepUpsertBarcodeStart).Milliseconds(), err)
|
||||
writeDBError(w, http.StatusInternalServerError, "upsert_item_barcodes", id, username, len(payload.Lines), err)
|
||||
return
|
||||
}
|
||||
log.Printf("[OrderProductionApplyRoute] rid=%s orderHeaderID=%s step=upsert_barcodes inserted=%d duration_ms=%d",
|
||||
rid, id, barcodeInserted, time.Since(stepUpsertBarcodeStart).Milliseconds())
|
||||
|
||||
stepUpsertAttrStart := time.Now()
|
||||
currentStep = "upsert_item_attributes"
|
||||
attributeAffected, err := queries.UpsertItemAttributesTx(tx, payload.ProductAttributes, username)
|
||||
if err != nil {
|
||||
log.Printf("[OrderProductionApplyRoute] rid=%s orderHeaderID=%s step=upsert_attributes failed duration_ms=%d err=%v",
|
||||
@@ -340,14 +421,120 @@ func OrderProductionApplyRoute(mssql *sql.DB, ml *mailer.GraphMailer) http.Handl
|
||||
}
|
||||
log.Printf("[OrderProductionApplyRoute] rid=%s orderHeaderID=%s step=upsert_attributes affected=%d duration_ms=%d",
|
||||
rid, id, attributeAffected, time.Since(stepUpsertAttrStart).Milliseconds())
|
||||
log.Printf("[OrderProductionApplyRoute] rid=%s orderHeaderID=%s table=prItemAttribute inputRows=%d affectedRows=%d",
|
||||
rid, id, len(payload.ProductAttributes), attributeAffected)
|
||||
if err := ensureTxAlive(tx, "after_upsert_item_attributes"); err != nil {
|
||||
writeDBError(w, http.StatusInternalServerError, "tx_not_active_after_upsert_item_attributes", id, username, len(payload.ProductAttributes), err)
|
||||
return
|
||||
}
|
||||
|
||||
var barcodeInserted int64
|
||||
// Barkod adimi:
|
||||
// - Eski kodlara girmemeli
|
||||
// - Yeni kod satirlari icin, varyant daha once olusmus olsa bile eksik barkod varsa tamamlamali
|
||||
// Bu nedenle "inserted > 0" yerine "newLineCount > 0" kosulu kullanilir.
|
||||
if len(newLines) > 0 && len(barcodeTargets) > 0 {
|
||||
stepUpsertBarcodeStart := time.Now()
|
||||
currentStep = "upsert_item_barcodes"
|
||||
barcodeInserted, err = queries.InsertItemBarcodesByTargetsTx(tx, barcodeTargets, username)
|
||||
if err != nil {
|
||||
log.Printf("[OrderProductionApplyRoute] rid=%s orderHeaderID=%s step=upsert_barcodes failed duration_ms=%d err=%v",
|
||||
rid, id, time.Since(stepUpsertBarcodeStart).Milliseconds(), err)
|
||||
writeDBError(w, http.StatusInternalServerError, "upsert_item_barcodes", id, username, len(barcodeTargets), err)
|
||||
return
|
||||
}
|
||||
log.Printf("[OrderProductionApplyRoute] rid=%s orderHeaderID=%s step=upsert_barcodes inserted=%d duration_ms=%d",
|
||||
rid, id, barcodeInserted, time.Since(stepUpsertBarcodeStart).Milliseconds())
|
||||
log.Printf("[OrderProductionApplyRoute] rid=%s orderHeaderID=%s table=prItemBarcode targetVariantRows=%d insertedRows=%d",
|
||||
rid, id, len(barcodeTargets), barcodeInserted)
|
||||
if err := ensureTxAlive(tx, "after_upsert_item_barcodes"); err != nil {
|
||||
writeDBError(w, http.StatusInternalServerError, "tx_not_active_after_upsert_item_barcodes", id, username, len(barcodeTargets), err)
|
||||
return
|
||||
}
|
||||
} else {
|
||||
log.Printf("[OrderProductionApplyRoute] rid=%s orderHeaderID=%s step=upsert_barcodes skipped newLineCount=%d targetVariantRows=%d",
|
||||
rid, id, len(newLines), len(barcodeTargets))
|
||||
}
|
||||
|
||||
stepUpdateHeaderStart := time.Now()
|
||||
currentStep = "update_order_header_average_due_date"
|
||||
if err := queries.UpdateOrderHeaderAverageDueDateTx(tx, id, payload.HeaderAverageDueDate, username); err != nil {
|
||||
writeDBError(w, http.StatusInternalServerError, "update_order_header_average_due_date", id, username, 0, err)
|
||||
return
|
||||
}
|
||||
log.Printf("[OrderProductionApplyRoute] rid=%s orderHeaderID=%s step=update_header_average_due_date changed=%t duration_ms=%d",
|
||||
rid, id, payload.HeaderAverageDueDate != nil, time.Since(stepUpdateHeaderStart).Milliseconds())
|
||||
if err := ensureTxAlive(tx, "after_update_order_header_average_due_date"); err != nil {
|
||||
writeDBError(w, http.StatusInternalServerError, "tx_not_active_after_update_order_header_average_due_date", id, username, 0, err)
|
||||
return
|
||||
}
|
||||
|
||||
currentStep = "touch_order_header"
|
||||
headerTouched, err := queries.TouchOrderHeaderTx(tx, id, username)
|
||||
if err != nil {
|
||||
writeDBError(w, http.StatusInternalServerError, "touch_order_header", id, username, len(payload.Lines), err)
|
||||
return
|
||||
}
|
||||
log.Printf("[OrderProductionApplyRoute] rid=%s orderHeaderID=%s table=trOrderHeader touchedRows=%d",
|
||||
rid, id, headerTouched)
|
||||
if err := ensureTxAlive(tx, "after_touch_order_header"); err != nil {
|
||||
writeDBError(w, http.StatusInternalServerError, "tx_not_active_after_touch_order_header", id, username, len(payload.Lines), err)
|
||||
return
|
||||
}
|
||||
|
||||
stepUpdateLinesStart := time.Now()
|
||||
currentStep = "update_order_lines"
|
||||
updated, err := queries.UpdateOrderLinesTx(tx, id, payload.Lines, username)
|
||||
if err != nil {
|
||||
log.Printf("[OrderProductionApplyRoute] rid=%s orderHeaderID=%s step=update_lines failed duration_ms=%d err=%v",
|
||||
rid, id, time.Since(stepUpdateLinesStart).Milliseconds(), err)
|
||||
writeDBError(w, http.StatusInternalServerError, "update_order_lines", id, username, len(payload.Lines), err)
|
||||
return
|
||||
}
|
||||
log.Printf("[OrderProductionApplyRoute] rid=%s orderHeaderID=%s step=update_lines updated=%d duration_ms=%d",
|
||||
rid, id, updated, time.Since(stepUpdateLinesStart).Milliseconds())
|
||||
log.Printf("[OrderProductionApplyRoute] rid=%s orderHeaderID=%s table=trOrderLine targetRows=%d updatedRows=%d",
|
||||
rid, id, len(payload.Lines), updated)
|
||||
if err := ensureTxAlive(tx, "after_update_order_lines"); err != nil {
|
||||
writeDBError(w, http.StatusInternalServerError, "tx_not_active_after_update_order_lines", id, username, len(payload.Lines), err)
|
||||
return
|
||||
}
|
||||
|
||||
currentStep = "verify_order_lines"
|
||||
verifyMismatchCount, verifySamples, verifyErr := queries.VerifyOrderLineUpdatesTx(tx, id, payload.Lines)
|
||||
if verifyErr != nil {
|
||||
writeDBError(w, http.StatusInternalServerError, "verify_order_lines", id, username, len(payload.Lines), verifyErr)
|
||||
return
|
||||
}
|
||||
if verifyMismatchCount > 0 {
|
||||
log.Printf("[OrderProductionApplyRoute] rid=%s orderHeaderID=%s table=trOrderLine verifyMismatchCount=%d samples=%v",
|
||||
rid, id, verifyMismatchCount, verifySamples)
|
||||
currentStep = "verify_order_lines_mismatch"
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
_ = json.NewEncoder(w).Encode(map[string]any{
|
||||
"message": "Order satirlari beklenen kod/renk degerlerine guncellenemedi",
|
||||
"step": "verify_order_lines_mismatch",
|
||||
"detail": fmt.Sprintf("mismatchCount=%d", verifyMismatchCount),
|
||||
"samples": verifySamples,
|
||||
})
|
||||
return
|
||||
}
|
||||
log.Printf("[OrderProductionApplyRoute] rid=%s orderHeaderID=%s table=trOrderLine verifyMismatchCount=0",
|
||||
rid, id)
|
||||
if err := ensureTxAlive(tx, "before_commit_tx"); err != nil {
|
||||
writeDBError(w, http.StatusInternalServerError, "tx_not_active_before_commit_tx", id, username, len(payload.Lines), err)
|
||||
return
|
||||
}
|
||||
|
||||
stepCommitStart := time.Now()
|
||||
currentStep = "commit_tx"
|
||||
if err := tx.Commit(); err != nil {
|
||||
log.Printf("[OrderProductionApplyRoute] rid=%s orderHeaderID=%s step=commit failed duration_ms=%d err=%v",
|
||||
rid, id, time.Since(stepCommitStart).Milliseconds(), err)
|
||||
writeDBError(w, http.StatusInternalServerError, "commit_tx", id, username, len(payload.Lines), err)
|
||||
return
|
||||
}
|
||||
committed = true
|
||||
log.Printf("[OrderProductionApplyRoute] rid=%s orderHeaderID=%s step=commit duration_ms=%d total_ms=%d",
|
||||
rid, id, time.Since(stepCommitStart).Milliseconds(), time.Since(start).Milliseconds())
|
||||
|
||||
@@ -372,6 +559,8 @@ func OrderProductionApplyRoute(mssql *sql.DB, ml *mailer.GraphMailer) http.Handl
|
||||
}
|
||||
log.Printf("[OrderProductionApplyRoute] rid=%s orderHeaderID=%s result updated=%d inserted=%d barcodeInserted=%d attributeUpserted=%d",
|
||||
rid, id, updated, inserted, barcodeInserted, attributeAffected)
|
||||
log.Printf("[OrderProductionApplyRoute] rid=%s orderHeaderID=%s summary tables cdItem/prItemVariant(newOnly)=%d trOrderLine(updated)=%d prItemBarcode(inserted,newOnly)=%d prItemAttribute(affected)=%d trOrderHeader(touched)=%d",
|
||||
rid, id, inserted, updated, barcodeInserted, attributeAffected, headerTouched)
|
||||
if err := json.NewEncoder(w).Encode(resp); err != nil {
|
||||
log.Printf("❌ encode error: %v", err)
|
||||
}
|
||||
@@ -416,6 +605,14 @@ func buildCdItemDraftMap(list []models.OrderProductionCdItemDraft) map[string]mo
|
||||
return out
|
||||
}
|
||||
|
||||
func isNoCorrespondingBeginTxErr(err error) bool {
|
||||
if err == nil {
|
||||
return false
|
||||
}
|
||||
msg := strings.ToLower(strings.TrimSpace(err.Error()))
|
||||
return strings.Contains(msg, "commit transaction request has no corresponding begin transaction")
|
||||
}
|
||||
|
||||
func buildTargetVariants(mssql *sql.DB, orderHeaderID string, lines []models.OrderProductionUpdateLine) ([]models.OrderProductionMissingVariant, error) {
|
||||
start := time.Now()
|
||||
lineDimsMap, err := queries.GetOrderLineDimsMap(mssql, orderHeaderID)
|
||||
@@ -468,11 +665,15 @@ func buildTargetVariants(mssql *sql.DB, orderHeaderID string, lines []models.Ord
|
||||
}
|
||||
|
||||
func buildMissingVariants(mssql *sql.DB, orderHeaderID string, lines []models.OrderProductionUpdateLine) ([]models.OrderProductionMissingVariant, error) {
|
||||
start := time.Now()
|
||||
targets, err := buildTargetVariants(mssql, orderHeaderID, lines)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return buildMissingVariantsFromTargets(mssql, orderHeaderID, targets)
|
||||
}
|
||||
|
||||
func buildMissingVariantsFromTargets(mssql *sql.DB, orderHeaderID string, targets []models.OrderProductionMissingVariant) ([]models.OrderProductionMissingVariant, error) {
|
||||
start := time.Now()
|
||||
missing := make([]models.OrderProductionMissingVariant, 0, len(targets))
|
||||
existsCache := make(map[string]bool, len(targets))
|
||||
|
||||
@@ -499,11 +700,69 @@ func buildMissingVariants(mssql *sql.DB, orderHeaderID string, lines []models.Or
|
||||
}
|
||||
}
|
||||
|
||||
log.Printf("[buildMissingVariants] orderHeaderID=%s lineCount=%d dimMapCount=%d missingCount=%d total_ms=%d",
|
||||
orderHeaderID, len(lines), len(targets), len(missing), time.Since(start).Milliseconds())
|
||||
log.Printf("[buildMissingVariants] orderHeaderID=%s targetCount=%d missingCount=%d total_ms=%d",
|
||||
orderHeaderID, len(targets), len(missing), time.Since(start).Milliseconds())
|
||||
return missing, nil
|
||||
}
|
||||
|
||||
func runWithTransientMSSQLRetry(op string, maxAttempts int, baseDelay time.Duration, fn func() error) error {
|
||||
if maxAttempts <= 1 {
|
||||
return fn()
|
||||
}
|
||||
var lastErr error
|
||||
for attempt := 1; attempt <= maxAttempts; attempt++ {
|
||||
err := fn()
|
||||
if err == nil {
|
||||
return nil
|
||||
}
|
||||
lastErr = err
|
||||
if !isTransientMSSQLNetworkErr(err) || attempt == maxAttempts {
|
||||
return err
|
||||
}
|
||||
wait := time.Duration(attempt) * baseDelay
|
||||
log.Printf("[MSSQLRetry] op=%s attempt=%d/%d wait_ms=%d err=%v",
|
||||
op, attempt, maxAttempts, wait.Milliseconds(), err)
|
||||
time.Sleep(wait)
|
||||
}
|
||||
return lastErr
|
||||
}
|
||||
|
||||
func isTransientMSSQLNetworkErr(err error) bool {
|
||||
if err == nil {
|
||||
return false
|
||||
}
|
||||
msg := strings.ToLower(strings.TrimSpace(err.Error()))
|
||||
needles := []string{
|
||||
"wsarecv",
|
||||
"read tcp",
|
||||
"connection reset",
|
||||
"connection refused",
|
||||
"broken pipe",
|
||||
"i/o timeout",
|
||||
"timeout",
|
||||
}
|
||||
for _, needle := range needles {
|
||||
if strings.Contains(msg, needle) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func ensureTxAlive(tx *sql.Tx, where string) error {
|
||||
if tx == nil {
|
||||
return fmt.Errorf("tx is nil at %s", where)
|
||||
}
|
||||
var tranCount int
|
||||
if err := tx.QueryRow(`SELECT @@TRANCOUNT`).Scan(&tranCount); err != nil {
|
||||
return fmt.Errorf("tx state query failed at %s: %w", where, err)
|
||||
}
|
||||
if tranCount <= 0 {
|
||||
return fmt.Errorf("transaction no longer active at %s (trancount=%d)", where, tranCount)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func validateUpdateLines(lines []models.OrderProductionUpdateLine) error {
|
||||
for _, line := range lines {
|
||||
if strings.TrimSpace(line.OrderLineID) == "" {
|
||||
@@ -520,6 +779,54 @@ func validateUpdateLines(lines []models.OrderProductionUpdateLine) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func splitLinesByCdItemDraft(lines []models.OrderProductionUpdateLine, cdItems []models.OrderProductionCdItemDraft) ([]models.OrderProductionUpdateLine, []models.OrderProductionUpdateLine) {
|
||||
if len(lines) == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
newCodeSet := make(map[string]struct{}, len(cdItems))
|
||||
for _, item := range cdItems {
|
||||
code := strings.ToUpper(strings.TrimSpace(item.ItemCode))
|
||||
if code == "" {
|
||||
continue
|
||||
}
|
||||
newCodeSet[code] = struct{}{}
|
||||
}
|
||||
if len(newCodeSet) == 0 {
|
||||
existingLines := make([]models.OrderProductionUpdateLine, 0, len(lines))
|
||||
existingLines = append(existingLines, lines...)
|
||||
return nil, existingLines
|
||||
}
|
||||
|
||||
newLines := make([]models.OrderProductionUpdateLine, 0, len(lines))
|
||||
existingLines := make([]models.OrderProductionUpdateLine, 0, len(lines))
|
||||
for _, line := range lines {
|
||||
code := strings.ToUpper(strings.TrimSpace(line.NewItemCode))
|
||||
if _, ok := newCodeSet[code]; ok {
|
||||
newLines = append(newLines, line)
|
||||
continue
|
||||
}
|
||||
existingLines = append(existingLines, line)
|
||||
}
|
||||
return newLines, existingLines
|
||||
}
|
||||
|
||||
func uniqueCodesFromLines(lines []models.OrderProductionUpdateLine) []string {
|
||||
set := make(map[string]struct{}, len(lines))
|
||||
out := make([]string, 0, len(lines))
|
||||
for _, line := range lines {
|
||||
code := strings.ToUpper(strings.TrimSpace(line.NewItemCode))
|
||||
if code == "" {
|
||||
continue
|
||||
}
|
||||
if _, ok := set[code]; ok {
|
||||
continue
|
||||
}
|
||||
set[code] = struct{}{}
|
||||
out = append(out, code)
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func writeDBError(w http.ResponseWriter, status int, step string, orderHeaderID string, username string, lineCount int, err error) {
|
||||
var sqlErr mssql.Error
|
||||
if errors.As(err, &sqlErr) {
|
||||
|
||||
Reference in New Issue
Block a user