123456789101112131415161718192021222324252627282930313233343536 |
- package api
- import (
- "box-cost/db/model"
- "box-cost/db/repo"
- "fmt"
- "go.mongodb.org/mongo-driver/bson"
- )
- func incrementer(ctx *ApiSession, typeName string) (serial string, err error) {
- // 获取类型
- cate := &model.Category{}
- found, err := repo.RepoSeachDoc(ctx.CreateRepoCtx(), &repo.DocSearchOptions{
- CollectName: "cates",
- Project: []string{"letterName"},
- Query: repo.Map{"name": typeName},
- Sort: bson.M{"_id": -1},
- }, cate)
- if !found || err != nil {
- return "", fmt.Errorf("未找到该分类")
- }
- // increment index加1
- increment := &model.Increment{}
- repo.RepoSeachDoc(ctx.CreateRepoCtx(), &repo.DocSearchOptions{
- CollectName: repo.CollectionIncrement,
- Project: []string{"index"},
- Sort: bson.M{"_id": -1},
- }, increment)
- index := increment.Index + 1
- repo.RepoAddDoc(ctx.CreateRepoCtx(), repo.CollectionIncrement, &model.Increment{Index: index})
- // 拼接为序号
- return fmt.Sprintf("%s-%05d", cate.LetterName, index), nil
- }
|