package app

import (
	"bwallet/pkg/errno"
	"bwallet/pkg/handler"
	"bwallet/pkg/util"
	"bwallet/service/fee_service"
	"github.com/Unknwon/com"
	"github.com/astaxie/beego/validation"
	"github.com/gin-gonic/gin"
)

func GetTransactionFee(c *gin.Context) {
	platform_id := com.StrTo(c.DefaultQuery("platform_id", "0")).MustInt()
	valid := validation.Validation{}
	valid.Min(platform_id, 1, "platform_id").Message("平台Id必须大于0")

	name := c.DefaultQuery("name", "")
	valid.Required(name, "name").Message("币种不能为空")

	if valid.HasErrors() {
		handler.SendResponse(c, valid.Errors[0], nil)
		return
	}

	feeService := fee_service.Fee{
		PlatformId: platform_id,
		CoinName:   name,
		PageNum:    util.GetPage(c),
		PageSize:   util.GetLimit(c),
	}

	total_fee, err_fee := feeService.Count()
	if err_fee != nil || 0 == total_fee {
		handler.SendResponse(c, errno.ErrCountCoin, nil)
		return
	}

	fee, err := feeService.Search()
	if err != nil {
		handler.SendResponse(c, errno.ErrCountCoin, nil)
		return
	}

	handler.SendResponse(c, nil, fee)
	return
}
