mirror of
https://github.com/TeaOSLab/EdgeAPI.git
synced 2025-11-09 03:50:26 +08:00
用户账单增加多个API
This commit is contained in:
@@ -68,6 +68,17 @@ func init() {
|
||||
})
|
||||
}
|
||||
|
||||
// FindUserBill 查找单个账单
|
||||
func (this *UserBillDAO) FindUserBill(tx *dbs.Tx, billId int64) (*UserBill, error) {
|
||||
one, err := this.Query(tx).
|
||||
Pk(billId).
|
||||
Find()
|
||||
if err != nil || one == nil {
|
||||
return nil, err
|
||||
}
|
||||
return one.(*UserBill), nil
|
||||
}
|
||||
|
||||
// CountAllUserBills 计算账单数量
|
||||
func (this *UserBillDAO) CountAllUserBills(tx *dbs.Tx, isPaid int32, userId int64, month string) (int64, error) {
|
||||
query := this.Query(tx)
|
||||
@@ -330,3 +341,33 @@ func (this *UserBillDAO) GenerateBillCode(tx *dbs.Tx) (string, error) {
|
||||
}
|
||||
return this.GenerateBillCode(tx)
|
||||
}
|
||||
|
||||
// CheckUserBill 检查用户账单
|
||||
func (this *UserBillDAO) CheckUserBill(tx *dbs.Tx, userId int64, billId int64) error {
|
||||
if userId <= 0 || billId <= 0 {
|
||||
return ErrNotFound
|
||||
}
|
||||
exists, err := this.Query(tx).
|
||||
Pk(billId).
|
||||
Attr("userId", userId).
|
||||
Exist()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !exists {
|
||||
return ErrNotFound
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// SumUnpaidUserBill 计算某个用户未支付总额
|
||||
func (this *UserBillDAO) SumUnpaidUserBill(tx *dbs.Tx, userId int64) (float32, error) {
|
||||
sum, err := this.Query(tx).
|
||||
Attr("userId", userId).
|
||||
Attr("isPaid", 0).
|
||||
Sum("amount", 0)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return float32(sum), nil
|
||||
}
|
||||
|
||||
@@ -3,8 +3,12 @@ package services
|
||||
import (
|
||||
"context"
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/db/models"
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/db/models/accounts"
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/userconfigs"
|
||||
"github.com/iwind/TeaGo/dbs"
|
||||
"github.com/iwind/TeaGo/maps"
|
||||
timeutil "github.com/iwind/TeaGo/utils/time"
|
||||
"regexp"
|
||||
)
|
||||
@@ -97,3 +101,142 @@ func (this *UserBillService) ListUserBills(ctx context.Context, req *pb.ListUser
|
||||
}
|
||||
return &pb.ListUserBillsResponse{UserBills: result}, nil
|
||||
}
|
||||
|
||||
// FindUserBill 查找账单信息
|
||||
func (this *UserBillService) FindUserBill(ctx context.Context, req *pb.FindUserBillRequest) (*pb.FindUserBillResponse, error) {
|
||||
_, userId, err := this.ValidateAdminAndUser(ctx, 0, 0)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var tx = this.NullTx()
|
||||
|
||||
// 检查用户
|
||||
if userId > 0 {
|
||||
err = models.SharedUserBillDAO.CheckUserBill(tx, userId, req.UserBillId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
bill, err := models.SharedUserBillDAO.FindUserBill(tx, req.UserBillId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if bill == nil {
|
||||
return &pb.FindUserBillResponse{UserBill: nil}, nil
|
||||
}
|
||||
|
||||
// 用户
|
||||
var pbUser = &pb.User{Id: int64(bill.UserId)}
|
||||
user, err := models.SharedUserDAO.FindEnabledUser(tx, int64(bill.UserId), nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if user != nil {
|
||||
pbUser = &pb.User{
|
||||
Id: int64(user.Id),
|
||||
Username: user.Username,
|
||||
Fullname: user.Fullname,
|
||||
}
|
||||
}
|
||||
|
||||
return &pb.FindUserBillResponse{
|
||||
UserBill: &pb.UserBill{
|
||||
Id: int64(bill.Id),
|
||||
User: pbUser,
|
||||
Type: bill.Type,
|
||||
TypeName: models.SharedUserBillDAO.BillTypeName(bill.Type),
|
||||
Description: bill.Description,
|
||||
Amount: float32(bill.Amount),
|
||||
Month: bill.Month,
|
||||
IsPaid: bill.IsPaid == 1,
|
||||
PaidAt: int64(bill.PaidAt),
|
||||
Code: bill.Code,
|
||||
},
|
||||
}, nil
|
||||
}
|
||||
|
||||
// PayUserBill 支付账单
|
||||
func (this *UserBillService) PayUserBill(ctx context.Context, req *pb.PayUserBillRequest) (*pb.RPCSuccess, error) {
|
||||
_, userId, err := this.ValidateAdminAndUser(ctx, 0, 0)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
err = this.RunTx(func(tx *dbs.Tx) error {
|
||||
// 检查用户
|
||||
if userId > 0 {
|
||||
err = models.SharedUserBillDAO.CheckUserBill(tx, userId, req.UserBillId)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
// 是否存在
|
||||
bill, err := models.SharedUserBillDAO.FindUserBill(tx, req.UserBillId)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if bill == nil {
|
||||
return nil
|
||||
}
|
||||
userId = int64(bill.UserId)
|
||||
|
||||
// 是否已支付
|
||||
if bill.IsPaid == 1 {
|
||||
return nil
|
||||
}
|
||||
|
||||
if bill.Amount <= 0 {
|
||||
// 直接修改为已支付
|
||||
return models.SharedUserBillDAO.UpdateUserBillIsPaid(tx, req.UserBillId, true)
|
||||
}
|
||||
|
||||
// 余额是否足够
|
||||
account, err := accounts.SharedUserAccountDAO.FindUserAccountWithUserId(tx, userId)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if account == nil {
|
||||
return errors.New("can not find user account")
|
||||
}
|
||||
|
||||
if account.Total < bill.Amount {
|
||||
return errors.New("not enough balance to pay")
|
||||
}
|
||||
|
||||
err = accounts.SharedUserAccountDAO.UpdateUserAccount(tx, int64(account.Id), -float32(bill.Amount), userconfigs.AccountEventTypePayBill, "支付账单"+bill.Code, maps.Map{"billId": bill.Id})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// 修改为已支付
|
||||
return models.SharedUserBillDAO.UpdateUserBillIsPaid(tx, req.UserBillId, true)
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return this.Success()
|
||||
}
|
||||
|
||||
// SumUserUnpaidBills 计算用户所有未支付账单总额
|
||||
func (this *UserBillService) SumUserUnpaidBills(ctx context.Context, req *pb.SumUserUnpaidBillsRequest) (*pb.SumUserUnpaidBillsResponse, error) {
|
||||
_, userId, err := this.ValidateAdminAndUser(ctx, 0, 0)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var tx = this.NullTx()
|
||||
if userId > 0 {
|
||||
req.UserId = userId
|
||||
}
|
||||
|
||||
sum, err := models.SharedUserBillDAO.SumUnpaidUserBill(tx, userId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &pb.SumUserUnpaidBillsResponse{Amount: sum}, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user