mirror of
https://github.com/TeaOSLab/EdgeAPI.git
synced 2025-12-09 19:50:30 +08:00
删除不必要的文件
This commit is contained in:
@@ -1,132 +0,0 @@
|
||||
// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
|
||||
|
||||
package services
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/db/models"
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/utils"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
||||
)
|
||||
|
||||
// ServerBillService 服务账单相关服务
|
||||
type ServerBillService struct {
|
||||
BaseService
|
||||
}
|
||||
|
||||
// CountAllServerBills 查询服务账单数量
|
||||
func (this *ServerBillService) CountAllServerBills(ctx context.Context, req *pb.CountAllServerBillsRequest) (*pb.RPCCountResponse, error) {
|
||||
_, userId, err := this.ValidateAdminAndUser(ctx, true)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if userId > 0 {
|
||||
req.UserId = userId
|
||||
}
|
||||
|
||||
var tx = this.NullTx()
|
||||
count, err := models.SharedServerBillDAO.CountServerBills(tx, req.UserId, req.Month)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return this.SuccessCount(count)
|
||||
}
|
||||
|
||||
// ListServerBills 查询服务账单列表
|
||||
func (this *ServerBillService) ListServerBills(ctx context.Context, req *pb.ListServerBillsRequest) (*pb.ListServerBillsResponse, error) {
|
||||
_, userId, err := this.ValidateAdminAndUser(ctx, true)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if userId > 0 {
|
||||
req.UserId = userId
|
||||
}
|
||||
|
||||
var tx = this.NullTx()
|
||||
serverBills, err := models.SharedServerBillDAO.ListServerBills(tx, req.UserId, req.Month, req.Offset, req.Size)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var pbServerBills = []*pb.ServerBill{}
|
||||
var cacheMap = utils.NewCacheMap()
|
||||
for _, bill := range serverBills {
|
||||
// user
|
||||
user, err := models.SharedUserDAO.FindBasicUserWithoutState(tx, int64(bill.UserId))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var pbUser = &pb.User{Id: int64(bill.UserId)}
|
||||
if user != nil {
|
||||
pbUser = &pb.User{
|
||||
Id: int64(bill.UserId),
|
||||
Username: user.Username,
|
||||
Fullname: user.Fullname,
|
||||
}
|
||||
}
|
||||
|
||||
// plan
|
||||
var pbPlan *pb.Plan
|
||||
if bill.PlanId > 0 {
|
||||
plan, err := models.SharedPlanDAO.FindEnabledPlan(tx, int64(bill.PlanId))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if plan != nil {
|
||||
pbPlan = &pb.Plan{
|
||||
Id: int64(plan.Id),
|
||||
Name: plan.Name,
|
||||
PriceType: plan.PriceType,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// user plan
|
||||
var pbUserPlan *pb.UserPlan
|
||||
if bill.UserPlanId > 0 {
|
||||
userPlan, err := models.SharedUserPlanDAO.FindEnabledUserPlan(tx, int64(bill.UserPlanId), cacheMap)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if userPlan != nil {
|
||||
pbUserPlan = &pb.UserPlan{
|
||||
Id: int64(userPlan.Id),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// server
|
||||
var pbServer *pb.Server
|
||||
if bill.ServerId > 0 {
|
||||
server, err := models.SharedServerDAO.FindEnabledServerBasic(tx, int64(bill.ServerId))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if server != nil {
|
||||
pbServer = &pb.Server{Id: int64(bill.ServerId), Name: server.Name}
|
||||
}
|
||||
}
|
||||
|
||||
pbServerBills = append(pbServerBills, &pb.ServerBill{
|
||||
Id: int64(bill.Id),
|
||||
UserId: int64(bill.UserId),
|
||||
ServerId: int64(bill.ServerId),
|
||||
Amount: float32(bill.Amount),
|
||||
PriceType: bill.PriceType,
|
||||
CreatedAt: int64(bill.CreatedAt),
|
||||
UserPlanId: int64(bill.UserPlanId),
|
||||
PlanId: int64(bill.PlanId),
|
||||
TotalTrafficBytes: int64(bill.TotalTrafficBytes),
|
||||
BandwidthPercentileBytes: int64(bill.BandwidthPercentileBytes),
|
||||
BandwidthPercentile: int32(bill.BandwidthPercentile),
|
||||
User: pbUser,
|
||||
Plan: pbPlan,
|
||||
UserPlan: pbUserPlan,
|
||||
Server: pbServer,
|
||||
})
|
||||
}
|
||||
|
||||
return &pb.ListServerBillsResponse{ServerBills: pbServerBills}, nil
|
||||
}
|
||||
@@ -1,255 +0,0 @@
|
||||
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"
|
||||
)
|
||||
|
||||
// UserBillService 账单相关服务
|
||||
type UserBillService struct {
|
||||
BaseService
|
||||
}
|
||||
|
||||
// GenerateAllUserBills 手工生成订单
|
||||
func (this *UserBillService) GenerateAllUserBills(ctx context.Context, req *pb.GenerateAllUserBillsRequest) (*pb.RPCSuccess, error) {
|
||||
_, err := this.ValidateAdmin(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 校验Month
|
||||
if !regexp.MustCompile(`^\d{6}$`).MatchString(req.Month) {
|
||||
return nil, errors.New("invalid month '" + req.Month + "'")
|
||||
}
|
||||
if req.Month >= timeutil.Format("Ym") {
|
||||
return nil, errors.New("invalid month '" + req.Month + "'")
|
||||
}
|
||||
|
||||
var tx = this.NullTx()
|
||||
|
||||
err = models.SharedUserBillDAO.GenerateBills(tx, req.Month)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return this.Success()
|
||||
}
|
||||
|
||||
// CountAllUserBills 计算所有账单数量
|
||||
func (this *UserBillService) CountAllUserBills(ctx context.Context, req *pb.CountAllUserBillsRequest) (*pb.RPCCountResponse, error) {
|
||||
_, userId, err := this.ValidateAdminAndUser(ctx, true)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var tx = this.NullTx()
|
||||
if userId > 0 {
|
||||
req.UserId = userId
|
||||
}
|
||||
|
||||
count, err := models.SharedUserBillDAO.CountAllUserBills(tx, req.PaidFlag, req.UserId, req.Month)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return this.SuccessCount(count)
|
||||
}
|
||||
|
||||
// ListUserBills 列出单页账单
|
||||
func (this *UserBillService) ListUserBills(ctx context.Context, req *pb.ListUserBillsRequest) (*pb.ListUserBillsResponse, error) {
|
||||
_, userId, err := this.ValidateAdminAndUser(ctx, true)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var tx = this.NullTx()
|
||||
if userId > 0 {
|
||||
req.UserId = userId
|
||||
}
|
||||
|
||||
bills, err := models.SharedUserBillDAO.ListUserBills(tx, req.PaidFlag, req.UserId, req.Month, req.Offset, req.Size)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
result := []*pb.UserBill{}
|
||||
for _, bill := range bills {
|
||||
user, err := models.SharedUserDAO.FindBasicUserWithoutState(tx, int64(bill.UserId))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if user == nil {
|
||||
user = &models.User{Id: bill.UserId}
|
||||
}
|
||||
|
||||
result = append(result, &pb.UserBill{
|
||||
Id: int64(bill.Id),
|
||||
User: &pb.User{
|
||||
Id: int64(bill.UserId),
|
||||
Fullname: user.Fullname,
|
||||
Username: user.Username,
|
||||
IsDeleted: user.State == models.UserStateDisabled,
|
||||
},
|
||||
Type: bill.Type,
|
||||
TypeName: models.SharedUserBillDAO.BillTypeName(bill.Type),
|
||||
Description: bill.Description,
|
||||
Amount: float32(bill.Amount),
|
||||
Month: bill.Month,
|
||||
CanPay: bill.CanPay,
|
||||
IsPaid: bill.IsPaid,
|
||||
PaidAt: int64(bill.PaidAt),
|
||||
Code: bill.Code,
|
||||
})
|
||||
}
|
||||
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, true)
|
||||
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,
|
||||
CanPay: bill.CanPay,
|
||||
IsPaid: bill.IsPaid,
|
||||
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, true)
|
||||
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 {
|
||||
return nil
|
||||
}
|
||||
|
||||
if bill.Amount <= 0 {
|
||||
// 直接修改为已支付
|
||||
return models.SharedUserBillDAO.UpdateUserBillIsPaid(tx, req.UserBillId, true)
|
||||
}
|
||||
|
||||
if !bill.CanPay {
|
||||
return errors.New("can not pay now")
|
||||
}
|
||||
|
||||
// 余额是否足够
|
||||
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, true)
|
||||
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