59 lines
1.5 KiB
Go
59 lines
1.5 KiB
Go
// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
|
|
|
|
package bills
|
|
|
|
import (
|
|
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
|
|
"github.com/TeaOSLab/EdgeCommon/pkg/langs/codes"
|
|
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
|
"github.com/iwind/TeaGo/types"
|
|
)
|
|
|
|
type PayAction struct {
|
|
actionutils.ParentAction
|
|
}
|
|
|
|
func (this *PayAction) RunPost(params struct {
|
|
BillId int64
|
|
}) {
|
|
defer this.CreateLogInfo(codes.UserBill_LogPayUserBill, params.BillId)
|
|
|
|
billResp, err := this.RPC().UserBillRPC().FindUserBill(this.AdminContext(), &pb.FindUserBillRequest{UserBillId: params.BillId})
|
|
if err != nil {
|
|
this.ErrorPage(err)
|
|
return
|
|
}
|
|
var bill = billResp.UserBill
|
|
if bill == nil {
|
|
this.Fail("找不到要支付的账单")
|
|
}
|
|
|
|
if bill.IsPaid {
|
|
this.Fail("此账单已支付,不需要重复支付")
|
|
}
|
|
|
|
// 账号余额
|
|
accountResp, err := this.RPC().UserAccountRPC().FindEnabledUserAccountWithUserId(this.AdminContext(), &pb.FindEnabledUserAccountWithUserIdRequest{UserId: bill.User.Id})
|
|
if err != nil {
|
|
this.ErrorPage(err)
|
|
return
|
|
}
|
|
var account = accountResp.UserAccount
|
|
if account == nil {
|
|
this.Fail("当前用户还没有账号")
|
|
}
|
|
|
|
if account.Total < bill.Amount {
|
|
this.Fail("账号余额不足,当前余额:" + types.String(account.Total) + ",需要支付:" + types.String(bill.Amount))
|
|
}
|
|
|
|
// 支付
|
|
_, err = this.RPC().UserBillRPC().PayUserBill(this.AdminContext(), &pb.PayUserBillRequest{UserBillId: params.BillId})
|
|
if err != nil {
|
|
this.ErrorPage(err)
|
|
return
|
|
}
|
|
|
|
this.Success()
|
|
}
|