projectindex.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. package main
  2. import (
  3. "go.mongodb.org/mongo-driver/bson/primitive"
  4. "math"
  5. "strconv"
  6. util "utils"
  7. "utils/mongodb"
  8. )
  9. func projectTask(data []byte, project, mapInfo map[string]interface{}) {
  10. defer util.Catch()
  11. q, _ := mapInfo["query"].(map[string]interface{})
  12. if q == nil {
  13. q = map[string]interface{}{
  14. "_id": map[string]interface{}{
  15. "$gt": mongodb.StringTOBsonId(mapInfo["gtid"].(string)),
  16. "$lte": mongodb.StringTOBsonId(mapInfo["lteid"].(string)),
  17. },
  18. }
  19. } else {
  20. if q["pici"] == nil {
  21. idMap, _ := q["_id"].(map[string]interface{})
  22. if idMap != nil {
  23. tmpQ := map[string]interface{}{}
  24. for c, id := range idMap {
  25. if idStr, ok := id.(string); ok && id != "" {
  26. tmpQ[c] = mongodb.StringTOBsonId(idStr)
  27. }
  28. }
  29. q["_id"] = tmpQ
  30. }
  31. }
  32. }
  33. conn := projectMgo.GetMgoConn()
  34. defer projectMgo.DestoryMongoConn(conn)
  35. c, _ := project["collect"].(string)
  36. count, _ := conn.DB(projectMgo.DbName).C(c).Find(&q).Count()
  37. util.Debug(c, "查询语句:", q, "同步总数:", count, "elastic库:", q["pici"])
  38. query := conn.DB(projectMgo.DbName).C(c).Find(q).Iter()
  39. n := 0
  40. for tmp := make(map[string]interface{}); query.Next(tmp); n++ {
  41. if n%20000 == 0 {
  42. util.Debug("current---------", n)
  43. }
  44. pp := map[string]map[string]interface{}{}
  45. if packages, ok := tmp["package"].(map[string]interface{}); ok {
  46. for _, pks := range packages {
  47. if pk, ok := pks.([]interface{}); ok {
  48. for _, v := range pk {
  49. if p, ok := v.(map[string]interface{}); ok {
  50. winner := util.ObjToString(p["winner"])
  51. bidamount := util.Float64All((p["bidamount"]))
  52. if len(winner) > 4 && bidamount > 0 {
  53. p := map[string]interface{}{
  54. "winner": winner,
  55. "bidamount": bidamount,
  56. }
  57. pp[winner] = p
  58. }
  59. }
  60. }
  61. }
  62. }
  63. } else {
  64. winner := util.ObjToString(tmp["winner"])
  65. bidamount := util.Float64All(tmp["bidamount"])
  66. if len(winner) > 4 && bidamount > 0 {
  67. p := map[string]interface{}{
  68. "winner": winner,
  69. "bidamount": bidamount,
  70. }
  71. pp[winner] = p
  72. }
  73. }
  74. pk1 := []map[string]interface{}{}
  75. for _, v := range pp {
  76. pk1 = append(pk1, v)
  77. }
  78. if len(pk1) > 0 {
  79. tmp["package1"] = pk1
  80. }
  81. budget := util.Float64All(tmp["budget"])
  82. bidamount := util.Float64All(tmp["bidamount"])
  83. if float64(budget) > 0 && float64(bidamount) > 0 {
  84. rate := float64(1) - float64(bidamount)/float64(budget)
  85. f, _ := strconv.ParseFloat(strconv.FormatFloat(rate, 'f', 4, 64), 64)
  86. //不在0~0.6之间,不生成费率;只生成预算,中标金额舍弃,索引增加折扣率异常标识
  87. if f < 0 || f > 0.6 {
  88. delete(tmp, "bidamount")
  89. tmp["prate_flag"] = 1
  90. } else {
  91. tmp["project_rate"] = f
  92. }
  93. }
  94. if topscopeclass, ok := tmp["topscopeclass"].([]interface{}); ok {
  95. tc := []string{}
  96. m2 := map[string]bool{}
  97. for _, v := range topscopeclass {
  98. str := util.ObjToString(v)
  99. str = reg_letter.ReplaceAllString(str, "") // 去除字母
  100. if !m2[str] {
  101. m2[str] = true
  102. tc = append(tc, str)
  103. }
  104. }
  105. tmp["topscopeclass"] = tc
  106. }
  107. //不生索引字段
  108. delete(tmp, "package")
  109. delete(tmp, "infofield")
  110. bidopentime := util.Int64All(tmp["bidopentime"]) //开标日期
  111. fzb_publishtime := int64(0) //记录第一个招标信息的publishtime
  112. bidcycle_flag := false //判断是否已计算出标书表编制周期
  113. list := []interface{}(tmp["list"].(primitive.A))
  114. for _, m := range list {
  115. tmpM := m.(map[string]interface{})
  116. //删除purchasing,review_experts
  117. delete(tmpM, "purchasing")
  118. delete(tmpM, "review_experts")
  119. if bidamount, ok := tmpM["bidamount"].(string); ok && len(bidamount) > 0 { //bidamount为string类型,转成float
  120. tmpB := util.Float64All(tmpM["bidamount"])
  121. tmpM["bidamount"] = tmpB
  122. }
  123. //projectscope截断
  124. listProjectscopeRune := []rune(util.ObjToString(tmpM["projectscope"]))
  125. if len(listProjectscopeRune) > 1000 {
  126. tmpM["projectscope"] = string(listProjectscopeRune[:1000])
  127. }
  128. //计算bidcycle标书表编制周期字段
  129. if !bidcycle_flag && bidopentime > 0 { //bidopentime>0证明list中有bidopentime,无则不用计算bidcycle
  130. if toptype := util.ObjToString(tmpM["toptype"]); toptype == "招标" {
  131. zb_bidopentime := util.Int64All(tmpM["bidopentime"])
  132. zb_publishtime := util.Int64All(tmpM["publishtime"])
  133. if zb_publishtime > 0 {
  134. if zb_bidopentime > 0 {
  135. if tmpTime := zb_bidopentime - zb_publishtime; tmpTime > 0 {
  136. f_day := float64(tmpTime) / float64(86400)
  137. day := math.Ceil(f_day)
  138. tmp["bidcycle"] = int(day)
  139. bidcycle_flag = true
  140. }
  141. }
  142. if fzb_publishtime == 0 { //仅赋值第一个招标信息的publishtime
  143. fzb_publishtime = zb_publishtime
  144. }
  145. }
  146. }
  147. }
  148. }
  149. //计算bidcycle标书表编制周期字段
  150. //list中招标信息中未能计算出bidcycle,用第一个招标信息的fzb_publishtime和外围bidopentime计算
  151. if !bidcycle_flag && bidopentime > 0 && fzb_publishtime > 0 {
  152. if tmpTime := bidopentime - fzb_publishtime; tmpTime > 0 {
  153. f_day := float64(tmpTime) / float64(86400)
  154. day := math.Ceil(f_day)
  155. tmp["bidcycle"] = int(day)
  156. }
  157. }
  158. //projectscope截断
  159. projectscopeRune := []rune(util.ObjToString(tmp["projectscope"]))
  160. if len(projectscopeRune) > 1000 {
  161. tmp["projectscope"] = string(projectscopeRune[:1000])
  162. }
  163. // if s_budget := fmt.Sprint(tmp["budget"]); s_budget == "" || s_budget == "<nil>" || s_budget == "null" {
  164. // tmp["budget"] = nil
  165. // }
  166. // if s_bidamount := fmt.Sprint(tmp["bidamount"]); s_bidamount == "" || s_bidamount == "<nil>" || s_bidamount == "null" {
  167. // tmp["bidamount"] = nil
  168. // }
  169. //go IS.Add("project")
  170. if tmp["bidamount"] != nil && util.Float64All(tmp["bidamount"]) > 1000000000 {
  171. delete(tmp, "bidamount")
  172. }
  173. if tmp["budget"] != nil && util.Float64All(tmp["budget"]) > 1000000000 {
  174. delete(tmp, "budget")
  175. }
  176. tmp["s_projectname"] = tmp["projectname"]
  177. saveProjectEsPool <- tmp
  178. tmp = make(map[string]interface{})
  179. }
  180. util.Debug(mapInfo, "create project index...over", n)
  181. }