71 lines
2.1 KiB
Go
71 lines
2.1 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 redemptionDao struct{ common.BaseDao }
|
|
|
|
var Redemption = &redemptionDao{BaseDao: common.BaseDao{Table: consts.TableRedemption}}
|
|
|
|
func (d *redemptionDao) Init(ctx context.Context) error {
|
|
_, err := g.DB().Exec(ctx, `
|
|
CREATE TABLE IF NOT EXISTS redemption (
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
user_id INTEGER NOT NULL,
|
|
prize_id INTEGER NOT NULL,
|
|
points_cost INTEGER NOT NULL,
|
|
status INTEGER NOT NULL DEFAULT 1,
|
|
code TEXT,
|
|
created_at DATETIME,
|
|
updated_at DATETIME
|
|
);
|
|
CREATE INDEX IF NOT EXISTS idx_redemption_user ON redemption(user_id, created_at);`)
|
|
return err
|
|
}
|
|
|
|
// GetByPk 主键查询(无缓存);不存在返回 nil, nil。
|
|
func (d *redemptionDao) GetByPk(ctx context.Context, id int64) (*entity.Redemption, error) {
|
|
return common.GetOne[entity.Redemption](d.Model().Ctx(ctx).WherePri(id))
|
|
}
|
|
|
|
// GetByPkInTx 事务内主键查询;不存在返回 nil, nil。
|
|
func (d *redemptionDao) GetByPkInTx(ctx context.Context, tx gdb.TX, id int64) (*entity.Redemption, error) {
|
|
rec, err := tx.Model(consts.TableRedemption).Ctx(ctx).WherePri(id).One()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if rec.IsEmpty() {
|
|
return nil, nil
|
|
}
|
|
dst := &entity.Redemption{}
|
|
if err = rec.Struct(dst); err != nil {
|
|
return nil, err
|
|
}
|
|
return dst, nil
|
|
}
|
|
|
|
// ListByUser 孩子兑换记录(按创建时间倒序)。
|
|
func (d *redemptionDao) ListByUser(ctx context.Context, userId int64) ([]*entity.Redemption, error) {
|
|
return common.GetList[entity.Redemption](d.Model().Ctx(ctx).Where("user_id", userId).Order("id DESC"))
|
|
}
|
|
|
|
// List 兑换记录(status/prizeId 可选过滤),按创建时间倒序。
|
|
func (d *redemptionDao) List(ctx context.Context, status int, prizeId int64) ([]*entity.Redemption, error) {
|
|
m := d.Model().Ctx(ctx)
|
|
if status > 0 {
|
|
m = m.Where("status", status)
|
|
}
|
|
if prizeId > 0 {
|
|
m = m.Where("prize_id", prizeId)
|
|
}
|
|
return common.GetList[entity.Redemption](m.Order("id DESC"))
|
|
}
|