utils.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. package api
  2. import (
  3. "box-cost/db/model"
  4. "box-cost/db/repo"
  5. "bytes"
  6. "fmt"
  7. "math/rand"
  8. "net/http"
  9. "time"
  10. "unsafe"
  11. "github.com/thecodingmachine/gotenberg-go-client/v7"
  12. "go.mongodb.org/mongo-driver/bson"
  13. "go.mongodb.org/mongo-driver/bson/primitive"
  14. )
  15. var ExcleColNum map[int]string = map[int]string{
  16. 0: "A",
  17. 1: "B",
  18. 2: "C",
  19. 3: "D",
  20. 4: "E",
  21. 5: "F",
  22. 6: "G",
  23. 7: "H",
  24. 8: "I",
  25. 9: "J",
  26. 10: "K",
  27. 11: "L",
  28. 12: "M",
  29. 13: "N",
  30. 14: "O",
  31. }
  32. var SignatureDir string = "https://www.3dqueen.cloud/box/v1/boxcost/public/"
  33. // 去重相邻元素
  34. func removeDuplicationSort(arr []string) []string {
  35. length := len(arr)
  36. if length == 0 {
  37. return arr
  38. }
  39. j := 0
  40. for i := 1; i < length; i++ {
  41. if arr[i] != arr[j] {
  42. j++
  43. if j < i {
  44. swap(arr, i, j)
  45. }
  46. }
  47. }
  48. return arr[:j+1]
  49. }
  50. func swap(arr []string, a, b int) {
  51. arr[a], arr[b] = arr[b], arr[a]
  52. }
  53. func generateSerial(ctx *ApiSession, typeName string) (serial string, err error) {
  54. // 获取类型
  55. cate := &model.Category{}
  56. found, err := repo.RepoSeachDoc(ctx.CreateRepoCtx(), &repo.DocSearchOptions{
  57. CollectName: "cates",
  58. Project: []string{"letterName"},
  59. Query: repo.Map{"name": typeName},
  60. Sort: bson.M{"_id": -1},
  61. }, cate)
  62. if !found || err != nil {
  63. return "", fmt.Errorf("未找到该类型")
  64. }
  65. // 自增器 increment index加1
  66. increment := &model.Increment{}
  67. found, _ = repo.RepoSeachDoc(ctx.CreateRepoCtx(), &repo.DocSearchOptions{
  68. CollectName: repo.CollectionIncrement,
  69. Query: repo.Map{"type": cate.LetterName},
  70. Project: []string{"index"},
  71. Sort: bson.M{"_id": -1},
  72. }, increment)
  73. if !found {
  74. repo.RepoAddDoc(ctx.CreateRepoCtx(), repo.CollectionIncrement, &model.Increment{
  75. Type: cate.LetterName,
  76. Index: 1,
  77. })
  78. return fmt.Sprintf("%s-%06d", cate.LetterName, 1), nil
  79. }
  80. index := increment.Index + 1
  81. repo.RepoUpdateSetDoc(ctx.CreateRepoCtx(), repo.CollectionIncrement, increment.Id.Hex(), &model.Increment{Index: index})
  82. // 拼接为序号
  83. return fmt.Sprintf("%s-%06d", cate.LetterName, index), nil
  84. }
  85. func searchBillTypeById(ctx *ApiSession, collectName string, id primitive.ObjectID) (string, error) {
  86. fmt.Println(id.Hex())
  87. found, curbill := repo.RepoSeachDocMap(ctx.CreateRepoCtx(), &repo.DocSearchOptions{
  88. CollectName: collectName,
  89. Project: []string{"type"},
  90. Query: repo.Map{"_id": id},
  91. Sort: bson.M{"_id": -1},
  92. })
  93. if !found {
  94. return "", fmt.Errorf("未找到该类型")
  95. }
  96. return curbill["type"].(string), nil
  97. }
  98. func excelToPdf(formBody *bytes.Buffer, pdfHost string) (*http.Response, error) {
  99. httpClient := &http.Client{
  100. Timeout: time.Duration(5) * time.Second,
  101. }
  102. client := &gotenberg.Client{Hostname: pdfHost, HTTPClient: httpClient}
  103. doc, err := gotenberg.NewDocumentFromBytes("foo.xlsx", formBody.Bytes())
  104. if err != nil {
  105. fmt.Println(" to pdf read data err:", err)
  106. return nil, err
  107. }
  108. req := gotenberg.NewOfficeRequest(doc)
  109. req.Landscape(true)
  110. return client.Post(req)
  111. }
  112. func isManager(roles []string) bool {
  113. if len(roles) > 0 {
  114. for _, role := range roles {
  115. if role == "manager" {
  116. return true
  117. }
  118. }
  119. }
  120. return false
  121. }
  122. func getUserById(apictx *ApiSession, id primitive.ObjectID) (*model.UserSmaple, error) {
  123. fmt.Println(id.Hex())
  124. user := &model.UserSmaple{}
  125. _, err := repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
  126. Db: "box-user",
  127. CollectName: repo.CollectionUsers,
  128. Query: repo.Map{"_id": id},
  129. Project: []string{"name", "avatar", "city", "loginName", "roles"},
  130. }, user)
  131. return user, err
  132. }
  133. // 获取一天的起始终止时间
  134. // func getDayRange(t time.Time) (start, end time.Time) {
  135. // loc, _ := time.LoadLocation("Local")
  136. // date := t.Format("2006-01-02")
  137. // startDate := date + " 00:00:00"
  138. // startTime, _ := time.ParseInLocation("2006-01-02 15:04:05", startDate, loc)
  139. // endDate := date + " 23:59:59"
  140. // endTime, _ := time.ParseInLocation("2006-01-02 15:04:05", endDate, loc)
  141. // return startTime, endTime
  142. // }
  143. // 获取时间跨度的起始终止时间
  144. // startDate/endDate 2023-01-31
  145. func getTimeRange(startDate, endDate string) (start, end time.Time) {
  146. loc, _ := time.LoadLocation("Local")
  147. startDateTime := startDate + " 00:00:00"
  148. endDateTime := endDate + " 23:59:59"
  149. start, _ = time.ParseInLocation("2006-01-02 15:04:05", startDateTime, loc)
  150. end, _ = time.ParseInLocation("2006-01-02 15:04:05", endDateTime, loc)
  151. return
  152. }
  153. // 处理报表query条件
  154. func handleReportQuery(query map[string]interface{}) map[string]interface{} {
  155. // 条件处理
  156. query["status"] = "complete"
  157. if _supplierId, ok := query["supplierId"]; ok {
  158. delete(query, "supplierId")
  159. fmt.Printf("id::::%#v\n", _supplierId)
  160. supplierId, _ := primitive.ObjectIDFromHex(_supplierId.(string))
  161. if !supplierId.IsZero() {
  162. query["supplierId"] = supplierId
  163. }
  164. }
  165. if _timeRange, ok := query["timeRange"]; ok {
  166. timeRange, _ := _timeRange.([]interface{})
  167. if len(timeRange) == 2 {
  168. start, end := getTimeRange(timeRange[0].(string), timeRange[1].(string))
  169. query["completeTime"] = bson.M{"$gte": start, "$lte": end}
  170. }
  171. delete(query, "timeRange")
  172. }
  173. if _planIds, ok := query["planIds"]; ok {
  174. if len(_planIds.([]interface{})) > 0 {
  175. planQuery := bson.A{}
  176. for _, _planId := range _planIds.([]interface{}) {
  177. planId, _ := primitive.ObjectIDFromHex(_planId.(string))
  178. planQuery = append(planQuery, bson.M{"planId": planId})
  179. }
  180. query["$or"] = planQuery
  181. }
  182. delete(query, "planIds")
  183. }
  184. return query
  185. }
  186. // 获取公司名字
  187. func getCompanyName(apictx *ApiSession) string {
  188. companyName := "中鱼互动"
  189. info := model.Setting{}
  190. found, _ := repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
  191. CollectName: "infos",
  192. }, &info)
  193. if found {
  194. if info.CompanyName != "" {
  195. companyName = info.CompanyName
  196. }
  197. }
  198. return companyName
  199. }
  200. const (
  201. letterIdBits = 6
  202. letterIdMask = 1<<letterIdBits - 1
  203. letterIdMax = 63 / letterIdBits
  204. letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
  205. )
  206. var src = rand.NewSource(time.Now().UnixNano())
  207. func randName(n int) string {
  208. b := make([]byte, n)
  209. for i, cache, remain := n-1, src.Int63(), letterIdMax; i >= 0; {
  210. if remain == 0 {
  211. cache, remain = src.Int63(), letterIdMax
  212. }
  213. if idx := int(cache & letterIdMask); idx < len(letters) {
  214. b[i] = letters[idx]
  215. i--
  216. }
  217. cache >>= letterIdBits
  218. remain--
  219. }
  220. return *(*string)(unsafe.Pointer(&b))
  221. }