biddingall.go 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. package main
  2. import (
  3. "log"
  4. qutil "qfw/util"
  5. elastic "qfw/util/elastic"
  6. "strings"
  7. "sync"
  8. "gopkg.in/mgo.v2/bson"
  9. )
  10. //对字段处理 bidamount budget
  11. //招标数据表和抽取表一一对应开始更新
  12. func biddingAllTask(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. index, _ := bidding["index"].(string)
  40. itype, _ := bidding["type"].(string)
  41. count, _ := session.DB(db).C(c).Find(&q).Count()
  42. fields := strings.Split(bidding["fields"].(string), ",")
  43. //线程池
  44. UpdatesLock := sync.Mutex{}
  45. log.Println("查询语句:", q, "同步总数:", count, "elastic库:", index)
  46. //查询招标数据
  47. query := session.DB(db).C(c).Find(q).Select(bson.M{
  48. "projectinfo.attachment": 0,
  49. "contenthtml": 0,
  50. }).Sort("_id").Iter()
  51. //查询抽取结果
  52. extractquery := extractsession.DB(extractdb).C(extractc).Find(q).Sort("_id").Iter()
  53. n := 0
  54. //更新数组
  55. arr := [][]map[string]interface{}{}
  56. arrEs := []map[string]interface{}{}
  57. //对比两张表数据,减少查询次数
  58. var compare bson.M
  59. bnil := false
  60. for tmp := make(map[string]interface{}); query.Next(tmp); n++ {
  61. if deldata := qutil.IntAll(tmp["del"]); deldata == 1 { //临时:重复数据不生索引
  62. continue
  63. }
  64. update := map[string]interface{}{}
  65. del := map[string]interface{}{} //记录extract没有值而bidding中有值的字段
  66. //对比方法----------------
  67. for {
  68. if compare == nil {
  69. compare = make(bson.M)
  70. if !extractquery.Next(compare) {
  71. break
  72. }
  73. }
  74. if compare != nil {
  75. //对比
  76. cid := qutil.BsonIdToSId(compare["_id"])
  77. tid := qutil.BsonIdToSId(tmp["_id"])
  78. if cid == tid {
  79. bnil = false
  80. //更新bidding表,生成索引
  81. for _, k := range fields { //fields更新到mongo的字段
  82. v1 := compare[k] //extract
  83. v2 := tmp[k] //bidding
  84. if v2 == nil && v1 != nil {
  85. update[k] = v1
  86. } else if v2 != nil && v1 != nil {
  87. update[k] = v1
  88. } else if v2 != nil && v1 == nil { //
  89. if k == "s_subscopeclass" && del["subscopeclass"] == nil {
  90. continue
  91. }
  92. del[k] = 1
  93. //qutil.Debug("抽取结果没有值,bidding有值:field--", k, "val--", v2)
  94. }
  95. }
  96. if qutil.IntAll(compare["repeat"]) == 1 {
  97. update["extracttype"] = -1
  98. } else {
  99. update["extracttype"] = 1
  100. }
  101. break
  102. } else {
  103. if cid < tid {
  104. bnil = false
  105. compare = nil
  106. continue
  107. } else {
  108. bnil = true
  109. break
  110. }
  111. }
  112. } else {
  113. bnil = false
  114. break
  115. }
  116. }
  117. //下面可以多线程跑的--->
  118. //处理分类
  119. mpool <- true
  120. go func(tmp, update, compare, del map[string]interface{}, bnil bool) {
  121. defer func() {
  122. <-mpool
  123. }()
  124. if !bnil && compare != nil {
  125. subscopeclass, _ := compare["subscopeclass"].([]interface{})
  126. if subscopeclass != nil {
  127. m1 := map[string]bool{}
  128. newclass := []string{}
  129. for _, sc := range subscopeclass {
  130. sclass, _ := sc.(string)
  131. if !m1[sclass] {
  132. m1[sclass] = true
  133. newclass = append(newclass, sclass)
  134. }
  135. }
  136. update["s_subscopeclass"] = strings.Join(newclass, ",")
  137. update["subscopeclass"] = newclass
  138. }
  139. //处理中标企业
  140. // winner, _ := compare["winner"].(string)
  141. // m1 := map[string]bool{}
  142. // if winner != "" {
  143. // m1[winner] = true
  144. // }
  145. // package1 := compare["package"]
  146. // if package1 != nil {
  147. // packageM, _ := package1.(map[string]interface{})
  148. // for _, p := range packageM {
  149. // pm, _ := p.(map[string]interface{})
  150. // pw, _ := pm["winner"].(string)
  151. // if pw != "" {
  152. // m1[pw] = true
  153. // }
  154. // }
  155. // }
  156. compare = nil
  157. // if len(m1) > 0 {
  158. // //str := ","
  159. // winnerarr := []string{}
  160. // for k, _ := range m1 {
  161. // //str += k + ","
  162. // winnerarr = append(winnerarr, k)
  163. // }
  164. // update["s_winner"] = strings.Join(winnerarr, ",")
  165. // }
  166. }
  167. //------------------对比结束
  168. //同时保存到elastic
  169. for tk, tv := range update {
  170. tmp[tk] = tv
  171. }
  172. //对projectscope字段的索引处理
  173. ps, _ := tmp["projectscope"].(string)
  174. // if ps == "" {
  175. // tmp["projectscope"] = ""
  176. // }
  177. if len(ps) > ESLEN {
  178. tmp["projectscope"] = string(([]rune(ps))[:4000])
  179. }
  180. //预算和中标金额
  181. // if s_budget := fmt.Sprint(tmp["budget"]); s_budget == "" || s_budget == "<nil>" || s_budget == "null" {
  182. // tmp["budget"] = nil
  183. // } else if sbd, ok := tmp["budget"].(string); ok {
  184. // tmp["budget"] = ObjToMoney([]interface{}{sbd, sbd})[0]
  185. // }
  186. // if s_bidamount := fmt.Sprint(tmp["bidamount"]); s_bidamount == "" || s_bidamount == "<nil>" || s_bidamount == "null" {
  187. // tmp["bidamount"] = nil
  188. // } else if sbd, ok := tmp["bidamount"].(string); ok {
  189. // tmp["bidamount"] = ObjToMoney([]interface{}{sbd, sbd})[0]
  190. // }
  191. //go IS.Add("bidding")
  192. UpdatesLock.Lock()
  193. if qutil.IntAll(update["extracttype"]) != -1 {
  194. newTmp := map[string]interface{}{}
  195. for _, v := range biddingIndexFields { //
  196. if tmp[v] != nil && del[v] == nil { //
  197. if "projectinfo" == v {
  198. mp, _ := tmp[v].(map[string]interface{})
  199. if mp != nil {
  200. newmap := map[string]interface{}{}
  201. for _, v1 := range projectinfoFields {
  202. if mp[v1] != nil {
  203. newmap[v1] = mp[v1]
  204. }
  205. }
  206. newTmp[v] = newmap
  207. attachments := mp["attachments"]
  208. con := ""
  209. if attachments != nil {
  210. am, _ := attachments.(map[string]interface{})
  211. if am != nil {
  212. for _, v1 := range am {
  213. vm, _ := v1.(map[string]interface{})
  214. if vm != nil {
  215. c, _ := vm["content"].(string)
  216. con += c
  217. }
  218. }
  219. }
  220. }
  221. con = FilterDetailSpace(con)
  222. if con != "" {
  223. newTmp["attachments"] = con
  224. }
  225. }
  226. } else {
  227. if v == "detail" {
  228. detail, _ := tmp[v].(string)
  229. newTmp[v] = FilterDetail(detail)
  230. } else {
  231. newTmp[v] = tmp[v]
  232. }
  233. }
  234. } /*else if v == "budget" || v == "bidamount" {
  235. newTmp[v] = nil
  236. }*/
  237. }
  238. arrEs = append(arrEs, newTmp)
  239. }
  240. if len(update) > 0 {
  241. queryId := map[string]interface{}{"_id": tmp["_id"]}
  242. set := map[string]interface{}{"$set": update}
  243. if len(del) > 0 { //删除的数据
  244. set["$unset"] = del
  245. }
  246. arr = append(arr, []map[string]interface{}{queryId, set})
  247. }
  248. if len(arr) >= BulkSize {
  249. mgo.UpdateBulkAll(db, c, arr...)
  250. arr = [][]map[string]interface{}{}
  251. }
  252. if len(arrEs) >= BulkSize {
  253. tmps := arrEs
  254. elastic.BulkSave(index, itype, &tmps, true)
  255. if len(multiIndex) == 2 {
  256. elastic.BulkSave(multiIndex[0], multiIndex[1], &tmps, true)
  257. }
  258. arrEs = []map[string]interface{}{}
  259. }
  260. UpdatesLock.Unlock()
  261. }(tmp, update, compare, del, bnil)
  262. if n%1000 == 0 {
  263. log.Println("current:", n, tmp["_id"])
  264. }
  265. tmp = make(map[string]interface{})
  266. }
  267. for i := 0; i < thread; i++ {
  268. mpool <- true
  269. }
  270. UpdatesLock.Lock()
  271. //log.Println(db, c, index, itype, arr, arrEs)
  272. if len(arr) > 0 {
  273. mgo.UpdateBulkAll(db, c, arr...)
  274. }
  275. if len(arrEs) > 0 {
  276. tmps := arrEs
  277. elastic.BulkSave(index, itype, &tmps, true)
  278. if len(multiIndex) == 2 {
  279. elastic.BulkSave(multiIndex[0], multiIndex[1], &tmps, true)
  280. }
  281. }
  282. UpdatesLock.Unlock()
  283. log.Println(mapInfo, "create bidding index...over", n)
  284. }