utils.go 993 B

123456789101112131415161718192021222324252627282930313233343536
  1. package api
  2. import (
  3. "box-cost/db/model"
  4. "box-cost/db/repo"
  5. "fmt"
  6. "go.mongodb.org/mongo-driver/bson"
  7. )
  8. func incrementer(ctx *ApiSession, typeName string) (serial string, err error) {
  9. // 获取类型
  10. cate := &model.Category{}
  11. found, err := repo.RepoSeachDoc(ctx.CreateRepoCtx(), &repo.DocSearchOptions{
  12. CollectName: "cates",
  13. Project: []string{"letterName"},
  14. Query: repo.Map{"name": typeName},
  15. Sort: bson.M{"_id": -1},
  16. }, cate)
  17. if !found || err != nil {
  18. return "", fmt.Errorf("未找到该分类")
  19. }
  20. // increment index加1
  21. increment := &model.Increment{}
  22. repo.RepoSeachDoc(ctx.CreateRepoCtx(), &repo.DocSearchOptions{
  23. CollectName: repo.CollectionIncrement,
  24. Project: []string{"index"},
  25. Sort: bson.M{"_id": -1},
  26. }, increment)
  27. index := increment.Index + 1
  28. repo.RepoAddDoc(ctx.CreateRepoCtx(), repo.CollectionIncrement, &model.Increment{Index: index})
  29. // 拼接为序号
  30. return fmt.Sprintf("%s-%05d", cate.LetterName, index), nil
  31. }