package api import ( "assetcenter/db/repo" "assetcenter/utils" "fmt" "math/rand" "time" "github.com/gin-gonic/gin" "go.mongodb.org/mongo-driver/bson" "go.mongodb.org/mongo-driver/bson/primitive" "infish.cn/comm" ) func init() { // 保证每次生成的随机数不一样 rand.Seed(time.Now().UnixNano()) } func CreateDatabaseRouter(router *GinRouter) { router.POSTJWT("/db/import/defaultdb", func(c *gin.Context, apictx *ApiSession) (interface{}, error) { search := &comm.Database{} ok, _ := repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{CollectName: repo.CollectionDatabase, Query: repo.Map{"name": "qdb-default"}, Project: []string{"_id"}}, search) if ok { return nil, NewError("默认数据库已存在!") } db := comm.CreateDefaultDatabase() return repo.RepoAddDoc(apictx.CreateRepoCtx(), repo.CollectionDatabase, db) }) router.POSTJWT("/db/category/save", func(c *gin.Context, apictx *ApiSession) (interface{}, error) { body := &struct { Id string `json:"_id"` Categories []*comm.CategoryNode }{} err := c.ShouldBindJSON(body) if err != nil { return nil, err } search := &comm.Database{} ok, _ := repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{CollectName: repo.CollectionDatabase, Query: repo.Map{"_id": body.Id}, Project: []string{"_id", "categories"}}, search) if ok { //已存在 search.Categories.Categories = body.Categories search.Categories.UpdateTime = time.Now() } else { search.Categories = &comm.DbCategory{ Id: primitive.NewObjectID(), Scope: "default", CreateTime: time.Now(), UpdateTime: time.Now(), Categories: body.Categories, } } return repo.RepoUpdateSetDoc(apictx.CreateRepoCtx(), "database", body.Id, bson.M{"categories": search.Categories}) }) CreateCRUD(router, "/db", &CRUDOption{ Collection: repo.CollectionDatabase, NewModel: func(c *gin.Context, apictx *ApiSession) (interface{}, error) { cat := &comm.Database{} c.ShouldBindJSON(cat) //前端指定Label if len(cat.Label) < 1 { return nil, NewError("参数不合法!") } //判断当前用户的数据库是否已经到上线 limit := &comm.UserDatabase{} ok, err := repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{CollectName: repo.CollectionUserDatabase, Query: repo.Map{"_id": apictx.User.Parent}}, limit) if err != nil { return nil, err } if !ok { //没有对于的数据 limit.Id, _ = primitive.ObjectIDFromHex(apictx.User.Parent) limit.UserName = apictx.User.Phone limit.Limits = 4 //默认只能创建3个数据库 _, err = repo.RepoAddDoc(apictx.CreateRepoCtx(), repo.CollectionUserDatabase, limit) if err != nil { return nil, err } } count, err := repo.RepoCountDoc(apictx.CreateRepoCtx(), repo.CollectionDatabase, repo.Map{"userId": apictx.User.Parent}) if err != nil { return nil, err } if count >= int64(limit.Limits) { return nil, fmt.Errorf("已经超出数据库个数上线,请联系管理员添加") } //数据库名字添加一个随机数 var createDbRandomName = func() string { return fmt.Sprintf("db-%s-%s", time.Now().Format("20060102"), utils.RandName(6)) } cat.CreateTime = time.Now() cat.Assets = comm.CreateDefaultDbAssets() cat.Categories = &comm.DbCategory{} cat.UserId = apictx.User.Parent cat.Name = createDbRandomName() search := &comm.Database{} ok, _ = repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{CollectName: repo.CollectionDatabase, Query: repo.Map{"name": cat.Name}, Project: []string{"_id"}}, search) if ok { return nil, NewError("数据库编号已存在!") } cat.Assets = comm.CreateDefaultDbAssets() cat.CreateTime = time.Now() return cat, nil }, EmtyModel: func(c *gin.Context, apictx *ApiSession) interface{} { return &comm.Database{} }, SearchFilter: func(c *gin.Context, apictx *ApiSession, query map[string]interface{}) map[string]interface{} { return map[string]interface{}{"userId": apictx.User.ID} // return map[string]interface{}{} }, JWT: true, SearchProject: []string{"name", "label", "createTime"}, DetailProject: []string{"name", "label", "createTime", "categories", "assetsCategory", "assets"}, Remove: func(c *gin.Context, apictx *ApiSession, id string) (interface{}, error) { // team := &model.ResCategory{} // ok, _ := repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{CollectName: repo.CollectionCategory, Query: repo.Map{"_id": id}, Project: []string{"orderId", "type"}}, team) // if !ok { // return nil, NewError("删除失败!") // } // minOrderId := team.OrderId //后面有几个零 // if minOrderId > 0 { // var maxStep int64 = 1 // currValue := minOrderId // for { // if currValue%10 == 0 { // currValue = currValue / 10 // maxStep = maxStep * 10 // } else { // break // } // } // maxOrderId := minOrderId + maxStep - 1 // return repo.RepoDeleteDocs(apictx.CreateRepoCtx(), repo.CollectionCategory, &bson.M{"orderId": bson.M{"$gte": minOrderId, "$lt": maxOrderId}, "type": team.Type}) // } return repo.RepoDeleteDoc(apictx.CreateRepoCtx(), repo.CollectionDatabase, id) }, }) }