117 lines
3.6 KiB
Go
117 lines
3.6 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/mongo"
|
|
"github.com/gogf/gf/v2/frame/g"
|
|
"github.com/gogf/gf/v2/util/gconv"
|
|
"go.mongodb.org/mongo-driver/v2/bson"
|
|
)
|
|
|
|
var PrivateCategory = new(privateCategory)
|
|
|
|
type privateCategory struct{}
|
|
|
|
// Insert 插入私域分类
|
|
func (d *privateCategory) Insert(ctx context.Context, req *dto.CreatePrivateCategoryReq) (ids []interface{}, err error) {
|
|
var result *entity.PrivateCategory
|
|
if err = gconv.Struct(req, &result); err != nil {
|
|
return
|
|
}
|
|
ids, err = mongo.DB().Insert(ctx, []interface{}{&result}, public.PrivateCategoryCollection)
|
|
return
|
|
}
|
|
|
|
// BatchInsert 批量插入私域分类
|
|
func (d *privateCategory) BatchInsert(ctx context.Context, req *dto.BatchCreatePrivateCategoryReq) (ids []interface{}, err error) {
|
|
items := make([]*entity.PrivateCategory, 0, len(req.Categories))
|
|
for _, item := range req.Categories {
|
|
var result *entity.PrivateCategory
|
|
if err = gconv.Struct(item, &result); err != nil {
|
|
return
|
|
}
|
|
items = append(items, result)
|
|
}
|
|
interfaces := make([]interface{}, len(items))
|
|
for i, item := range items {
|
|
interfaces[i] = item
|
|
}
|
|
ids, err = mongo.DB().Insert(ctx, interfaces, public.PrivateCategoryCollection)
|
|
return
|
|
}
|
|
|
|
// GetOne 获取单个私域分类
|
|
func (d *privateCategory) GetOne(ctx context.Context, id *bson.ObjectID) (category *entity.PrivateCategory, err error) {
|
|
filter := bson.M{"_id": id}
|
|
err = mongo.DB().FindOne(ctx, filter, &category, public.PrivateCategoryCollection)
|
|
return
|
|
}
|
|
|
|
// Update 更新私域分类
|
|
func (d *privateCategory) Update(ctx context.Context, req *dto.UpdatePrivateCategoryReq) (err error) {
|
|
buildUpdateData, err := mongo.BuildUpdateData(ctx, req)
|
|
if err != nil {
|
|
return
|
|
}
|
|
filter := bson.M{"_id": req.ID}
|
|
update := bson.M{"$set": buildUpdateData}
|
|
_, err = mongo.DB().Update(ctx, filter, update, public.PrivateCategoryCollection)
|
|
return
|
|
}
|
|
|
|
// DeleteFake 删除私域分类-根据id进行假删
|
|
func (d *privateCategory) DeleteFake(ctx context.Context, id *bson.ObjectID) (err error) {
|
|
filter := bson.M{"_id": id}
|
|
_, err = mongo.DB().DeleteSoft(ctx, filter, public.PrivateCategoryCollection)
|
|
return
|
|
}
|
|
|
|
// List 获取私域分类列表
|
|
func (d *privateCategory) List(ctx context.Context, req *dto.ListPrivateCategoryReq) (res []*entity.PrivateCategory, total int64, err error) {
|
|
filter, err := d.buildListFilter(ctx, req)
|
|
if err != nil {
|
|
return
|
|
}
|
|
|
|
total, err = mongo.DB().Find(ctx, filter, &res, public.PrivateCategoryCollection, nil, nil)
|
|
return
|
|
}
|
|
|
|
// GetTree 获取私域分类树
|
|
func (d *privateCategory) GetTree(ctx context.Context) (res []*entity.PrivateCategory, err error) {
|
|
filter := bson.M{}
|
|
_, err = mongo.DB().Find(ctx, filter, &res, public.PrivateCategoryCollection, nil, nil)
|
|
return
|
|
}
|
|
|
|
// GetByParentId 根据父ID获取子分类列表
|
|
func (d *privateCategory) GetByParentId(ctx context.Context, parentId string) (res []*entity.PrivateCategory, err error) {
|
|
filter := bson.M{"parentId": parentId}
|
|
_, err = mongo.DB().Find(ctx, filter, &res, public.PrivateCategoryCollection, nil, nil)
|
|
return
|
|
}
|
|
|
|
// buildListFilter 构建列表查询的过滤条件
|
|
func (d *privateCategory) buildListFilter(ctx context.Context, req *dto.ListPrivateCategoryReq) (filter bson.M, err error) {
|
|
_ = ctx
|
|
filter = bson.M{}
|
|
|
|
if !g.IsEmpty(req.Name) {
|
|
filter["name"] = bson.M{"$regex": req.Name, "$options": "i"}
|
|
}
|
|
if !g.IsEmpty(req.ParentID) {
|
|
filter["parentId"] = req.ParentID
|
|
}
|
|
if !g.IsEmpty(req.Level) {
|
|
filter["level"] = req.Level
|
|
}
|
|
if req.IsLeafNode != nil {
|
|
filter["isLeafNode"] = *req.IsLeafNode
|
|
}
|
|
return
|
|
}
|