biddingall.go 7.2 KB

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