init: 1.0.0

This commit is contained in:
Chang lue Tsen
2025-04-25 12:08:29 +09:00
commit 8addcc584b
1031 changed files with 76472 additions and 0 deletions
+126
View File
@@ -0,0 +1,126 @@
package subscribe
import (
"context"
"errors"
"fmt"
"github.com/perfect-panel/ppanel-server/pkg/cache"
"github.com/redis/go-redis/v9"
"gorm.io/gorm"
)
var _ Model = (*customSubscribeModel)(nil)
var (
cacheSubscribeIdPrefix = "cache:subscribe:id:"
)
type (
Model interface {
subscribeModel
customSubscribeLogicModel
}
subscribeModel interface {
Insert(ctx context.Context, data *Subscribe, tx ...*gorm.DB) error
FindOne(ctx context.Context, id int64) (*Subscribe, error)
Update(ctx context.Context, data *Subscribe, tx ...*gorm.DB) error
Delete(ctx context.Context, id int64, tx ...*gorm.DB) error
Transaction(ctx context.Context, fn func(db *gorm.DB) error) error
}
customSubscribeModel struct {
*defaultSubscribeModel
}
defaultSubscribeModel struct {
cache.CachedConn
table string
}
)
func newSubscribeModel(db *gorm.DB, c *redis.Client) *defaultSubscribeModel {
return &defaultSubscribeModel{
CachedConn: cache.NewConn(db, c),
table: "`subscribe`",
}
}
//nolint:unused
func (m *defaultSubscribeModel) batchGetCacheKeys(Subscribes ...*Subscribe) []string {
var keys []string
for _, subscribe := range Subscribes {
keys = append(keys, m.getCacheKeys(subscribe)...)
}
return keys
}
func (m *defaultSubscribeModel) getCacheKeys(data *Subscribe) []string {
if data == nil {
return []string{}
}
SubscribeIdKey := fmt.Sprintf("%s%v", cacheSubscribeIdPrefix, data.Id)
cacheKeys := []string{
SubscribeIdKey,
}
return cacheKeys
}
func (m *defaultSubscribeModel) Insert(ctx context.Context, data *Subscribe, tx ...*gorm.DB) error {
err := m.ExecCtx(ctx, func(conn *gorm.DB) error {
if len(tx) > 0 {
conn = tx[0]
}
return conn.Create(&data).Error
}, m.getCacheKeys(data)...)
return err
}
func (m *defaultSubscribeModel) FindOne(ctx context.Context, id int64) (*Subscribe, error) {
SubscribeIdKey := fmt.Sprintf("%s%v", cacheSubscribeIdPrefix, id)
var resp Subscribe
err := m.QueryCtx(ctx, &resp, SubscribeIdKey, func(conn *gorm.DB, v interface{}) error {
return conn.Model(&Subscribe{}).Where("`id` = ?", id).First(&resp).Error
})
switch {
case err == nil:
return &resp, nil
default:
return nil, err
}
}
func (m *defaultSubscribeModel) Update(ctx context.Context, data *Subscribe, tx ...*gorm.DB) error {
old, err := m.FindOne(ctx, data.Id)
if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
return err
}
err = m.ExecCtx(ctx, func(conn *gorm.DB) error {
db := conn
if len(tx) > 0 {
db = tx[0]
}
return db.Save(data).Error
}, m.getCacheKeys(old)...)
return err
}
func (m *defaultSubscribeModel) Delete(ctx context.Context, id int64, tx ...*gorm.DB) error {
data, err := m.FindOne(ctx, id)
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil
}
return err
}
err = m.ExecCtx(ctx, func(conn *gorm.DB) error {
db := conn
if len(tx) > 0 {
db = tx[0]
}
return db.Delete(&Subscribe{}, id).Error
}, m.getCacheKeys(data)...)
return err
}
func (m *defaultSubscribeModel) Transaction(ctx context.Context, fn func(db *gorm.DB) error) error {
return m.TransactCtx(ctx, fn)
}
+109
View File
@@ -0,0 +1,109 @@
package subscribe
import (
"context"
"github.com/redis/go-redis/v9"
"gorm.io/gorm"
)
// type Details struct {
// Id int64 `gorm:"primaryKey"`
// Name string `gorm:"type:varchar(255);not null;default:'';comment:Subscribe Name"`
// 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:0;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"`
// GroupId int64 `gorm:"type:bigint;comment:Group Id"`
// Quota int64 `gorm:"type:int;not null;default:0;comment:Quota"`
// Show *bool `gorm:"type:tinyint(1);not null;default:0;comment:Show"`
// Sell *bool `gorm:"type:tinyint(1);not null;default:0;comment:Sell"`
// DeductionRatio int64 `gorm:"type:int;default:0;comment:Deduction Ratio"`
// PurchaseWithDiscount bool `gorm:"type:tinyint(1);default:0;comment:PurchaseWithDiscount"`
// ResetCycle int64 `gorm:"type:int;default:0;comment:Reset Cycle"`
// RenewalReset bool `gorm:"type:tinyint(1);default:0;comment:Renew Reset"`
// }
type customSubscribeLogicModel interface {
QuerySubscribeListByPage(ctx context.Context, page, size int, group int64, search string) (total int64, list []*Subscribe, err error)
QuerySubscribeList(ctx context.Context) ([]*Subscribe, error)
QuerySubscribeListByShow(ctx context.Context) ([]*Subscribe, error)
QuerySubscribeIdsByServerIdAndServerGroupId(ctx context.Context, serverId, serverGroupId int64) ([]*Subscribe, error)
QuerySubscribeMinSortByIds(ctx context.Context, ids []int64) (int64, error)
QuerySubscribeListByIds(ctx context.Context, ids []int64) ([]*Subscribe, error)
}
// NewModel returns a model for the database table.
func NewModel(conn *gorm.DB, c *redis.Client) Model {
return &customSubscribeModel{
defaultSubscribeModel: newSubscribeModel(conn, c),
}
}
// QuerySubscribeListByPage Get Subscribe List
func (m *customSubscribeModel) QuerySubscribeListByPage(ctx context.Context, page, size int, group int64, search string) (total int64, list []*Subscribe, err error) {
err = m.QueryNoCacheCtx(ctx, &list, func(conn *gorm.DB, v interface{}) error {
// About to be abandoned
_ = conn.Model(&Subscribe{}).
Where("sort = ?", 0).
Update("sort", gorm.Expr("id"))
conn = conn.Model(&Subscribe{})
if group > 0 {
conn = conn.Where("group_id = ?", group)
}
if search != "" {
conn = conn.Where("`name` like ? or `description` like ?", "%"+search+"%", "%"+search+"%")
}
return conn.Count(&total).Order("sort ASC").Limit(size).Offset((page - 1) * size).Find(v).Error
})
return total, list, err
}
// QuerySubscribeList Get Subscribe List
func (m *customSubscribeModel) QuerySubscribeList(ctx context.Context) ([]*Subscribe, error) {
var list []*Subscribe
err := m.QueryNoCacheCtx(ctx, &list, func(conn *gorm.DB, v interface{}) error {
conn = conn.Model(&Subscribe{})
return conn.Where("`sell` = true").Order("sort ").Find(v).Error
})
return list, err
}
func (m *customSubscribeModel) QuerySubscribeIdsByServerIdAndServerGroupId(ctx context.Context, serverId, serverGroupId int64) ([]*Subscribe, error) {
var data []*Subscribe
err := m.QueryNoCacheCtx(ctx, &data, func(conn *gorm.DB, v interface{}) error {
return conn.Model(&Subscribe{}).Where("FIND_IN_SET(?, server)", serverId).Or("FIND_IN_SET(?, server_group)", serverGroupId).Find(v).Error
})
return data, err
}
// QuerySubscribeListByShow Get Subscribe List By Show
func (m *customSubscribeModel) QuerySubscribeListByShow(ctx context.Context) ([]*Subscribe, error) {
var list []*Subscribe
err := m.QueryNoCacheCtx(ctx, &list, func(conn *gorm.DB, v interface{}) error {
conn = conn.Model(&Subscribe{})
return conn.Where("`show` = true").Find(v).Error
})
return list, err
}
func (m *customSubscribeModel) QuerySubscribeMinSortByIds(ctx context.Context, ids []int64) (int64, error) {
var minSort int64
err := m.QueryNoCacheCtx(ctx, &minSort, func(conn *gorm.DB, v interface{}) error {
return conn.Model(&Subscribe{}).Where("id IN ?", ids).Select("COALESCE(MIN(sort), 0)").Scan(v).Error
})
return minSort, err
}
func (m *customSubscribeModel) QuerySubscribeListByIds(ctx context.Context, ids []int64) ([]*Subscribe, error) {
var list []*Subscribe
err := m.QueryNoCacheCtx(ctx, &list, func(conn *gorm.DB, v interface{}) error {
return conn.Model(&Subscribe{}).Where("id IN ?", ids).Find(v).Error
})
return list, err
}
+66
View File
@@ -0,0 +1,66 @@
package subscribe
import (
"time"
"gorm.io/gorm"
)
type Subscribe struct {
Id int64 `gorm:"primaryKey"`
Name string `gorm:"type:varchar(255);not null;default:'';comment:Subscribe Name"`
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:0;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"`
GroupId int64 `gorm:"type:bigint;comment:Group Id"`
ServerGroup string `gorm:"type:varchar(255);comment:Server Group"`
Server string `gorm:"type:varchar(255);comment:Server"`
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"`
CreatedAt time.Time `gorm:"<-:create;comment:Create Time"`
UpdatedAt time.Time `gorm:"comment:Update Time"`
}
func (*Subscribe) TableName() string {
return "subscribe"
}
func (s *Subscribe) BeforeCreate(tx *gorm.DB) error {
if s.Sort == 0 {
var maxSort int64
if err := tx.Model(&Subscribe{}).Select("COALESCE(MAX(sort), 0)").Scan(&maxSort).Error; err != nil {
return err
}
s.Sort = maxSort + 1
}
return nil
}
type Discount struct {
Months int64 `json:"months"`
Discount int64 `json:"discount"`
}
type Group struct {
Id int64 `gorm:"primaryKey"`
Name string `gorm:"type:varchar(255);not null;default:'';comment:Group Name"`
Description string `gorm:"type:text;comment:Group Description"`
CreatedAt time.Time `gorm:"<-:create;comment:Create Time"`
UpdatedAt time.Time `gorm:"comment:Update Time"`
}
func (Group) TableName() string {
return "subscribe_group"
}