Files
solosw 99b11b04e4 1.0
2026-01-05 14:11:34 +08:00

169 lines
5.0 KiB
Go

package handlers
import (
"awesomeProject/internal/common"
"awesomeProject/internal/config"
"awesomeProject/internal/models"
"fmt"
"gorm.io/gorm"
"log"
"strconv"
)
// PriceConfigHandler 价格配置变更处理器
type PriceConfigHandler struct{}
// GetConfigKeys 返回该处理器关心的配置键
func (h *PriceConfigHandler) GetConfigKeys() []string {
return []string{
config.ConfigPriceIncreaseRate,
config.ConfigMinSalePrice,
}
}
// Handle 处理配置变更事件
func (h *PriceConfigHandler) Handle(event config.ConfigEvent) error {
switch event.ConfigKey {
case config.ConfigPriceIncreaseRate:
return h.handlePriceIncreaseRateChange(event)
case config.ConfigMinSalePrice:
return h.handleMinSalePriceChange(event)
default:
return fmt.Errorf("未知的配置键: %s", event.ConfigKey)
}
}
// handlePriceIncreaseRateChange 处理价格增长率变更
func (h *PriceConfigHandler) handlePriceIncreaseRateChange(event config.ConfigEvent) error {
log.Printf("价格增长率配置变更: %v -> %v", event.OldValue, event.NewValue)
// 解析新的增长率
newRateStr, ok := event.NewValue.(string)
if !ok {
return fmt.Errorf("价格增长率配置值类型错误")
}
newRate, err := strconv.ParseFloat(newRateStr, 64)
if err != nil {
return fmt.Errorf("价格增长率配置值格式错误: %v", err)
}
db := common.GetDB()
var secondList []models.SecondaryProduct
result := db.Where("status = ? AND quantity > ?", 1, 0).Find(&secondList) // 修正:传递切片指针而不是值
if result.Error != nil {
log.Printf("查询失败: %v", result.Error)
return result.Error
}
// 如果没有找到符合条件的记录,直接返回
if len(secondList) == 0 {
log.Printf("没有找到需要更新的二级商品")
return nil
}
tx := db.Begin()
for _, product := range secondList {
// 检查商品是否有关联的仓库记录
if product.UserWarehouseID == 0 {
log.Printf("二级商品 ID: %d 没有关联的仓库记录,跳过", product.ID)
continue
}
var warehouse models.UserWarehouse
if err := tx.Where("id = ?", product.UserWarehouseID).First(&warehouse).Error; err != nil {
tx.Rollback()
log.Printf("查询仓库记录失败,ID: %d, 错误: %v", product.UserWarehouseID, err)
return err
}
newWarehousePrice := warehouse.PurchasePrice + newRate
if err := tx.Model(&models.UserWarehouse{}).Where("id = ?", product.UserWarehouseID).
Update("quantity", product.Quantity+warehouse.Quantity).Error; err != nil {
tx.Rollback()
log.Printf("更新仓库价格失败,ID: %d, 错误: %v", product.UserWarehouseID, err)
return err
}
log.Printf("已更新仓库商品 ID: %d 的价格,新价格: %.2f", product.UserWarehouseID, newWarehousePrice)
tx.Model(&models.SecondaryProduct{}).Where("id = ?", product.ID).Update("status", 0)
}
tx.Model(&models.UserWarehouse{}).Where("quantity>?", 0).Update("status", 1).
Update("warehouse_price", gorm.Expr("purchase_price + ?", newRate))
tx.Commit()
if err := tx.Commit().Error; err != nil {
return err
}
log.Printf("价格增长率已更新为: %.2f%%", newRate)
return nil
}
// handleMinSalePriceChange 处理最低销售价格变更
func (h *PriceConfigHandler) handleMinSalePriceChange(event config.ConfigEvent) error {
log.Printf("最低销售价格配置变更: %v -> %v", event.OldValue, event.NewValue)
// 解析新的最低价格
newPriceStr, ok := event.NewValue.(string)
if !ok {
return fmt.Errorf("最低销售价格配置值类型错误")
}
newPrice, err := strconv.ParseFloat(newPriceStr, 64)
if err != nil {
return fmt.Errorf("最低销售价格配置值格式错误: %v", err)
}
// 验证价格范围
if newPrice < 0 {
return fmt.Errorf("最低销售价格不能为负数")
}
// 业务逻辑:检查并更新现有的低于最低价格的商品
if err := h.updateLowPriceProducts(newPrice); err != nil {
log.Printf("更新低价商品失败: %v", err)
// 不返回错误,避免阻塞其他处理器
}
log.Printf("最低销售价格已更新为: %.2f", newPrice)
return nil
}
// updateLowPriceProducts 更新低于最低价格的商品
func (h *PriceConfigHandler) updateLowPriceProducts(minPrice float64) error {
db := common.GetDB()
// 查找低于最低价格的二级商品
var lowPriceProducts []models.SecondaryProduct
err := db.Where("sale_price < ? AND status = 1", minPrice).Find(&lowPriceProducts).Error
if err != nil {
return fmt.Errorf("查询低价商品失败: %v", err)
}
if len(lowPriceProducts) == 0 {
log.Printf("没有发现低于最低价格(%.2f)的商品", minPrice)
return nil
}
// 批量更新这些商品的状态为下架
result := db.Model(&models.SecondaryProduct{}).
Where("sale_price < ? AND status = 1", minPrice).
Update("status", 0)
if result.Error != nil {
return fmt.Errorf("批量下架低价商品失败: %v", result.Error)
}
log.Printf("已下架 %d 个低于最低价格的商品", result.RowsAffected)
// 这里可以添加更多逻辑,比如:
// 1. 发送通知给商品卖家
// 2. 记录下架日志
// 3. 更新相关统计数据
return nil
}