package api import ( "mats/db/model" "mats/db/repo" "github.com/gin-gonic/gin" "go.mongodb.org/mongo-driver/bson" "go.mongodb.org/mongo-driver/bson/primitive" ) func CreateDatabaseAssetConfRouter(router *GinRouter) { //创建资产定义 router.POST("/assetconf/create/:id", func(c *gin.Context, apictx *ApiSession) (interface{}, error) { id := c.Param("id") if len(id) < 1 { return nil, NewError("数据库Id不能为空") } body := &model.DbAsset{} c.ShouldBindJSON(body) body.Id = primitive.NewObjectID().Hex() if len(body.Collection) < 1 || len(body.Label) < 1 || body.Type == 0 { return nil, NewError("数据不合法!") } database := &model.Database{} ok, _ := repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{CollectName: repo.CollectionDatabase, Query: repo.Map{"assets.collection": body.Collection, "_id": id}, Project: []string{"_id"}}, database) if ok { return nil, NewError("资产库编号不能重复") } return repo.RepoDocArrayAppend( apictx.CreateRepoCtx(), repo.CollectionDatabase, id, "assets", body, ) }) //创建资产定义 router.GET("/assetconf/list/:id", func(c *gin.Context, apictx *ApiSession) (interface{}, error) { id := c.Param("id") if len(id) < 1 { return nil, NewError("数据库Id不能为空") } database := &model.Database{} _, err := repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{CollectName: repo.CollectionDatabase, Query: repo.Map{"_id": id}, Project: []string{"assets"}}, database) return database.Assets, err }) //删除资产定义 router.POST("/assetconf/delete/:id", func(c *gin.Context, apictx *ApiSession) (interface{}, error) { id := c.Param("id") if len(id) < 1 { return nil, NewError("数据库Id不能为空") } body := &model.DbAsset{} c.ShouldBindJSON(body) if len(body.Id) < 1 { return nil, NewError("资产Id不能为空!") } return repo.RepoDocArrayOneRemove( apictx.CreateRepoCtx(), &repo.ArrayOneRemoveOption{ CollectName: repo.CollectionDatabase, Id: id, ArrayQuery: repo.Map{"assets": bson.M{"id": body.Id}}, }, ) }) //更新资产标签 router.POST("/assetconf/update/label/:id", func(c *gin.Context, apictx *ApiSession) (interface{}, error) { id := c.Param("id") if len(id) < 1 { return nil, NewError("数据库Id不能为空") } body := &model.DbAsset{} c.ShouldBindJSON(body) if len(body.Id) < 1 || len(body.Label) < 1 { return nil, NewError("参数非法") } optSet := repo.Map{} optSet["assets.$.label"] = body.Label option := &repo.ArrayOneUpdateOption{ CollectName: repo.CollectionDatabase, Id: id, Query: repo.Map{"assets.id": body.Id}, Set: optSet, } return repo.RepoDocArrayOneUpdate( apictx.CreateRepoCtx(), option, ) }) //更新资产分类 router.POST("/assetconf/update/categoryIds/:id", func(c *gin.Context, apictx *ApiSession) (interface{}, error) { id := c.Param("id") if len(id) < 1 { return nil, NewError("数据库Id不能为空") } body := &model.DbAsset{} c.ShouldBindJSON(body) if len(body.Id) < 1 { return nil, NewError("参数非法") } optSet := repo.Map{} optSet["assets.$.categoryIds"] = body.CategoryIds option := &repo.ArrayOneUpdateOption{ CollectName: repo.CollectionDatabase, Id: id, Query: repo.Map{"assets.id": body.Id}, Set: optSet, } return repo.RepoDocArrayOneUpdate( apictx.CreateRepoCtx(), option, ) }) }