package app

import (
	"bwallet/pkg/errno"
	"bwallet/pkg/handler"
	"bwallet/pkg/util"
	"bwallet/service/coin_service"
	"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
	}

	coinService := coin_service.Coin{
		Name:     name,
		PageNum:  util.GetPage(c),
		PageSize: util.GetLimit(c),
	}

	total, err := coinService.Count()
	if err != nil || 0 == total {
		handler.SendResponse(c, errno.ErrCountCoin, nil)
		return
	}

	coins, err := coinService.GetAll()
	if err != nil {
		handler.SendResponse(c, errno.InternalServerError, nil)
		return
	}
	feeService := fee_service.Fee{
		PlatformId: platform_id,
		Cid:        coins[1
		].ID,
		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
}
