This commit is contained in:
@@ -8,6 +8,33 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// commandItemFull mappe toutes les colonnes de command_items pour les insertions batch avec infos client.
|
||||||
|
type commandItemFull struct {
|
||||||
|
CommandID int `gorm:"column:command_id"`
|
||||||
|
Produit string `gorm:"column:produit"`
|
||||||
|
ProductID int `gorm:"column:product_id"`
|
||||||
|
Quantite float64 `gorm:"column:quantite"`
|
||||||
|
Prix float64 `gorm:"column:prix"`
|
||||||
|
IsReward bool `gorm:"column:is_reward"`
|
||||||
|
RewardPoolKey string `gorm:"column:reward_pool_key"`
|
||||||
|
ClientUsername string `gorm:"column:client_username"`
|
||||||
|
ClientNom string `gorm:"column:client_nom"`
|
||||||
|
ClientPrenom string `gorm:"column:client_prenom"`
|
||||||
|
ClientTelephone string `gorm:"column:client_telephone"`
|
||||||
|
DeliveryAddress string `gorm:"column:delivery_address"`
|
||||||
|
Status string `gorm:"column:status"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (commandItemFull) TableName() string { return "command_items" }
|
||||||
|
|
||||||
|
// InsertCommandItemsBatch insère plusieurs items en une seule requête.
|
||||||
|
func (d *Database) InsertCommandItemsBatch(items []commandItemFull) error {
|
||||||
|
if len(items) == 0 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return d.GDB.Create(&items).Error
|
||||||
|
}
|
||||||
|
|
||||||
// ============================================
|
// ============================================
|
||||||
// VALIDATION HELPERS
|
// VALIDATION HELPERS
|
||||||
// ============================================
|
// ============================================
|
||||||
|
|||||||
@@ -128,13 +128,13 @@ func (d *Database) CreateCommand(username string) (*models.Command, error) {
|
|||||||
}
|
}
|
||||||
productNames, _ := d.GetProductNamesByIDs(productIDs)
|
productNames, _ := d.GetProductNamesByIDs(productIDs)
|
||||||
|
|
||||||
|
cmdItems := make([]models.CommandItem, 0, len(basketItems))
|
||||||
for _, item := range basketItems {
|
for _, item := range basketItems {
|
||||||
productName := productNames[item.ProductID]
|
productName := productNames[item.ProductID]
|
||||||
if productName == "" {
|
if productName == "" {
|
||||||
productName = "Produit inconnu"
|
productName = "Produit inconnu"
|
||||||
}
|
}
|
||||||
|
cmdItems = append(cmdItems, models.CommandItem{
|
||||||
cmdItem := models.CommandItem{
|
|
||||||
CommandID: commandID,
|
CommandID: commandID,
|
||||||
Produit: productName,
|
Produit: productName,
|
||||||
ProductID: item.ProductID,
|
ProductID: item.ProductID,
|
||||||
@@ -142,13 +142,12 @@ func (d *Database) CreateCommand(username string) (*models.Command, error) {
|
|||||||
Price: item.Price,
|
Price: item.Price,
|
||||||
IsReward: item.IsReward,
|
IsReward: item.IsReward,
|
||||||
RewardPoolKey: item.RewardPoolKey,
|
RewardPoolKey: item.RewardPoolKey,
|
||||||
|
})
|
||||||
}
|
}
|
||||||
if err := d.GDB.Create(&cmdItem).Error; err != nil {
|
if err := d.GDB.Create(&cmdItems).Error; err != nil {
|
||||||
return nil, fmt.Errorf("erreur lors de l'insertion des items: %w", err)
|
return nil, fmt.Errorf("erreur lors de l'insertion des items: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := d.GDB.Exec(`DELETE FROM baskets WHERE username = ?`, username).Error; err != nil {
|
if err := d.GDB.Exec(`DELETE FROM baskets WHERE username = ?`, username).Error; err != nil {
|
||||||
return nil, fmt.Errorf("erreur lors du vidage du panier: %w", err)
|
return nil, fmt.Errorf("erreur lors du vidage du panier: %w", err)
|
||||||
}
|
}
|
||||||
@@ -229,33 +228,33 @@ func (d *Database) CreateCommandWithAddress(username, deliveryAddress string) (*
|
|||||||
}
|
}
|
||||||
productNames2, _ := d.GetProductNamesByIDs(productIDs2)
|
productNames2, _ := d.GetProductNamesByIDs(productIDs2)
|
||||||
|
|
||||||
|
batchItems := make([]commandItemFull, 0, len(basketItems))
|
||||||
for _, item := range basketItems {
|
for _, item := range basketItems {
|
||||||
productName := productNames2[item.ProductID]
|
productName := productNames2[item.ProductID]
|
||||||
if productName == "" {
|
if productName == "" {
|
||||||
productName = fmt.Sprintf("Produit #%d", item.ProductID)
|
productName = fmt.Sprintf("Produit #%d", item.ProductID)
|
||||||
}
|
}
|
||||||
|
batchItems = append(batchItems, commandItemFull{
|
||||||
err = d.InsertCommandItemWithClientInfo(
|
CommandID: commandID,
|
||||||
commandID,
|
Produit: productName,
|
||||||
productName,
|
ProductID: item.ProductID,
|
||||||
item.ProductID,
|
Quantite: item.Quantity,
|
||||||
item.Quantity,
|
Prix: item.Price,
|
||||||
item.Price,
|
IsReward: item.IsReward,
|
||||||
item.IsReward,
|
RewardPoolKey: item.RewardPoolKey,
|
||||||
item.RewardPoolKey,
|
ClientUsername: username,
|
||||||
username,
|
ClientNom: clientNom,
|
||||||
clientNom,
|
ClientPrenom: clientPrenom,
|
||||||
clientPrenom,
|
ClientTelephone: clientTelephone,
|
||||||
clientTelephone,
|
DeliveryAddress: deliveryAddress,
|
||||||
deliveryAddress,
|
Status: "pending",
|
||||||
)
|
})
|
||||||
if err != nil {
|
|
||||||
log.Printf("❌ Erreur INSERT command_items: %v", err)
|
|
||||||
return nil, fmt.Errorf("erreur insertion items: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Stock déjà déduit à l'ajout au panier — ne pas déduire une seconde fois ici.
|
// Stock déjà déduit à l'ajout au panier — ne pas déduire une seconde fois ici.
|
||||||
}
|
}
|
||||||
|
if err := d.InsertCommandItemsBatch(batchItems); err != nil {
|
||||||
|
log.Printf("❌ Erreur INSERT command_items batch: %v", err)
|
||||||
|
return nil, fmt.Errorf("erreur insertion items: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
if err := d.GDB.Exec(`DELETE FROM baskets WHERE username = ?`, username).Error; err != nil {
|
if err := d.GDB.Exec(`DELETE FROM baskets WHERE username = ?`, username).Error; err != nil {
|
||||||
log.Printf("⚠️ Erreur vidage panier: %v", err)
|
log.Printf("⚠️ Erreur vidage panier: %v", err)
|
||||||
|
|||||||
@@ -73,14 +73,21 @@ func (d *Database) CreateProduct(product any) error {
|
|||||||
p.SetCreatedAt(result.CreatedAt)
|
p.SetCreatedAt(result.CreatedAt)
|
||||||
p.SetUpdatedAt(result.UpdatedAt)
|
p.SetUpdatedAt(result.UpdatedAt)
|
||||||
|
|
||||||
for i, price := range p.GetPrices() {
|
if rawPrices := p.GetPrices(); len(rawPrices) > 0 {
|
||||||
err := d.GDB.Exec(`INSERT INTO product_prices (product_id, quantity, price, active_price) VALUES (?, ?, ?, ?)`,
|
priceRows := make([]models.ProductPrice, len(rawPrices))
|
||||||
result.ID, price.Quantity, price.Price, price.ActivePrice).Error
|
for i, price := range rawPrices {
|
||||||
if err != nil {
|
priceRows[i] = models.ProductPrice{
|
||||||
log.Printf("❌ [DB CreateProduct] Erreur insertion prix[%d]: %v", i, err)
|
ProductID: result.ID,
|
||||||
|
Quantity: price.Quantity,
|
||||||
|
Price: price.Price,
|
||||||
|
ActivePrice: price.ActivePrice,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if err := d.GDB.Create(&priceRows).Error; err != nil {
|
||||||
|
log.Printf("❌ [DB CreateProduct] Erreur insertion prix batch: %v", err)
|
||||||
return fmt.Errorf("erreur insertion prix: %v", err)
|
return fmt.Errorf("erreur insertion prix: %v", err)
|
||||||
}
|
}
|
||||||
log.Printf("✅ [DB CreateProduct] Prix[%d] inséré: quantity=%g, price=%.2f", i, price.Quantity, price.Price)
|
log.Printf("✅ [DB CreateProduct] %d prix insérés", len(priceRows))
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Printf("🎉 [DB CreateProduct] Produit créé avec succès! ID=%d", result.ID)
|
log.Printf("🎉 [DB CreateProduct] Produit créé avec succès! ID=%d", result.ID)
|
||||||
@@ -216,9 +223,17 @@ func (d *Database) UpdateProduct(productID int, name, category, description, uni
|
|||||||
|
|
||||||
d.GDB.Exec(`DELETE FROM product_prices WHERE product_id = ?`, productID)
|
d.GDB.Exec(`DELETE FROM product_prices WHERE product_id = ?`, productID)
|
||||||
|
|
||||||
for _, price := range prices {
|
if len(prices) > 0 {
|
||||||
if err := d.GDB.Exec(`INSERT INTO product_prices (product_id, quantity, price, active_price) VALUES (?, ?, ?, ?)`,
|
priceRows := make([]models.ProductPrice, len(prices))
|
||||||
productID, price.Quantity, price.Price, price.ActivePrice).Error; err != nil {
|
for i, price := range prices {
|
||||||
|
priceRows[i] = models.ProductPrice{
|
||||||
|
ProductID: productID,
|
||||||
|
Quantity: price.Quantity,
|
||||||
|
Price: price.Price,
|
||||||
|
ActivePrice: price.ActivePrice,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if err := d.GDB.Create(&priceRows).Error; err != nil {
|
||||||
return fmt.Errorf("erreur insertion prix: %w", err)
|
return fmt.Errorf("erreur insertion prix: %w", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user