package bus import ( "assetcenter/conf" "assetcenter/db" "assetcenter/db/model" "assetcenter/db/repo" "context" "fmt" "math/rand" "time" "github.com/nats-io/nats.go" "go.mongodb.org/mongo-driver/bson" "go.mongodb.org/mongo-driver/bson/primitive" "infish.cn/comm" ) func init() { // 保证每次生成的随机数不一样 rand.Seed(time.Now().UnixNano()) } func addTreeDefineMatgroup() *comm.NatsMsgReplyer { return &comm.NatsMsgReplyer{ Subject: "tree.define.add.matgroup", Entity: func() interface{} { return &TreeAddDefReq{} }, Cb2: func(msg *nats.Msg, entity interface{}) (interface{}, error) { req, ok := entity.(*TreeAddDefReq) if !ok || len(req.Name) < 1 { return nil, fmt.Errorf("参数错误") } var createRandomColl = func() string { return fmt.Sprintf("matgrp-%s-%s", time.Now().Format("20060102"), RandName(6)) } collName := createRandomColl() if len(req.DbName) < 1 { return nil, fmt.Errorf("数据库名字不能为空") } body := &comm.DbAsset{ Collection: collName, Label: req.Name, Type: model.AssetTypeMaterialGroup, CategoryIds: []string{}, CreateTime: time.Now(), UserId: req.UserId, } body.Id = primitive.NewObjectID().Hex() database := &comm.Database{} var CreateRepoCtx = func() *repo.RepoSession { return &repo.RepoSession{ Ctx: context.Background(), Client: db.MongoClient, } } ok, err := repo.RepoSeachDoc(CreateRepoCtx(), &repo.DocSearchOptions{CollectName: repo.CollectionDatabase, Query: repo.Map{"name": req.DbName}, Project: []string{"_id"}}, database) if err != nil { return nil, fmt.Errorf("数据库不存在") } if !ok { return nil, err } ok, _ = repo.RepoSeachDoc(CreateRepoCtx(), &repo.DocSearchOptions{CollectName: repo.CollectionDatabase, Query: repo.Map{"assets.collection": body.Collection, "_id": database.Id}, Project: []string{"_id"}}, database) if ok { return nil, fmt.Errorf("材料库编号重复") } ret, err := repo.RepoDocArrayAppend( CreateRepoCtx(), repo.CollectionDatabase, database.Id.Hex(), "assets", body, ) if err != nil { return nil, err } if ret.MatchedCount < 1 { return nil, fmt.Errorf("添加失败") } return &TreeAddDefineResp{ DbId: database.Id.Hex(), DefineId: body.Id, Collection: body.Collection, Host: conf.AppConfig.TreeDNS, }, nil }, } } func removeTreeDefineMatgroup() *comm.NatsMsgReplyer { return &comm.NatsMsgReplyer{ Subject: "tree.define.remove.matgroup", Entity: func() interface{} { return &TreeRemoveDefReq{} }, Cb2: func(msg *nats.Msg, entity interface{}) (interface{}, error) { req, ok := entity.(*TreeRemoveDefReq) if !ok || len(req.DbId) < 1 || len(req.DefineId) < 1 { return nil, fmt.Errorf("参数错误") } var CreateRepoCtx = func() *repo.RepoSession { return &repo.RepoSession{ Ctx: context.Background(), Client: db.MongoClient, } } filter := repo.Map{"assets.id": req.DefineId} enity := &model.DatabaseAssetV0{} err := repo.RepoDocArraySearch(CreateRepoCtx(), &repo.ArrayOneSearchOption{ CollectName: repo.CollectionDatabase, ArrayQuery: filter, Id: req.DbId, Field: "assets", }, enity) if err != nil { return nil, err } if enity.Assets == nil { return true, nil } //删除数据表 coll := enity.Assets.Collection fmt.Println("todo fixed me need delete collection ", coll) ret, err := repo.RepoDocArrayOneRemove( CreateRepoCtx(), &repo.ArrayOneRemoveOption{ CollectName: repo.CollectionDatabase, Id: req.DbId, ArrayQuery: repo.Map{"assets": bson.M{"id": req.DefineId}}, }, ) if err != nil { return nil, err } if ret.MatchedCount < 1 { return nil, fmt.Errorf("删除失败") } return nil, nil }, } }