Files
EdgeAPI/internal/db/models/user_plan_dao.go

200 lines
4.8 KiB
Go
Raw Permalink Normal View History

2022-10-14 10:03:29 +08:00
//go:build !plus
2021-10-29 14:02:40 +08:00
package models
import (
"github.com/TeaOSLab/EdgeAPI/internal/errors"
2021-11-11 14:16:42 +08:00
"github.com/TeaOSLab/EdgeAPI/internal/utils"
2021-10-29 14:02:40 +08:00
_ "github.com/go-sql-driver/mysql"
"github.com/iwind/TeaGo/Tea"
"github.com/iwind/TeaGo/dbs"
2021-11-10 14:54:27 +08:00
"github.com/iwind/TeaGo/types"
2021-10-29 14:02:40 +08:00
)
const (
UserPlanStateEnabled = 1 // 已启用
UserPlanStateDisabled = 0 // 已禁用
2021-11-09 15:36:25 +08:00
DefaultUserPlanMaxDay = "3000-01-01"
2021-10-29 14:02:40 +08:00
)
type UserPlanDAO dbs.DAO
func NewUserPlanDAO() *UserPlanDAO {
return dbs.NewDAO(&UserPlanDAO{
DAOObject: dbs.DAOObject{
DB: Tea.Env,
Table: "edgeUserPlans",
Model: new(UserPlan),
PkName: "id",
},
}).(*UserPlanDAO)
}
var SharedUserPlanDAO *UserPlanDAO
func init() {
dbs.OnReady(func() {
SharedUserPlanDAO = NewUserPlanDAO()
})
}
// EnableUserPlan 启用条目
func (this *UserPlanDAO) EnableUserPlan(tx *dbs.Tx, id uint64) error {
_, err := this.Query(tx).
Pk(id).
Set("state", UserPlanStateEnabled).
Update()
return err
}
// DisableUserPlan 禁用条目
func (this *UserPlanDAO) DisableUserPlan(tx *dbs.Tx, id int64) error {
_, err := this.Query(tx).
Pk(id).
Set("state", UserPlanStateDisabled).
Update()
2021-11-09 15:36:25 +08:00
if err != nil {
return err
}
return this.NotifyUpdate(tx, id)
2021-10-29 14:02:40 +08:00
}
// FindEnabledUserPlan 查找启用中的条目
2021-11-11 14:16:42 +08:00
func (this *UserPlanDAO) FindEnabledUserPlan(tx *dbs.Tx, userPlanId int64, cacheMap *utils.CacheMap) (*UserPlan, error) {
2021-11-10 14:54:27 +08:00
var cacheKey = this.Table + ":FindEnabledUserPlan:" + types.String(userPlanId)
if cacheMap != nil {
2021-11-11 14:16:42 +08:00
cache, ok := cacheMap.Get(cacheKey)
2021-11-10 14:54:27 +08:00
if ok {
return cache.(*UserPlan), nil
}
}
2021-10-29 14:02:40 +08:00
result, err := this.Query(tx).
2021-11-09 15:36:25 +08:00
Pk(userPlanId).
2021-10-29 14:02:40 +08:00
Attr("state", UserPlanStateEnabled).
Find()
if result == nil {
return nil, err
}
2021-11-10 14:54:27 +08:00
if cacheMap != nil {
2021-11-11 14:16:42 +08:00
cacheMap.Put(cacheKey, result)
2021-11-10 14:54:27 +08:00
}
2021-10-29 14:02:40 +08:00
return result.(*UserPlan), err
}
2022-01-23 19:16:52 +08:00
// FindUserPlanWithoutState 查找套餐,并不检查状态
// 防止因为删除套餐而导致计费失败
func (this *UserPlanDAO) FindUserPlanWithoutState(tx *dbs.Tx, userPlanId int64, cacheMap *utils.CacheMap) (*UserPlan, error) {
var cacheKey = this.Table + ":FindUserPlanWithoutState:" + types.String(userPlanId)
if cacheMap != nil {
cache, ok := cacheMap.Get(cacheKey)
if ok {
return cache.(*UserPlan), nil
}
}
result, err := this.Query(tx).
Pk(userPlanId).
Find()
if result == nil {
return nil, err
}
if cacheMap != nil {
cacheMap.Put(cacheKey, result)
}
return result.(*UserPlan), err
}
2021-10-29 14:02:40 +08:00
// CountAllEnabledUserPlans 计算套餐数量
2021-11-28 14:28:00 +08:00
func (this *UserPlanDAO) CountAllEnabledUserPlans(tx *dbs.Tx, userId int64, isAvailable bool, isExpired bool, expiringDays int32) (int64, error) {
2022-10-14 10:03:29 +08:00
return 0, nil
2021-10-29 14:02:40 +08:00
}
// ListEnabledUserPlans 列出单页套餐
2021-11-09 15:36:25 +08:00
func (this *UserPlanDAO) ListEnabledUserPlans(tx *dbs.Tx, userId int64, isAvailable bool, isExpired bool, expiringDays int32, offset int64, size int64) (result []*UserPlan, err error) {
2021-10-29 14:02:40 +08:00
return
}
// CreateUserPlan 创建套餐
2021-11-28 20:11:36 +08:00
func (this *UserPlanDAO) CreateUserPlan(tx *dbs.Tx, userId int64, planId int64, name string, dayTo string) (int64, error) {
2021-10-29 14:02:40 +08:00
var op = NewUserPlanOperator()
op.UserId = userId
op.PlanId = planId
2021-11-28 20:11:36 +08:00
op.Name = name
2021-10-29 14:02:40 +08:00
op.DayTo = dayTo
op.IsOn = true
op.State = UserStateEnabled
return this.SaveInt64(tx, op)
}
// UpdateUserPlan 修改套餐
2021-11-28 20:11:36 +08:00
func (this *UserPlanDAO) UpdateUserPlan(tx *dbs.Tx, userPlanId int64, planId int64, name string, dayTo string, isOn bool) error {
2021-10-29 14:02:40 +08:00
if userPlanId <= 0 {
return errors.New("invalid userPlanId")
}
var op = NewUserPlanOperator()
op.Id = userPlanId
op.PlanId = planId
2021-11-28 20:11:36 +08:00
op.Name = name
2021-10-29 14:02:40 +08:00
op.DayTo = dayTo
op.IsOn = isOn
2021-11-09 15:36:25 +08:00
err := this.Save(tx, op)
if err != nil {
return err
}
return this.NotifyUpdate(tx, userPlanId)
2021-10-29 14:02:40 +08:00
}
// UpdateUserPlanDayTo 修改套餐日期
func (this *UserPlanDAO) UpdateUserPlanDayTo(tx *dbs.Tx, userPlanId int64, dayTo string) error {
if userPlanId <= 0 {
return errors.New("invalid userPlanId")
}
var op = NewUserPlanOperator()
op.Id = userPlanId
op.DayTo = dayTo
2021-11-09 15:36:25 +08:00
err := this.Save(tx, op)
if err != nil {
return err
}
return this.NotifyUpdate(tx, userPlanId)
}
// FindAllEnabledPlansForServer 列出服务可用的套餐
func (this *UserPlanDAO) FindAllEnabledPlansForServer(tx *dbs.Tx, userId int64, serverId int64) (result []*UserPlan, err error) {
return
}
2021-11-28 14:28:00 +08:00
// CheckUserPlan 检查用户套餐
func (this *UserPlanDAO) CheckUserPlan(tx *dbs.Tx, userId int64, userPlanId int64) error {
exists, err := this.Query(tx).
Pk(userPlanId).
Attr("userId", userId).
State(UserPlanStateEnabled).
Exist()
if err != nil {
return err
}
if !exists {
return ErrNotFound
}
return nil
}
2021-11-09 15:36:25 +08:00
// NotifyUpdate 通知更新
func (this *UserPlanDAO) NotifyUpdate(tx *dbs.Tx, userPlanId int64) error {
serverId, err := SharedServerDAO.FindEnabledServerIdWithUserPlanId(tx, userPlanId)
if err != nil {
return err
}
if serverId > 0 {
return SharedServerDAO.NotifyUpdate(tx, serverId)
}
return nil
}