projects.go 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. package service
  2. import (
  3. "errors"
  4. "fmt"
  5. "sfbase/core"
  6. "sfbase/elastic"
  7. "sfbase/utils"
  8. "sfis/db"
  9. "sfis/model"
  10. "log"
  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","package1","list","bidstatus"`
  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(productId int, appid, 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. userProduct := &model.UserProduct{}
  78. db.GetSFISDB().First(userProduct, &model.UserProduct{AppID: appid, ProductID: productId})
  79. repl, err := GetAllByNgram(Es, INDEX, TYPE, qstr, "", pjt_sort, fields, 0, userProduct.DataNumLimitOneTimes, 0, false)
  80. if err != nil {
  81. err = errors.New("查询失败")
  82. return
  83. } else if repl != nil && len(*repl) > 0 {
  84. for _, i := range *repl {
  85. if isDetail {
  86. if i["list"] != nil {
  87. bidStatusStr := "中标、成交、流标、废标、合同"
  88. subStr := "中标、成交、合同"
  89. subStrs := "招标、邀标、询价、竞谈、竞价、单一"
  90. publish := int64(0)
  91. if listArr, ok := i["list"].([]interface{}); ok {
  92. if utils.ObjToString(i["bidstatus"]) == "招标" {
  93. for _, v := range listArr {
  94. if vs, oks := v.(map[string]interface{}); oks {
  95. if strings.Contains(subStrs, utils.ObjToString(vs["subrtype"])) {
  96. publishs := publish
  97. publish = utils.Int64All(vs["publishtime"])
  98. if publishs > publish {
  99. publish = publishs
  100. }
  101. }
  102. }
  103. }
  104. } else if strings.Contains(bidStatusStr, utils.ObjToString(i["bidstatus"])) {
  105. for _, v := range listArr {
  106. if vs, oks := v.(map[string]interface{}); oks {
  107. if strings.Contains(subStr, utils.ObjToString(vs["subrtype"])) {
  108. publishs := publish
  109. publish = utils.Int64All(vs["publishtime"])
  110. if publishs > publish {
  111. publish = publishs
  112. }
  113. }
  114. }
  115. }
  116. }
  117. for _, v := range listArr {
  118. if vs, oks := v.(map[string]interface{}); oks {
  119. publishs := utils.Int64All(vs["publishtime"])
  120. if publishs == publish {
  121. i["url"] = vs["href"]
  122. infoid := utils.ObjToString(vs["infoid"])
  123. i["jyUrl"] = `https://www.jianyu360.com/article/content/` + utils.CommonEncodeArticle("content", infoid) + `.html`
  124. }
  125. }
  126. }
  127. }
  128. }
  129. dataMap := map[string]interface{}{
  130. "projectId": SE.EncodeString(utils.ObjToString(i["_id"])),
  131. "projectName": i["projectname"],
  132. "winner": i["s_winner"],
  133. "projectCode": i["projectcode"],
  134. "buyer": i["buyer"],
  135. "buyerPerson": i["buyerperson"],
  136. "buyerTel": i["buyertel"],
  137. "buyerClass": i["buyerclass"],
  138. "tenderDate": i["zbtime"],
  139. "bidDate": i["jgtime"],
  140. "bidAmount": i["bidamount"],
  141. "budget": i["budget"],
  142. "province": i["area"],
  143. "city": i["city"],
  144. "package": i["package1"],
  145. "url": i["url"],
  146. "jyUrl": i["jyUrl"],
  147. }
  148. data = append(data, dataMap)
  149. } else {
  150. dataMap := map[string]interface{}{
  151. "projectId": SE.EncodeString(utils.ObjToString(i["_id"])),
  152. "projectName": i["projectname"],
  153. "buyer": i["buyer"],
  154. "buyerClass": i["buyerclass"],
  155. "tenderDate": i["zbtime"],
  156. "bidDate": i["jgtime"],
  157. }
  158. data = append(data, dataMap)
  159. }
  160. }
  161. }
  162. return
  163. }
  164. func GetAllByNgram(Es *elastic.Elastic, index, itype, qstr, findfields, order, fields string, start, limit, count int, highlight bool) (*[]map[string]interface{}, error) {
  165. if qstr != "" {
  166. if highlight {
  167. ws := []string{}
  168. for _, w := range strings.Split(findfields, ",") {
  169. ws = append(ws, fmt.Sprintf(highlightStr, w, count))
  170. }
  171. qstr = qstr[:len(qstr)-1] + `,` + fmt.Sprintf(HL, strings.Join(ws, ",")) + `}`
  172. }
  173. if len(fields) > 0 {
  174. qstr = qstr[:len(qstr)-1] + `,"_source":[` + fields + "]}"
  175. }
  176. if len(order) > 0 {
  177. qstr = qstr[:len(qstr)-1] + `,"sort":[` + SR(SR(SR(SR(order, ",", "},{", -1), " ", "", -1), ":-1", `:"desc"`, -1), ":1", `:"asc"`, -1) + `]}`
  178. }
  179. if start > -1 {
  180. qstr = qstr[:len(qstr)-1] + `,"from":` + strconv.Itoa(start) + `,"size":` + strconv.Itoa(limit) + "}"
  181. }
  182. log.Println("es语句", qstr)
  183. return Es.Get(index, itype, qstr)
  184. } else {
  185. return nil, nil
  186. }
  187. }