feat: 会员套餐 DAO(建表 + 月卡/年卡 seed)
This commit is contained in:
@@ -0,0 +1,68 @@
|
||||
package dao
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"slogan-agent/styleagent/consts"
|
||||
"slogan-agent/styleagent/model/entity"
|
||||
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
)
|
||||
|
||||
var MemberPlan = &memberPlanDao{}
|
||||
|
||||
type memberPlanDao struct{}
|
||||
|
||||
func init() {
|
||||
ctx := context.Background()
|
||||
_, err := g.DB().Exec(ctx, `CREATE TABLE IF NOT EXISTS `+consts.TableNameMemberPlan+` (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
name TEXT NOT NULL DEFAULT '',
|
||||
price_fen INTEGER NOT NULL DEFAULT 0,
|
||||
duration_days INTEGER NOT NULL DEFAULT 30,
|
||||
features TEXT NOT NULL DEFAULT '[]',
|
||||
sort INTEGER NOT NULL DEFAULT 0,
|
||||
status INTEGER NOT NULL DEFAULT 1,
|
||||
created_at DATETIME DEFAULT (datetime('now','localtime'))
|
||||
)`)
|
||||
if err != nil {
|
||||
g.Log().Warningf(ctx, "create member_plan table failed: %v", err)
|
||||
}
|
||||
seedMemberPlans(ctx)
|
||||
}
|
||||
|
||||
func seedMemberPlans(ctx context.Context) {
|
||||
r, err := g.DB().Model(consts.TableNameMemberPlan).Ctx(ctx).Count()
|
||||
if err != nil || r > 0 {
|
||||
return
|
||||
}
|
||||
plans := []struct {
|
||||
name string
|
||||
price int
|
||||
days int
|
||||
features string
|
||||
sort int
|
||||
}{
|
||||
{"月卡 ¥29.9", 2990, 30, `["effect_unlimited","cps_commission_x15"]`, 1},
|
||||
{"年卡 ¥199", 19900, 365, `["effect_unlimited","ai_priority","cps_commission_x15","store_discount"]`, 2},
|
||||
}
|
||||
for _, p := range plans {
|
||||
_, _ = g.DB().Exec(ctx,
|
||||
"INSERT INTO "+consts.TableNameMemberPlan+" (name, price_fen, duration_days, features, sort, status, created_at) VALUES (?, ?, ?, ?, ?, 1, datetime('now','localtime'))",
|
||||
p.name, p.price, p.days, p.features, p.sort)
|
||||
}
|
||||
}
|
||||
|
||||
func (d *memberPlanDao) ListEnabled(ctx context.Context) ([]*entity.MemberPlan, error) {
|
||||
var list []*entity.MemberPlan
|
||||
err := g.DB().Model(consts.TableNameMemberPlan).Ctx(ctx).
|
||||
Where("status", 1).OrderAsc("sort").OrderAsc("id").Scan(&list)
|
||||
return list, err
|
||||
}
|
||||
|
||||
func (d *memberPlanDao) GetOne(ctx context.Context, id int64) (*entity.MemberPlan, error) {
|
||||
var p *entity.MemberPlan
|
||||
err := g.DB().Model(consts.TableNameMemberPlan).Ctx(ctx).
|
||||
Where("id", id).Where("status", 1).Scan(&p)
|
||||
return p, err
|
||||
}
|
||||
Reference in New Issue
Block a user