projectindex.go 4.9 KB

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