186 lines
4.3 KiB
Go
186 lines
4.3 KiB
Go
|
|
package handler
|
|||
|
|
|
|||
|
|
import (
|
|||
|
|
"dianshang/internal/service"
|
|||
|
|
"dianshang/pkg/response"
|
|||
|
|
"dianshang/pkg/utils"
|
|||
|
|
|
|||
|
|
"github.com/gin-gonic/gin"
|
|||
|
|
)
|
|||
|
|
|
|||
|
|
// CouponHandler 优惠券处理器
|
|||
|
|
type CouponHandler struct {
|
|||
|
|
couponService *service.CouponService
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// NewCouponHandler 创建优惠券处理器
|
|||
|
|
func NewCouponHandler(couponService *service.CouponService) *CouponHandler {
|
|||
|
|
return &CouponHandler{
|
|||
|
|
couponService: couponService,
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// GetAvailableCoupons 获取可用优惠券列表
|
|||
|
|
func (h *CouponHandler) GetAvailableCoupons(c *gin.Context) {
|
|||
|
|
// 尝试获取用户ID,如果有用户登录则返回包含已领取状态的数据
|
|||
|
|
userIDValue, exists := c.Get("user_id")
|
|||
|
|
var userID uint = 0
|
|||
|
|
if exists {
|
|||
|
|
userID = userIDValue.(uint)
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
coupons, err := h.couponService.GetAvailableCouponsWithUserStatus(userID)
|
|||
|
|
if err != nil {
|
|||
|
|
response.ErrorWithMessage(c, response.ERROR, err.Error())
|
|||
|
|
return
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
response.Success(c, coupons)
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// GetUserCoupons 获取用户优惠券
|
|||
|
|
func (h *CouponHandler) GetUserCoupons(c *gin.Context) {
|
|||
|
|
userIDValue, exists := c.Get("user_id")
|
|||
|
|
if !exists {
|
|||
|
|
response.Unauthorized(c)
|
|||
|
|
return
|
|||
|
|
}
|
|||
|
|
userID := userIDValue.(uint)
|
|||
|
|
|
|||
|
|
status := utils.StringToInt(c.DefaultQuery("status", "1")) // 1未使用,2已使用,3已过期
|
|||
|
|
coupons, err := h.couponService.GetUserCoupons(userID, status)
|
|||
|
|
if err != nil {
|
|||
|
|
response.ErrorWithMessage(c, response.ERROR, err.Error())
|
|||
|
|
return
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
response.Success(c, coupons)
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// ReceiveCoupon 领取优惠券
|
|||
|
|
func (h *CouponHandler) ReceiveCoupon(c *gin.Context) {
|
|||
|
|
userIDValue, exists := c.Get("user_id")
|
|||
|
|
if !exists {
|
|||
|
|
response.Unauthorized(c)
|
|||
|
|
return
|
|||
|
|
}
|
|||
|
|
userID := userIDValue.(uint)
|
|||
|
|
|
|||
|
|
couponID := utils.StringToUint(c.Param("id"))
|
|||
|
|
if couponID == 0 {
|
|||
|
|
response.BadRequest(c, "无效的优惠券ID")
|
|||
|
|
return
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
err := h.couponService.ReceiveCoupon(userID, couponID)
|
|||
|
|
if err != nil {
|
|||
|
|
response.ErrorWithMessage(c, response.ERROR, err.Error())
|
|||
|
|
return
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
response.Success(c, nil)
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// UseCoupon 使用优惠券
|
|||
|
|
func (h *CouponHandler) UseCoupon(c *gin.Context) {
|
|||
|
|
userIDValue, exists := c.Get("user_id")
|
|||
|
|
if !exists {
|
|||
|
|
response.Unauthorized(c)
|
|||
|
|
return
|
|||
|
|
}
|
|||
|
|
userID := userIDValue.(uint)
|
|||
|
|
|
|||
|
|
userCouponID := utils.StringToUint(c.Param("id"))
|
|||
|
|
if userCouponID == 0 {
|
|||
|
|
response.BadRequest(c, "无效的优惠券ID")
|
|||
|
|
return
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
var req struct {
|
|||
|
|
OrderID uint `json:"order_id" binding:"required"`
|
|||
|
|
}
|
|||
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|||
|
|
response.BadRequest(c, "参数错误")
|
|||
|
|
return
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
err := h.couponService.UseCoupon(userID, userCouponID, req.OrderID)
|
|||
|
|
if err != nil {
|
|||
|
|
response.ErrorWithMessage(c, response.ERROR, err.Error())
|
|||
|
|
return
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
response.Success(c, nil)
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// GetAvailableCouponsForOrder 获取订单可用优惠券
|
|||
|
|
func (h *CouponHandler) GetAvailableCouponsForOrder(c *gin.Context) {
|
|||
|
|
userIDValue, exists := c.Get("user_id")
|
|||
|
|
if !exists {
|
|||
|
|
response.Unauthorized(c)
|
|||
|
|
return
|
|||
|
|
}
|
|||
|
|
userID := userIDValue.(uint)
|
|||
|
|
|
|||
|
|
orderAmountStr := c.Query("order_amount")
|
|||
|
|
if orderAmountStr == "" {
|
|||
|
|
response.BadRequest(c, "订单金额不能为空")
|
|||
|
|
return
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
orderAmount := utils.StringToFloat64(orderAmountStr)
|
|||
|
|
if orderAmount <= 0 {
|
|||
|
|
response.BadRequest(c, "订单金额必须大于0")
|
|||
|
|
return
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
coupons, err := h.couponService.GetAvailableCouponsForOrder(userID, orderAmount)
|
|||
|
|
if err != nil {
|
|||
|
|
response.ErrorWithMessage(c, response.ERROR, err.Error())
|
|||
|
|
return
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
response.Success(c, coupons)
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// ValidateCoupon 验证优惠券
|
|||
|
|
func (h *CouponHandler) ValidateCoupon(c *gin.Context) {
|
|||
|
|
userIDValue, exists := c.Get("user_id")
|
|||
|
|
if !exists {
|
|||
|
|
response.Unauthorized(c)
|
|||
|
|
return
|
|||
|
|
}
|
|||
|
|
userID := userIDValue.(uint)
|
|||
|
|
|
|||
|
|
userCouponID := utils.StringToUint(c.Param("id"))
|
|||
|
|
if userCouponID == 0 {
|
|||
|
|
response.BadRequest(c, "无效的优惠券ID")
|
|||
|
|
return
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
orderAmountStr := c.Query("order_amount")
|
|||
|
|
if orderAmountStr == "" {
|
|||
|
|
response.BadRequest(c, "订单金额不能为空")
|
|||
|
|
return
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
orderAmount := utils.StringToFloat64(orderAmountStr)
|
|||
|
|
if orderAmount <= 0 {
|
|||
|
|
response.BadRequest(c, "订单金额必须大于0")
|
|||
|
|
return
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
userCoupon, discountAmount, err := h.couponService.ValidateCoupon(userID, userCouponID, orderAmount)
|
|||
|
|
if err != nil {
|
|||
|
|
response.ErrorWithMessage(c, response.ERROR, err.Error())
|
|||
|
|
return
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
result := map[string]interface{}{
|
|||
|
|
"user_coupon": userCoupon,
|
|||
|
|
"discount_amount": discountAmount,
|
|||
|
|
"can_use": true,
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
response.Success(c, result)
|
|||
|
|
}
|