biddingmerge.go 5.6 KB

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