Files
36Wisdom/biz/dao/dao_user_collection.go
2026-08-14 16:11:10 +08:00

50 lines
1.5 KiB
Go

package dao
import (
"context"
"github.com/gogf/gf/v2/database/gdb"
"github.com/gogf/gf/v2/frame/g"
"36wisdom/biz/consts"
"36wisdom/biz/model/entity"
"36wisdom/common"
)
type userCollectionDao struct{ common.BaseDao }
var UserCollection = &userCollectionDao{BaseDao: common.BaseDao{Table: consts.TableUserCollection}}
func (d *userCollectionDao) Init(ctx context.Context) error {
_, err := g.DB().Exec(ctx, `
CREATE TABLE IF NOT EXISTS user_collection (
id INTEGER PRIMARY KEY AUTOINCREMENT,
user_id INTEGER NOT NULL,
strategy_id INTEGER NOT NULL,
unlocked_at DATETIME,
UNIQUE(user_id, strategy_id)
);`)
return err
}
// ExistsInTx 事务内判断孩子是否已收集该计策。
func (d *userCollectionDao) ExistsInTx(ctx context.Context, tx gdb.TX, userId, strategyId int64) (bool, error) {
n, err := tx.Model(consts.TableUserCollection).Ctx(ctx).
Where("user_id", userId).Where("strategy_id", strategyId).Count()
return n > 0, err
}
// InsertInTx 事务内写入计策卡收集。
func (d *userCollectionDao) InsertInTx(ctx context.Context, tx gdb.TX, userId, strategyId int64) error {
_, err := tx.Model(consts.TableUserCollection).Ctx(ctx).Data(g.Map{
"user_id": userId,
"strategy_id": strategyId,
}).Insert()
return err
}
// ListByUser 孩子已收集计策列表(不缓存)。
func (d *userCollectionDao) ListByUser(ctx context.Context, userId int64) ([]*entity.UserCollection, error) {
return common.GetList[entity.UserCollection](d.Model().Ctx(ctx).Where("user_id", userId))
}