94 lines
2.7 KiB
Go
94 lines
2.7 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/frame/g"
|
|
"github.com/gogf/gf/v2/util/gconv"
|
|
)
|
|
|
|
var Category = new(category)
|
|
|
|
type category struct {
|
|
}
|
|
|
|
// Insert 插入分类
|
|
func (d *category) Insert(ctx context.Context, req *dto.CreateCategoryReq) (id int64, err error) {
|
|
var res *entity.Category
|
|
if err = gconv.Struct(req, &res); err != nil {
|
|
return
|
|
}
|
|
r, err := gfdb.DB(ctx).Model(ctx, public.CategoryCollection).Insert(&res)
|
|
if err != nil {
|
|
return
|
|
}
|
|
return r.LastInsertId()
|
|
}
|
|
|
|
// Update 更新分类
|
|
func (d *category) Update(ctx context.Context, req *dto.UpdateCategoryReq) (rows int64, err error) {
|
|
r, err := gfdb.DB(ctx).Model(ctx, public.CategoryCollection).Data(&req).OmitEmpty().Where(entity.CategoryCol.Id, req.Id).Update()
|
|
if err != nil {
|
|
return
|
|
}
|
|
return r.RowsAffected()
|
|
}
|
|
|
|
// Delete 删除分类-根据id及父id进行假删
|
|
func (d *category) Delete(ctx context.Context, req *dto.DeleteCategoryReq) (rows int64, err error) {
|
|
r, err := gfdb.DB(ctx).Model(ctx, public.CategoryCollection).Where(entity.CategoryCol.Id, req.Id).Delete()
|
|
if err != nil {
|
|
return
|
|
}
|
|
return r.RowsAffected()
|
|
}
|
|
|
|
// GetOne 获取单个分类
|
|
func (d *category) GetOne(ctx context.Context, req *dto.GetCategoryReq, fields ...string) (res *entity.Category, err error) {
|
|
r, err := gfdb.DB(ctx).Model(ctx, public.CategoryCollection).Where(entity.CategoryCol.Id, req.Id).Fields(fields).One()
|
|
if err != nil {
|
|
return
|
|
}
|
|
err = r.Struct(&res)
|
|
return
|
|
}
|
|
|
|
// Count 根据条件统计分类数量
|
|
func (d *category) Count(ctx context.Context, req *dto.ListCategoryReq) (count int, err error) {
|
|
return d.buildListFilter(ctx, req).Count()
|
|
}
|
|
|
|
// 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)
|
|
model.OrderAsc(entity.CategoryCol.Sort)
|
|
model.OrderDesc(entity.CategoryCol.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
|
|
}
|
|
|
|
// 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.OmitEmptyWhere()
|
|
return model
|
|
}
|