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)
|
||||
|
||||
Reference in New Issue
Block a user