biddingmerge.go 5.8 KB

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