refactor: 重构资产模型与DAO层实现
This commit is contained in:
@@ -8,10 +8,8 @@ import (
|
||||
|
||||
"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"
|
||||
"github.com/gogf/gf/v2/util/gconv"
|
||||
)
|
||||
|
||||
var Category = new(category)
|
||||
@@ -20,63 +18,65 @@ type category struct {
|
||||
}
|
||||
|
||||
// Insert 插入分类
|
||||
func (d *category) Insert(ctx context.Context, req *dto.CreateCategoryReq) (res *entity.Category, err error) {
|
||||
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
|
||||
}
|
||||
res.Bid = guid.S()
|
||||
_, err = gfdb.DB(ctx).Model(ctx, public.CategoryCollection).Insert(&res)
|
||||
return res, nil
|
||||
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) (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)
|
||||
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
|
||||
}
|
||||
if !g.IsEmpty(req.Id) {
|
||||
model.Where("id", req.Id)
|
||||
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
|
||||
}
|
||||
_, err = model.Update()
|
||||
return
|
||||
return r.RowsAffected()
|
||||
}
|
||||
|
||||
// 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)
|
||||
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
|
||||
}
|
||||
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()
|
||||
err = r.Struct(&res)
|
||||
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
|
||||
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)
|
||||
err = gconv.Structs(r.List(), &res)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
err = r.Structs(&res)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -88,8 +88,6 @@ func (d *category) buildListFilter(ctx context.Context, req *dto.ListCategoryReq
|
||||
}
|
||||
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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user