feat: 添加购买分组功能,替代跨分类购物选项
This commit is contained in:
@@ -49,17 +49,17 @@ func (h *CategoryHandler) Create(c *gin.Context) {
|
||||
}
|
||||
|
||||
category := models.Category{
|
||||
Name: req.Name,
|
||||
Description: req.Description,
|
||||
ParentID: req.ParentID,
|
||||
MinAmount: req.MinAmount,
|
||||
MaxAmount: req.MaxAmount,
|
||||
MinQuantity: req.MinQuantity,
|
||||
MaxQuantity: req.MaxQuantity,
|
||||
MinWeight: req.MinWeight,
|
||||
MaxWeight: req.MaxWeight,
|
||||
AllowCrossCategory: req.AllowCrossCategory != nil && *req.AllowCrossCategory,
|
||||
SortOrder: req.SortOrder,
|
||||
Name: req.Name,
|
||||
Description: req.Description,
|
||||
ParentID: req.ParentID,
|
||||
PurchaseGroupID: req.PurchaseGroupID,
|
||||
MinAmount: req.MinAmount,
|
||||
MaxAmount: req.MaxAmount,
|
||||
MinQuantity: req.MinQuantity,
|
||||
MaxQuantity: req.MaxQuantity,
|
||||
MinWeight: req.MinWeight,
|
||||
MaxWeight: req.MaxWeight,
|
||||
SortOrder: req.SortOrder,
|
||||
}
|
||||
|
||||
if err := utils.DB.Create(&category).Error; err != nil {
|
||||
@@ -123,8 +123,8 @@ func (h *CategoryHandler) Update(c *gin.Context) {
|
||||
if req.SortOrder != nil {
|
||||
updates["sort_order"] = *req.SortOrder
|
||||
}
|
||||
if req.AllowCrossCategory != nil {
|
||||
updates["allow_cross_category"] = *req.AllowCrossCategory
|
||||
if req.PurchaseGroupID != nil {
|
||||
updates["purchase_group_id"] = *req.PurchaseGroupID
|
||||
}
|
||||
|
||||
utils.DB.Model(&category).Updates(updates)
|
||||
|
||||
@@ -0,0 +1,105 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"sale/internal/models"
|
||||
"sale/internal/schemas"
|
||||
"sale/internal/utils"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type PurchaseGroupHandler struct{}
|
||||
|
||||
func NewPurchaseGroupHandler() *PurchaseGroupHandler {
|
||||
return &PurchaseGroupHandler{}
|
||||
}
|
||||
|
||||
func (h *PurchaseGroupHandler) List(c *gin.Context) {
|
||||
var groups []models.PurchaseGroup
|
||||
utils.DB.Order("sort_order ASC, id ASC").Preload("Categories").Find(&groups)
|
||||
c.JSON(http.StatusOK, gin.H{"data": groups})
|
||||
}
|
||||
|
||||
func (h *PurchaseGroupHandler) Create(c *gin.Context) {
|
||||
var req schemas.CreatePurchaseGroupRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
var existing models.PurchaseGroup
|
||||
if err := utils.DB.Where("name = ?", req.Name).First(&existing).Error; err == nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "分组名称已存在"})
|
||||
return
|
||||
}
|
||||
|
||||
group := models.PurchaseGroup{
|
||||
Name: req.Name,
|
||||
Description: req.Description,
|
||||
SortOrder: req.SortOrder,
|
||||
}
|
||||
|
||||
if err := utils.DB.Create(&group).Error; err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to create purchase group"})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusCreated, gin.H{"data": group})
|
||||
}
|
||||
|
||||
func (h *PurchaseGroupHandler) Update(c *gin.Context) {
|
||||
id, _ := strconv.Atoi(c.Param("id"))
|
||||
var group models.PurchaseGroup
|
||||
if err := utils.DB.First(&group, id).Error; err != nil {
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "Purchase group not found"})
|
||||
return
|
||||
}
|
||||
|
||||
var req schemas.UpdatePurchaseGroupRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
if req.Name != nil && *req.Name != group.Name {
|
||||
var existing models.PurchaseGroup
|
||||
if err := utils.DB.Where("name = ? AND id != ?", *req.Name, id).First(&existing).Error; err == nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "分组名称已存在"})
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
updates := make(map[string]interface{})
|
||||
if req.Name != nil {
|
||||
updates["name"] = *req.Name
|
||||
}
|
||||
if req.Description != nil {
|
||||
updates["description"] = *req.Description
|
||||
}
|
||||
if req.SortOrder != nil {
|
||||
updates["sort_order"] = *req.SortOrder
|
||||
}
|
||||
|
||||
utils.DB.Model(&group).Updates(updates)
|
||||
c.JSON(http.StatusOK, gin.H{"data": group})
|
||||
}
|
||||
|
||||
func (h *PurchaseGroupHandler) Delete(c *gin.Context) {
|
||||
id, _ := strconv.Atoi(c.Param("id"))
|
||||
|
||||
var categoryCount int64
|
||||
utils.DB.Model(&models.Category{}).Where("purchase_group_id = ?", id).Count(&categoryCount)
|
||||
if categoryCount > 0 {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "该分组下有分类,无法删除"})
|
||||
return
|
||||
}
|
||||
|
||||
if err := utils.DB.Delete(&models.PurchaseGroup{}, id).Error; err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to delete purchase group"})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{"message": "Purchase group deleted successfully"})
|
||||
}
|
||||
@@ -26,6 +26,7 @@ func SetupRoutes(r *gin.Engine) {
|
||||
paymentChannelHandler := handlers.NewPaymentChannelHandler()
|
||||
paymentHandler := handlers.NewPaymentHandler()
|
||||
logHandler := handlers.NewLogHandler()
|
||||
purchaseGroupHandler := handlers.NewPurchaseGroupHandler()
|
||||
|
||||
r.Use(middlewares.CORSMiddleware())
|
||||
|
||||
@@ -154,6 +155,14 @@ func SetupRoutes(r *gin.Engine) {
|
||||
adminCategories.DELETE("/:id", categoryHandler.Delete)
|
||||
}
|
||||
|
||||
adminPurchaseGroups := admin.Group("/purchase-groups")
|
||||
{
|
||||
adminPurchaseGroups.GET("", purchaseGroupHandler.List)
|
||||
adminPurchaseGroups.POST("", purchaseGroupHandler.Create)
|
||||
adminPurchaseGroups.PUT("/:id", purchaseGroupHandler.Update)
|
||||
adminPurchaseGroups.DELETE("/:id", purchaseGroupHandler.Delete)
|
||||
}
|
||||
|
||||
adminBrands := admin.Group("/brands")
|
||||
{
|
||||
adminBrands.GET("", brandHandler.List)
|
||||
|
||||
@@ -7,23 +7,24 @@ import (
|
||||
)
|
||||
|
||||
type Category struct {
|
||||
ID uint `gorm:"primaryKey" json:"id"`
|
||||
Name string `gorm:"size:100;not null;uniqueIndex" json:"name"`
|
||||
Description string `json:"description"`
|
||||
ParentID *uint `json:"parent_id"`
|
||||
MinAmount *float64 `json:"min_amount"`
|
||||
MaxAmount *float64 `json:"max_amount"`
|
||||
MinQuantity *int `json:"min_quantity"`
|
||||
MaxQuantity *int `json:"max_quantity"`
|
||||
MinWeight *float64 `json:"min_weight"`
|
||||
MaxWeight *float64 `json:"max_weight"`
|
||||
AllowCrossCategory bool `gorm:"default:false" json:"allow_cross_category"`
|
||||
SortOrder int `gorm:"default:0" json:"sort_order"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
DeletedAt gorm.DeletedAt `gorm:"index" json:"-"`
|
||||
ProductCount int64 `gorm:"-" json:"product_count"`
|
||||
Children []Category `gorm:"foreignKey:ParentID" json:"children,omitempty"`
|
||||
ID uint `gorm:"primaryKey" json:"id"`
|
||||
Name string `gorm:"size:100;not null;uniqueIndex" json:"name"`
|
||||
Description string `json:"description"`
|
||||
ParentID *uint `json:"parent_id"`
|
||||
PurchaseGroupID *uint `json:"purchase_group_id"`
|
||||
PurchaseGroup *PurchaseGroup `gorm:"foreignKey:PurchaseGroupID" json:"purchase_group,omitempty"`
|
||||
MinAmount *float64 `json:"min_amount"`
|
||||
MaxAmount *float64 `json:"max_amount"`
|
||||
MinQuantity *int `json:"min_quantity"`
|
||||
MaxQuantity *int `json:"max_quantity"`
|
||||
MinWeight *float64 `json:"min_weight"`
|
||||
MaxWeight *float64 `json:"max_weight"`
|
||||
SortOrder int `gorm:"default:0" json:"sort_order"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
DeletedAt gorm.DeletedAt `gorm:"index" json:"-"`
|
||||
ProductCount int64 `gorm:"-" json:"product_count"`
|
||||
Children []Category `gorm:"foreignKey:ParentID" json:"children,omitempty"`
|
||||
}
|
||||
|
||||
func (Category) TableName() string {
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type PurchaseGroup struct {
|
||||
ID uint `gorm:"primaryKey" json:"id"`
|
||||
Name string `gorm:"size:100;not null;uniqueIndex" json:"name"`
|
||||
Description string `json:"description"`
|
||||
SortOrder int `gorm:"default:0" json:"sort_order"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
DeletedAt gorm.DeletedAt `gorm:"index" json:"-"`
|
||||
Categories []Category `gorm:"foreignKey:PurchaseGroupID" json:"categories,omitempty"`
|
||||
}
|
||||
|
||||
func (PurchaseGroup) TableName() string {
|
||||
return "purchase_groups"
|
||||
}
|
||||
@@ -1,31 +1,43 @@
|
||||
package schemas
|
||||
|
||||
type CreateCategoryRequest struct {
|
||||
Name string `json:"name" binding:"required"`
|
||||
Description string `json:"description"`
|
||||
ParentID *uint `json:"parent_id"`
|
||||
MinAmount *float64 `json:"min_amount"`
|
||||
MaxAmount *float64 `json:"max_amount"`
|
||||
MinQuantity *int `json:"min_quantity"`
|
||||
MaxQuantity *int `json:"max_quantity"`
|
||||
MinWeight *float64 `json:"min_weight"`
|
||||
MaxWeight *float64 `json:"max_weight"`
|
||||
AllowCrossCategory *bool `json:"allow_cross_category"`
|
||||
SortOrder int `json:"sort_order"`
|
||||
Name string `json:"name" binding:"required"`
|
||||
Description string `json:"description"`
|
||||
ParentID *uint `json:"parent_id"`
|
||||
PurchaseGroupID *uint `json:"purchase_group_id"`
|
||||
MinAmount *float64 `json:"min_amount"`
|
||||
MaxAmount *float64 `json:"max_amount"`
|
||||
MinQuantity *int `json:"min_quantity"`
|
||||
MaxQuantity *int `json:"max_quantity"`
|
||||
MinWeight *float64 `json:"min_weight"`
|
||||
MaxWeight *float64 `json:"max_weight"`
|
||||
SortOrder int `json:"sort_order"`
|
||||
}
|
||||
|
||||
type UpdateCategoryRequest struct {
|
||||
Name *string `json:"name"`
|
||||
Description *string `json:"description"`
|
||||
ParentID *uint `json:"parent_id"`
|
||||
MinAmount *float64 `json:"min_amount"`
|
||||
MaxAmount *float64 `json:"max_amount"`
|
||||
MinQuantity *int `json:"min_quantity"`
|
||||
MaxQuantity *int `json:"max_quantity"`
|
||||
MinWeight *float64 `json:"min_weight"`
|
||||
MaxWeight *float64 `json:"max_weight"`
|
||||
AllowCrossCategory *bool `json:"allow_cross_category"`
|
||||
SortOrder *int `json:"sort_order"`
|
||||
Name *string `json:"name"`
|
||||
Description *string `json:"description"`
|
||||
ParentID *uint `json:"parent_id"`
|
||||
PurchaseGroupID *uint `json:"purchase_group_id"`
|
||||
MinAmount *float64 `json:"min_amount"`
|
||||
MaxAmount *float64 `json:"max_amount"`
|
||||
MinQuantity *int `json:"min_quantity"`
|
||||
MaxQuantity *int `json:"max_quantity"`
|
||||
MinWeight *float64 `json:"min_weight"`
|
||||
MaxWeight *float64 `json:"max_weight"`
|
||||
SortOrder *int `json:"sort_order"`
|
||||
}
|
||||
|
||||
type CreatePurchaseGroupRequest struct {
|
||||
Name string `json:"name" binding:"required"`
|
||||
Description string `json:"description"`
|
||||
SortOrder int `json:"sort_order"`
|
||||
}
|
||||
|
||||
type UpdatePurchaseGroupRequest struct {
|
||||
Name *string `json:"name"`
|
||||
Description *string `json:"description"`
|
||||
SortOrder *int `json:"sort_order"`
|
||||
}
|
||||
|
||||
type CreateProductRequest struct {
|
||||
|
||||
@@ -63,6 +63,7 @@ func AutoMigrate() {
|
||||
err := DB.AutoMigrate(
|
||||
&models.User{},
|
||||
&models.Category{},
|
||||
&models.PurchaseGroup{},
|
||||
&models.Brand{},
|
||||
&models.Product{},
|
||||
&models.ProductCustomField{},
|
||||
|
||||
Reference in New Issue
Block a user