313 lines
8.5 KiB
Go
313 lines
8.5 KiB
Go
package handler
|
|
|
|
import (
|
|
"dianshang/internal/service"
|
|
"dianshang/pkg/response"
|
|
"dianshang/pkg/utils"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
// PointsHandler 积分处理器
|
|
type PointsHandler struct {
|
|
pointsService *service.PointsService
|
|
}
|
|
|
|
// NewPointsHandler 创建积分处理器
|
|
func NewPointsHandler(pointsService *service.PointsService) *PointsHandler {
|
|
return &PointsHandler{
|
|
pointsService: pointsService,
|
|
}
|
|
}
|
|
|
|
// GetUserPoints 获取用户积分
|
|
// @Summary 获取用户积分
|
|
// @Description 获取当前用户的积分余额
|
|
// @Tags 积分
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Success 200 {object} response.Response
|
|
// @Router /api/points [get]
|
|
func (h *PointsHandler) GetUserPoints(c *gin.Context) {
|
|
userIDValue, exists := c.Get("user_id")
|
|
if !exists {
|
|
response.Unauthorized(c)
|
|
return
|
|
}
|
|
userID := userIDValue.(uint)
|
|
|
|
points, err := h.pointsService.GetUserPoints(userID)
|
|
if err != nil {
|
|
response.ErrorWithMessage(c, response.ERROR, err.Error())
|
|
return
|
|
}
|
|
|
|
response.Success(c, gin.H{
|
|
"points": points,
|
|
})
|
|
}
|
|
|
|
// GetPointsOverview 获取积分概览
|
|
// @Summary 获取积分概览
|
|
// @Description 获取用户积分概览信息,包括当前积分、总获得、总消费
|
|
// @Tags 积分
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Success 200 {object} response.Response
|
|
// @Router /api/points/overview [get]
|
|
func (h *PointsHandler) GetPointsOverview(c *gin.Context) {
|
|
userIDValue, exists := c.Get("user_id")
|
|
if !exists {
|
|
response.Unauthorized(c)
|
|
return
|
|
}
|
|
userID := userIDValue.(uint)
|
|
|
|
overview, err := h.pointsService.GetPointsOverview(userID)
|
|
if err != nil {
|
|
response.ErrorWithMessage(c, response.ERROR, err.Error())
|
|
return
|
|
}
|
|
|
|
response.Success(c, overview)
|
|
}
|
|
|
|
// GetPointsHistory 获取积分历史记录
|
|
// @Summary 获取积分历史记录
|
|
// @Description 获取用户的积分历史记录列表
|
|
// @Tags 积分
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Param page query int false "页码" default(1)
|
|
// @Param page_size query int false "每页数量" default(20)
|
|
// @Success 200 {object} response.Response
|
|
// @Router /api/points/history [get]
|
|
func (h *PointsHandler) GetPointsHistory(c *gin.Context) {
|
|
userIDValue, exists := c.Get("user_id")
|
|
if !exists {
|
|
response.Unauthorized(c)
|
|
return
|
|
}
|
|
userID := userIDValue.(uint)
|
|
|
|
page := utils.StringToInt(c.DefaultQuery("page", "1"))
|
|
pageSize := utils.StringToInt(c.DefaultQuery("page_size", "20"))
|
|
|
|
histories, pagination, err := h.pointsService.GetPointsHistory(userID, page, pageSize)
|
|
if err != nil {
|
|
response.ErrorWithMessage(c, response.ERROR, err.Error())
|
|
return
|
|
}
|
|
|
|
response.Page(c, histories, pagination["total"].(int64), pagination["page"].(int), pagination["page_size"].(int))
|
|
}
|
|
|
|
// GetPointsRules 获取积分规则列表
|
|
// @Summary 获取积分规则列表
|
|
// @Description 获取积分获得和消费规则列表
|
|
// @Tags 积分
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Success 200 {object} response.Response
|
|
// @Router /api/points/rules [get]
|
|
func (h *PointsHandler) GetPointsRules(c *gin.Context) {
|
|
rules, err := h.pointsService.GetPointsRules()
|
|
if err != nil {
|
|
response.ErrorWithMessage(c, response.ERROR, err.Error())
|
|
return
|
|
}
|
|
|
|
response.Success(c, rules)
|
|
}
|
|
|
|
// GetPointsExchangeList 获取积分兑换商品列表
|
|
// @Summary 获取积分兑换商品列表
|
|
// @Description 获取可用积分兑换的商品列表
|
|
// @Tags 积分
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Success 200 {object} response.Response
|
|
// @Router /api/points/exchange [get]
|
|
func (h *PointsHandler) GetPointsExchangeList(c *gin.Context) {
|
|
exchanges, err := h.pointsService.GetPointsExchangeList()
|
|
if err != nil {
|
|
response.ErrorWithMessage(c, response.ERROR, err.Error())
|
|
return
|
|
}
|
|
|
|
response.Success(c, exchanges)
|
|
}
|
|
|
|
// ExchangePoints 积分兑换
|
|
// @Summary 积分兑换
|
|
// @Description 使用积分兑换商品
|
|
// @Tags 积分
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Param id path int true "兑换商品ID"
|
|
// @Success 200 {object} response.Response
|
|
// @Router /api/points/exchange/{id} [post]
|
|
func (h *PointsHandler) ExchangePoints(c *gin.Context) {
|
|
userIDValue, exists := c.Get("user_id")
|
|
if !exists {
|
|
response.Unauthorized(c)
|
|
return
|
|
}
|
|
userID := userIDValue.(uint)
|
|
|
|
exchangeID := utils.StringToUint(c.Param("id"))
|
|
if exchangeID == 0 {
|
|
response.BadRequest(c, "无效的兑换商品ID")
|
|
return
|
|
}
|
|
|
|
err := h.pointsService.ExchangePoints(userID, exchangeID)
|
|
if err != nil {
|
|
response.ErrorWithMessage(c, response.ERROR, err.Error())
|
|
return
|
|
}
|
|
|
|
response.SuccessWithMessage(c, "兑换成功", nil)
|
|
}
|
|
|
|
// GetUserExchangeRecords 获取用户兑换记录
|
|
// @Summary 获取用户兑换记录
|
|
// @Description 获取用户的积分兑换记录列表
|
|
// @Tags 积分
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Param page query int false "页码" default(1)
|
|
// @Param page_size query int false "每页数量" default(20)
|
|
// @Success 200 {object} response.Response
|
|
// @Router /api/points/exchange/records [get]
|
|
func (h *PointsHandler) GetUserExchangeRecords(c *gin.Context) {
|
|
userIDValue, exists := c.Get("user_id")
|
|
if !exists {
|
|
response.Unauthorized(c)
|
|
return
|
|
}
|
|
userID := userIDValue.(uint)
|
|
|
|
page := utils.StringToInt(c.DefaultQuery("page", "1"))
|
|
pageSize := utils.StringToInt(c.DefaultQuery("page_size", "20"))
|
|
|
|
records, pagination, err := h.pointsService.GetUserExchangeRecords(userID, page, pageSize)
|
|
if err != nil {
|
|
response.ErrorWithMessage(c, response.ERROR, err.Error())
|
|
return
|
|
}
|
|
|
|
response.Page(c, records, pagination["total"].(int64), pagination["page"].(int), pagination["page_size"].(int))
|
|
}
|
|
|
|
// AddPointsRequest 添加积分请求结构
|
|
type AddPointsRequest struct {
|
|
Amount int `json:"amount" binding:"required,min=1"`
|
|
Description string `json:"description" binding:"required"`
|
|
OrderNo string `json:"order_no"`
|
|
ProductName string `json:"product_name"`
|
|
}
|
|
|
|
// AddPoints 添加积分(管理员接口)
|
|
// @Summary 添加积分
|
|
// @Description 管理员为用户添加积分
|
|
// @Tags 积分
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Param id path int true "用户ID"
|
|
// @Param request body AddPointsRequest true "添加积分请求"
|
|
// @Success 200 {object} response.Response
|
|
// @Router /api/admin/points/add/{id} [post]
|
|
func (h *PointsHandler) AddPoints(c *gin.Context) {
|
|
userID := utils.StringToUint(c.Param("id"))
|
|
if userID == 0 {
|
|
response.BadRequest(c, "无效的用户ID")
|
|
return
|
|
}
|
|
|
|
var req AddPointsRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
response.BadRequest(c, "参数错误: "+err.Error())
|
|
return
|
|
}
|
|
|
|
err := h.pointsService.AddPoints(userID, req.Amount, req.Description, nil, req.OrderNo, req.ProductName)
|
|
if err != nil {
|
|
response.ErrorWithMessage(c, response.ERROR, err.Error())
|
|
return
|
|
}
|
|
|
|
response.SuccessWithMessage(c, "积分添加成功", nil)
|
|
}
|
|
|
|
// DeductPointsRequest 扣减积分请求结构
|
|
type DeductPointsRequest struct {
|
|
Amount int `json:"amount" binding:"required,min=1"`
|
|
Description string `json:"description" binding:"required"`
|
|
OrderNo string `json:"order_no"`
|
|
ProductName string `json:"product_name"`
|
|
}
|
|
|
|
// DeductPoints 扣减积分(管理员接口)
|
|
// @Summary 扣减积分
|
|
// @Description 管理员为用户扣减积分
|
|
// @Tags 积分
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Param id path int true "用户ID"
|
|
// @Param request body DeductPointsRequest true "扣减积分请求"
|
|
// @Success 200 {object} response.Response
|
|
// @Router /api/admin/points/deduct/{id} [post]
|
|
func (h *PointsHandler) DeductPoints(c *gin.Context) {
|
|
userID := utils.StringToUint(c.Param("id"))
|
|
if userID == 0 {
|
|
response.BadRequest(c, "无效的用户ID")
|
|
return
|
|
}
|
|
|
|
var req DeductPointsRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
response.BadRequest(c, "参数错误: "+err.Error())
|
|
return
|
|
}
|
|
|
|
err := h.pointsService.DeductPoints(userID, req.Amount, req.Description, nil, req.OrderNo, req.ProductName)
|
|
if err != nil {
|
|
response.ErrorWithMessage(c, response.ERROR, err.Error())
|
|
return
|
|
}
|
|
|
|
response.SuccessWithMessage(c, "积分扣减成功", nil)
|
|
}
|
|
|
|
// DailyCheckin 每日签到
|
|
// @Summary 每日签到
|
|
// @Description 每日签到
|
|
// @Tags 积分
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Success 200 {object} response.Response
|
|
// @Router /api/points/exchange/records [get]
|
|
func (h *PointsHandler) DailyCheckin(c *gin.Context) {
|
|
userIDValue, exists := c.Get("user_id")
|
|
if !exists {
|
|
response.Unauthorized(c)
|
|
return
|
|
}
|
|
userID := userIDValue.(uint)
|
|
|
|
awarded, err := h.pointsService.CheckAndGiveDailyLoginPoints(userID)
|
|
if err != nil {
|
|
response.ErrorWithMessage(c, response.ERROR, err.Error())
|
|
return
|
|
}
|
|
|
|
msg := "今日已领取,无需重复领取"
|
|
if awarded {
|
|
msg = "今日登录积分已发放"
|
|
}
|
|
|
|
response.SuccessWithMessage(c, msg, gin.H{
|
|
"awarded": awarded,
|
|
})
|
|
} |