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

var txService = 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 /tx/createRedPackageTx [post]
func (txApi *RedPackageTxApi) CreateRedPackageTx(c *gin.Context) {
	var tx airDrop.RedPackageTx
	err := c.ShouldBindJSON(&tx)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
    tx.CreatedBy = utils.GetUserID(c)
    verify := utils.Rules{
        "Amount":{utils.NotEmpty()},
        "Hash":{utils.NotEmpty()},
        "Block_time":{utils.NotEmpty()},
        "Height":{utils.NotEmpty()},
    }
	if err := utils.Verify(tx, verify); err != nil {
    		response.FailWithMessage(err.Error(), c)
    		return
    	}
	if err := txService.CreateRedPackageTx(tx); 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 /tx/deleteRedPackageTx [delete]
func (txApi *RedPackageTxApi) DeleteRedPackageTx(c *gin.Context) {
	var tx airDrop.RedPackageTx
	err := c.ShouldBindJSON(&tx)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
    tx.DeletedBy = utils.GetUserID(c)
	if err := txService.DeleteRedPackageTx(tx); 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 /tx/deleteRedPackageTxByIds [delete]
func (txApi *RedPackageTxApi) DeleteRedPackageTxByIds(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 := txService.DeleteRedPackageTxByIds(IDS,deletedBy); 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 /tx/updateRedPackageTx [put]
func (txApi *RedPackageTxApi) UpdateRedPackageTx(c *gin.Context) {
	var tx airDrop.RedPackageTx
	err := c.ShouldBindJSON(&tx)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
    tx.UpdatedBy = utils.GetUserID(c)
      verify := utils.Rules{
          "Amount":{utils.NotEmpty()},
          "Hash":{utils.NotEmpty()},
          "Block_time":{utils.NotEmpty()},
          "Height":{utils.NotEmpty()},
      }
    if err := utils.Verify(tx, verify); err != nil {
      	response.FailWithMessage(err.Error(), c)
      	return
     }
	if err := txService.UpdateRedPackageTx(tx); 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 /tx/findRedPackageTx [get]
func (txApi *RedPackageTxApi) FindRedPackageTx(c *gin.Context) {
	var tx airDrop.RedPackageTx
	err := c.ShouldBindQuery(&tx)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if retx, err := txService.GetRedPackageTx(tx.ID); err != nil {
        global.GVA_LOG.Error("查询失败!", zap.Error(err))
		response.FailWithMessage("查询失败", c)
	} else {
		response.OkWithData(gin.H{"retx": retx}, 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 /tx/getRedPackageTxList [get]
func (txApi *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 := txService.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)
    }
}
