From eb628e99c27994cd81c201b6e2f5b7e641ed497a Mon Sep 17 00:00:00 2001 From: M_Kececi Date: Tue, 14 Apr 2026 17:23:09 +0300 Subject: [PATCH] Merge remote-tracking branch 'origin/master' --- svc/queries/orderproduction_items.go | 26 +++++++++++++++----------- svc/routes/orderproductionitems.go | 10 +++++++++- 2 files changed, 24 insertions(+), 12 deletions(-) diff --git a/svc/queries/orderproduction_items.go b/svc/queries/orderproduction_items.go index 5cd8772..a8d1429 100644 --- a/svc/queries/orderproduction_items.go +++ b/svc/queries/orderproduction_items.go @@ -583,6 +583,7 @@ WHERE l.OrderHeaderID = @p%d; chunkStart := time.Now() res, execErr := tx.Exec(query, args...) if execErr != nil { + log.Printf("[UpdateOrderLinesTx] ERROR orderHeaderID=%s chunk=%d-%d err=%v", orderHeaderID, i, end, execErr) return updated, fmt.Errorf("update lines chunk failed chunkStart=%d chunkEnd=%d duration_ms=%d: %w", i, end, time.Since(chunkStart).Milliseconds(), execErr) } log.Printf("[UpdateOrderLinesTx] orderHeaderID=%s chunk=%d-%d duration_ms=%d", orderHeaderID, i, end, time.Since(chunkStart).Milliseconds()) @@ -784,10 +785,10 @@ func ValidateProductionBarcodePlan(q sqlQueryRower, variants []models.OrderProdu return validations, nil } -func UpsertItemBarcodesTx(tx *sql.Tx, orderHeaderID string, lines []models.OrderProductionUpdateLine, username string) (int64, error) { +func InsertItemBarcodesTx(tx *sql.Tx, orderHeaderID string, lines []models.OrderProductionUpdateLine, username string) (int64, error) { start := time.Now() if len(lines) == 0 { - log.Printf("[UpsertItemBarcodesTx] lines=0 inserted=0 duration_ms=0") + log.Printf("[InsertItemBarcodesTx] lines=0 inserted=0 duration_ms=0") return 0, nil } @@ -805,7 +806,7 @@ func UpsertItemBarcodesTx(tx *sql.Tx, orderHeaderID string, lines []models.Order lineIDs = append(lineIDs, lineID) } if len(lineIDs) == 0 { - log.Printf("[UpsertItemBarcodesTx] lines=%d uniqueLineIDs=0 inserted=0 duration_ms=%d", len(lines), time.Since(start).Milliseconds()) + log.Printf("[InsertItemBarcodesTx] lines=%d uniqueLineIDs=0 inserted=0 duration_ms=%d", len(lines), time.Since(start).Milliseconds()) return 0, nil } @@ -833,7 +834,6 @@ func UpsertItemBarcodesTx(tx *sql.Tx, orderHeaderID string, lines []models.Order args = append(args, orderHeaderID, username) query := fmt.Sprintf(` -SET NOCOUNT ON; ;WITH srcLine (OrderLineID) AS ( SELECT * FROM (VALUES %s) AS v (OrderLineID) @@ -865,14 +865,14 @@ missing AS ( ) AS RowNo FROM src s LEFT JOIN dbo.prItemBarcode b WITH (UPDLOCK, HOLDLOCK) - ON UPPER(LTRIM(RTRIM(ISNULL(b.BarcodeTypeCode, '')))) = 'BAGGI3' + ON b.BarcodeTypeCode = 'BAGGI3' + AND b.UnitOfMeasureCode = 'AD' AND b.ItemTypeCode = s.ItemTypeCode AND UPPER(LTRIM(RTRIM(ISNULL(b.ItemCode, '')))) = s.ItemCode AND UPPER(LTRIM(RTRIM(ISNULL(b.ColorCode, '')))) = s.ColorCode AND UPPER(LTRIM(RTRIM(ISNULL(b.ItemDim1Code, '')))) = s.ItemDim1Code AND UPPER(LTRIM(RTRIM(ISNULL(b.ItemDim2Code, '')))) = s.ItemDim2Code AND UPPER(LTRIM(RTRIM(ISNULL(b.ItemDim3Code, '')))) = s.ItemDim3Code - AND UPPER(LTRIM(RTRIM(ISNULL(b.UnitOfMeasureCode, '')))) = 'AD' WHERE b.Barcode IS NULL ) INSERT INTO dbo.prItemBarcode ( @@ -912,6 +912,7 @@ FROM missing m CROSS JOIN ( SELECT ISNULL(MAX(TRY_CONVERT(BIGINT, NULLIF(LTRIM(RTRIM(Barcode)), ''))), 0) AS MaxBarcode FROM dbo.prItemBarcode WITH (UPDLOCK, HOLDLOCK) + WHERE TRY_CONVERT(BIGINT, NULLIF(LTRIM(RTRIM(Barcode)), '')) IS NOT NULL ) seed; SELECT @@ROWCOUNT AS Inserted; @@ -920,14 +921,15 @@ SELECT @@ROWCOUNT AS Inserted; chunkStart := time.Now() var chunkInserted int64 if err := tx.QueryRow(query, args...).Scan(&chunkInserted); err != nil { + log.Printf("[InsertItemBarcodesTx] ERROR orderHeaderID=%s chunk=%d-%d err=%v", orderHeaderID, i, end, err) return inserted, fmt.Errorf("upsert item barcodes chunk failed chunkStart=%d chunkEnd=%d duration_ms=%d: %w", i, end, time.Since(chunkStart).Milliseconds(), err) } inserted += chunkInserted - log.Printf("[UpsertItemBarcodesTx] orderHeaderID=%s chunk=%d-%d chunkInserted=%d cumulative=%d duration_ms=%d", + log.Printf("[InsertItemBarcodesTx] orderHeaderID=%s chunk=%d-%d chunkInserted=%d cumulative=%d duration_ms=%d", orderHeaderID, i, end, chunkInserted, inserted, time.Since(chunkStart).Milliseconds()) } - log.Printf("[UpsertItemBarcodesTx] orderHeaderID=%s lines=%d uniqueLineIDs=%d inserted=%d duration_ms=%d", + log.Printf("[InsertItemBarcodesTx] orderHeaderID=%s lines=%d uniqueLineIDs=%d inserted=%d duration_ms=%d", orderHeaderID, len(lines), len(lineIDs), inserted, time.Since(start).Milliseconds()) return inserted, nil } @@ -1018,12 +1020,14 @@ SELECT (@updated + @inserted) AS Affected; `, strings.Join(values, ","), usernameParam, strings.Join(values, ","), usernameParam, usernameParam) var chunkAffected int64 + chunkStart := time.Now() if err := tx.QueryRow(query, args...).Scan(&chunkAffected); err != nil { - return affected, err + log.Printf("[UpsertItemAttributesTx] ERROR chunk=%d-%d err=%v", i, end, err) + return affected, fmt.Errorf("upsert item attributes chunk failed chunkStart=%d chunkEnd=%d duration_ms=%d: %w", i, end, time.Since(chunkStart).Milliseconds(), err) } affected += chunkAffected - log.Printf("[UpsertItemAttributesTx] chunk=%d-%d chunkAffected=%d cumulative=%d", - i, end, chunkAffected, affected) + log.Printf("[UpsertItemAttributesTx] chunk=%d-%d chunkAffected=%d cumulative=%d duration_ms=%d", + i, end, chunkAffected, affected, time.Since(chunkStart).Milliseconds()) } log.Printf("[UpsertItemAttributesTx] attrs=%d affected=%d duration_ms=%d", len(attrs), affected, time.Since(start).Milliseconds()) diff --git a/svc/routes/orderproductionitems.go b/svc/routes/orderproductionitems.go index a4ba933..2db72b5 100644 --- a/svc/routes/orderproductionitems.go +++ b/svc/routes/orderproductionitems.go @@ -311,6 +311,8 @@ func OrderProductionApplyRoute(mssql *sql.DB, ml *mailer.GraphMailer) http.Handl 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 } @@ -318,8 +320,10 @@ func OrderProductionApplyRoute(mssql *sql.DB, ml *mailer.GraphMailer) http.Handl rid, id, updated, time.Since(stepUpdateLinesStart).Milliseconds()) stepUpsertBarcodeStart := time.Now() - barcodeInserted, err := queries.UpsertItemBarcodesTx(tx, id, payload.Lines, username) + 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 } @@ -329,6 +333,8 @@ func OrderProductionApplyRoute(mssql *sql.DB, ml *mailer.GraphMailer) http.Handl stepUpsertAttrStart := time.Now() 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", + rid, id, time.Since(stepUpsertAttrStart).Milliseconds(), err) writeDBError(w, http.StatusInternalServerError, "upsert_item_attributes", id, username, len(payload.ProductAttributes), err) return } @@ -337,6 +343,8 @@ func OrderProductionApplyRoute(mssql *sql.DB, ml *mailer.GraphMailer) http.Handl stepCommitStart := time.Now() 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 }