315 lines
11 KiB
Go
315 lines
11 KiB
Go
package admin
|
|
|
|
import (
|
|
"net/http"
|
|
"strconv"
|
|
"time"
|
|
|
|
"bindbox-game/internal/code"
|
|
"bindbox-game/internal/pkg/core"
|
|
"bindbox-game/internal/pkg/validation"
|
|
thresholdsvc "bindbox-game/internal/service/threshold_activity"
|
|
)
|
|
|
|
type saveThresholdActivityRequest struct {
|
|
Title string `json:"title" binding:"required"`
|
|
Type string `json:"type" binding:"required"`
|
|
QualificationMode string `json:"qualification_mode" binding:"required"`
|
|
SpendThresholdAmount int64 `json:"spend_threshold_amount"`
|
|
InviteThresholdCount int64 `json:"invite_threshold_count"`
|
|
InviteEffectiveAmount int64 `json:"invite_effective_amount"`
|
|
MinParticipants int64 `json:"min_participants"`
|
|
StartTime string `json:"start_time" binding:"required"`
|
|
EndTime string `json:"end_time" binding:"required"`
|
|
DrawTime string `json:"draw_time" binding:"required"`
|
|
Status string `json:"status"`
|
|
Description string `json:"description"`
|
|
CoverImage string `json:"cover_image"`
|
|
Prizes []thresholdsvc.PrizeInput `json:"prizes"`
|
|
}
|
|
|
|
type listAdminThresholdActivitiesRequest struct {
|
|
Title string `form:"title"`
|
|
Type string `form:"type"`
|
|
Status string `form:"status"`
|
|
Page int `form:"page"`
|
|
PageSize int `form:"page_size"`
|
|
}
|
|
|
|
type listAdminThresholdWinnersRequest struct {
|
|
Page int `form:"page"`
|
|
PageSize int `form:"page_size"`
|
|
}
|
|
|
|
type thresholdCostSummaryRequest struct {
|
|
StartTime string `form:"start_time"`
|
|
EndTime string `form:"end_time"`
|
|
}
|
|
|
|
type copyThresholdActivityRequest struct {
|
|
Title string `json:"title"`
|
|
Type string `json:"type" binding:"required"`
|
|
QualificationMode string `json:"qualification_mode" binding:"required"`
|
|
SpendThresholdAmount int64 `json:"spend_threshold_amount"`
|
|
InviteThresholdCount int64 `json:"invite_threshold_count"`
|
|
InviteEffectiveAmount int64 `json:"invite_effective_amount"`
|
|
MinParticipants int64 `json:"min_participants"`
|
|
StartTime string `json:"start_time" binding:"required"`
|
|
EndTime string `json:"end_time" binding:"required"`
|
|
DrawTime string `json:"draw_time" binding:"required"`
|
|
Status string `json:"status"`
|
|
}
|
|
|
|
func (h *handler) CreateThresholdActivity() core.HandlerFunc {
|
|
return func(ctx core.Context) {
|
|
req := new(saveThresholdActivityRequest)
|
|
if err := ctx.ShouldBindJSON(req); err != nil {
|
|
ctx.AbortWithError(core.Error(http.StatusBadRequest, code.ParamBindError, validation.Error(err)))
|
|
return
|
|
}
|
|
input, err := req.toInput()
|
|
if err != nil {
|
|
ctx.AbortWithError(core.Error(http.StatusBadRequest, code.ParamBindError, err.Error()))
|
|
return
|
|
}
|
|
item, err := h.threshold.CreateActivity(ctx.RequestContext(), input)
|
|
if err != nil {
|
|
ctx.AbortWithError(core.Error(http.StatusBadRequest, code.CreateActivityError, err.Error()))
|
|
return
|
|
}
|
|
ctx.Payload(map[string]interface{}{"id": item.ID, "message": "操作成功"})
|
|
}
|
|
}
|
|
|
|
func (h *handler) UpdateThresholdActivity() core.HandlerFunc {
|
|
return func(ctx core.Context) {
|
|
id, err := strconv.ParseInt(ctx.Param("id"), 10, 64)
|
|
if err != nil {
|
|
ctx.AbortWithError(core.Error(http.StatusBadRequest, code.ParamBindError, "活动ID无效"))
|
|
return
|
|
}
|
|
req := new(saveThresholdActivityRequest)
|
|
if err := ctx.ShouldBindJSON(req); err != nil {
|
|
ctx.AbortWithError(core.Error(http.StatusBadRequest, code.ParamBindError, validation.Error(err)))
|
|
return
|
|
}
|
|
input, err := req.toInput()
|
|
if err != nil {
|
|
ctx.AbortWithError(core.Error(http.StatusBadRequest, code.ParamBindError, err.Error()))
|
|
return
|
|
}
|
|
if err := h.threshold.UpdateActivity(ctx.RequestContext(), id, input); err != nil {
|
|
ctx.AbortWithError(core.Error(http.StatusBadRequest, code.ModifyActivityError, err.Error()))
|
|
return
|
|
}
|
|
ctx.Payload(simpleMessageResponse{Message: "操作成功"})
|
|
}
|
|
}
|
|
|
|
func (h *handler) ListThresholdActivities() core.HandlerFunc {
|
|
return func(ctx core.Context) {
|
|
req := new(listAdminThresholdActivitiesRequest)
|
|
if err := ctx.ShouldBindForm(req); err != nil {
|
|
ctx.AbortWithError(core.Error(http.StatusBadRequest, code.ParamBindError, err.Error()))
|
|
return
|
|
}
|
|
res, err := h.threshold.ListActivities(ctx.RequestContext(), thresholdsvc.ListActivitiesRequest{Type: req.Type, Status: req.Status, Title: req.Title, 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) GetThresholdActivity() core.HandlerFunc {
|
|
return func(ctx core.Context) {
|
|
id, err := strconv.ParseInt(ctx.Param("id"), 10, 64)
|
|
if err != nil {
|
|
ctx.AbortWithError(core.Error(http.StatusBadRequest, code.ParamBindError, "活动ID无效"))
|
|
return
|
|
}
|
|
res, err := h.threshold.GetActivityAdmin(ctx.RequestContext(), id)
|
|
if err != nil {
|
|
ctx.AbortWithError(core.Error(http.StatusBadRequest, code.GetActivityError, err.Error()))
|
|
return
|
|
}
|
|
ctx.Payload(res)
|
|
}
|
|
}
|
|
|
|
func (h *handler) DeleteThresholdActivity() core.HandlerFunc {
|
|
return func(ctx core.Context) {
|
|
id, err := strconv.ParseInt(ctx.Param("id"), 10, 64)
|
|
if err != nil {
|
|
ctx.AbortWithError(core.Error(http.StatusBadRequest, code.ParamBindError, "活动ID无效"))
|
|
return
|
|
}
|
|
if err := h.threshold.DeleteActivity(ctx.RequestContext(), id); err != nil {
|
|
ctx.AbortWithError(core.Error(http.StatusBadRequest, code.DeleteActivityError, err.Error()))
|
|
return
|
|
}
|
|
ctx.Payload(simpleMessageResponse{Message: "操作成功"})
|
|
}
|
|
}
|
|
|
|
func (h *handler) CopyThresholdActivity() core.HandlerFunc {
|
|
return func(ctx core.Context) {
|
|
id, err := strconv.ParseInt(ctx.Param("id"), 10, 64)
|
|
if err != nil {
|
|
ctx.AbortWithError(core.Error(http.StatusBadRequest, code.ParamBindError, "活动ID无效"))
|
|
return
|
|
}
|
|
req := new(copyThresholdActivityRequest)
|
|
if err := ctx.ShouldBindJSON(req); err != nil {
|
|
ctx.AbortWithError(core.Error(http.StatusBadRequest, code.ParamBindError, validation.Error(err)))
|
|
return
|
|
}
|
|
start, err := parseRequiredTime(req.StartTime)
|
|
if err != nil {
|
|
ctx.AbortWithError(core.Error(http.StatusBadRequest, code.ParamBindError, err.Error()))
|
|
return
|
|
}
|
|
end, err := parseRequiredTime(req.EndTime)
|
|
if err != nil {
|
|
ctx.AbortWithError(core.Error(http.StatusBadRequest, code.ParamBindError, err.Error()))
|
|
return
|
|
}
|
|
draw, err := parseRequiredTime(req.DrawTime)
|
|
if err != nil {
|
|
ctx.AbortWithError(core.Error(http.StatusBadRequest, code.ParamBindError, err.Error()))
|
|
return
|
|
}
|
|
newID, err := h.threshold.CopyActivity(ctx.RequestContext(), id, thresholdsvc.SaveActivityRequest{
|
|
Title: req.Title,
|
|
Type: req.Type,
|
|
QualificationMode: req.QualificationMode,
|
|
SpendThresholdAmount: req.SpendThresholdAmount,
|
|
InviteThresholdCount: req.InviteThresholdCount,
|
|
InviteEffectiveAmount: req.InviteEffectiveAmount,
|
|
MinParticipants: req.MinParticipants,
|
|
StartTime: start,
|
|
EndTime: end,
|
|
DrawTime: draw,
|
|
Status: req.Status,
|
|
})
|
|
if err != nil {
|
|
ctx.AbortWithError(core.Error(http.StatusBadRequest, code.CreateActivityError, err.Error()))
|
|
return
|
|
}
|
|
ctx.Payload(map[string]interface{}{"new_activity_id": newID, "status": "success"})
|
|
}
|
|
}
|
|
|
|
func (h *handler) ListThresholdParticipants() core.HandlerFunc {
|
|
return func(ctx core.Context) {
|
|
id, _ := strconv.ParseInt(ctx.Param("id"), 10, 64)
|
|
res, err := h.threshold.ListParticipants(ctx.RequestContext(), id, 1, 100)
|
|
if err != nil {
|
|
ctx.AbortWithError(core.Error(http.StatusBadRequest, code.ListActivitiesError, err.Error()))
|
|
return
|
|
}
|
|
ctx.Payload(res)
|
|
}
|
|
}
|
|
|
|
func (h *handler) ListThresholdWinners() core.HandlerFunc {
|
|
return func(ctx core.Context) {
|
|
id, _ := strconv.ParseInt(ctx.Param("id"), 10, 64)
|
|
req := new(listAdminThresholdWinnersRequest)
|
|
if err := ctx.ShouldBindForm(req); err != nil {
|
|
ctx.AbortWithError(core.Error(http.StatusBadRequest, code.ParamBindError, err.Error()))
|
|
return
|
|
}
|
|
res, err := h.threshold.ListWinners(ctx.RequestContext(), id, req.Page, req.PageSize)
|
|
if err != nil {
|
|
ctx.AbortWithError(core.Error(http.StatusBadRequest, code.ListActivitiesError, err.Error()))
|
|
return
|
|
}
|
|
ctx.Payload(res)
|
|
}
|
|
}
|
|
|
|
func (h *handler) DrawThresholdActivity() core.HandlerFunc {
|
|
return func(ctx core.Context) {
|
|
id, _ := strconv.ParseInt(ctx.Param("id"), 10, 64)
|
|
if err := h.threshold.Draw(ctx.RequestContext(), id); err != nil {
|
|
ctx.AbortWithError(core.Error(http.StatusBadRequest, code.ModifyActivityError, err.Error()))
|
|
return
|
|
}
|
|
ctx.Payload(simpleMessageResponse{Message: "操作成功"})
|
|
}
|
|
}
|
|
|
|
func (h *handler) GetThresholdCost() core.HandlerFunc {
|
|
return func(ctx core.Context) {
|
|
id, _ := strconv.ParseInt(ctx.Param("id"), 10, 64)
|
|
res, err := h.threshold.GetCost(ctx.RequestContext(), id)
|
|
if err != nil {
|
|
ctx.AbortWithError(core.Error(http.StatusBadRequest, code.ListActivitiesError, err.Error()))
|
|
return
|
|
}
|
|
ctx.Payload(res)
|
|
}
|
|
}
|
|
|
|
func (h *handler) GetThresholdCostSummary() core.HandlerFunc {
|
|
return func(ctx core.Context) {
|
|
req := new(thresholdCostSummaryRequest)
|
|
if err := ctx.ShouldBindForm(req); err != nil {
|
|
ctx.AbortWithError(core.Error(http.StatusBadRequest, code.ParamBindError, err.Error()))
|
|
return
|
|
}
|
|
start, _ := parseOptionalTime(req.StartTime)
|
|
end, _ := parseOptionalTime(req.EndTime)
|
|
res, err := h.threshold.GetCostSummary(ctx.RequestContext(), start, end)
|
|
if err != nil {
|
|
ctx.AbortWithError(core.Error(http.StatusBadRequest, code.ListActivitiesError, err.Error()))
|
|
return
|
|
}
|
|
ctx.Payload(res)
|
|
}
|
|
}
|
|
|
|
func (r *saveThresholdActivityRequest) toInput() (thresholdsvc.SaveActivityRequest, error) {
|
|
start, err := parseRequiredTime(r.StartTime)
|
|
if err != nil {
|
|
return thresholdsvc.SaveActivityRequest{}, err
|
|
}
|
|
end, err := parseRequiredTime(r.EndTime)
|
|
if err != nil {
|
|
return thresholdsvc.SaveActivityRequest{}, err
|
|
}
|
|
if sameDay(start, end) && end.Hour() == 0 && end.Minute() == 0 && end.Second() == 0 {
|
|
end = time.Date(end.Year(), end.Month(), end.Day(), 23, 59, 59, 0, end.Location())
|
|
}
|
|
draw, err := parseRequiredTime(r.DrawTime)
|
|
if err != nil {
|
|
return thresholdsvc.SaveActivityRequest{}, err
|
|
}
|
|
prizes := make([]thresholdsvc.PrizeInput, 0, len(r.Prizes))
|
|
for _, prize := range r.Prizes {
|
|
if prize.RewardType == "" && prize.ProductID > 0 {
|
|
prize.RewardType = thresholdsvc.RewardTypeProduct
|
|
prize.RewardRefID = prize.ProductID
|
|
}
|
|
prizes = append(prizes, prize)
|
|
}
|
|
return thresholdsvc.SaveActivityRequest{
|
|
Title: r.Title,
|
|
Type: r.Type,
|
|
QualificationMode: r.QualificationMode,
|
|
SpendThresholdAmount: r.SpendThresholdAmount,
|
|
InviteThresholdCount: r.InviteThresholdCount,
|
|
InviteEffectiveAmount: r.InviteEffectiveAmount,
|
|
MinParticipants: r.MinParticipants,
|
|
StartTime: start,
|
|
EndTime: end,
|
|
DrawTime: draw,
|
|
Status: r.Status,
|
|
Description: r.Description,
|
|
CoverImage: r.CoverImage,
|
|
Prizes: prizes,
|
|
}, nil
|
|
}
|