package bus

import (
	"context"
	"fmt"
	"mesh/db"
	"mesh/db/model"
	"mesh/db/repo"

	"github.com/nats-io/nats.go"
	"infish.cn/comm"
)

type ReqDetail struct {
	Id string
}

func RegLibDetail() *comm.NatsMsgReplyer {

	return &comm.NatsMsgReplyer{
		Subject: "meshlib.item.detail",
		Entity:  func() interface{} { return &ReqDetail{} },
		Cb2: func(msg *nats.Msg, entity interface{}) (interface{}, error) {
			req, ok := entity.(*ReqDetail)
			if !ok {
				return nil, fmt.Errorf("参数错误不是ReqDetail类型")
			}
			if len(req.Id) < 1 {
				return nil, fmt.Errorf("Id不能为空")
			}

			var CreateRepoCtx = func() *repo.RepoSession {
				return &repo.RepoSession{
					Ctx:    context.Background(),
					Client: db.MongoClient,
				}
			}
			out := &model.HubMesh{}
			ok, err := repo.RepoSeachDoc(CreateRepoCtx(), &repo.DocSearchOptions{CollectName: repo.CollectionHubMeshes, Query: repo.Map{"_id": req.Id}}, out)
			if err != nil {
				return nil, err
			}
			if !ok {
				return nil, fmt.Errorf("查询的模型库不存在")
			}
			return out, nil
		},
	}
}
