Merge remote-tracking branch 'origin/master'
This commit is contained in:
@@ -150,6 +150,9 @@ func OrderProductionInsertMissingRoute(mssql *sql.DB) http.Handler {
|
||||
func OrderProductionValidateRoute(mssql *sql.DB) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
||||
rid := fmt.Sprintf("opv-%d", time.Now().UnixNano())
|
||||
w.Header().Set("X-Debug-Request-Id", rid)
|
||||
start := time.Now()
|
||||
|
||||
id := mux.Vars(r)["id"]
|
||||
if id == "" {
|
||||
@@ -167,11 +170,16 @@ func OrderProductionValidateRoute(mssql *sql.DB) http.Handler {
|
||||
return
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
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())
|
||||
|
||||
resp := map[string]any{
|
||||
"missingCount": len(missing),
|
||||
@@ -189,6 +197,9 @@ func OrderProductionValidateRoute(mssql *sql.DB) http.Handler {
|
||||
func OrderProductionApplyRoute(mssql *sql.DB) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
||||
rid := fmt.Sprintf("opa-%d", time.Now().UnixNano())
|
||||
w.Header().Set("X-Debug-Request-Id", rid)
|
||||
start := time.Now()
|
||||
|
||||
id := mux.Vars(r)["id"]
|
||||
if id == "" {
|
||||
@@ -206,13 +217,20 @@ func OrderProductionApplyRoute(mssql *sql.DB) http.Handler {
|
||||
return
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
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())
|
||||
|
||||
if len(missing) > 0 && !payload.InsertMissing {
|
||||
log.Printf("[OrderProductionApplyRoute] rid=%s orderHeaderID=%s early_exit=missing_variants total_ms=%d",
|
||||
rid, id, time.Since(start).Milliseconds())
|
||||
w.WriteHeader(http.StatusConflict)
|
||||
_ = json.NewEncoder(w).Encode(map[string]any{
|
||||
"missingCount": len(missing),
|
||||
@@ -231,43 +249,68 @@ func OrderProductionApplyRoute(mssql *sql.DB) http.Handler {
|
||||
username = "system"
|
||||
}
|
||||
|
||||
stepBeginStart := time.Now()
|
||||
tx, err := mssql.Begin()
|
||||
if err != nil {
|
||||
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())
|
||||
|
||||
stepTxSettingsStart := time.Now()
|
||||
if _, err := tx.Exec(`SET XACT_ABORT ON; SET LOCK_TIMEOUT 15000;`); 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())
|
||||
|
||||
var inserted int64
|
||||
if payload.InsertMissing {
|
||||
cdItemByCode := buildCdItemDraftMap(payload.CdItems)
|
||||
stepInsertMissingStart := time.Now()
|
||||
inserted, err = queries.InsertMissingVariantsTx(tx, missing, username, cdItemByCode)
|
||||
if err != nil {
|
||||
writeDBError(w, http.StatusInternalServerError, "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()
|
||||
if err := validateProductAttributes(payload.ProductAttributes); err != nil {
|
||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
attributeAffected, err := queries.UpsertItemAttributesTx(tx, payload.ProductAttributes, username)
|
||||
if err != nil {
|
||||
writeDBError(w, http.StatusInternalServerError, "upsert_item_attributes", id, username, len(payload.ProductAttributes), err)
|
||||
return
|
||||
}
|
||||
log.Printf("[OrderProductionApplyRoute] rid=%s orderHeaderID=%s step=validate_attributes count=%d duration_ms=%d",
|
||||
rid, id, len(payload.ProductAttributes), time.Since(stepValidateAttrStart).Milliseconds())
|
||||
|
||||
stepUpdateLinesStart := time.Now()
|
||||
updated, err := queries.UpdateOrderLinesTx(tx, id, payload.Lines, username)
|
||||
if err != nil {
|
||||
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())
|
||||
|
||||
stepUpsertAttrStart := time.Now()
|
||||
attributeAffected, err := queries.UpsertItemAttributesTx(tx, payload.ProductAttributes, username)
|
||||
if err != nil {
|
||||
writeDBError(w, http.StatusInternalServerError, "upsert_item_attributes", id, username, len(payload.ProductAttributes), err)
|
||||
return
|
||||
}
|
||||
log.Printf("[OrderProductionApplyRoute] rid=%s orderHeaderID=%s step=upsert_attributes affected=%d duration_ms=%d",
|
||||
rid, id, attributeAffected, time.Since(stepUpsertAttrStart).Milliseconds())
|
||||
|
||||
stepCommitStart := time.Now()
|
||||
if err := tx.Commit(); err != nil {
|
||||
writeDBError(w, http.StatusInternalServerError, "commit_tx", id, username, len(payload.Lines), err)
|
||||
return
|
||||
}
|
||||
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())
|
||||
|
||||
resp := map[string]any{
|
||||
"updated": updated,
|
||||
@@ -319,7 +362,13 @@ func buildCdItemDraftMap(list []models.OrderProductionCdItemDraft) map[string]mo
|
||||
}
|
||||
|
||||
func buildMissingVariants(mssql *sql.DB, orderHeaderID string, lines []models.OrderProductionUpdateLine) ([]models.OrderProductionMissingVariant, error) {
|
||||
start := time.Now()
|
||||
missing := make([]models.OrderProductionMissingVariant, 0)
|
||||
lineDimsMap, err := queries.GetOrderLineDimsMap(mssql, orderHeaderID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
existsCache := make(map[string]bool, len(lines))
|
||||
|
||||
for _, line := range lines {
|
||||
lineID := strings.TrimSpace(line.OrderLineID)
|
||||
@@ -331,28 +380,43 @@ func buildMissingVariants(mssql *sql.DB, orderHeaderID string, lines []models.Or
|
||||
continue
|
||||
}
|
||||
|
||||
itemTypeCode, dim1, _, dim3, err := queries.GetOrderLineDims(mssql, orderHeaderID, lineID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
dims, ok := lineDimsMap[lineID]
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
|
||||
exists, err := queries.VariantExists(mssql, itemTypeCode, newItem, newColor, dim1, newDim2, dim3)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
cacheKey := fmt.Sprintf("%d|%s|%s|%s|%s|%s",
|
||||
dims.ItemTypeCode,
|
||||
strings.ToUpper(strings.TrimSpace(newItem)),
|
||||
strings.ToUpper(strings.TrimSpace(newColor)),
|
||||
strings.ToUpper(strings.TrimSpace(dims.ItemDim1Code)),
|
||||
strings.ToUpper(strings.TrimSpace(newDim2)),
|
||||
strings.ToUpper(strings.TrimSpace(dims.ItemDim3Code)),
|
||||
)
|
||||
exists, cached := existsCache[cacheKey]
|
||||
if !cached {
|
||||
var checkErr error
|
||||
exists, checkErr = queries.VariantExists(mssql, dims.ItemTypeCode, newItem, newColor, dims.ItemDim1Code, newDim2, dims.ItemDim3Code)
|
||||
if checkErr != nil {
|
||||
return nil, checkErr
|
||||
}
|
||||
existsCache[cacheKey] = exists
|
||||
}
|
||||
if !exists {
|
||||
missing = append(missing, models.OrderProductionMissingVariant{
|
||||
OrderLineID: lineID,
|
||||
ItemTypeCode: itemTypeCode,
|
||||
ItemTypeCode: dims.ItemTypeCode,
|
||||
ItemCode: newItem,
|
||||
ColorCode: newColor,
|
||||
ItemDim1Code: dim1,
|
||||
ItemDim1Code: dims.ItemDim1Code,
|
||||
ItemDim2Code: newDim2,
|
||||
ItemDim3Code: dim3,
|
||||
ItemDim3Code: dims.ItemDim3Code,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
log.Printf("[buildMissingVariants] orderHeaderID=%s lineCount=%d dimMapCount=%d missingCount=%d total_ms=%d",
|
||||
orderHeaderID, len(lines), len(lineDimsMap), len(missing), time.Since(start).Milliseconds())
|
||||
return missing, nil
|
||||
}
|
||||
|
||||
|
||||
45
svc/routes/productnewcolor.go
Normal file
45
svc/routes/productnewcolor.go
Normal file
@@ -0,0 +1,45 @@
|
||||
package routes
|
||||
|
||||
import (
|
||||
"bssapp-backend/auth"
|
||||
"bssapp-backend/db"
|
||||
"bssapp-backend/models"
|
||||
"bssapp-backend/queries"
|
||||
"encoding/json"
|
||||
"log"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func GetProductNewColorsHandler(w http.ResponseWriter, r *http.Request) {
|
||||
claims, ok := auth.GetClaimsFromContext(r.Context())
|
||||
if !ok || claims == nil {
|
||||
http.Error(w, "unauthorized", http.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
|
||||
code := r.URL.Query().Get("code")
|
||||
if code == "" {
|
||||
http.Error(w, "Eksik parametre: code gerekli", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
rows, err := db.MssqlDB.Query(queries.GetProductNewColors, code)
|
||||
if err != nil {
|
||||
http.Error(w, "Yeni urun renk listesi alinamadi: "+err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
var list []models.ProductColor
|
||||
for rows.Next() {
|
||||
var c models.ProductColor
|
||||
if err := rows.Scan(&c.ProductCode, &c.ColorCode, &c.ColorDescription); err != nil {
|
||||
log.Println("Satir okunamadi:", err)
|
||||
continue
|
||||
}
|
||||
list = append(list, c)
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
||||
_ = json.NewEncoder(w).Encode(list)
|
||||
}
|
||||
51
svc/routes/productnewsecondcolor.go
Normal file
51
svc/routes/productnewsecondcolor.go
Normal file
@@ -0,0 +1,51 @@
|
||||
package routes
|
||||
|
||||
import (
|
||||
"bssapp-backend/auth"
|
||||
"bssapp-backend/db"
|
||||
"bssapp-backend/models"
|
||||
"bssapp-backend/queries"
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
"log"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func GetProductNewSecondColorsHandler(w http.ResponseWriter, r *http.Request) {
|
||||
claims, ok := auth.GetClaimsFromContext(r.Context())
|
||||
if !ok || claims == nil {
|
||||
http.Error(w, "unauthorized", http.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
|
||||
code := r.URL.Query().Get("code")
|
||||
color := r.URL.Query().Get("color")
|
||||
if code == "" || color == "" {
|
||||
http.Error(w, "Eksik parametre: code ve color gerekli", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
rows, err := db.MssqlDB.Query(
|
||||
queries.GetProductNewSecondColors,
|
||||
sql.Named("ProductCode", code),
|
||||
sql.Named("ColorCode", color),
|
||||
)
|
||||
if err != nil {
|
||||
http.Error(w, "Yeni urun 2. renk listesi alinamadi: "+err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
var list []models.ProductSecondColor
|
||||
for rows.Next() {
|
||||
var c models.ProductSecondColor
|
||||
if err := rows.Scan(&c.ProductCode, &c.ColorCode, &c.ItemDim2Code, &c.ColorDescription); err != nil {
|
||||
log.Println("Satir okunamadi:", err)
|
||||
continue
|
||||
}
|
||||
list = append(list, c)
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
||||
_ = json.NewEncoder(w).Encode(list)
|
||||
}
|
||||
Reference in New Issue
Block a user