feat(subscribe): add traffic limit rules and user traffic stats
- Add subscribe traffic_limit schema and migration\n- Support traffic_limit in admin create/update and list/details\n- Apply traffic_limit when building server user list speed limits\n- Add public user traffic stats API
This commit is contained in:
@@ -82,7 +82,7 @@ type customUserLogicModel interface {
|
||||
FindOneSubscribeDetailsById(ctx context.Context, id int64) (*SubscribeDetails, error)
|
||||
FindOneUserSubscribe(ctx context.Context, id int64) (*SubscribeDetails, error)
|
||||
FindUsersSubscribeBySubscribeId(ctx context.Context, subscribeId int64) ([]*Subscribe, error)
|
||||
UpdateUserSubscribeWithTraffic(ctx context.Context, id, download, upload int64, tx ...*gorm.DB) error
|
||||
UpdateUserSubscribeWithTraffic(ctx context.Context, id, download, upload int64, isExpired bool, tx ...*gorm.DB) error
|
||||
QueryResisterUserTotalByDate(ctx context.Context, date time.Time) (int64, error)
|
||||
QueryResisterUserTotalByMonthly(ctx context.Context, date time.Time) (int64, error)
|
||||
QueryResisterUserTotal(ctx context.Context) (int64, error)
|
||||
@@ -181,7 +181,7 @@ func (m *customUserModel) BatchDeleteUser(ctx context.Context, ids []int64, tx .
|
||||
}, m.batchGetCacheKeys(users...)...)
|
||||
}
|
||||
|
||||
func (m *customUserModel) UpdateUserSubscribeWithTraffic(ctx context.Context, id, download, upload int64, tx ...*gorm.DB) error {
|
||||
func (m *customUserModel) UpdateUserSubscribeWithTraffic(ctx context.Context, id, download, upload int64, isExpired bool, tx ...*gorm.DB) error {
|
||||
sub, err := m.FindOneSubscribe(ctx, id)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -198,10 +198,21 @@ func (m *customUserModel) UpdateUserSubscribeWithTraffic(ctx context.Context, id
|
||||
if len(tx) > 0 {
|
||||
conn = tx[0]
|
||||
}
|
||||
return conn.Model(&Subscribe{}).Where("id = ?", id).Updates(map[string]interface{}{
|
||||
"download": gorm.Expr("download + ?", download),
|
||||
"upload": gorm.Expr("upload + ?", upload),
|
||||
}).Error
|
||||
|
||||
// 根据订阅状态更新对应的流量字段
|
||||
if isExpired {
|
||||
// 过期期间,更新过期流量字段
|
||||
return conn.Model(&Subscribe{}).Where("id = ?", id).Updates(map[string]interface{}{
|
||||
"expired_download": gorm.Expr("expired_download + ?", download),
|
||||
"expired_upload": gorm.Expr("expired_upload + ?", upload),
|
||||
}).Error
|
||||
} else {
|
||||
// 正常期间,更新正常流量字段
|
||||
return conn.Model(&Subscribe{}).Where("id = ?", id).Updates(map[string]interface{}{
|
||||
"download": gorm.Expr("download + ?", download),
|
||||
"upload": gorm.Expr("upload + ?", upload),
|
||||
}).Error
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
+21
-19
@@ -85,25 +85,27 @@ func (*User) TableName() string {
|
||||
}
|
||||
|
||||
type Subscribe struct {
|
||||
Id int64 `gorm:"primaryKey"`
|
||||
UserId int64 `gorm:"index:idx_user_id;not null;comment:User ID"`
|
||||
User User `gorm:"foreignKey:UserId;references:Id"`
|
||||
OrderId int64 `gorm:"index:idx_order_id;not null;comment:Order ID"`
|
||||
SubscribeId int64 `gorm:"index:idx_subscribe_id;not null;comment:Subscription ID"`
|
||||
NodeGroupId int64 `gorm:"index:idx_node_group_id;not null;default:0;comment:Node Group ID (single ID)"`
|
||||
GroupLocked *bool `gorm:"type:tinyint(1);not null;default:0;comment:Group Locked"`
|
||||
StartTime time.Time `gorm:"default:CURRENT_TIMESTAMP(3);not null;comment:Subscription Start Time"`
|
||||
ExpireTime time.Time `gorm:"default:NULL;comment:Subscription Expire Time"`
|
||||
FinishedAt *time.Time `gorm:"default:NULL;comment:Finished Time"`
|
||||
Traffic int64 `gorm:"default:0;comment:Traffic"`
|
||||
Download int64 `gorm:"default:0;comment:Download Traffic"`
|
||||
Upload int64 `gorm:"default:0;comment:Upload Traffic"`
|
||||
Token string `gorm:"index:idx_token;unique;type:varchar(255);default:'';comment:Token"`
|
||||
UUID string `gorm:"type:varchar(255);unique;index:idx_uuid;default:'';comment:UUID"`
|
||||
Status uint8 `gorm:"type:tinyint(1);default:0;comment:Subscription Status: 0: Pending 1: Active 2: Finished 3: Expired 4: Deducted 5: stopped"`
|
||||
Note string `gorm:"type:varchar(500);default:'';comment:User note for subscription"`
|
||||
CreatedAt time.Time `gorm:"<-:create;comment:Creation Time"`
|
||||
UpdatedAt time.Time `gorm:"comment:Update Time"`
|
||||
Id int64 `gorm:"primaryKey"`
|
||||
UserId int64 `gorm:"index:idx_user_id;not null;comment:User ID"`
|
||||
User User `gorm:"foreignKey:UserId;references:Id"`
|
||||
OrderId int64 `gorm:"index:idx_order_id;not null;comment:Order ID"`
|
||||
SubscribeId int64 `gorm:"index:idx_subscribe_id;not null;comment:Subscription ID"`
|
||||
NodeGroupId int64 `gorm:"index:idx_node_group_id;not null;default:0;comment:Node Group ID (single ID)"`
|
||||
GroupLocked *bool `gorm:"type:tinyint(1);not null;default:0;comment:Group Locked"`
|
||||
StartTime time.Time `gorm:"default:CURRENT_TIMESTAMP(3);not null;comment:Subscription Start Time"`
|
||||
ExpireTime time.Time `gorm:"default:NULL;comment:Subscription Expire Time"`
|
||||
FinishedAt *time.Time `gorm:"default:NULL;comment:Finished Time"`
|
||||
Traffic int64 `gorm:"default:0;comment:Traffic"`
|
||||
Download int64 `gorm:"default:0;comment:Download Traffic"`
|
||||
Upload int64 `gorm:"default:0;comment:Upload Traffic"`
|
||||
ExpiredDownload int64 `gorm:"default:0;comment:Expired period download traffic (bytes)"`
|
||||
ExpiredUpload int64 `gorm:"default:0;comment:Expired period upload traffic (bytes)"`
|
||||
Token string `gorm:"index:idx_token;unique;type:varchar(255);default:'';comment:Token"`
|
||||
UUID string `gorm:"type:varchar(255);unique;index:idx_uuid;default:'';comment:UUID"`
|
||||
Status uint8 `gorm:"type:tinyint(1);default:0;comment:Subscription Status: 0: Pending 1: Active 2: Finished 3: Expired 4: Deducted 5: stopped"`
|
||||
Note string `gorm:"type:varchar(500);default:'';comment:User note for subscription"`
|
||||
CreatedAt time.Time `gorm:"<-:create;comment:Creation Time"`
|
||||
UpdatedAt time.Time `gorm:"comment:Update Time"`
|
||||
}
|
||||
|
||||
func (*Subscribe) TableName() string {
|
||||
|
||||
Reference in New Issue
Block a user