utils.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. package main
  2. import (
  3. "app.yhyue.com/data_processing/common_utils/mongodb"
  4. "math"
  5. "sort"
  6. )
  7. //convertToMongoID convertToMongoID
  8. func convertToMongoID(query map[string]interface{}) map[string]interface{} {
  9. result := make(map[string]interface{})
  10. if query == nil {
  11. return result
  12. }
  13. idMap := query["_id"].(map[string]interface{})
  14. if idMap != nil {
  15. tmpQ := map[string]interface{}{}
  16. for c, id := range idMap {
  17. if idStr, ok := id.(string); ok && id != "" {
  18. tmpQ[c] = mongodb.StringTOBsonId(idStr)
  19. }
  20. }
  21. result["_id"] = tmpQ
  22. }
  23. return result
  24. }
  25. //StringSliceValuesEqual 判断切片相等
  26. func StringSliceValuesEqual(a, b []string) bool {
  27. if len(a) != len(b) {
  28. return false
  29. }
  30. sort.Strings(a)
  31. sort.Strings(b)
  32. for i := range a {
  33. if a[i] != b[i] {
  34. return false
  35. }
  36. }
  37. return true
  38. }
  39. //Float64SliceSum float64 数据求和
  40. func Float64SliceSum(nums []float64) float64 {
  41. sum := 0.0
  42. for _, num := range nums {
  43. sum += num
  44. }
  45. return sum
  46. }
  47. func Float64Equal1Precision(a, b float64) bool {
  48. return int(math.Round(a*10)) == int(math.Round(b*10))
  49. }
  50. //chargeType 判断mongo 字段类型和 es 字段类型相匹配
  51. func chargeType(ftype, etype string) bool {
  52. if ftype != "" {
  53. switch ftype {
  54. case "string":
  55. if etype == "keyword" || etype == "text" {
  56. return true
  57. } else {
  58. return false
  59. }
  60. case "bool":
  61. if etype == "boolean" {
  62. return true
  63. } else {
  64. return false
  65. }
  66. case "int64", "int32", "int":
  67. if etype == "long" || etype == "integer" {
  68. return true
  69. } else {
  70. return false
  71. }
  72. case "float64", "float32":
  73. if etype == "double" || etype == "float" {
  74. return true
  75. } else {
  76. return false
  77. }
  78. }
  79. }
  80. return false
  81. }