utils.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. package api
  2. import (
  3. "box-cost/db/model"
  4. "box-cost/db/repo"
  5. "bytes"
  6. "fmt"
  7. "net/http"
  8. "time"
  9. "github.com/thecodingmachine/gotenberg-go-client/v7"
  10. "go.mongodb.org/mongo-driver/bson"
  11. "go.mongodb.org/mongo-driver/bson/primitive"
  12. )
  13. func generateSerial(ctx *ApiSession, typeName string) (serial string, err error) {
  14. // 获取类型
  15. cate := &model.Category{}
  16. found, err := repo.RepoSeachDoc(ctx.CreateRepoCtx(), &repo.DocSearchOptions{
  17. CollectName: "cates",
  18. Project: []string{"letterName"},
  19. Query: repo.Map{"name": typeName},
  20. Sort: bson.M{"_id": -1},
  21. }, cate)
  22. if !found || err != nil {
  23. return "", fmt.Errorf("未找到该类型")
  24. }
  25. // 自增器 increment index加1
  26. increment := &model.Increment{}
  27. found, _ = repo.RepoSeachDoc(ctx.CreateRepoCtx(), &repo.DocSearchOptions{
  28. CollectName: repo.CollectionIncrement,
  29. Query: repo.Map{"type": cate.LetterName},
  30. Project: []string{"index"},
  31. Sort: bson.M{"_id": -1},
  32. }, increment)
  33. if !found {
  34. repo.RepoAddDoc(ctx.CreateRepoCtx(), repo.CollectionIncrement, &model.Increment{
  35. Type: cate.LetterName,
  36. Index: 1,
  37. })
  38. return fmt.Sprintf("%s-%06d", cate.LetterName, 1), nil
  39. }
  40. index := increment.Index + 1
  41. repo.RepoUpdateSetDoc(ctx.CreateRepoCtx(), repo.CollectionIncrement, increment.Id.Hex(), &model.Increment{Index: index})
  42. // 拼接为序号
  43. return fmt.Sprintf("%s-%06d", cate.LetterName, index), nil
  44. }
  45. func searchBillTypeById(ctx *ApiSession, collectName string, id primitive.ObjectID) (string, error) {
  46. found, curbill := repo.RepoSeachDocMap(ctx.CreateRepoCtx(), &repo.DocSearchOptions{
  47. CollectName: collectName,
  48. Project: []string{"type"},
  49. Query: repo.Map{"_id": id},
  50. Sort: bson.M{"_id": -1},
  51. })
  52. if !found {
  53. return "", fmt.Errorf("未找到该类型")
  54. }
  55. return curbill["type"].(string), nil
  56. }
  57. func excelToPdf(formBody *bytes.Buffer, pdfHost string) (*http.Response, error) {
  58. httpClient := &http.Client{
  59. Timeout: time.Duration(5) * time.Second,
  60. }
  61. client := &gotenberg.Client{Hostname: pdfHost, HTTPClient: httpClient}
  62. doc, err := gotenberg.NewDocumentFromBytes("foo.xlsx", formBody.Bytes())
  63. if err != nil {
  64. fmt.Println(" to pdf read data err:", err)
  65. return nil, err
  66. }
  67. req := gotenberg.NewOfficeRequest(doc)
  68. req.Landscape(true)
  69. return client.Post(req)
  70. }
  71. func isManager(roles []string) bool {
  72. if len(roles) > 0 {
  73. for _, role := range roles {
  74. if role == "manager" {
  75. return true
  76. }
  77. }
  78. }
  79. return false
  80. }
  81. func getUserById(apictx *ApiSession, id primitive.ObjectID) (*model.UserSmaple, error) {
  82. fmt.Println(id.Hex())
  83. user := &model.UserSmaple{}
  84. _, err := repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
  85. Db: "box-user",
  86. CollectName: repo.CollectionUsers,
  87. Query: repo.Map{"_id": id},
  88. Project: []string{"name", "avatar", "city", "loginName", "roles"},
  89. }, user)
  90. return user, err
  91. }
  92. // 获取一天的起始终止时间
  93. // func getDayRange(t time.Time) (start, end time.Time) {
  94. // loc, _ := time.LoadLocation("Local")
  95. // date := t.Format("2006-01-02")
  96. // startDate := date + " 00:00:00"
  97. // startTime, _ := time.ParseInLocation("2006-01-02 15:04:05", startDate, loc)
  98. // endDate := date + " 23:59:59"
  99. // endTime, _ := time.ParseInLocation("2006-01-02 15:04:05", endDate, loc)
  100. // return startTime, endTime
  101. // }
  102. // 获取时间跨度的起始终止时间
  103. // startDate/endDate 2023-01-31
  104. func getTimeRange(startDate, endDate string) (start, end time.Time) {
  105. loc, _ := time.LoadLocation("Local")
  106. startDateTime := startDate + " 00:00:00"
  107. endDateTime := endDate + " 23:59:59"
  108. start, _ = time.ParseInLocation("2006-01-02 15:04:05", startDateTime, loc)
  109. end, _ = time.ParseInLocation("2006-01-02 15:04:05", endDateTime, loc)
  110. return
  111. }