Merge remote-tracking branch 'origin/master'
This commit is contained in:
@@ -12,6 +12,7 @@ import (
|
||||
"strings"
|
||||
|
||||
"github.com/gorilla/mux"
|
||||
mssql "github.com/microsoft/go-mssqldb"
|
||||
)
|
||||
|
||||
// ======================================================
|
||||
@@ -136,8 +137,7 @@ func OrderProductionValidateRoute(mssql *sql.DB) http.Handler {
|
||||
|
||||
missing, err := buildMissingVariants(mssql, id, payload.Lines)
|
||||
if err != nil {
|
||||
log.Printf("❌ validate error: %v", err)
|
||||
http.Error(w, "Veritabani hatasi", http.StatusInternalServerError)
|
||||
writeDBError(w, http.StatusInternalServerError, "validate_missing_variants", id, "", len(payload.Lines), err)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -176,8 +176,7 @@ func OrderProductionApplyRoute(mssql *sql.DB) http.Handler {
|
||||
|
||||
missing, err := buildMissingVariants(mssql, id, payload.Lines)
|
||||
if err != nil {
|
||||
log.Printf("❌ apply validate error: %v", err)
|
||||
http.Error(w, "Veritabani hatasi", http.StatusInternalServerError)
|
||||
writeDBError(w, http.StatusInternalServerError, "apply_validate_missing_variants", id, "", len(payload.Lines), err)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -202,7 +201,7 @@ func OrderProductionApplyRoute(mssql *sql.DB) http.Handler {
|
||||
|
||||
tx, err := mssql.Begin()
|
||||
if err != nil {
|
||||
http.Error(w, "Veritabani hatasi", http.StatusInternalServerError)
|
||||
writeDBError(w, http.StatusInternalServerError, "begin_tx", id, username, len(payload.Lines), err)
|
||||
return
|
||||
}
|
||||
defer tx.Rollback()
|
||||
@@ -211,22 +210,19 @@ func OrderProductionApplyRoute(mssql *sql.DB) http.Handler {
|
||||
if payload.InsertMissing {
|
||||
inserted, err = queries.InsertMissingVariantsTx(tx, missing, username)
|
||||
if err != nil {
|
||||
log.Printf("❌ insert missing error: %v", err)
|
||||
http.Error(w, "Veritabani hatasi", http.StatusInternalServerError)
|
||||
writeDBError(w, http.StatusInternalServerError, "insert_missing_variants", id, username, len(missing), err)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
updated, err := queries.UpdateOrderLinesTx(tx, id, payload.Lines, username)
|
||||
if err != nil {
|
||||
log.Printf("❌ update order lines error: %v", err)
|
||||
http.Error(w, "Veritabani hatasi", http.StatusInternalServerError)
|
||||
writeDBError(w, http.StatusInternalServerError, "update_order_lines", id, username, len(payload.Lines), err)
|
||||
return
|
||||
}
|
||||
|
||||
if err := tx.Commit(); err != nil {
|
||||
log.Printf("❌ commit error: %v", err)
|
||||
http.Error(w, "Veritabani hatasi", http.StatusInternalServerError)
|
||||
writeDBError(w, http.StatusInternalServerError, "commit_tx", id, username, len(payload.Lines), err)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -289,3 +285,26 @@ func validateUpdateLines(lines []models.OrderProductionUpdateLine) error {
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
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) {
|
||||
log.Printf(
|
||||
"❌ SQL error step=%s orderHeaderID=%s user=%s lineCount=%d number=%d state=%d class=%d server=%s proc=%s line=%d message=%s",
|
||||
step, orderHeaderID, username, lineCount,
|
||||
sqlErr.Number, sqlErr.State, sqlErr.Class, sqlErr.ServerName, sqlErr.ProcName, sqlErr.LineNo, sqlErr.Message,
|
||||
)
|
||||
} else {
|
||||
log.Printf(
|
||||
"❌ DB error step=%s orderHeaderID=%s user=%s lineCount=%d err=%v",
|
||||
step, orderHeaderID, username, lineCount, err,
|
||||
)
|
||||
}
|
||||
|
||||
w.WriteHeader(status)
|
||||
_ = json.NewEncoder(w).Encode(map[string]any{
|
||||
"message": "Veritabani hatasi",
|
||||
"step": step,
|
||||
"detail": err.Error(),
|
||||
})
|
||||
}
|
||||
|
||||
128
svc/routes/product_stock_query_by_attributes.go
Normal file
128
svc/routes/product_stock_query_by_attributes.go
Normal file
@@ -0,0 +1,128 @@
|
||||
package routes
|
||||
|
||||
import (
|
||||
"bssapp-backend/db"
|
||||
"bssapp-backend/queries"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"log"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
// GetProductStockAttributeOptionsHandler
|
||||
// GET /api/product-stock-attribute-options
|
||||
func GetProductStockAttributeOptionsHandler(w http.ResponseWriter, _ *http.Request) {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
|
||||
defer cancel()
|
||||
|
||||
rows, err := db.MssqlDB.QueryContext(ctx, queries.GetProductStockAttributeOptionsQuery)
|
||||
if err != nil {
|
||||
log.Printf("[PRODUCT-STOCK-ATTR-OPTIONS] SQL hatasi: %v", err)
|
||||
http.Error(w, "SQL hatasi: "+err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
result := map[string][]string{
|
||||
"att01": {},
|
||||
"att02": {},
|
||||
"att10": {},
|
||||
"att11": {},
|
||||
"att21": {},
|
||||
"att35": {},
|
||||
"att36": {},
|
||||
"att44": {},
|
||||
}
|
||||
|
||||
for rows.Next() {
|
||||
var fieldName, fieldValue string
|
||||
if err := rows.Scan(&fieldName, &fieldValue); err != nil {
|
||||
continue
|
||||
}
|
||||
fieldName = strings.TrimSpace(fieldName)
|
||||
fieldValue = strings.TrimSpace(fieldValue)
|
||||
if fieldName == "" || fieldValue == "" {
|
||||
continue
|
||||
}
|
||||
result[fieldName] = append(result[fieldName], fieldValue)
|
||||
}
|
||||
|
||||
if err := rows.Err(); err != nil {
|
||||
http.Error(w, "Satir okuma hatasi: "+err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
||||
_ = json.NewEncoder(w).Encode(result)
|
||||
}
|
||||
|
||||
// GetProductStockQueryByAttributesHandler
|
||||
// GET /api/product-stock-query-by-attributes?att01=...&att02=...
|
||||
func GetProductStockQueryByAttributesHandler(w http.ResponseWriter, r *http.Request) {
|
||||
att01 := strings.TrimSpace(r.URL.Query().Get("att01"))
|
||||
att02 := strings.TrimSpace(r.URL.Query().Get("att02"))
|
||||
att10 := strings.TrimSpace(r.URL.Query().Get("att10"))
|
||||
att11 := strings.TrimSpace(r.URL.Query().Get("att11"))
|
||||
att21 := strings.TrimSpace(r.URL.Query().Get("att21"))
|
||||
att35 := strings.TrimSpace(r.URL.Query().Get("att35"))
|
||||
att36 := strings.TrimSpace(r.URL.Query().Get("att36"))
|
||||
att44 := strings.TrimSpace(r.URL.Query().Get("att44"))
|
||||
|
||||
hasAny := att01 != "" || att02 != "" || att10 != "" || att11 != "" || att21 != "" || att35 != "" || att36 != "" || att44 != ""
|
||||
if !hasAny {
|
||||
http.Error(w, "En az bir urun ozelligi secilmelidir", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
start := time.Now()
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 90*time.Second)
|
||||
defer cancel()
|
||||
log.Printf(
|
||||
"[PRODUCT-STOCK-BY-ATTRS] request att01=%q att02=%q att10=%q att11=%q att21=%q att35=%q att36=%q att44=%q",
|
||||
att01, att02, att10, att11, att21, att35, att36, att44,
|
||||
)
|
||||
|
||||
rows, err := db.MssqlDB.QueryContext(ctx, queries.GetProductStockQueryByAttributes, att01, att02, att10, att11, att21, att35, att36, att44)
|
||||
if err != nil {
|
||||
log.Printf("[PRODUCT-STOCK-BY-ATTRS] SQL hatasi: %v", err)
|
||||
http.Error(w, "SQL hatasi: "+err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
columns, err := rows.Columns()
|
||||
if err != nil {
|
||||
http.Error(w, "Kolon bilgisi alinamadi", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
result := make([]map[string]interface{}, 0, 256)
|
||||
for rows.Next() {
|
||||
raw := make([]interface{}, len(columns))
|
||||
dest := make([]interface{}, len(columns))
|
||||
for i := range raw {
|
||||
dest[i] = &raw[i]
|
||||
}
|
||||
if err := rows.Scan(dest...); err != nil {
|
||||
continue
|
||||
}
|
||||
|
||||
rowMap := make(map[string]interface{}, len(columns))
|
||||
for i, c := range columns {
|
||||
rowMap[c] = normalizeSQLValue(raw[i])
|
||||
}
|
||||
result = append(result, rowMap)
|
||||
}
|
||||
|
||||
if err := rows.Err(); err != nil {
|
||||
log.Printf("[PRODUCT-STOCK-BY-ATTRS] rows err elapsed=%s err=%v", time.Since(start), err)
|
||||
http.Error(w, "Satir okuma hatasi: "+err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
log.Printf("[PRODUCT-STOCK-BY-ATTRS] success rows=%d elapsed=%s", len(result), time.Since(start))
|
||||
|
||||
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
||||
_ = json.NewEncoder(w).Encode(result)
|
||||
}
|
||||
Reference in New Issue
Block a user