package controller

import (
	"github.com/gin-gonic/gin"
	"github.com/sirupsen/logrus"
	"youngee_b_api/app/service"
	"youngee_b_api/app/util"
	"youngee_b_api/app/vo"
)

type FinanceController struct{}

// 充值管理——对公转账
func (f FinanceController) TransferToPublic(c *gin.Context) {
	param := &vo.RechargeTransferParam{}
	err := c.BindJSON(param)
	if err != nil {
		logrus.Errorf("Request bind err:%+v\n", err)
		returnError(c, 40000, "Parameter Error: "+err.Error())
		return
	}
	rechargeId, err := service.RechargeService{}.TransferToPublic(param)
	if err != nil {
		logrus.Errorf("[TransferToPublic] call Show err:%+v\n", err)
		returnError(c, 40000, err.Error())
		return
	}
	resultMap := make(map[string]string)
	resultMap["rechargeId"] = *rechargeId
	returnSuccess(c, 20000, resultMap)
}

// 获取微信支付CodeUrl
func (f FinanceController) GetCodeUrl(c *gin.Context) {
	param := &vo.GetCodeUrlParam{}
	err := c.BindJSON(param)
	if err != nil {
		logrus.Errorf("Request bind err:%+v\n", err)
		returnError(c, 40000, "Parameter Error: "+err.Error())
		return
	}
	tradeId := util.GenerateDateRelatedUUID(16)
	codeUrl, timeExpire, err := service.RechargeService{}.NativeApiServicePrepay(param.EnterpriseId, param.SubAccountId, tradeId, param.Amount)
	if err != nil {
		logrus.Errorf("[GetCodeUrl] call Show err:%+v\n", err)
		returnError(c, 40000, err.Error())
		return
	}
	reCodeUrl := vo.ReCodeUrl{
		CodeUrl:    codeUrl,
		TradeId:    tradeId,
		TimeExpire: timeExpire.Format("2006-01-02 15:04:05"),
	}
	returnSuccess(c, 20000, reCodeUrl)
}

// 根据交易id查询微信是否扫码付款
func (f FinanceController) QueryOrderByTradeId(c *gin.Context) {
	param := &vo.QueryOrderByTradeIdParam{}
	err := c.BindJSON(param)
	if err != nil {
		logrus.Errorf("Request bind err:%+v\n", err)
		returnError(c, 40000, "Parameter Error: "+err.Error())
		return
	}
	tradeState, err := service.RechargeService{}.QueryOrderByTradeId(param.EnterpriseId, param.SubAccountId, param.TradeId)
	if err != nil {
		logrus.Errorf("[QueryOrderByTradeId] call Show err:%+v\n", err)
		returnError(c, 40000, err.Error())
		return
	}
	resultMap := make(map[string]string)
	resultMap["tradeState"] = tradeState
	returnSuccess(c, 20000, resultMap)
}

// 余额管理——总金额、可用余额、冻结金额
func (f FinanceController) ShowBalance(c *gin.Context) {
	param := &vo.BalanceParam{}
	err := c.BindJSON(param)
	if err != nil {
		logrus.Errorf("Request bind err:%+v\n", err)
		returnError(c, 40000, "Parameter Error: "+err.Error())
		return
	}
	reBalanceShow, err := service.RechargeService{}.ShowBalance(param)
	if err != nil {
		logrus.Errorf("[ShowBalance] call Show err:%+v\n", err)
		returnError(c, 40000, err.Error())
		return
	}
	returnSuccess(c, 20000, reBalanceShow)
}

// 余额管理——冻结记录
func (f FinanceController) FrozenInfoList(c *gin.Context) {
	param := &vo.BalanceParam{}
	err := c.BindJSON(param)
	if err != nil {
		logrus.Errorf("Request bind err:%+v\n", err)
		returnError(c, 40000, "Parameter Error: "+err.Error())
		return
	}
	result, err := service.RechargeService{}.FrozenInfoList(param)
	if err != nil {
		logrus.Errorf("[FrozenInfoList] call Show err:%+v\n", err)
		returnError(c, 40000, err.Error())
		return
	}
	returnSuccess(c, 20000, result)
}

// 余额管理——冻结记录-角标
func (f FinanceController) FrozenInfoCount(c *gin.Context) {
	param := &vo.BalanceParam{}
	err := c.BindJSON(param)
	if err != nil {
		logrus.Errorf("Request bind err:%+v\n", err)
		returnError(c, 40000, "Parameter Error: "+err.Error())
		return
	}
	result := service.RechargeService{}.FrozenInfoCount(param)

	returnSuccess(c, 20000, result)
}

// 充值管理——累计充值金额、确认中金额
func (f FinanceController) ShowRecharge(c *gin.Context) {
	param := &vo.RechargeParam{}
	err := c.BindJSON(param)
	if err != nil {
		logrus.Errorf("Request bind err:%+v\n", err)
		returnError(c, 40000, "Parameter Error: "+err.Error())
		return
	}
	reRechargeShow, err := service.RechargeService{}.ShowRecharge(param)
	if err != nil {
		logrus.Errorf("[ShowRecharge] call Show err:%+v\n", err)
		returnError(c, 40000, err.Error())
		return
	}
	returnSuccess(c, 20000, reRechargeShow)
}

