projects.go 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. package service
  2. import (
  3. "errors"
  4. "fmt"
  5. "sfbase/core"
  6. "sfbase/elastic"
  7. "sfbase/global"
  8. "sfbase/utils"
  9. "sfis/db"
  10. "sfis/model"
  11. "strconv"
  12. "strings"
  13. "time"
  14. "go.uber.org/zap"
  15. )
  16. var (
  17. pjt_count = 100
  18. pjt_field = `"_id","jgtime","zbtime","projectname","buyer","buyerclass"`
  19. pjt_fields = `"_id","area","city","bidamount","budget","zbtime","jgtime","projectname","projectcode","s_winner","buyer","buyerclass","buyerperson","buyertel","package1","list","bidstatus"`
  20. pjt_sort = `{"jgtime":-1}`
  21. query = `{"query": {"bool": {"must":[%s],"should":[%s],"minimum_should_match": 1}}}`
  22. 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%%"}}`
  23. query_winner = `{"term": {"s_winner": "%s"}}`
  24. SR = strings.Replace
  25. HL = `"highlight": {"pre_tags": [""],"post_tags": [""],"fields": {%s}}`
  26. highlightStr = `%s: {"fragment_size": %d,"number_of_fragments": 1}`
  27. )
  28. func ProjectListData(productId int, appid, projectName, winner, times string, isDetail bool) (data []map[string]interface{}, httpStatus int, err error) {
  29. times = strings.TrimSpace(times)
  30. winner = strings.TrimSpace(winner)
  31. pjt_name := strings.TrimSpace(projectName)
  32. pjt_len := len([]rune(pjt_name))
  33. httpStatus = 200
  34. qstr := ""
  35. timestr := ""
  36. if times != "" {
  37. start := times + " 00:00:00"
  38. end := times + " 23:59:59"
  39. loc, _ := time.LoadLocation("Local")
  40. startTimes := ""
  41. endTimes := ""
  42. if startTime, err := time.ParseInLocation("2006-01-02 15:04:05", start, loc); err == nil {
  43. startTimes = fmt.Sprint(startTime.Unix())
  44. }
  45. if endTime, err := time.ParseInLocation("2006-01-02 15:04:05", end, loc); err == nil {
  46. endTimes = fmt.Sprint(endTime.Unix())
  47. }
  48. timestr = `{"range": {"jgtime": {"gte": ` + startTimes + `,"lte": ` + endTimes + `}}}`
  49. }
  50. if pjt_len >= 4 && winner == "" {
  51. qstr = fmt.Sprintf(query, "", fmt.Sprintf(query_string, pjt_name, pjt_name))
  52. if timestr != "" {
  53. qstr = fmt.Sprintf(query, timestr, fmt.Sprintf(query_string, pjt_name, pjt_name))
  54. }
  55. } else if pjt_len >= 4 && winner != "" {
  56. qstr = fmt.Sprintf(query, fmt.Sprintf(query_winner, winner), fmt.Sprintf(query_string, pjt_name, pjt_name))
  57. if timestr != "" {
  58. qstr = fmt.Sprintf(query, fmt.Sprintf(query_winner, winner)+","+timestr, fmt.Sprintf(query_string, pjt_name, pjt_name))
  59. }
  60. } else if winner != "" {
  61. qstr = fmt.Sprintf(query, fmt.Sprintf(query_winner, winner), "")
  62. if timestr != "" {
  63. qstr = fmt.Sprintf(query, fmt.Sprintf(query_winner, winner)+","+timestr, "")
  64. }
  65. } else {
  66. err = errors.New("项目名长度小于4")
  67. return
  68. }
  69. INDEX := core.GetStringConf("es.project.index")
  70. TYPE := core.GetStringConf("es.project.itype")
  71. Es := db.GetEs()
  72. fields := ""
  73. if isDetail {
  74. fields = pjt_fields
  75. } else {
  76. fields = pjt_field
  77. }
  78. userProduct := &model.UserProduct{}
  79. db.GetSFISDB().First(userProduct, &model.UserProduct{AppID: appid, ProductID: productId})
  80. repl, err := GetAllByNgram(Es, INDEX, TYPE, qstr, "", pjt_sort, fields, 0, userProduct.DataNumLimitOneTimes, 0, false)
  81. if err != nil {
  82. err = errors.New("查询失败")
  83. return
  84. } else if repl != nil && len(*repl) > 0 {
  85. for _, i := range *repl {
  86. if isDetail {
  87. if i["list"] != nil {
  88. bidStatusStr := "中标、成交、流标、废标、合同"
  89. subStr := "中标、成交、合同"
  90. subStrs := "招标、邀标、询价、竞谈、竞价、单一"
  91. publish := int64(0)
  92. if listArr, ok := i["list"].([]interface{}); ok {
  93. if utils.ObjToString(i["bidstatus"]) == "招标" {
  94. for _, v := range listArr {
  95. if vs, oks := v.(map[string]interface{}); oks {
  96. if strings.Contains(subStrs, utils.ObjToString(vs["subrtype"])) {
  97. publishs := publish
  98. publish = utils.Int64All(vs["publishtime"])
  99. if publishs > publish {
  100. publish = publishs
  101. }
  102. }
  103. }
  104. }
  105. } else if strings.Contains(bidStatusStr, utils.ObjToString(i["bidstatus"])) {
  106. for _, v := range listArr {
  107. if vs, oks := v.(map[string]interface{}); oks {
  108. if strings.Contains(subStr, utils.ObjToString(vs["subrtype"])) {
  109. publishs := publish
  110. publish = utils.Int64All(vs["publishtime"])
  111. if publishs > publish {
  112. publish = publishs
  113. }
  114. }
  115. }
  116. }
  117. }
  118. for _, v := range listArr {
  119. if vs, oks := v.(map[string]interface{}); oks {
  120. publishs := utils.Int64All(vs["publishtime"])
  121. if publishs == publish {
  122. i["url"] = vs["href"]
  123. infoid := utils.ObjToString(vs["infoid"])
  124. i["jyUrl"] = `https://www.jianyu360.com/article/content/` + utils.CommonEncodeArticle("content", infoid) + `.html`
  125. }
  126. }
  127. }
  128. }
  129. }
  130. dataMap := map[string]interface{}{
  131. "projectId": SE.EncodeString(utils.ObjToString(i["_id"])),
  132. "projectName": i["projectname"],
  133. "winner": i["s_winner"],
  134. "projectCode": i["projectcode"],
  135. "buyer": i["buyer"],
  136. "buyerPerson": i["buyerperson"],
  137. "buyerTel": i["buyertel"],
  138. "buyerClass": i["buyerclass"],
  139. "tenderDate": i["zbtime"],
  140. "bidDate": i["jgtime"],
  141. "bidAmount": i["bidamount"],
  142. "budget": i["budget"],
  143. "province": i["area"],
  144. "city": i["city"],
  145. "package": i["package1"],
  146. "url": i["url"],
  147. "jyUrl": i["jyUrl"],
  148. }
  149. data = append(data, dataMap)
  150. } else {
  151. dataMap := map[string]interface{}{
  152. "projectId": SE.EncodeString(utils.ObjToString(i["_id"])),
  153. "projectName": i["projectname"],
  154. "buyer": i["buyer"],
  155. "buyerClass": i["buyerclass"],
  156. "tenderDate": i["zbtime"],
  157. "bidDate": i["jgtime"],
  158. }
  159. data = append(data, dataMap)
  160. }
  161. }
  162. }
  163. return
  164. }
  165. func GetAllByNgram(Es *elastic.Elastic, index, itype, qstr, findfields, order, fields string, start, limit, count int, highlight bool) (*[]map[string]interface{}, error) {
  166. if qstr != "" {
  167. if highlight {
  168. ws := []string{}
  169. for _, w := range strings.Split(findfields, ",") {
  170. ws = append(ws, fmt.Sprintf(highlightStr, w, count))
  171. }
  172. qstr = qstr[:len(qstr)-1] + `,` + fmt.Sprintf(HL, strings.Join(ws, ",")) + `}`
  173. }
  174. if len(fields) > 0 {
  175. qstr = qstr[:len(qstr)-1] + `,"_source":[` + fields + "]}"
  176. }
  177. if len(order) > 0 {
  178. qstr = qstr[:len(qstr)-1] + `,"sort":[` + SR(SR(SR(SR(order, ",", "},{", -1), " ", "", -1), ":-1", `:"desc"`, -1), ":1", `:"asc"`, -1) + `]}`
  179. }
  180. if start > -1 {
  181. qstr = qstr[:len(qstr)-1] + `,"from":` + strconv.Itoa(start) + `,"size":` + strconv.Itoa(limit) + "}"
  182. }
  183. global.Logger.Info("GetAllByNgram方法es查询", zap.Any("es语句", qstr))
  184. return Es.Get(index, itype, qstr)
  185. } else {
  186. return nil, nil
  187. }
  188. }