projects.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. package service
  2. import (
  3. "errors"
  4. "fmt"
  5. "go.uber.org/zap"
  6. "sfbase/core"
  7. "sfbase/elastic"
  8. "sfbase/global"
  9. "sfbase/utils"
  10. "sfis/db"
  11. "strconv"
  12. "strings"
  13. "time"
  14. )
  15. var (
  16. pjt_count = 100
  17. pjt_field = `"_id","jgtime","zbtime","projectname","buyer","buyerclass"`
  18. pjt_fields = `"_id","area","city","bidamount","budget","zbtime","jgtime","projectname","projectcode","s_winner","buyer","buyerclass","buyerperson","buyertel","sourceinfourl"`
  19. pjt_sort = `{"jgtime":-1}`
  20. query = `{"query": {"bool": {"must":[%s],"should":[%s],"minimum_should_match": 1}}}`
  21. query_string = `{"constant_score": {"boost": 2,"query": {"match_phrase": {"projectname.pname": {"analyzer": "my_ngram","query": "%s","slop": 6}}}}},{"multi_match": {"query": "%s","fields": ["projectname.pname"],"analyzer": "ik","minimum_should_match": "100%%"}}`
  22. query_winner = `{"term": {"s_winner": "%s"}}`
  23. SR = strings.Replace
  24. HL = `"highlight": {"pre_tags": [""],"post_tags": [""],"fields": {%s}}`
  25. highlightStr = `%s: {"fragment_size": %d,"number_of_fragments": 1}`
  26. )
  27. func ProjectListData(projectName, winner, times string, isDetail bool) (data []map[string]interface{}, httpStatus int, err error) {
  28. times = strings.TrimSpace(times)
  29. winner = strings.TrimSpace(winner)
  30. pjt_name := strings.TrimSpace(projectName)
  31. pjt_len := len([]rune(pjt_name))
  32. httpStatus = 200
  33. qstr := ""
  34. timestr := ""
  35. if times != "" {
  36. start := times + " 00:00:00"
  37. end := times + " 23:59:59"
  38. loc, _ := time.LoadLocation("Local")
  39. startTimes := ""
  40. endTimes := ""
  41. if startTime, err := time.ParseInLocation("2006-01-02 15:04:05", start, loc); err == nil {
  42. startTimes = fmt.Sprint(startTime.Unix())
  43. }
  44. if endTime, err := time.ParseInLocation("2006-01-02 15:04:05", end, loc); err == nil {
  45. endTimes = fmt.Sprint(endTime.Unix())
  46. }
  47. timestr = `{"range": {"jgtime": {"gte": ` + startTimes + `,"lte": ` + endTimes + `}}}`
  48. }
  49. if pjt_len >= 4 && winner == "" {
  50. qstr = fmt.Sprintf(query, "", fmt.Sprintf(query_string, pjt_name, pjt_name))
  51. if timestr != "" {
  52. qstr = fmt.Sprintf(query, timestr, fmt.Sprintf(query_string, pjt_name, pjt_name))
  53. }
  54. } else if pjt_len >= 4 && winner != "" {
  55. qstr = fmt.Sprintf(query, fmt.Sprintf(query_winner, winner), fmt.Sprintf(query_string, pjt_name, pjt_name))
  56. if timestr != "" {
  57. qstr = fmt.Sprintf(query, fmt.Sprintf(query_winner, winner)+","+timestr, fmt.Sprintf(query_string, pjt_name, pjt_name))
  58. }
  59. } else if winner != "" {
  60. qstr = fmt.Sprintf(query, fmt.Sprintf(query_winner, winner), "")
  61. if timestr != "" {
  62. qstr = fmt.Sprintf(query, fmt.Sprintf(query_winner, winner)+","+timestr, "")
  63. }
  64. } else {
  65. err = errors.New("项目名长度小于4")
  66. return
  67. }
  68. INDEX := core.GetStringConf("es.project.index")
  69. TYPE := core.GetStringConf("es.project.itype")
  70. Es := db.GetEs()
  71. fields := ""
  72. if isDetail {
  73. fields = pjt_fields
  74. } else {
  75. fields = pjt_field
  76. }
  77. repl := GetAllByNgram(Es, INDEX, TYPE, qstr, "", pjt_sort, fields, 0, pjt_count, 0, false)
  78. if repl != nil && len(*repl) > 0 {
  79. data = *repl
  80. for _, i := range data {
  81. i["project_id"] = SE.EncodeString(utils.ObjToString(i["_id"]))
  82. delete(i, "_id")
  83. }
  84. }
  85. return
  86. }
  87. func GetAllByNgram(Es *elastic.Elastic, index, itype, qstr, findfields, order, fields string, start, limit, count int, highlight bool) *[]map[string]interface{} {
  88. if qstr != "" {
  89. if highlight {
  90. ws := []string{}
  91. for _, w := range strings.Split(findfields, ",") {
  92. ws = append(ws, fmt.Sprintf(highlightStr, w, count))
  93. }
  94. qstr = qstr[:len(qstr)-1] + `,` + fmt.Sprintf(HL, strings.Join(ws, ",")) + `}`
  95. }
  96. if len(fields) > 0 {
  97. qstr = qstr[:len(qstr)-1] + `,"_source":[` + fields + "]}"
  98. }
  99. if len(order) > 0 {
  100. qstr = qstr[:len(qstr)-1] + `,"sort":[` + SR(SR(SR(SR(order, ",", "},{", -1), " ", "", -1), ":-1", `:"desc"`, -1), ":1", `:"asc"`, -1) + `]}`
  101. }
  102. if start > -1 {
  103. qstr = qstr[:len(qstr)-1] + `,"from":` + strconv.Itoa(start) + `,"size":` + strconv.Itoa(limit) + "}"
  104. }
  105. global.Logger.Info("GetAllByNgram方法es查询",zap.Any("es语句",qstr))
  106. return Es.Get(index, itype, qstr)
  107. } else {
  108. return nil
  109. }
  110. }