utils.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. }
  112. // 处理报表query条件
  113. func handleReportQuery(query map[string]interface{}) map[string]interface{} {
  114. // 条件处理
  115. query["status"] = "complete"
  116. if _supplierId, ok := query["supplierId"]; ok {
  117. delete(query, "supplierId")
  118. fmt.Printf("id::::%#v\n", _supplierId)
  119. supplierId, _ := primitive.ObjectIDFromHex(_supplierId.(string))
  120. if !supplierId.IsZero() {
  121. query["supplierId"] = supplierId
  122. }
  123. }
  124. if _timeRange, ok := query["timeRange"]; ok {
  125. timeRange, _ := _timeRange.([]interface{})
  126. if len(timeRange) == 2 {
  127. start, end := getTimeRange(timeRange[0].(string), timeRange[1].(string))
  128. query["completeTime"] = bson.M{"$gte": start, "$lte": end}
  129. }
  130. delete(query, "timeRange")
  131. }
  132. if _planIds, ok := query["planIds"]; ok {
  133. if len(_planIds.([]interface{})) > 0 {
  134. planQuery := bson.A{}
  135. for _, _planId := range _planIds.([]interface{}) {
  136. planId, _ := primitive.ObjectIDFromHex(_planId.(string))
  137. planQuery = append(planQuery, bson.M{"planId": planId})
  138. }
  139. query["$or"] = planQuery
  140. }
  141. delete(query, "planIds")
  142. }
  143. return query
  144. }
  145. // 获取公司名字
  146. func getCompanyName(apictx *ApiSession) string {
  147. companyName := "中鱼互动"
  148. info := model.Setting{}
  149. found, _ := repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
  150. CollectName: "infos",
  151. }, &info)
  152. if found {
  153. if info.CompanyName != "" {
  154. companyName = info.CompanyName
  155. }
  156. }
  157. return companyName
  158. }