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

var poolService = service.ServiceGroupApp.EntrustPoolServiceGroup.PoolService


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

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

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

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

// FindPool 用id查询Pool
// @Tags Pool
// @Summary 用id查询Pool
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query entrustPool.Pool true "用id查询Pool"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"查询成功"}"
// @Router /pool/findPool [get]
func (poolApi *PoolApi) FindPool(c *gin.Context) {
	var pool entrustPool.Pool
	err := c.ShouldBindQuery(&pool)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if pool.ID > 0 {
		if repool, err := poolService.GetPool(pool.ID); err != nil {
			global.GVA_LOG.Error("查询失败!", zap.Error(err))
			response.FailWithMessage("查询失败", c)
		} else {
			response.OkWithData(gin.H{"repool": repool}, c)
		}
	} else if pool.MinerAddr != "" {
		if repool, err := poolService.GetPoolByAddr(pool.MinerAddr); err != nil {
			global.GVA_LOG.Error("查询失败!", zap.Error(err))
			response.FailWithMessage("查询失败", c)
		} else {
			response.OkWithData(gin.H{"repool": repool}, c)
		}
	} else if pool.Creator != "" {
		if repool, err := poolService.GetPoolByCreator(pool.Creator); err != nil {
			global.GVA_LOG.Error("查询失败!", zap.Error(err))
			response.FailWithMessage("查询失败", c)
		} else {
			response.OkWithData(gin.H{"repool": repool}, c)
		}
	}

}

// GetPoolList2 分页获取Pool列表
// @Tags Pool
// @Summary 分页获取Pool列表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query entrustPoolReq.PoolSearch true "分页获取Pool列表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /pool/getPoolList [get]
func (poolApi *PoolApi) GetPoolList2(c *gin.Context) {
	var pageInfo entrustPoolReq.PoolSearch
	err := c.ShouldBindQuery(&pageInfo)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if list, total, err := poolService.GetPoolInfoList2(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)
	}
}

// GetPoolList 分页获取Pool列表
// @Tags Pool
// @Summary 分页获取Pool列表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query entrustPoolReq.PoolSearch true "分页获取Pool列表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /pool/getPoolList [get]
func (poolApi *PoolApi) GetPoolList(c *gin.Context) {
	var pageInfo entrustPoolReq.PoolSearch
	err := c.ShouldBindQuery(&pageInfo)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if list, total, err := poolService.GetPoolInfoList(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)
    }
}


// AddPool 创建Pool
// @Tags Pool
// @Summary 创建Pool
// @accept application/json
// @Produce application/json
// @Param data body entrustPool.Pool true "创建Pool"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /poolWeb/addPool [post]
func (poolApi *PoolApi) AddPool(c *gin.Context) {
	var pool entrustPool.Pool
	err := c.ShouldBindJSON(&pool)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	verify := utils.Rules{
		"MinerAddr":{utils.NotEmpty()},
		"Name":{utils.NotEmpty()},
		"FeeAddr":{utils.NotEmpty()},
	}
	if err := utils.Verify(pool, verify); err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := poolService.CreatePool(pool); err != nil {
		global.GVA_LOG.Error("创建失败!", zap.Error(err))
		response.FailWithMessage(err.Error(), c)
	} else {
		response.OkWithMessage("创建成功", c)
	}
}

// UpdateWebPool 更新Pool
// @Tags Pool
// @Summary 更新Pool
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body entrustPool.PoolNameReq true "更新Pool"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"更新成功"}"
// @Router /poolWeb/updatePool [put]
func (poolApi *PoolApi) UpdateWebPool(c *gin.Context) {
	var pool entrustPool.PoolNameReq
	err := c.ShouldBindJSON(&pool)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := poolService.UpdateWebPool(pool); err != nil {
		global.GVA_LOG.Error("更新失败!", zap.Error(err))
		response.FailWithMessage("更新失败", c)
	} else {
		response.OkWithMessage("更新成功", c)
	}
}