utils.go 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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. "go.mongodb.org/mongo-driver/bson/primitive"
  8. )
  9. func incrementer(ctx *ApiSession, typeName string) (serial string, err error) {
  10. // 获取类型
  11. cate := &model.Category{}
  12. found, err := repo.RepoSeachDoc(ctx.CreateRepoCtx(), &repo.DocSearchOptions{
  13. CollectName: "cates",
  14. Project: []string{"letterName"},
  15. Query: repo.Map{"name": typeName},
  16. Sort: bson.M{"_id": -1},
  17. }, cate)
  18. if !found || err != nil {
  19. return "", fmt.Errorf("未找到该分类")
  20. }
  21. // increment index加1
  22. increment := &model.Increment{}
  23. repo.RepoSeachDoc(ctx.CreateRepoCtx(), &repo.DocSearchOptions{
  24. CollectName: repo.CollectionIncrement,
  25. Project: []string{"index"},
  26. Sort: bson.M{"_id": -1},
  27. }, increment)
  28. index := increment.Index + 1
  29. repo.RepoAddDoc(ctx.CreateRepoCtx(), repo.CollectionIncrement, &model.Increment{Index: index})
  30. // 拼接为序号
  31. return fmt.Sprintf("%s-%05d", cate.LetterName, index), nil
  32. }
  33. func SearchBillTypeById(ctx *ApiSession, collectName string, id primitive.ObjectID) (string, error) {
  34. found, curbill := repo.RepoSeachDocMap(ctx.CreateRepoCtx(), &repo.DocSearchOptions{
  35. CollectName: collectName,
  36. Project: []string{"type"},
  37. Query: repo.Map{"_id": id},
  38. Sort: bson.M{"_id": -1},
  39. })
  40. if !found {
  41. return "", fmt.Errorf("未找到该分类")
  42. }
  43. return curbill["type"].(string), nil
  44. }