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"
)

type PoolAddrApi struct {
}

var poolAddrService = service.ServiceGroupApp.EntrustPoolServiceGroup.PoolAddrService


// CreatePoolAddr 创建PoolAddr
// @Tags PoolAddr
// @Summary 创建PoolAddr
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body entrustPool.PoolAddr true "创建PoolAddr"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /poolAddr/createPoolAddr [post]
func (poolAddrApi *PoolAddrApi) CreatePoolAddr(c *gin.Context) {
	var poolAddr entrustPool.PoolAddr
	err := c.ShouldBindJSON(&poolAddr)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := poolAddrService.CreatePoolAddr(poolAddr); err != nil {
        global.GVA_LOG.Error("创建失败!", zap.Error(err))
		response.FailWithMessage("创建失败", c)
	} else {
		response.OkWithMessage("创建成功", c)
	}
}

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

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

// UpdatePoolAddr 更新PoolAddr
// @Tags PoolAddr
// @Summary 更新PoolAddr
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body entrustPool.PoolAddr true "更新PoolAddr"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"更新成功"}"
// @Router /poolAddr/updatePoolAddr [put]
func (poolAddrApi *PoolAddrApi) UpdatePoolAddr(c *gin.Context) {
	var poolAddr entrustPool.PoolAddr
	err := c.ShouldBindJSON(&poolAddr)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := poolAddrService.UpdatePoolAddr(poolAddr); err != nil {
        global.GVA_LOG.Error("更新失败!", zap.Error(err))
		response.FailWithMessage("更新失败", c)
	} else {
		response.OkWithMessage("更新成功", c)
	}
}

// FindPoolAddr 用id查询PoolAddr
// @Tags PoolAddr
// @Summary 用id查询PoolAddr
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query entrustPool.PoolAddr true "用id查询PoolAddr"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"查询成功"}"
// @Router /poolAddr/findPoolAddr [get]
func (poolAddrApi *PoolAddrApi) FindPoolAddr(c *gin.Context) {
	var poolAddr entrustPool.PoolAddr
	err := c.ShouldBindQuery(&poolAddr)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if repoolAddr, err := poolAddrService.GetPoolAddr(poolAddr.ID); err != nil {
        global.GVA_LOG.Error("查询失败!", zap.Error(err))
		response.FailWithMessage("查询失败", c)
	} else {
		response.OkWithData(gin.H{"repoolAddr": repoolAddr}, c)
	}
}

// GetAddrInfo 查询PoolAddr相关信息
// @Tags GetAddrInfo
// @Summary 查询PoolAddr相关信息
// @accept application/json
// @Produce application/json
// @Param data query entrustPoolReq.PoolAddrAccount true "查询PoolAddr"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"查询成功"}"
// @Router /poolAddrWeb/getAddrInfo [get]
func (poolAddrApi *PoolAddrApi) GetAddrInfo(c *gin.Context) {
	var poolAddr entrustPoolReq.PoolAddrInfoReq
	err := c.ShouldBindQuery(&poolAddr)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if repoolAddr, err := poolAddrService.GetPoolAddrByAddr(poolAddr.Addr); err != nil {
		global.GVA_LOG.Error("查询失败!", zap.Error(err))
		response.FailWithMessage(err.Error(), c)
	} else {
		response.OkWithData(gin.H{"data": repoolAddr}, c)
	}
}

// UpdateAddrFee 更新PoolAddr信息
// @Tags UpdateAddrFee
// @Summary 更新PoolAddr信息
// @accept application/json
// @Produce application/json
// @Param data query entrustPoolReq.AddrFeeUpdateReq true "更新PoolAddr信息"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"更新成功"}"
// @Router /poolAddrWeb/updateAddrFee [post]
func (poolAddrApi *PoolAddrApi) UpdateAddrFee(c *gin.Context) {
	var poolAddr entrustPoolReq.AddrFeeUpdateReq
	err := c.ShouldBindJSON(&poolAddr)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if _, err := poolAddrService.UpdateAddrFee(poolAddr.Addr,poolAddr.Miner,poolAddr.Hash); err != nil {
		global.GVA_LOG.Error("更新失败!", zap.Error(err))
		response.FailWithMessage("更新失败", c)
	} else {
		response.OkWithMessage("更新成功", c)
	}
}


// GetPoolAddrList 分页获取PoolAddr列表
// @Tags PoolAddr
// @Summary 分页获取PoolAddr列表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query entrustPoolReq.PoolAddrSearch true "分页获取PoolAddr列表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /poolAddr/getPoolAddrList [get]
func (poolAddrApi *PoolAddrApi) GetPoolAddrList(c *gin.Context) {
	var pageInfo entrustPoolReq.PoolAddrSearch
	err := c.ShouldBindQuery(&pageInfo)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if list, total, err := poolAddrService.GetPoolAddrInfoList(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)
    }
}
