package airDrop

import (
	"courseSign/server/global"
    "courseSign/server/model/airDrop"
    "courseSign/server/model/common/request"
    airDropReq "courseSign/server/model/airDrop/request"
    "courseSign/server/model/common/response"
    "courseSign/server/service"
    "github.com/gin-gonic/gin"
    "go.uber.org/zap"
    "courseSign/server/utils"
)

type RedPackageApi struct {
}

var 红包Service = service.ServiceGroupApp.AirDropServiceGroup.RedPackageService


// CreateRedPackage 创建RedPackage
// @Tags RedPackage
// @Summary 创建RedPackage
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body airDrop.RedPackage true "创建RedPackage"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /红包/createRedPackage [post]
func (红包Api *RedPackageApi) CreateRedPackage(c *gin.Context) {
	var 红包 airDrop.RedPackage
	err := c.ShouldBindJSON(&红包)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
    红包.CreatedBy = utils.GetUserID(c)
    verify := utils.Rules{
        "Balance":{utils.NotEmpty()},
        "Num":{utils.NotEmpty()},
        "Start_time":{utils.NotEmpty()},
        "End_time":{utils.NotEmpty()},
        "Ip_limit":{utils.NotEmpty()},
    }
	if err := utils.Verify(红包, verify); err != nil {
    		response.FailWithMessage(err.Error(), c)
    		return
    	}
	if err := 红包Service.CreateRedPackage(红包); err != nil {
        global.GVA_LOG.Error("创建失败!", zap.Error(err))
		response.FailWithMessage("创建失败", c)
	} else {
		response.OkWithMessage("创建成功", c)
	}
}

// DeleteRedPackage 删除RedPackage
// @Tags RedPackage
// @Summary 删除RedPackage
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body airDrop.RedPackage true "删除RedPackage"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"删除成功"}"
// @Router /红包/deleteRedPackage [delete]
func (红包Api *RedPackageApi) DeleteRedPackage(c *gin.Context) {
	var 红包 airDrop.RedPackage
	err := c.ShouldBindJSON(&红包)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
    红包.DeletedBy = utils.GetUserID(c)
	if err := 红包Service.DeleteRedPackage(红包); err != nil {
        global.GVA_LOG.Error("删除失败!", zap.Error(err))
		response.FailWithMessage("删除失败", c)
	} else {
		response.OkWithMessage("删除成功", c)
	}
}

// DeleteRedPackageByIds 批量删除RedPackage
// @Tags RedPackage
// @Summary 批量删除RedPackage
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body request.IdsReq true "批量删除RedPackage"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"批量删除成功"}"
// @Router /红包/deleteRedPackageByIds [delete]
func (红包Api *RedPackageApi) DeleteRedPackageByIds(c *gin.Context) {
	var IDS request.IdsReq
    err := c.ShouldBindJSON(&IDS)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
    deletedBy := utils.GetUserID(c)
	if err := 红包Service.DeleteRedPackageByIds(IDS,deletedBy); err != nil {
        global.GVA_LOG.Error("批量删除失败!", zap.Error(err))
		response.FailWithMessage("批量删除失败", c)
	} else {
		response.OkWithMessage("批量删除成功", c)
	}
}

// UpdateRedPackage 更新RedPackage
// @Tags RedPackage
// @Summary 更新RedPackage
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body airDrop.RedPackage true "更新RedPackage"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"更新成功"}"
// @Router /红包/updateRedPackage [put]
func (红包Api *RedPackageApi) UpdateRedPackage(c *gin.Context) {
	var 红包 airDrop.RedPackage
	err := c.ShouldBindJSON(&红包)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
    红包.UpdatedBy = utils.GetUserID(c)
      verify := utils.Rules{
          "Balance":{utils.NotEmpty()},
          "Num":{utils.NotEmpty()},
          "Start_time":{utils.NotEmpty()},
          "End_time":{utils.NotEmpty()},
          "Ip_limit":{utils.NotEmpty()},
      }
    if err := utils.Verify(红包, verify); err != nil {
      	response.FailWithMessage(err.Error(), c)
      	return
     }
	if err := 红包Service.UpdateRedPackage(红包); err != nil {
        global.GVA_LOG.Error("更新失败!", zap.Error(err))
		response.FailWithMessage("更新失败", c)
	} else {
		response.OkWithMessage("更新成功", c)
	}
}

// FindRedPackage 用id查询RedPackage
// @Tags RedPackage
// @Summary 用id查询RedPackage
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query airDrop.RedPackage true "用id查询RedPackage"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"查询成功"}"
// @Router /红包/findRedPackage [get]
func (红包Api *RedPackageApi) FindRedPackage(c *gin.Context) {
	var 红包 airDrop.RedPackage
	err := c.ShouldBindQuery(&红包)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if re红包, err := 红包Service.GetRedPackage(红包.ID); err != nil {
        global.GVA_LOG.Error("查询失败!", zap.Error(err))
		response.FailWithMessage("查询失败", c)
	} else {
		response.OkWithData(gin.H{"re红包": re红包}, c)
	}
}

// GetRedPackageList 分页获取RedPackage列表
// @Tags RedPackage
// @Summary 分页获取RedPackage列表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query airDropReq.RedPackageSearch true "分页获取RedPackage列表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /红包/getRedPackageList [get]
func (红包Api *RedPackageApi) GetRedPackageList(c *gin.Context) {
	var pageInfo airDropReq.RedPackageSearch
	err := c.ShouldBindQuery(&pageInfo)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if list, total, err := 红包Service.GetRedPackageInfoList(pageInfo); err != nil {
	    global.GVA_LOG.Error("获取失败!", zap.Error(err))
        response.FailWithMessage("获取失败", c)
    } else {
        response.OkWithDetailed(response.PageResult{
            List:     list,
            Total:    total,
            Page:     pageInfo.Page,
            PageSize: pageInfo.PageSize,
        }, "获取成功", c)
    }
}
