projectindex.go 6.4 KB

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