package bus import ( "assetcenter/db" "assetcenter/db/repo" "assetcenter/tree" "strings" "context" "fmt" "time" "github.com/nats-io/nats.go" "go.mongodb.org/mongo-driver/bson" "go.mongodb.org/mongo-driver/bson/primitive" "infish.cn/comm" ) type TreeListReq struct { DbName string Collection string Page int Size int Project []string Query map[string]interface{} } func RegTreeAssetList() *comm.NatsMsgReplyer { return &comm.NatsMsgReplyer{ Subject: "tree.asset.list", Entity: func() interface{} { return &TreeListReq{} }, Cb2: func(msg *nats.Msg, entity interface{}) (interface{}, error) { req, ok := entity.(*TreeListReq) if !ok { return nil, fmt.Errorf("参数错误不是TreeListReq类型") } if len(req.DbName) < 1 || len(req.Collection) < 1 { return nil, fmt.Errorf("DbName Collection AssetId 不能为空") } if req.Page == 0 { req.Page = 1 } if req.Size == 0 { req.Size = 10 } fmt.Println("tree.asset.list ==>: ", req.DbName, req.Collection) var CreateRepoCtx = func() *repo.RepoSession { return &repo.RepoSession{ Ctx: context.Background(), Client: db.MongoClient, } } fmt.Println("list request query", req.Query) if req.Query["userId"] != nil { userId, ok := req.Query["userId"].(string) if ok { req.Query["userId"], _ = primitive.ObjectIDFromHex(userId) } } if req.Query["ownerId"] != nil && len(req.Query["ownerId"].(string)) > 0 { req.Query["ownerId"], _ = primitive.ObjectIDFromHex(req.Query["ownerId"].(string)) } dbConf := &comm.AssetDbConf{} if req.Query["categories"] != nil { enity := &comm.Database{} ok, _ = repo.RepoSeachDoc(CreateRepoCtx(), &repo.DocSearchOptions{ CollectName: repo.CollectionDatabase, Query: repo.Map{"name": req.DbName}, Project: []string{"categories"}, }, enity) if !ok { return nil, fmt.Errorf("没有查询到数据库:%s", req.DbName) } dbConf.Db = enity } ctx := CreateRepoCtx() err := ParseCategories(req.Query, ctx, dbConf) if err != nil { return nil, err } return repo.RepoPageSearch(CreateRepoCtx(), &repo.PageSearchOptions{ Db: req.DbName, CollectName: req.Collection, Page: int64(req.Page), Size: int64(req.Size), Query: req.Query, Project: req.Project, Sort: bson.M{"createTime": -1}, }) }, } } type AssetInsertReq struct { DbName string Coll string Pack *comm.AssetPackage Mat3d *comm.AssetMatGroup Img *comm.AssetImage OtherMat *comm.AssetMat } type AssetInsertResp struct { Id string } func RegTreeAssetInsert() *comm.NatsMsgReplyer { return &comm.NatsMsgReplyer{ Subject: "tree.asset.insert", Entity: func() interface{} { return &AssetInsertReq{} }, Cb2: func(msg *nats.Msg, entity interface{}) (interface{}, error) { req, ok := entity.(*AssetInsertReq) if !ok { return nil, fmt.Errorf("参数错误不是AssetInsertReq类型") } if len(req.DbName) < 1 || len(req.Coll) < 1 { return nil, fmt.Errorf("DbName Collection AssetId 不能为空") } var CreateRepoCtx = func() *repo.RepoSession { return &repo.RepoSession{ Ctx: context.Background(), Client: db.MongoClient, } } var insert interface{} = nil if req.Pack != nil { insert = req.Pack req.Pack.Id = primitive.NilObjectID req.Pack.CreateTime = time.Now() req.Pack.UpdateTime = time.Now() } else if req.Img != nil { insert = req.Img req.Img.Id = primitive.NilObjectID req.Img.CreateTime = time.Now() req.Img.UpdateTime = time.Now() } else if req.Mat3d != nil { insert = req.Mat3d req.Mat3d.Id = primitive.NilObjectID req.Mat3d.CreateTime = time.Now() req.Mat3d.UpdateTime = time.Now() } else if req.OtherMat != nil { insert = req.OtherMat req.OtherMat.Id = primitive.NilObjectID req.OtherMat.CreateTime = time.Now() req.OtherMat.UpdateTime = time.Now() } else { return nil, fmt.Errorf("不支持的类型") } fmt.Println("tree.asset.insert ==>: ", req.DbName, req.Coll) fmt.Println("tree.asset.insert req data==>: \n", req) id, err := repo.RepoDbAddDoc(CreateRepoCtx(), req.DbName, req.Coll, insert) return &AssetInsertResp{Id: id}, err }, } } type TreeAssetReq struct { DbName string Coll string Id string } func RegTreeAssetPackDetail() *comm.NatsMsgReplyer { return &comm.NatsMsgReplyer{ Subject: "tree.asset.scenepack.detail", Entity: func() interface{} { return &TreeAssetReq{} }, Cb2: func(msg *nats.Msg, entity interface{}) (interface{}, error) { req, ok := entity.(*TreeAssetReq) if !ok { return nil, fmt.Errorf("参数错误不是TreeAssetReq类型") } if len(req.DbName) < 1 || len(req.Coll) < 1 { return nil, fmt.Errorf("DbName Collection AssetId 不能为空") } var CreateRepoCtx = func() *repo.RepoSession { return &repo.RepoSession{ Ctx: context.Background(), Client: db.MongoClient, } } // out := &comm.AssetPackage{} // ok, err := repo.RepoSeachDoc(CreateRepoCtx(), &repo.DocSearchOptions{ // Db: req.DbName, // CollectName: req.Coll, // Query: repo.Map{"_id": req.Id}, // }, out) // if !ok { // return nil, fmt.Errorf("没有对应的数据") // } // return out, err ok, ret := repo.RepoSeachDocMap(CreateRepoCtx(), &repo.DocSearchOptions{ Db: req.DbName, CollectName: req.Coll, Query: repo.Map{"_id": req.Id}, }) if !ok { return nil, fmt.Errorf("没有对应的数据") } return ret, nil }, } } func RegTreeAssetImageDetail() *comm.NatsMsgReplyer { return &comm.NatsMsgReplyer{ Subject: "tree.asset.image.detail", Entity: func() interface{} { return &TreeAssetReq{} }, Cb2: func(msg *nats.Msg, entity interface{}) (interface{}, error) { req, ok := entity.(*TreeAssetReq) if !ok { return nil, fmt.Errorf("参数错误不是TreeAssetReq类型") } if len(req.DbName) < 1 || len(req.Coll) < 1 { return nil, fmt.Errorf("DbName Collection AssetId 不能为空") } var CreateRepoCtx = func() *repo.RepoSession { return &repo.RepoSession{ Ctx: context.Background(), Client: db.MongoClient, } } out := &comm.AssetImage{} ok, err := repo.RepoSeachDoc(CreateRepoCtx(), &repo.DocSearchOptions{ Db: req.DbName, CollectName: req.Coll, Query: repo.Map{"_id": req.Id}, }, out) if !ok { return nil, fmt.Errorf("没有对应的数据") } return out, err }, } } func RegTreeAssetMatgroupDetail() *comm.NatsMsgReplyer { return &comm.NatsMsgReplyer{ Subject: "tree.asset.matgroup.detail", Entity: func() interface{} { return &TreeAssetReq{} }, Cb2: func(msg *nats.Msg, entity interface{}) (interface{}, error) { req, ok := entity.(*TreeAssetReq) if !ok { return nil, fmt.Errorf("参数错误不是TreeAssetReq类型") } if len(req.DbName) < 1 || len(req.Coll) < 1 { return nil, fmt.Errorf("DbName Collection AssetId 不能为空") } var CreateRepoCtx = func() *repo.RepoSession { return &repo.RepoSession{ Ctx: context.Background(), Client: db.MongoClient, } } out := &comm.AssetMatGroup{} ok, err := repo.RepoSeachDoc(CreateRepoCtx(), &repo.DocSearchOptions{ Db: req.DbName, CollectName: req.Coll, Query: repo.Map{"_id": req.Id}, }, out) if !ok { return nil, fmt.Errorf("没有对应的数据") } return out, err }, } } func RegTreeAssetDetail() *comm.NatsMsgReplyer { return &comm.NatsMsgReplyer{ Subject: "tree.asset.detail", Entity: func() interface{} { return &TreeAssetReq{} }, Cb2: func(msg *nats.Msg, entity interface{}) (interface{}, error) { req, ok := entity.(*TreeAssetReq) if !ok { return nil, fmt.Errorf("参数错误不是TreeAssetReq类型") } if len(req.DbName) < 1 || len(req.Coll) < 1 || len(req.Id) < 1 { return nil, fmt.Errorf("DbName Collection AssetId 不能为空") } var CreateRepoCtx = func() *repo.RepoSession { return &repo.RepoSession{ Ctx: context.Background(), Client: db.MongoClient, } } // tree.asset.detail接口日志 fmt.Println("[tree.asset.detail]: db:", req.DbName, "coll:", req.Coll, "id:", req.Id) ok, ret := repo.RepoSeachDocMap(CreateRepoCtx(), &repo.DocSearchOptions{ Db: req.DbName, CollectName: req.Coll, Query: repo.Map{"_id": req.Id}, }) if !ok { return nil, fmt.Errorf("没有对应的数据") } return ret, nil }, } } func RegTreeAssetRemove() *comm.NatsMsgReplyer { return &comm.NatsMsgReplyer{ Subject: "tree.asset.remove", Entity: func() interface{} { return &TreeAssetReq{} }, Cb2: func(msg *nats.Msg, entity interface{}) (interface{}, error) { req, ok := entity.(*TreeAssetReq) if !ok { return nil, fmt.Errorf("参数错误不是TreeAssetReq类型") } if len(req.DbName) < 1 || len(req.Coll) < 1 || len(req.Id) < 1 { return nil, fmt.Errorf("DbName Collection AssetId 不能为空") } var CreateRepoCtx = func() *repo.RepoSession { return &repo.RepoSession{ Ctx: context.Background(), Client: db.MongoClient, } } // 如果删除的时面料类型,则对应删除图片向量 userId := "" if strings.Contains(req.Coll, "mat") { // 查询面料url result := map[string]interface{}{} found, _ := repo.RepoSeachDoc(CreateRepoCtx(), &repo.DocSearchOptions{ Db: req.DbName, CollectName: req.Coll, Query: repo.Map{"_id": req.Id}, Project: []string{"userId"}, }, &result) if found { if u, ok := result["userId"]; ok { if _userId, ok := u.(primitive.ObjectID); ok { userId = _userId.Hex() } } } } fmt.Println("from mlivus remove: ", req.Coll, userId) _, err := repo.RepoDeleteDbDoc(CreateRepoCtx(), req.DbName, req.Coll, req.Id) // 成功删除 if err != nil { if len(userId) > 0 { tableName := fmt.Sprintf("%s_%s", req.Coll, userId) // 删除向量数据库对应的图片 go deleteImageToMlivus(tableName, req.Id) } } return nil, err }, } } type UpdateCommReq struct { Pack *comm.AssetPackage OtherMat *comm.AssetMat Env3d *comm.AssetEnv3dHdr Mat3d *comm.AssetMatGroup Img *comm.AssetImage Coll string Db string } type CreateShadowReq struct { Width int Scale int Id string Coll string Db string } func RegTreeAssetUpdate() *comm.NatsMsgReplyer { return &comm.NatsMsgReplyer{ Subject: "tree.asset.update.header", Entity: func() interface{} { return &UpdateCommReq{} }, Cb2: func(msg *nats.Msg, entity interface{}) (interface{}, error) { req, ok := entity.(*UpdateCommReq) if !ok { return nil, fmt.Errorf("参数错误不是UpdateCommReq类型") } if len(req.Db) < 1 || len(req.Coll) < 1 || (req.Pack == nil && req.Mat3d == nil && req.Img == nil) { return nil, fmt.Errorf("参数错误") } if req.Pack != nil { url, err := NatsCenter.RequestConfig("bus-network") if err != nil { return nil, err } req.Pack.UpdateTime = time.Now() // id := req.Pack.Id.Hex() // req.Pack.Id = primitive.NilObjectID // _, err := repo.RepoUpdateSeDbDoc(CreateRepoCtx(), req.Db, req.Coll, id, req.Pack) _, err = tree.UpdateAssetPackageHeader(CreateRepoCtx(), req.Db, req.Coll, req.Pack, url) return nil, err } if req.OtherMat != nil { id := req.OtherMat.Id.Hex() req.OtherMat.Id = primitive.NilObjectID _, err := repo.RepoUpdateSeDbDoc(CreateRepoCtx(), req.Db, req.Coll, id, req.OtherMat) return nil, err } if req.Env3d != nil { req.Env3d.UpdateTime = time.Now() id := req.Env3d.Id.Hex() req.Env3d.Id = primitive.NilObjectID _, err := repo.RepoUpdateSeDbDoc(CreateRepoCtx(), req.Db, req.Coll, id, req.Env3d) return nil, err } if req.Mat3d != nil { req.Mat3d.UpdateTime = time.Now() id := req.Mat3d.Id.Hex() req.Mat3d.Id = primitive.NilObjectID _, err := repo.RepoUpdateSeDbDoc(CreateRepoCtx(), req.Db, req.Coll, id, req.Mat3d) return nil, err } if req.Img != nil { req.Img.UpdateTime = time.Now() id := req.Img.Id.Hex() req.Img.Id = primitive.NilObjectID _, err := repo.RepoUpdateSeDbDoc(CreateRepoCtx(), req.Db, req.Coll, id, req.Img) return nil, err } return nil, fmt.Errorf("不支持的更新类型") }, } } func RegTreeAssetPackProcess() *comm.NatsMsgReplyer { return &comm.NatsMsgReplyer{ Subject: "tree.asset.pack.retreat", Entity: func() interface{} { return &UpdateCommReq{} }, Cb2: func(msg *nats.Msg, entity interface{}) (interface{}, error) { req, ok := entity.(*UpdateCommReq) if !ok { return nil, fmt.Errorf("参数错误不是UpdateCommReq类型") } if len(req.Db) < 1 || len(req.Coll) < 1 || req.Pack == nil { return nil, fmt.Errorf("参数错误") } var CreateRepoCtx = func() *repo.RepoSession { return &repo.RepoSession{ Ctx: context.Background(), Client: db.MongoClient, } } url, err := NatsCenter.RequestConfig("bus-network") if err != nil { return nil, err } return nil, tree.ProcessPack(CreateRepoCtx(), req.Pack.Id.Hex(), req.Db, req.Coll, url) }, } } func RegTreeAssetRecreateShadow() *comm.NatsMsgReplyer { return &comm.NatsMsgReplyer{ Subject: "tree.asset.pack.reshadow", Entity: func() interface{} { return &CreateShadowReq{} }, Cb2: func(msg *nats.Msg, entity interface{}) (interface{}, error) { req, ok := entity.(*CreateShadowReq) if !ok { return nil, fmt.Errorf("参数错误不是CreateShadowReq类型") } if len(req.Db) < 1 || len(req.Coll) < 1 || len(req.Id) < 1 { return nil, fmt.Errorf("参数错误") } var CreateRepoCtx = func() *repo.RepoSession { return &repo.RepoSession{ Ctx: context.Background(), Client: db.MongoClient, } } url, err := NatsCenter.RequestConfig("bus-network") if err != nil { return nil, err } return nil, tree.CreatePackShadow(CreateRepoCtx(), req.Id, req.Db, req.Coll, req.Scale, req.Width, url) }, } } type AssetEvent struct { DefId string DbId string // 哪个资产库的统计表 Api string } func RegTreeAssetCreate() *comm.NatsMsgReplyer { return &comm.NatsMsgReplyer{ Subject: "tree.asset.create", Entity: func() interface{} { return &UpdateCommReq{} }, Cb2: func(msg *nats.Msg, entity interface{}) (interface{}, error) { req, ok := entity.(*UpdateCommReq) if !ok { return nil, fmt.Errorf("参数错误不是UpdateCommReq类型") } if len(req.Db) < 1 || len(req.Coll) < 1 { return nil, fmt.Errorf("参数错误") } var CreateRepoCtx = func() *repo.RepoSession { return &repo.RepoSession{ Ctx: context.Background(), Client: db.MongoClient, } } fmt.Println("tree.asset.create ==>: ", req.Db, req.Coll) fmt.Println("tree.asset.create req data==>: \n", req) url, err := NatsCenter.RequestConfig("bus-network") if err != nil { return nil, err } if req.Pack != nil { return tree.UploadAssetPackage(CreateRepoCtx(), req.Db, req.Coll, req.Pack, url) } if req.OtherMat != nil { return repo.RepoDbAddDoc(CreateRepoCtx(), req.Db, req.Coll, req.OtherMat) } if req.Env3d != nil { return repo.RepoDbAddDoc(CreateRepoCtx(), req.Db, req.Coll, req.Env3d) } if req.Mat3d != nil { matId, err := repo.RepoDbAddDoc(CreateRepoCtx(), req.Db, req.Coll, req.Mat3d) if err != nil { return "", err } // 上传成功存储对应封面图图片向量 if len(req.Mat3d.Thumbnail.Url) > 0 { tableName := fmt.Sprintf("%s_%s", req.Coll, req.Mat3d.UserId.Hex()) fmt.Println("upload to mlivus: ", tableName, req.Mat3d.Thumbnail.Url, matId) go uploadImageToMlivus(tableName, matId, req.Mat3d.Thumbnail.Url) } return matId, err } if req.Img != nil { return repo.RepoDbAddDoc(CreateRepoCtx(), req.Db, req.Coll, req.Img) } base := &comm.Database{} ok, _ = repo.RepoSeachDoc(CreateRepoCtx(), &repo.DocSearchOptions{ CollectName: repo.CollectionDatabase, Query: repo.Map{"name": req.Db}, Project: []string{"assets"}, }, base) if ok { for _, v := range base.Assets { if v.Collection == req.Coll { NatsCenter.PublishObj("asset.added", &AssetEvent{Api: "upload", DbId: base.Id.Hex(), DefId: v.Id}) break } } } return nil, fmt.Errorf("不支持的类型") }, } }