projects.go 4.2 KB

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