Features:
- Node group CRUD operations with traffic-based filtering - Three grouping modes: average distribution, subscription-based, and traffic-based - Automatic and manual group recalculation with history tracking - Group assignment preview before applying changes - User subscription group locking to prevent automatic reassignment - Subscribe-to-group mapping configuration - Group calculation history and detailed reports - System configuration for group management (enabled/mode/auto_create) Database: - Add node_group table for group definitions - Add group_history and group_history_detail tables for tracking - Add node_group_ids (JSON) to nodes and subscribe tables - Add node_group_id and group_locked fields to user_subscribe table - Add migration files for schema changes
This commit is contained in:
@@ -2,6 +2,7 @@ package subscribe
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strings"
|
||||
|
||||
"github.com/perfect-panel/server/pkg/tool"
|
||||
"github.com/redis/go-redis/v9"
|
||||
@@ -19,6 +20,13 @@ type FilterParams struct {
|
||||
Language string // Language
|
||||
DefaultLanguage bool // Default Subscribe Language Data
|
||||
Search string // Search Keywords
|
||||
NodeGroupId *int64 // Node Group ID
|
||||
}
|
||||
|
||||
type FilterByNodeGroupsParams struct {
|
||||
Page int // Page Number
|
||||
Size int // Page Size
|
||||
NodeGroupIds []int64 // Node Group IDs (multiple)
|
||||
}
|
||||
|
||||
func (p *FilterParams) Normalize() {
|
||||
@@ -32,6 +40,7 @@ func (p *FilterParams) Normalize() {
|
||||
|
||||
type customSubscribeLogicModel interface {
|
||||
FilterList(ctx context.Context, params *FilterParams) (int64, []*Subscribe, error)
|
||||
FilterListByNodeGroups(ctx context.Context, params *FilterByNodeGroupsParams) (int64, []*Subscribe, error)
|
||||
ClearCache(ctx context.Context, id ...int64) error
|
||||
QuerySubscribeMinSortByIds(ctx context.Context, ids []int64) (int64, error)
|
||||
}
|
||||
@@ -102,6 +111,10 @@ func (m *customSubscribeModel) FilterList(ctx context.Context, params *FilterPar
|
||||
if len(params.Tags) > 0 {
|
||||
query = query.Scopes(InSet("node_tags", params.Tags))
|
||||
}
|
||||
if params.NodeGroupId != nil {
|
||||
// Filter by node_group_ids using JSON_CONTAINS
|
||||
query = query.Where("JSON_CONTAINS(node_group_ids, ?)", *params.NodeGroupId)
|
||||
}
|
||||
if lang != "" {
|
||||
query = query.Where("language = ?", lang)
|
||||
} else if params.DefaultLanguage {
|
||||
@@ -154,3 +167,67 @@ func InSet(field string, values []string) func(db *gorm.DB) *gorm.DB {
|
||||
return query
|
||||
}
|
||||
}
|
||||
|
||||
// FilterListByNodeGroups Filter subscribes by node groups
|
||||
// Match if subscribe's node_group_id OR node_group_ids contains any of the provided node group IDs
|
||||
func (m *customSubscribeModel) FilterListByNodeGroups(ctx context.Context, params *FilterByNodeGroupsParams) (int64, []*Subscribe, error) {
|
||||
if params == nil {
|
||||
params = &FilterByNodeGroupsParams{
|
||||
Page: 1,
|
||||
Size: 10,
|
||||
}
|
||||
}
|
||||
if params.Page <= 0 {
|
||||
params.Page = 1
|
||||
}
|
||||
if params.Size <= 0 {
|
||||
params.Size = 10
|
||||
}
|
||||
|
||||
var list []*Subscribe
|
||||
var total int64
|
||||
|
||||
err := m.QueryNoCacheCtx(ctx, &list, func(conn *gorm.DB, v interface{}) error {
|
||||
query := conn.Model(&Subscribe{})
|
||||
|
||||
// Filter by node groups: match if node_group_id or node_group_ids contains any of the provided IDs
|
||||
if len(params.NodeGroupIds) > 0 {
|
||||
var conditions []string
|
||||
var args []interface{}
|
||||
|
||||
// Condition 1: node_group_id IN (...)
|
||||
placeholders := make([]string, len(params.NodeGroupIds))
|
||||
for i, id := range params.NodeGroupIds {
|
||||
placeholders[i] = "?"
|
||||
args = append(args, id)
|
||||
}
|
||||
conditions = append(conditions, "node_group_id IN ("+strings.Join(placeholders, ",")+")")
|
||||
|
||||
// Condition 2: JSON_CONTAINS(node_group_ids, id) for each id
|
||||
for _, id := range params.NodeGroupIds {
|
||||
conditions = append(conditions, "JSON_CONTAINS(node_group_ids, ?)")
|
||||
args = append(args, id)
|
||||
}
|
||||
|
||||
// Combine with OR: (node_group_id IN (...) OR JSON_CONTAINS(node_group_ids, id1) OR ...)
|
||||
query = query.Where("("+strings.Join(conditions, " OR ")+")", args...)
|
||||
}
|
||||
|
||||
// Count total
|
||||
if err := query.Count(&total).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Find with pagination
|
||||
return query.Order("sort ASC").
|
||||
Limit(params.Size).
|
||||
Offset((params.Page - 1) * params.Size).
|
||||
Find(v).Error
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
return 0, nil, err
|
||||
}
|
||||
|
||||
return total, list, nil
|
||||
}
|
||||
|
||||
@@ -1,37 +1,86 @@
|
||||
package subscribe
|
||||
|
||||
import (
|
||||
"database/sql/driver"
|
||||
"encoding/json"
|
||||
"time"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// JSONInt64Slice is a custom type for handling []int64 as JSON in database
|
||||
type JSONInt64Slice []int64
|
||||
|
||||
// Scan implements sql.Scanner interface
|
||||
func (j *JSONInt64Slice) Scan(value interface{}) error {
|
||||
if value == nil {
|
||||
*j = []int64{}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Handle []byte
|
||||
bytes, ok := value.([]byte)
|
||||
if !ok {
|
||||
// Try to handle string
|
||||
str, ok := value.(string)
|
||||
if !ok {
|
||||
*j = []int64{}
|
||||
return nil
|
||||
}
|
||||
bytes = []byte(str)
|
||||
}
|
||||
|
||||
if len(bytes) == 0 {
|
||||
*j = []int64{}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Check if it's a JSON array
|
||||
if bytes[0] != '[' {
|
||||
// Not a JSON array, return empty slice
|
||||
*j = []int64{}
|
||||
return nil
|
||||
}
|
||||
|
||||
return json.Unmarshal(bytes, j)
|
||||
}
|
||||
|
||||
// Value implements driver.Valuer interface
|
||||
func (j JSONInt64Slice) Value() (driver.Value, error) {
|
||||
if len(j) == 0 {
|
||||
return "[]", nil
|
||||
}
|
||||
return json.Marshal(j)
|
||||
}
|
||||
|
||||
type Subscribe struct {
|
||||
Id int64 `gorm:"primaryKey"`
|
||||
Name string `gorm:"type:varchar(255);not null;default:'';comment:Subscribe Name"`
|
||||
Language string `gorm:"type:varchar(255);not null;default:'';comment:Language"`
|
||||
Description string `gorm:"type:text;comment:Subscribe Description"`
|
||||
UnitPrice int64 `gorm:"type:int;not null;default:0;comment:Unit Price"`
|
||||
UnitTime string `gorm:"type:varchar(255);not null;default:'';comment:Unit Time"`
|
||||
Discount string `gorm:"type:text;comment:Discount"`
|
||||
Replacement int64 `gorm:"type:int;not null;default:0;comment:Replacement"`
|
||||
Inventory int64 `gorm:"type:int;not null;default:-1;comment:Inventory"`
|
||||
Traffic int64 `gorm:"type:int;not null;default:0;comment:Traffic"`
|
||||
SpeedLimit int64 `gorm:"type:int;not null;default:0;comment:Speed Limit"`
|
||||
DeviceLimit int64 `gorm:"type:int;not null;default:0;comment:Device Limit"`
|
||||
Quota int64 `gorm:"type:int;not null;default:0;comment:Quota"`
|
||||
Nodes string `gorm:"type:varchar(255);comment:Node Ids"`
|
||||
NodeTags string `gorm:"type:varchar(255);comment:Node Tags"`
|
||||
Show *bool `gorm:"type:tinyint(1);not null;default:0;comment:Show portal page"`
|
||||
Sell *bool `gorm:"type:tinyint(1);not null;default:0;comment:Sell"`
|
||||
Sort int64 `gorm:"type:int;not null;default:0;comment:Sort"`
|
||||
DeductionRatio int64 `gorm:"type:int;default:0;comment:Deduction Ratio"`
|
||||
AllowDeduction *bool `gorm:"type:tinyint(1);default:1;comment:Allow deduction"`
|
||||
ResetCycle int64 `gorm:"type:int;default:0;comment:Reset Cycle: 0: No Reset, 1: 1st, 2: Monthly, 3: Yearly"`
|
||||
RenewalReset *bool `gorm:"type:tinyint(1);default:0;comment:Renew Reset"`
|
||||
ShowOriginalPrice bool `gorm:"type:tinyint(1);not null;default:1;comment:Show Original Price"`
|
||||
CreatedAt time.Time `gorm:"<-:create;comment:Create Time"`
|
||||
UpdatedAt time.Time `gorm:"comment:Update Time"`
|
||||
Id int64 `gorm:"primaryKey"`
|
||||
Name string `gorm:"type:varchar(255);not null;default:'';comment:Subscribe Name"`
|
||||
Language string `gorm:"type:varchar(255);not null;default:'';comment:Language"`
|
||||
Description string `gorm:"type:text;comment:Subscribe Description"`
|
||||
UnitPrice int64 `gorm:"type:int;not null;default:0;comment:Unit Price"`
|
||||
UnitTime string `gorm:"type:varchar(255);not null;default:'';comment:Unit Time"`
|
||||
Discount string `gorm:"type:text;comment:Discount"`
|
||||
Replacement int64 `gorm:"type:int;not null;default:0;comment:Replacement"`
|
||||
Inventory int64 `gorm:"type:int;not null;default:-1;comment:Inventory"`
|
||||
Traffic int64 `gorm:"type:int;not null;default:0;comment:Traffic"`
|
||||
SpeedLimit int64 `gorm:"type:int;not null;default:0;comment:Speed Limit"`
|
||||
DeviceLimit int64 `gorm:"type:int;not null;default:0;comment:Device Limit"`
|
||||
Quota int64 `gorm:"type:int;not null;default:0;comment:Quota"`
|
||||
Nodes string `gorm:"type:varchar(255);comment:Node Ids"`
|
||||
NodeTags string `gorm:"type:varchar(255);comment:Node Tags"`
|
||||
NodeGroupIds JSONInt64Slice `gorm:"type:json;comment:Node Group IDs (JSON array, multiple groups)"`
|
||||
NodeGroupId int64 `gorm:"default:0;index:idx_node_group_id;comment:Default Node Group ID (single ID)"`
|
||||
Show *bool `gorm:"type:tinyint(1);not null;default:0;comment:Show portal page"`
|
||||
Sell *bool `gorm:"type:tinyint(1);not null;default:0;comment:Sell"`
|
||||
Sort int64 `gorm:"type:int;not null;default:0;comment:Sort"`
|
||||
DeductionRatio int64 `gorm:"type:int;default:0;comment:Deduction Ratio"`
|
||||
AllowDeduction *bool `gorm:"type:tinyint(1);default:1;comment:Allow deduction"`
|
||||
ResetCycle int64 `gorm:"type:int;default:0;comment:Reset Cycle: 0: No Reset, 1: 1st, 2: Monthly, 3: Yearly"`
|
||||
RenewalReset *bool `gorm:"type:tinyint(1);default:0;comment:Renew Reset"`
|
||||
ShowOriginalPrice bool `gorm:"type:tinyint(1);not null;default:1;comment:Show Original Price"`
|
||||
CreatedAt time.Time `gorm:"<-:create;comment:Create Time"`
|
||||
UpdatedAt time.Time `gorm:"comment:Update Time"`
|
||||
}
|
||||
|
||||
func (*Subscribe) TableName() string {
|
||||
|
||||
Reference in New Issue
Block a user