代码初始化
This commit is contained in:
137
dao/knapsack/knapsack_dao.go
Normal file
137
dao/knapsack/knapsack_dao.go
Normal file
@@ -0,0 +1,137 @@
|
||||
package dao
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"gitea.com/red-future/common/db/gfdb"
|
||||
"github.com/gogf/gf/v2/database/gdb"
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/util/gconv"
|
||||
|
||||
knapsackConsts "shop-user-trade/consts/knapsack"
|
||||
"shop-user-trade/consts/public"
|
||||
knapsackDto "shop-user-trade/model/dto/knapsack"
|
||||
knapsackEntity "shop-user-trade/model/entity/knapsack"
|
||||
)
|
||||
|
||||
var Knapsack = new(knapsackDao)
|
||||
|
||||
type knapsackDao struct{}
|
||||
|
||||
// Insert 插入背包项
|
||||
func (d *knapsackDao) Insert(ctx context.Context, req *knapsackDto.CreateKnapsackReq) (id int64, err error) {
|
||||
var entity *knapsackEntity.Knapsack
|
||||
if err = gconv.Struct(req, &entity); err != nil {
|
||||
return
|
||||
}
|
||||
entity.Status = knapsackConsts.KnapsackStatusActive
|
||||
r, err := gfdb.DB(ctx).Model(ctx, public.TableNameKnapsack).Data(entity).Insert()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
return r.LastInsertId()
|
||||
}
|
||||
|
||||
// Update 更新背包项
|
||||
func (d *knapsackDao) Update(ctx context.Context, req *knapsackDto.UpdateKnapsackReq) (rows int64, err error) {
|
||||
r, err := gfdb.DB(ctx).Model(ctx, public.TableNameKnapsack).
|
||||
Data(req).
|
||||
Where(knapsackEntity.KnapsackCol.Id, req.Id).
|
||||
OmitEmpty().
|
||||
Update()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
return r.RowsAffected()
|
||||
}
|
||||
|
||||
// Delete 删除背包项(软删除)
|
||||
func (d *knapsackDao) Delete(ctx context.Context, req *knapsackDto.DeleteKnapsackReq) (rows int64, err error) {
|
||||
r, err := gfdb.DB(ctx).Model(ctx, public.TableNameKnapsack).
|
||||
Where(knapsackEntity.KnapsackCol.Id, req.Id).
|
||||
Delete()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
return r.RowsAffected()
|
||||
}
|
||||
|
||||
// GetOne 获取单个背包项
|
||||
func (d *knapsackDao) GetOne(ctx context.Context, req *knapsackDto.GetKnapsackReq) (res *knapsackEntity.Knapsack, err error) {
|
||||
r, err := gfdb.DB(ctx).Model(ctx, public.TableNameKnapsack).
|
||||
Where(knapsackEntity.KnapsackCol.Id, req.Id).
|
||||
One()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
err = r.Struct(&res)
|
||||
return
|
||||
}
|
||||
|
||||
// GetByID 根据ID获取背包项
|
||||
func (d *knapsackDao) GetByID(ctx context.Context, id int64) (res *knapsackEntity.Knapsack, err error) {
|
||||
r, err := gfdb.DB(ctx).Model(ctx, public.TableNameKnapsack).
|
||||
Where(knapsackEntity.KnapsackCol.Id, id).
|
||||
One()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
err = r.Struct(&res)
|
||||
return
|
||||
}
|
||||
|
||||
// Count 获取背包数量
|
||||
func (d *knapsackDao) Count(ctx context.Context, req *knapsackDto.ListKnapsackReq) (count int, err error) {
|
||||
return d.buildListFilter(ctx, req).Count()
|
||||
}
|
||||
|
||||
// List 获取背包列表
|
||||
func (d *knapsackDao) List(ctx context.Context, req *knapsackDto.ListKnapsackReq) (res []knapsackEntity.Knapsack, total int, err error) {
|
||||
model := d.buildListFilter(ctx, req).OrderDesc(knapsackEntity.KnapsackCol.CreatedAt)
|
||||
if req.Page != nil {
|
||||
model.Page(int(req.Page.PageNum), int(req.Page.PageSize))
|
||||
}
|
||||
r, total, err := model.AllAndCount(false)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
err = r.Structs(&res)
|
||||
return
|
||||
}
|
||||
|
||||
// ListExpired 获取过期背包项列表
|
||||
func (d *knapsackDao) ListExpired(ctx context.Context) (res []knapsackEntity.Knapsack, err error) {
|
||||
now := time.Now().Unix()
|
||||
r, err := gfdb.DB(ctx).Model(ctx, public.TableNameKnapsack).
|
||||
WhereIn(knapsackEntity.KnapsackCol.Status, []knapsackConsts.KnapsackStatus{
|
||||
knapsackConsts.KnapsackStatusActive,
|
||||
knapsackConsts.KnapsackStatusListed,
|
||||
}).
|
||||
WhereLT(knapsackEntity.KnapsackCol.ExpireAt, now).
|
||||
WhereNotNull(knapsackEntity.KnapsackCol.ExpireAt).
|
||||
All()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
err = r.Structs(&res)
|
||||
return
|
||||
}
|
||||
|
||||
// buildListFilter 构建列表查询过滤条件
|
||||
func (d *knapsackDao) buildListFilter(ctx context.Context, req *knapsackDto.ListKnapsackReq) *gdb.Model {
|
||||
model := gfdb.DB(ctx).Model(ctx, public.TableNameKnapsack).OmitEmpty()
|
||||
if !g.IsEmpty(req.UserID) {
|
||||
model = model.Where(knapsackEntity.KnapsackCol.UserID, req.UserID)
|
||||
}
|
||||
if req.Status != nil {
|
||||
model = model.Where(knapsackEntity.KnapsackCol.Status, *req.Status)
|
||||
}
|
||||
if !g.IsEmpty(req.Type) {
|
||||
model = model.Where(knapsackEntity.KnapsackCol.Type, req.Type)
|
||||
}
|
||||
if !g.IsEmpty(req.Keyword) {
|
||||
model = model.WhereLike(knapsackEntity.KnapsackCol.AssetName, "%"+req.Keyword+"%")
|
||||
}
|
||||
return model
|
||||
}
|
||||
Reference in New Issue
Block a user