package entrustPool

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

type PoolActionTxsApi struct {
}

var poolActionTxsService = service.ServiceGroupApp.EntrustPoolServiceGroup.PoolActionTxsService


// CreatePoolActionTxs 创建PoolActionTxs
// @Tags PoolActionTxs
// @Summary 创建PoolActionTxs
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body entrustPool.PoolActionTxs true "创建PoolActionTxs"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /poolActionTxs/createPoolActionTxs [post]
func (poolActionTxsApi *PoolActionTxsApi) CreatePoolActionTxs(c *gin.Context) {
	var poolActionTxs entrustPool.PoolActionTxs
	err := c.ShouldBindJSON(&poolActionTxs)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
    verify := utils.Rules{
        "From":{utils.NotEmpty()},
        "To":{utils.NotEmpty()},
        "Action":{utils.NotEmpty()},
        "Execer":{utils.NotEmpty()},
        "Height":{utils.NotEmpty()},
        "Txhash":{utils.NotEmpty()},
        "Blocktime":{utils.NotEmpty()},
    }
	if err := utils.Verify(poolActionTxs, verify); err != nil {
    		response.FailWithMessage(err.Error(), c)
    		return
    	}
	if err := poolActionTxsService.CreatePoolActionTxs(poolActionTxs); err != nil {
        global.GVA_LOG.Error("创建失败!", zap.Error(err))
		response.FailWithMessage("创建失败", c)
	} else {
		response.OkWithMessage("创建成功", c)
	}
}

// DeletePoolActionTxs 删除PoolActionTxs
// @Tags PoolActionTxs
// @Summary 删除PoolActionTxs
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body entrustPool.PoolActionTxs true "删除PoolActionTxs"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"删除成功"}"
// @Router /poolActionTxs/deletePoolActionTxs [delete]
func (poolActionTxsApi *PoolActionTxsApi) DeletePoolActionTxs(c *gin.Context) {
	var poolActionTxs entrustPool.PoolActionTxs
	err := c.ShouldBindJSON(&poolActionTxs)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := poolActionTxsService.DeletePoolActionTxs(poolActionTxs); err != nil {
        global.GVA_LOG.Error("删除失败!", zap.Error(err))
		response.FailWithMessage("删除失败", c)
	} else {
		response.OkWithMessage("删除成功", c)
	}
}

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

// UpdatePoolActionTxs 更新PoolActionTxs
// @Tags PoolActionTxs
// @Summary 更新PoolActionTxs
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body entrustPool.PoolActionTxs true "更新PoolActionTxs"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"更新成功"}"
// @Router /poolActionTxs/updatePoolActionTxs [put]
func (poolActionTxsApi *PoolActionTxsApi) UpdatePoolActionTxs(c *gin.Context) {
	var poolActionTxs entrustPool.PoolActionTxs
	err := c.ShouldBindJSON(&poolActionTxs)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
      verify := utils.Rules{
          "From":{utils.NotEmpty()},
          "To":{utils.NotEmpty()},
          "Action":{utils.NotEmpty()},
          "Execer":{utils.NotEmpty()},
          "Height":{utils.NotEmpty()},
          "Txhash":{utils.NotEmpty()},
          "Blocktime":{utils.NotEmpty()},
      }
    if err := utils.Verify(poolActionTxs, verify); err != nil {
      	response.FailWithMessage(err.Error(), c)
      	return
     }
	if err := poolActionTxsService.UpdatePoolActionTxs(poolActionTxs); err != nil {
        global.GVA_LOG.Error("更新失败!", zap.Error(err))
		response.FailWithMessage("更新失败", c)
	} else {
		response.OkWithMessage("更新成功", c)
	}
}

// FindPoolActionTxs 用id查询PoolActionTxs
// @Tags PoolActionTxs
// @Summary 用id查询PoolActionTxs
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query entrustPool.PoolActionTxs true "用id查询PoolActionTxs"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"查询成功"}"
// @Router /poolActionTxs/findPoolActionTxs [get]
func (poolActionTxsApi *PoolActionTxsApi) FindPoolActionTxs(c *gin.Context) {
	var poolActionTxs entrustPool.PoolActionTxs
	err := c.ShouldBindQuery(&poolActionTxs)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if repoolActionTxs, err := poolActionTxsService.GetPoolActionTxs(poolActionTxs.ID); err != nil {
        global.GVA_LOG.Error("查询失败!", zap.Error(err))
		response.FailWithMessage("查询失败", c)
	} else {
		response.OkWithData(gin.H{"repoolActionTxs": repoolActionTxs}, c)
	}
}

// GetPoolActionTxsList 分页获取PoolActionTxs列表
// @Tags PoolActionTxs
// @Summary 分页获取PoolActionTxs列表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query entrustPoolReq.PoolActionTxsSearch true "分页获取PoolActionTxs列表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /poolActionTxs/getPoolActionTxsList [get]
func (poolActionTxsApi *PoolActionTxsApi) GetPoolActionTxsList(c *gin.Context) {
	var pageInfo entrustPoolReq.PoolActionTxsSearch
	err := c.ShouldBindQuery(&pageInfo)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if list, total, err := poolActionTxsService.GetPoolActionTxsInfoList(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)
    }
}
