package airDrop

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

type RedPackageTxApi struct {
}

var redPackageTxService = service.ServiceGroupApp.AirDropServiceGroup.RedPackageTxService


// CreateRedPackageTx 创建RedPackageTx
// @Tags RedPackageTx
// @Summary 创建RedPackageTx
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body airDrop.RedPackageTx true "创建RedPackageTx"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /redPackageTx/createRedPackageTx [post]
func (redPackageTxApi *RedPackageTxApi) CreateRedPackageTx(c *gin.Context) {
	var redPackageTx airDrop.RedPackageTx
	err := c.ShouldBindJSON(&redPackageTx)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
    verify := utils.Rules{
        "Height":{utils.NotEmpty()},
        "Hash":{utils.NotEmpty()},
        "Amount":{utils.NotEmpty()},
        "Blocktime":{utils.NotEmpty()},
    }
	if err := utils.Verify(redPackageTx, verify); err != nil {
    		response.FailWithMessage(err.Error(), c)
    		return
    	}
	if err := redPackageTxService.CreateRedPackageTx(redPackageTx); err != nil {
        global.GVA_LOG.Error("创建失败!", zap.Error(err))
		response.FailWithMessage("创建失败", c)
	} else {
		response.OkWithMessage("创建成功", c)
	}
}

// DeleteRedPackageTx 删除RedPackageTx
// @Tags RedPackageTx
// @Summary 删除RedPackageTx
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body airDrop.RedPackageTx true "删除RedPackageTx"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"删除成功"}"
// @Router /redPackageTx/deleteRedPackageTx [delete]
func (redPackageTxApi *RedPackageTxApi) DeleteRedPackageTx(c *gin.Context) {
	var redPackageTx airDrop.RedPackageTx
	err := c.ShouldBindJSON(&redPackageTx)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := redPackageTxService.DeleteRedPackageTx(redPackageTx); err != nil {
        global.GVA_LOG.Error("删除失败!", zap.Error(err))
		response.FailWithMessage("删除失败", c)
	} else {
		response.OkWithMessage("删除成功", c)
	}
}

// DeleteRedPackageTxByIds 批量删除RedPackageTx
// @Tags RedPackageTx
// @Summary 批量删除RedPackageTx
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body request.IdsReq true "批量删除RedPackageTx"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"批量删除成功"}"
// @Router /redPackageTx/deleteRedPackageTxByIds [delete]
func (redPackageTxApi *RedPackageTxApi) DeleteRedPackageTxByIds(c *gin.Context) {
	var IDS request.IdsReq
    err := c.ShouldBindJSON(&IDS)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := redPackageTxService.DeleteRedPackageTxByIds(IDS); err != nil {
        global.GVA_LOG.Error("批量删除失败!", zap.Error(err))
		response.FailWithMessage("批量删除失败", c)
	} else {
		response.OkWithMessage("批量删除成功", c)
	}
}

// UpdateRedPackageTx 更新RedPackageTx
// @Tags RedPackageTx
// @Summary 更新RedPackageTx
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body airDrop.RedPackageTx true "更新RedPackageTx"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"更新成功"}"
// @Router /redPackageTx/updateRedPackageTx [put]
func (redPackageTxApi *RedPackageTxApi) UpdateRedPackageTx(c *gin.Context) {
	var redPackageTx airDrop.RedPackageTx
	err := c.ShouldBindJSON(&redPackageTx)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
      verify := utils.Rules{
          "Height":{utils.NotEmpty()},
          "Hash":{utils.NotEmpty()},
          "Amount":{utils.NotEmpty()},
          "Blocktime":{utils.NotEmpty()},
      }
    if err := utils.Verify(redPackageTx, verify); err != nil {
      	response.FailWithMessage(err.Error(), c)
      	return
     }
	if err := redPackageTxService.UpdateRedPackageTx(redPackageTx); err != nil {
        global.GVA_LOG.Error("更新失败!", zap.Error(err))
		response.FailWithMessage("更新失败", c)
	} else {
		response.OkWithMessage("更新成功", c)
	}
}

// FindRedPackageTx 用id查询RedPackageTx
// @Tags RedPackageTx
// @Summary 用id查询RedPackageTx
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query airDrop.RedPackageTx true "用id查询RedPackageTx"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"查询成功"}"
// @Router /redPackageTx/findRedPackageTx [get]
func (redPackageTxApi *RedPackageTxApi) FindRedPackageTx(c *gin.Context) {
	var redPackageTx airDrop.RedPackageTx
	err := c.ShouldBindQuery(&redPackageTx)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if reredPackageTx, err := redPackageTxService.GetRedPackageTx(redPackageTx.ID); err != nil {
        global.GVA_LOG.Error("查询失败!", zap.Error(err))
		response.FailWithMessage("查询失败", c)
	} else {
		response.OkWithData(gin.H{"reredPackageTx": reredPackageTx}, c)
	}
}

// GetRedPackageTxList 分页获取RedPackageTx列表
// @Tags RedPackageTx
// @Summary 分页获取RedPackageTx列表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query airDropReq.RedPackageTxSearch true "分页获取RedPackageTx列表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /redPackageTx/getRedPackageTxList [get]
func (redPackageTxApi *RedPackageTxApi) GetRedPackageTxList(c *gin.Context) {
	var pageInfo airDropReq.RedPackageTxSearch
	err := c.ShouldBindQuery(&pageInfo)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if list, total, err := redPackageTxService.GetRedPackageTxInfoList(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)
    }
}
