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 RedPackageTestApi struct {
}

var rptestService = service.ServiceGroupApp.AirDropServiceGroup.RedPackageTestService


// CreateRedPackageTest 创建RedPackageTest
// @Tags RedPackageTest
// @Summary 创建RedPackageTest
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body airDrop.RedPackageTest true "创建RedPackageTest"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /rptest/createRedPackageTest [post]
func (rptestApi *RedPackageTestApi) CreateRedPackageTest(c *gin.Context) {
	var rptest airDrop.RedPackageTest
	err := c.ShouldBindJSON(&rptest)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
    rptest.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(rptest, verify); err != nil {
    		response.FailWithMessage(err.Error(), c)
    		return
    	}
	if err := rptestService.CreateRedPackageTest(rptest); err != nil {
        global.GVA_LOG.Error("创建失败!", zap.Error(err))
		response.FailWithMessage("创建失败", c)
	} else {
		response.OkWithMessage("创建成功", c)
	}
}

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

// DeleteRedPackageTestByIds 批量删除RedPackageTest
// @Tags RedPackageTest
// @Summary 批量删除RedPackageTest
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body request.IdsReq true "批量删除RedPackageTest"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"批量删除成功"}"
// @Router /rptest/deleteRedPackageTestByIds [delete]
func (rptestApi *RedPackageTestApi) DeleteRedPackageTestByIds(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 := rptestService.DeleteRedPackageTestByIds(IDS,deletedBy); err != nil {
        global.GVA_LOG.Error("批量删除失败!", zap.Error(err))
		response.FailWithMessage("批量删除失败", c)
	} else {
		response.OkWithMessage("批量删除成功", c)
	}
}

// UpdateRedPackageTest 更新RedPackageTest
// @Tags RedPackageTest
// @Summary 更新RedPackageTest
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body airDrop.RedPackageTest true "更新RedPackageTest"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"更新成功"}"
// @Router /rptest/updateRedPackageTest [put]
func (rptestApi *RedPackageTestApi) UpdateRedPackageTest(c *gin.Context) {
	var rptest airDrop.RedPackageTest
	err := c.ShouldBindJSON(&rptest)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
    rptest.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(rptest, verify); err != nil {
      	response.FailWithMessage(err.Error(), c)
      	return
     }
	if err := rptestService.UpdateRedPackageTest(rptest); err != nil {
        global.GVA_LOG.Error("更新失败!", zap.Error(err))
		response.FailWithMessage("更新失败", c)
	} else {
		response.OkWithMessage("更新成功", c)
	}
}

// FindRedPackageTest 用id查询RedPackageTest
// @Tags RedPackageTest
// @Summary 用id查询RedPackageTest
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query airDrop.RedPackageTest true "用id查询RedPackageTest"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"查询成功"}"
// @Router /rptest/findRedPackageTest [get]
func (rptestApi *RedPackageTestApi) FindRedPackageTest(c *gin.Context) {
	var rptest airDrop.RedPackageTest
	err := c.ShouldBindQuery(&rptest)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if rerptest, err := rptestService.GetRedPackageTest(rptest.ID); err != nil {
        global.GVA_LOG.Error("查询失败!", zap.Error(err))
		response.FailWithMessage("查询失败", c)
	} else {
		response.OkWithData(gin.H{"rerptest": rerptest}, c)
	}
}

// GetRedPackageTestList 分页获取RedPackageTest列表
// @Tags RedPackageTest
// @Summary 分页获取RedPackageTest列表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query airDropReq.RedPackageTestSearch true "分页获取RedPackageTest列表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /rptest/getRedPackageTestList [get]
func (rptestApi *RedPackageTestApi) GetRedPackageTestList(c *gin.Context) {
	var pageInfo airDropReq.RedPackageTestSearch
	err := c.ShouldBindQuery(&pageInfo)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if list, total, err := rptestService.GetRedPackageTestInfoList(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)
    }
}