// 充值管理——充值记录
func (f FinanceController) RechargeInfoList(c *gin.Context) {
	param := &vo.RechargeParam{}
	err := c.BindJSON(param)
	if err != nil {
		logrus.Errorf("Request bind err:%+v\n", err)
		returnError(c, 40000, "Parameter Error: "+err.Error())
		return
	}
	result, err := service.RechargeService{}.RechargeInfoList(param)
	if err != nil {
		logrus.Errorf("[RechargeInfoList] call Show err:%+v\n", err)
		returnError(c, 40000, err.Error())
		return
	}
	returnSuccess(c, 20000, result)
}

// 充值管理——充值记录-角标
func (f FinanceController) RechargeInfoCount(c *gin.Context) {
	param := &vo.RechargeParam{}
	err := c.BindJSON(param)
	if err != nil {
		logrus.Errorf("Request bind err:%+v\n", err)
		returnError(c, 40000, "Parameter Error: "+err.Error())
		return
	}
	result := service.RechargeService{}.RechargeInfoCount(param)

	returnSuccess(c, 20000, result)
}

// 设置默认开票抬头
func (f FinanceController) UpdateInvoiceDefault(c *gin.Context) {
	param := &vo.InvoiceDefaultParam{}
	err := c.BindJSON(param)
	if err != nil {
		logrus.Errorf("Request bind err:%+v\n", err)
		returnError(c, 40000, "Parameter Error: "+err.Error())
		return
	}
	invoiceId, err := service.InvoiceService{}.UpdateInvoiceDefault(param)
	if err != nil {
		logrus.Errorf("[UpdateInvoiceDefault] call Show err:%+v\n", err)
		returnError(c, 40000, err.Error())
		return
	}
	resultMap := make(map[string]int64)
	resultMap["invoiceId"] = *invoiceId
	returnSuccess(c, 20000, resultMap)
}

// 获取默认开票抬头
func (f FinanceController) GetInvoiceDefault(c *gin.Context) {
	param := &vo.InvoiceDefaultParam{}
	err := c.BindJSON(param)
	if err != nil {
		logrus.Errorf("Request bind err:%+v\n", err)
		returnError(c, 40000, "Parameter Error: "+err.Error())
		return
	}
	reInvoiceInfo, err := service.InvoiceService{}.GetInvoiceDefault(param)
	if err != nil {
		logrus.Errorf("[GetInvoiceDefault] call Show err:%+v\n", err)
		returnError(c, 40000, err.Error())
		return
	}
	returnSuccess(c, 20000, reInvoiceInfo)
}

// 确认开票
func (f FinanceController) BillInvoice(c *gin.Context) {
	param := &vo.InvoiceBillParam{}
	err := c.BindJSON(param)
	if err != nil || len(param.TaskIds) != 3 {
		logrus.Errorf("Request bind err:%+v\n", err)
		returnError(c, 40000, "Parameter Error: "+err.Error())
		return
	}
	billingId, err := service.InvoiceService{}.BillInvoice(param)
	if err != nil {
		logrus.Errorf("[BillInvoice] call Show err:%+v\n", err)
		returnError(c, 40000, err.Error())
		return
	}
	resultMap := make(map[string]string)
	resultMap["billingId"] = *billingId
	returnSuccess(c, 20000, resultMap)
}

// 开票记录
func (f FinanceController) GetBillList(c *gin.Context) {
	param := &vo.InvoiceBillListParam{}
	err := c.BindJSON(param)
	if err != nil {
		logrus.Errorf("Request bind err:%+v\n", err)
		returnError(c, 40000, "Parameter Error: "+err.Error())
		return
	}
	result, err := service.InvoiceService{}.GetBillList(param)
	if err != nil {
		logrus.Errorf("[GetBillList] call Show err:%+v\n", err)
		returnError(c, 40000, err.Error())
		return
	}
	returnSuccess(c, 20000, result)
}

// 可开票账单
func (f FinanceController) GetBillableList(c *gin.Context) {
	param := &vo.InvoiceBillListParam{}
	err := c.BindJSON(param)
	if err != nil {
		logrus.Errorf("Request bind err:%+v\n", err)
		returnError(c, 40000, "Parameter Error: "+err.Error())
		return
	}
	result, err := service.InvoiceService{}.GetBillableList(param)
	if err != nil {
		logrus.Errorf("[GetBillableList] call Show err:%+v\n", err)
		returnError(c, 40000, err.Error())
		return
	}
	returnSuccess(c, 20000, result)
}

// 开票金额数据-已开票、可开票、开票中
func (f FinanceController) ShowInvoiceBill(c *gin.Context) {
	param := &vo.InvoiceBillListParam{}
	err := c.BindJSON(param)
	if err != nil {
		logrus.Errorf("Request bind err:%+v\n", err)
		returnError(c, 40000, "Parameter Error: "+err.Error())
		return
	}
	res, err := service.InvoiceService{}.ShowInvoiceBill(param)
	if err != nil {
		logrus.Errorf("[ShowInvoiceBill] call Show err:%+v\n", err)
		returnError(c, 40000, err.Error())
		return
	}
	returnSuccess(c, 20000, res)
}
