234 lines
8.2 KiB
Go
234 lines
8.2 KiB
Go
package admin
|
|
|
|
import (
|
|
"net/http"
|
|
"strconv"
|
|
"strings"
|
|
|
|
"bindbox-game/internal/code"
|
|
"bindbox-game/internal/pkg/core"
|
|
"bindbox-game/internal/pkg/validation"
|
|
prizegrantsvc "bindbox-game/internal/service/prize_grant_activity"
|
|
)
|
|
|
|
type savePrizeGrantActivityRequest struct {
|
|
Reason string `json:"reason" binding:"required"`
|
|
Status string `json:"status"`
|
|
Rewards []prizegrantsvc.RewardInput `json:"rewards" binding:"required"`
|
|
}
|
|
|
|
type listPrizeGrantActivitiesRequest struct {
|
|
Reason string `form:"reason"`
|
|
Status string `form:"status"`
|
|
Page int `form:"page"`
|
|
PageSize int `form:"page_size"`
|
|
}
|
|
|
|
type listPrizeGrantRecordsRequest struct {
|
|
Status string `form:"status"`
|
|
Keyword string `form:"keyword"`
|
|
Page int `form:"page"`
|
|
PageSize int `form:"page_size"`
|
|
}
|
|
|
|
type markPrizeGrantUsersRequest struct {
|
|
UserIDs []int64 `json:"user_ids" binding:"required"`
|
|
}
|
|
|
|
func (h *handler) CreatePrizeGrantActivity() core.HandlerFunc {
|
|
return func(ctx core.Context) {
|
|
if ctx.SessionUserInfo().IsSuper != 1 {
|
|
ctx.AbortWithError(core.Error(http.StatusForbidden, code.AuthorizationError, "无权限操作"))
|
|
return
|
|
}
|
|
req := new(savePrizeGrantActivityRequest)
|
|
if err := ctx.ShouldBindJSON(req); err != nil {
|
|
ctx.AbortWithError(core.Error(http.StatusBadRequest, code.ParamBindError, validation.Error(err)))
|
|
return
|
|
}
|
|
item, err := h.prizeGrant.CreateActivity(ctx.RequestContext(), prizegrantsvc.SaveActivityRequest{Reason: req.Reason, Status: req.Status, Rewards: req.Rewards})
|
|
if err != nil {
|
|
ctx.AbortWithError(core.Error(http.StatusBadRequest, code.CreateActivityError, err.Error()))
|
|
return
|
|
}
|
|
ctx.Payload(item)
|
|
}
|
|
}
|
|
|
|
func (h *handler) UpdatePrizeGrantActivity() core.HandlerFunc {
|
|
return func(ctx core.Context) {
|
|
if ctx.SessionUserInfo().IsSuper != 1 {
|
|
ctx.AbortWithError(core.Error(http.StatusForbidden, code.AuthorizationError, "无权限操作"))
|
|
return
|
|
}
|
|
id, err := strconv.ParseInt(ctx.Param("id"), 10, 64)
|
|
if err != nil || id <= 0 {
|
|
ctx.AbortWithError(core.Error(http.StatusBadRequest, code.ParamBindError, "活动ID无效"))
|
|
return
|
|
}
|
|
req := new(savePrizeGrantActivityRequest)
|
|
if err := ctx.ShouldBindJSON(req); err != nil {
|
|
ctx.AbortWithError(core.Error(http.StatusBadRequest, code.ParamBindError, validation.Error(err)))
|
|
return
|
|
}
|
|
if err := h.prizeGrant.UpdateActivity(ctx.RequestContext(), id, prizegrantsvc.SaveActivityRequest{Reason: req.Reason, Status: req.Status, Rewards: req.Rewards}); err != nil {
|
|
ctx.AbortWithError(core.Error(http.StatusBadRequest, code.ModifyActivityError, err.Error()))
|
|
return
|
|
}
|
|
ctx.Payload(simpleMessageResponse{Message: "操作成功"})
|
|
}
|
|
}
|
|
|
|
func (h *handler) DeletePrizeGrantActivity() core.HandlerFunc {
|
|
return func(ctx core.Context) {
|
|
if ctx.SessionUserInfo().IsSuper != 1 {
|
|
ctx.AbortWithError(core.Error(http.StatusForbidden, code.AuthorizationError, "无权限操作"))
|
|
return
|
|
}
|
|
id, err := strconv.ParseInt(ctx.Param("id"), 10, 64)
|
|
if err != nil || id <= 0 {
|
|
ctx.AbortWithError(core.Error(http.StatusBadRequest, code.ParamBindError, "活动ID无效"))
|
|
return
|
|
}
|
|
if err := h.prizeGrant.DeleteActivity(ctx.RequestContext(), id); err != nil {
|
|
ctx.AbortWithError(core.Error(http.StatusBadRequest, code.DeleteActivityError, err.Error()))
|
|
return
|
|
}
|
|
ctx.Payload(simpleMessageResponse{Message: "删除成功"})
|
|
}
|
|
}
|
|
|
|
func (h *handler) ListPrizeGrantActivities() core.HandlerFunc {
|
|
return func(ctx core.Context) {
|
|
req := new(listPrizeGrantActivitiesRequest)
|
|
if err := ctx.ShouldBindForm(req); err != nil {
|
|
ctx.AbortWithError(core.Error(http.StatusBadRequest, code.ParamBindError, err.Error()))
|
|
return
|
|
}
|
|
res, err := h.prizeGrant.ListActivities(ctx.RequestContext(), prizegrantsvc.ListActivitiesRequest{Reason: req.Reason, Status: req.Status, Page: req.Page, PageSize: req.PageSize})
|
|
if err != nil {
|
|
ctx.AbortWithError(core.Error(http.StatusBadRequest, code.ListActivitiesError, err.Error()))
|
|
return
|
|
}
|
|
ctx.Payload(res)
|
|
}
|
|
}
|
|
|
|
func (h *handler) GetPrizeGrantCostSummary() core.HandlerFunc {
|
|
return func(ctx core.Context) {
|
|
res, err := h.prizeGrant.GetCostSummary(ctx.RequestContext())
|
|
if err != nil {
|
|
ctx.AbortWithError(core.Error(http.StatusBadRequest, code.ListActivitiesError, err.Error()))
|
|
return
|
|
}
|
|
ctx.Payload(res)
|
|
}
|
|
}
|
|
|
|
func (h *handler) GetPrizeGrantActivity() core.HandlerFunc {
|
|
return func(ctx core.Context) {
|
|
id, err := strconv.ParseInt(ctx.Param("id"), 10, 64)
|
|
if err != nil || id <= 0 {
|
|
ctx.AbortWithError(core.Error(http.StatusBadRequest, code.ParamBindError, "活动ID无效"))
|
|
return
|
|
}
|
|
res, err := h.prizeGrant.GetActivity(ctx.RequestContext(), id)
|
|
if err != nil {
|
|
ctx.AbortWithError(core.Error(http.StatusBadRequest, code.GetActivityError, err.Error()))
|
|
return
|
|
}
|
|
ctx.Payload(res)
|
|
}
|
|
}
|
|
|
|
func (h *handler) ListPrizeGrantUserRecords() core.HandlerFunc {
|
|
return func(ctx core.Context) {
|
|
id, err := strconv.ParseInt(ctx.Param("id"), 10, 64)
|
|
if err != nil || id <= 0 {
|
|
ctx.AbortWithError(core.Error(http.StatusBadRequest, code.ParamBindError, "活动ID无效"))
|
|
return
|
|
}
|
|
req := new(listPrizeGrantRecordsRequest)
|
|
if err := ctx.ShouldBindForm(req); err != nil {
|
|
ctx.AbortWithError(core.Error(http.StatusBadRequest, code.ParamBindError, err.Error()))
|
|
return
|
|
}
|
|
res, err := h.prizeGrant.ListUserRecords(ctx.RequestContext(), id, strings.TrimSpace(req.Status), strings.TrimSpace(req.Keyword), req.Page, req.PageSize)
|
|
if err != nil {
|
|
ctx.AbortWithError(core.Error(http.StatusBadRequest, code.ListActivitiesError, err.Error()))
|
|
return
|
|
}
|
|
ctx.Payload(res)
|
|
}
|
|
}
|
|
|
|
func (h *handler) MarkPrizeGrantUsersProcessed() core.HandlerFunc {
|
|
return func(ctx core.Context) {
|
|
if ctx.SessionUserInfo().IsSuper != 1 {
|
|
ctx.AbortWithError(core.Error(http.StatusForbidden, code.AuthorizationError, "无权限操作"))
|
|
return
|
|
}
|
|
id, err := strconv.ParseInt(ctx.Param("id"), 10, 64)
|
|
if err != nil || id <= 0 {
|
|
ctx.AbortWithError(core.Error(http.StatusBadRequest, code.ParamBindError, "活动ID无效"))
|
|
return
|
|
}
|
|
req := new(markPrizeGrantUsersRequest)
|
|
if err := ctx.ShouldBindJSON(req); err != nil {
|
|
ctx.AbortWithError(core.Error(http.StatusBadRequest, code.ParamBindError, validation.Error(err)))
|
|
return
|
|
}
|
|
adminID := int64(ctx.SessionUserInfo().Id)
|
|
if err := h.prizeGrant.MarkUsersProcessed(ctx.RequestContext(), id, adminID, req.UserIDs); err != nil {
|
|
ctx.AbortWithError(core.Error(http.StatusBadRequest, code.ModifyActivityError, err.Error()))
|
|
return
|
|
}
|
|
ctx.Payload(simpleMessageResponse{Message: "操作成功"})
|
|
}
|
|
}
|
|
|
|
func (h *handler) MarkAllPrizeGrantUsersProcessed() core.HandlerFunc {
|
|
return func(ctx core.Context) {
|
|
if ctx.SessionUserInfo().IsSuper != 1 {
|
|
ctx.AbortWithError(core.Error(http.StatusForbidden, code.AuthorizationError, "无权限操作"))
|
|
return
|
|
}
|
|
id, err := strconv.ParseInt(ctx.Param("id"), 10, 64)
|
|
if err != nil || id <= 0 {
|
|
ctx.AbortWithError(core.Error(http.StatusBadRequest, code.ParamBindError, "活动ID无效"))
|
|
return
|
|
}
|
|
adminID := int64(ctx.SessionUserInfo().Id)
|
|
count, err := h.prizeGrant.MarkAllUsersProcessed(ctx.RequestContext(), id, adminID)
|
|
if err != nil {
|
|
ctx.AbortWithError(core.Error(http.StatusBadRequest, code.ModifyActivityError, err.Error()))
|
|
return
|
|
}
|
|
ctx.Payload(map[string]any{"message": "操作成功", "count": count})
|
|
}
|
|
}
|
|
|
|
func (h *handler) DeletePrizeGrantUserRecord() core.HandlerFunc {
|
|
return func(ctx core.Context) {
|
|
if ctx.SessionUserInfo().IsSuper != 1 {
|
|
ctx.AbortWithError(core.Error(http.StatusForbidden, code.AuthorizationError, "无权限操作"))
|
|
return
|
|
}
|
|
activityID, err := strconv.ParseInt(ctx.Param("id"), 10, 64)
|
|
if err != nil || activityID <= 0 {
|
|
ctx.AbortWithError(core.Error(http.StatusBadRequest, code.ParamBindError, "活动ID无效"))
|
|
return
|
|
}
|
|
recordID, err := strconv.ParseInt(ctx.Param("record_id"), 10, 64)
|
|
if err != nil || recordID <= 0 {
|
|
ctx.AbortWithError(core.Error(http.StatusBadRequest, code.ParamBindError, "记录ID无效"))
|
|
return
|
|
}
|
|
if err := h.prizeGrant.DeleteUserRecord(ctx.RequestContext(), activityID, recordID); err != nil {
|
|
ctx.AbortWithError(core.Error(http.StatusBadRequest, code.DeleteActivityError, err.Error()))
|
|
return
|
|
}
|
|
ctx.Payload(simpleMessageResponse{Message: "删除成功"})
|
|
}
|
|
}
|