96 lines
2.8 KiB
Go
96 lines
2.8 KiB
Go
package dao
|
|
|
|
import (
|
|
"assets/consts/public"
|
|
dto "assets/model/dto/asset"
|
|
entity "assets/model/entity/asset"
|
|
"context"
|
|
|
|
"gitea.com/red-future/common/db/gfdb"
|
|
"github.com/gogf/gf/v2/database/gdb"
|
|
"github.com/gogf/gf/v2/util/gconv"
|
|
"github.com/gogf/gf/v2/util/guid"
|
|
|
|
"github.com/gogf/gf/v2/frame/g"
|
|
)
|
|
|
|
var Category = new(category)
|
|
|
|
type category struct {
|
|
}
|
|
|
|
// Insert 插入分类
|
|
func (d *category) Insert(ctx context.Context, req *dto.CreateCategoryReq) (res *entity.Category, err error) {
|
|
if err = gconv.Struct(req, &res); err != nil {
|
|
return
|
|
}
|
|
res.Bid = guid.S()
|
|
_, err = gfdb.DB(ctx).Model(ctx, public.CategoryCollection).Insert(&res)
|
|
return res, nil
|
|
}
|
|
|
|
// Update 更新分类
|
|
func (d *category) Update(ctx context.Context, req *dto.UpdateCategoryReq) (err error) {
|
|
model := gfdb.DB(ctx).Model(ctx, public.CategoryCollection).Data(gconv.Map(&req)).OmitEmpty()
|
|
if !g.IsEmpty(req.Bid) {
|
|
model.Where("bid", req.Bid)
|
|
}
|
|
if !g.IsEmpty(req.Id) {
|
|
model.Where("id", req.Id)
|
|
}
|
|
_, err = model.Update()
|
|
return
|
|
}
|
|
|
|
// GetOne 获取单个分类
|
|
func (d *category) GetOne(ctx context.Context, req *dto.GetCategoryReq, fields ...string) (category *entity.Category, err error) {
|
|
model := gfdb.DB(ctx).Model(ctx, public.CategoryCollection)
|
|
if !g.IsEmpty(req.Bid) {
|
|
model.Where(entity.CategoryCol.Bid, req.Bid)
|
|
}
|
|
if !g.IsEmpty(req.Id) {
|
|
model.Where(entity.CategoryCol.Id, req.Id)
|
|
}
|
|
res, err := model.Fields(fields).One()
|
|
err = res.Struct(&category)
|
|
return
|
|
}
|
|
|
|
// DeleteFake 删除分类-根据id及父id进行假删
|
|
func (d *category) DeleteFake(ctx context.Context, req *dto.DeleteCategoryReq) (err error) {
|
|
model := gfdb.DB(ctx).Model(ctx, public.CategoryCollection)
|
|
model.Where(entity.CategoryCol.Bid, req.Bid)
|
|
_, err = model.Delete()
|
|
return
|
|
}
|
|
|
|
// Count 根据条件统计分类数量
|
|
func (d *category) Count(ctx context.Context, req *dto.ListCategoryReq) (count int64, err error) {
|
|
m := d.buildListFilter(ctx, req)
|
|
c, err := m.Count()
|
|
return int64(c), err
|
|
}
|
|
|
|
// List 获取分类列表
|
|
func (d *category) List(ctx context.Context, req *dto.ListCategoryReq, fields ...string) (res []entity.Category, total int, err error) {
|
|
model := d.buildListFilter(ctx, req)
|
|
model.Fields(fields)
|
|
r, total, err := model.AllAndCount(false)
|
|
err = gconv.Structs(r.List(), &res)
|
|
return
|
|
}
|
|
|
|
// buildListFilter 构建列表查询的过滤条件
|
|
func (d *category) buildListFilter(ctx context.Context, req *dto.ListCategoryReq) *gdb.Model {
|
|
model := gfdb.DB(ctx).Model(ctx, public.CategoryCollection).Model
|
|
if !g.IsEmpty(req.Keyword) {
|
|
model.WhereLike(entity.CategoryCol.Name, "%"+req.Keyword+"%")
|
|
}
|
|
model.Where(entity.CategoryCol.ParentId, req.ParentId)
|
|
model.Where(entity.CategoryCol.Status, req.Status)
|
|
model.OrderAsc(entity.CategoryCol.Sort)
|
|
model.OrderDesc(entity.CategoryCol.CreatedAt)
|
|
model.OmitEmptyWhere()
|
|
return model
|
|
}
|