util.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. package util
  2. import (
  3. "app.yhyue.com/moapp/jybase/common"
  4. elastic "app.yhyue.com/moapp/jybase/es"
  5. "context"
  6. "fmt"
  7. es "github.com/olivere/elastic/v7"
  8. "github.com/shopspring/decimal"
  9. "go.mongodb.org/mongo-driver/bson/primitive"
  10. "log"
  11. "net/http"
  12. "reflect"
  13. "regexp"
  14. "runtime"
  15. "strconv"
  16. "strings"
  17. )
  18. // 数据类型转换
  19. func GetPostForm(r *http.Request) map[string]interface{} {
  20. val := map[string]interface{}{}
  21. for k, _ := range r.Form {
  22. // if len(k) < 2 {
  23. // continue
  24. // }
  25. if k != "_id" {
  26. v := r.FormValue(k)
  27. switch k[:2] {
  28. case "s_": //string型
  29. val[k] = v
  30. case "l_": //int64位
  31. val[k], _ = strconv.ParseInt(v, 10, 64)
  32. case "i_": //int型
  33. val[k], _ = strconv.Atoi(v)
  34. default:
  35. if v == "true" || v == "false" {
  36. b, _ := strconv.ParseBool(v)
  37. val[k] = b
  38. } else {
  39. val[k] = v
  40. }
  41. }
  42. }
  43. }
  44. return val
  45. }
  46. // 从数组中删除元素
  47. func deleteSlice(arr []string, v string) []string {
  48. for k, v1 := range arr {
  49. if v1 == v {
  50. return append(arr[:k], arr[k+1:]...)
  51. }
  52. }
  53. return arr
  54. }
  55. /**
  56. * 前端科学计数法处理
  57. */
  58. func FormatNumber(tmp map[string]interface{}) {
  59. for k, v := range tmp {
  60. if reflect.TypeOf(v).Name() == reflect.Float64.String() {
  61. num, _ := decimal.NewFromString(strconv.FormatFloat(common.Float64All(v), 'e', -1, 64))
  62. tmp[k], _ = num.Float64()
  63. }
  64. }
  65. }
  66. // 获取最新年报
  67. func Sort_year_report(year_report primitive.A) map[string]interface{} {
  68. new_year_report := year_report[0]
  69. for i := 0; i < len(year_report); i++ {
  70. if common.IntAll(new_year_report.(map[string]interface{})["report_year"]) < common.IntAll(year_report[i].(map[string]interface{})["report_year"]) {
  71. new_year_report = year_report[i]
  72. }
  73. }
  74. return new_year_report.(map[string]interface{})
  75. }
  76. func BulkSave(index string, obj *[]map[string]interface{}, isDelBefore bool, e *elastic.EsV7) {
  77. client := e.GetEsConn()
  78. defer e.DestoryEsConn(client)
  79. if client != nil {
  80. defer func() {
  81. if r := recover(); r != nil {
  82. for skip := 1; ; skip++ {
  83. _, file, line, ok := runtime.Caller(skip)
  84. if !ok {
  85. break
  86. }
  87. go log.Printf("%v,%v\n", file, line)
  88. }
  89. }
  90. }()
  91. req := client.Bulk()
  92. for _, v := range *obj {
  93. if v == nil || len(v) == 0 {
  94. continue
  95. }
  96. id := fmt.Sprint(v["id"])
  97. if isDelBefore {
  98. req = req.Add(es.NewBulkDeleteRequest().Index(index).Id(id))
  99. }
  100. req = req.Add(es.NewBulkIndexRequest().Index(index).Id(id).Doc(v))
  101. }
  102. _, err := req.Do(context.TODO())
  103. if err != nil {
  104. log.Println("批量保存到ES出错", err.Error())
  105. }
  106. }
  107. }
  108. func Save(index string, obj map[string]interface{}, e *elastic.EsV7) bool {
  109. client := e.GetEsConn()
  110. defer e.DestoryEsConn(client)
  111. defer func() {
  112. if r := recover(); r != nil {
  113. log.Println("[E]", r)
  114. for skip := 1; ; skip++ {
  115. _, file, line, ok := runtime.Caller(skip)
  116. if !ok {
  117. break
  118. }
  119. go log.Printf("%v,%v\n", file, line)
  120. }
  121. }
  122. }()
  123. _id := fmt.Sprint(obj["id"])
  124. _, err := client.Index().Index(index).Id(_id).BodyJson(obj).Do(context.TODO())
  125. if err != nil {
  126. log.Println("保存到ES出错", err.Error(), obj)
  127. return false
  128. } else {
  129. return true
  130. }
  131. }
  132. func DelBy(index string, query interface{}, e *elastic.EsV7) bool {
  133. client := e.GetEsConn()
  134. defer e.DestoryEsConn(client)
  135. b := false
  136. if client != nil {
  137. defer func() {
  138. if r := recover(); r != nil {
  139. log.Println("[E]", r)
  140. for skip := 1; ; skip++ {
  141. _, file, line, ok := runtime.Caller(skip)
  142. if !ok {
  143. break
  144. }
  145. go log.Printf("%v,%v\n", file, line)
  146. }
  147. }
  148. }()
  149. var err error
  150. if qs, ok := query.(string); ok {
  151. //temp := es.NewQueryStringQuery(qs)
  152. _, err = client.DeleteByQuery().Index(index).QueryString(qs).Do(context.Background())
  153. } else if qi, ok2 := query.(es.Query); ok2 {
  154. _, err = client.DeleteByQuery().Index(index).Query(qi).Do(context.Background())
  155. }
  156. if err != nil {
  157. log.Println("删除索引出错:", err.Error())
  158. } else {
  159. b = true
  160. }
  161. }
  162. return b
  163. }
  164. var FilteReg = regexp.MustCompile("[{}]*")
  165. func GetFieldData(tmp map[string]interface{}, field string) string {
  166. if strings.Contains(field, "procurementlist.") {
  167. text := ""
  168. field = strings.ReplaceAll(field, "procurementlist.", "")
  169. if tmp["procurementlist"] != nil {
  170. for _, pm := range tmp["procurementlist"].([]interface{}) {
  171. pm1 := pm.(map[string]interface{})
  172. text += common.ObjToString(pm1[field])
  173. }
  174. }
  175. return text
  176. } else {
  177. return common.ObjToString(tmp[field])
  178. }
  179. }
  180. // 处理文本
  181. func ProcessData(text string) string {
  182. defer common.Catch()
  183. text = strings.ToUpper(text) //文本中的英文全转为大写
  184. text = FilteReg.ReplaceAllString(text, "") //去除一些特殊符号
  185. return text
  186. }