biddingmerge.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. package main
  2. import (
  3. "fmt"
  4. "log"
  5. qutil "qfw/util"
  6. "strings"
  7. "sync"
  8. "gopkg.in/mgo.v2/bson"
  9. )
  10. //对字段处理 bidamount budget
  11. //招标数据表和抽取表一一对应开始更新
  12. func biddingMergeTask(data []byte, mapInfo map[string]interface{}) {
  13. defer qutil.Catch()
  14. thread := 40
  15. var mpool = make(chan bool, thread)
  16. q, _ := mapInfo["query"].(map[string]interface{})
  17. if q == nil {
  18. q = map[string]interface{}{
  19. "_id": bson.M{
  20. "$gt": qutil.StringTOBsonId(mapInfo["gtid"].(string)),
  21. "$lte": qutil.StringTOBsonId(mapInfo["lteid"].(string)),
  22. },
  23. }
  24. }
  25. //bidding库
  26. session := mgo.GetMgoConn()
  27. defer mgo.DestoryMongoConn(session)
  28. //extract库
  29. extractsession := extractmgo.GetMgoConn()
  30. defer extractmgo.DestoryMongoConn(extractsession)
  31. //连接信息
  32. c, _ := mapInfo["coll"].(string)
  33. if c == "" {
  34. c, _ = bidding["collect"].(string)
  35. }
  36. extractc, _ := bidding["extractcollect"].(string)
  37. db, _ := bidding["db"].(string)
  38. extractdb, _ := bidding["extractdb"].(string)
  39. count, _ := session.DB(db).C(c).Find(&q).Count()
  40. fields := strings.Split(bidding["fields"].(string), ",")
  41. //线程池
  42. UpdatesLock := sync.Mutex{}
  43. log.Println("查询语句:", q, "同步总数:", count)
  44. //查询招标数据
  45. query := session.DB(db).C(c).Find(q).Select(bson.M{
  46. "projectinfo.attachment": 0,
  47. "contenthtml": 0,
  48. }).Sort("_id").Iter()
  49. //查询抽取结果
  50. extractquery := extractsession.DB(extractdb).C(extractc).Find(q).Sort("_id").Iter()
  51. n := 0
  52. //更新数组
  53. arr := [][]map[string]interface{}{}
  54. //对比两张表数据,减少查询次数
  55. var compare bson.M
  56. bnil := false
  57. for tmp := make(map[string]interface{}); query.Next(tmp); n++ {
  58. update := map[string]interface{}{}
  59. //对比方法----------------
  60. for {
  61. if compare == nil {
  62. compare = make(bson.M)
  63. if !extractquery.Next(compare) {
  64. break
  65. }
  66. }
  67. if compare != nil {
  68. //对比
  69. cid := qutil.BsonIdToSId(compare["_id"])
  70. tid := qutil.BsonIdToSId(tmp["_id"])
  71. if cid == tid {
  72. bnil = false
  73. //更新bidding表,生成索引
  74. for _, k := range fields {
  75. v1 := compare[k]
  76. v2 := tmp[k]
  77. if v2 == nil && v1 != nil {
  78. update[k] = v1
  79. } else if v2 != nil && v1 != nil {
  80. //update[k+"_b"] = v2
  81. update[k] = v1
  82. } else if v2 != nil && v1 == nil {
  83. //update[k+"_b"] = v2
  84. }
  85. }
  86. if qutil.IntAll(compare["repeat"]) == 1 {
  87. update["extracttype"] = -1
  88. //} else if qutil.IntAll(tmp["extracttype"]) == -1 {
  89. } else {
  90. update["extracttype"] = 1
  91. }
  92. break
  93. } else {
  94. if cid < tid {
  95. bnil = false
  96. compare = nil
  97. continue
  98. } else {
  99. bnil = true
  100. break
  101. }
  102. }
  103. } else {
  104. bnil = false
  105. break
  106. }
  107. }
  108. //下面可以多线程跑的--->
  109. //处理分类
  110. mpool <- true
  111. go func(tmp, update, compare map[string]interface{}, bnil bool) {
  112. defer func() {
  113. <-mpool
  114. }()
  115. if !bnil && compare != nil {
  116. subscopeclass, _ := compare["subscopeclass"].([]interface{})
  117. if subscopeclass != nil {
  118. //str := ","
  119. m1 := map[string]bool{}
  120. newclass := []string{}
  121. for _, sc := range subscopeclass {
  122. sclass, _ := sc.(string)
  123. if !m1[sclass] {
  124. m1[sclass] = true
  125. //str += sclass + ","
  126. newclass = append(newclass, sclass)
  127. }
  128. }
  129. update["s_subscopeclass"] = strings.Join(newclass, ",")
  130. update["subscopeclass"] = newclass
  131. }
  132. //处理中标企业
  133. winner, _ := compare["winner"].(string)
  134. m1 := map[string]bool{}
  135. if winner != "" {
  136. m1[winner] = true
  137. }
  138. package1 := compare["package"]
  139. if package1 != nil {
  140. packageM, _ := package1.(map[string]interface{})
  141. for _, p := range packageM {
  142. pm, _ := p.(map[string]interface{})
  143. pw, _ := pm["winner"].(string)
  144. if pw != "" {
  145. m1[pw] = true
  146. }
  147. }
  148. }
  149. compare = nil
  150. if len(m1) > 0 {
  151. //str := ","
  152. winnerarr := []string{}
  153. for k, _ := range m1 {
  154. //str += k + ","
  155. winnerarr = append(winnerarr, k)
  156. }
  157. update["s_winner"] = strings.Join(winnerarr, ",")
  158. }
  159. }
  160. //------------------对比结束
  161. //处理key descript
  162. // if bkey == "" {
  163. // DealInfo(&tmp, &update)
  164. // }
  165. //同时保存到elastic
  166. for tk, tv := range update {
  167. tmp[tk] = tv
  168. }
  169. //对projectscope字段的索引处理
  170. ps, _ := tmp["projectscope"].(string)
  171. if ps == "" {
  172. tmp["projectscope"] = "" //= tmp["detail"]
  173. }
  174. if len(ps) > ESLEN {
  175. tmp["projectscope"] = string(([]rune(ps))[:4000])
  176. }
  177. if s_budget := fmt.Sprint(tmp["budget"]); s_budget == "" || s_budget == "<nil>" || s_budget == "null" {
  178. tmp["budget"] = nil
  179. } else if sbd, ok := tmp["budget"].(string); ok {
  180. tmp["budget"] = ObjToMoney([]interface{}{sbd, sbd})[0]
  181. }
  182. if s_bidamount := fmt.Sprint(tmp["bidamount"]); s_bidamount == "" || s_bidamount == "<nil>" || s_bidamount == "null" {
  183. tmp["bidamount"] = nil
  184. } else if sbd, ok := tmp["bidamount"].(string); ok {
  185. tmp["bidamount"] = ObjToMoney([]interface{}{sbd, sbd})[0]
  186. }
  187. // for k1, _ := range tmp {
  188. // if strings.HasSuffix(k1, "_b") || k1 == "contenthtml" {
  189. // delete(tmp, k1)
  190. // }
  191. // }
  192. //go IS.Add("bidding")
  193. UpdatesLock.Lock()
  194. if len(update) > 0 {
  195. arr = append(arr, []map[string]interface{}{
  196. map[string]interface{}{
  197. "_id": tmp["_id"],
  198. },
  199. map[string]interface{}{
  200. "$set": update,
  201. },
  202. })
  203. }
  204. if len(arr) >= BulkSize-1 {
  205. mgo.UpdateBulkAll(db, c, arr...)
  206. arr = [][]map[string]interface{}{}
  207. }
  208. UpdatesLock.Unlock()
  209. }(tmp, update, compare, bnil)
  210. if n%100 == 0 {
  211. log.Println("current:", n)
  212. }
  213. tmp = make(map[string]interface{})
  214. }
  215. for i := 0; i < thread; i++ {
  216. mpool <- true
  217. }
  218. UpdatesLock.Lock()
  219. if len(arr) > 0 {
  220. mgo.UpdateBulkAll(db, c, arr...)
  221. }
  222. UpdatesLock.Unlock()
  223. log.Println(mapInfo, "merge bidding...over", n)
  224. }