biddingall.go 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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. var mpool = make(chan bool, 10)
  14. func biddingAllTask(data []byte, mapInfo map[string]interface{}) {
  15. defer qutil.Catch()
  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. update := map[string]interface{}{}
  62. //对比方法----------------
  63. for {
  64. if compare == nil {
  65. compare = make(bson.M)
  66. if !extractquery.Next(compare) {
  67. break
  68. }
  69. }
  70. if compare != nil {
  71. //对比
  72. cid := qutil.BsonIdToSId(compare["_id"])
  73. tid := qutil.BsonIdToSId(tmp["_id"])
  74. if cid == tid {
  75. bnil = false
  76. //更新bidding表,生成索引
  77. for _, k := range fields {
  78. v1 := compare[k]
  79. v2 := tmp[k]
  80. if v2 == nil && v1 != nil {
  81. update[k] = v1
  82. } else if v2 != nil && v1 != nil {
  83. //update[k+"_b"] = v2
  84. update[k] = v1
  85. } else if v2 != nil && v1 == nil {
  86. //update[k+"_b"] = v2
  87. }
  88. }
  89. if qutil.IntAll(compare["repeat"]) == 1 {
  90. update["extracttype"] = -1
  91. } else if qutil.IntAll(tmp["extracttype"]) == -1 {
  92. update["extracttype"] = 1
  93. }
  94. break
  95. } else {
  96. if cid < tid {
  97. bnil = false
  98. compare = nil
  99. continue
  100. } else {
  101. bnil = true
  102. break
  103. }
  104. }
  105. } else {
  106. bnil = false
  107. break
  108. }
  109. }
  110. //下面可以多线程跑的--->
  111. //处理分类
  112. mpool <- true
  113. go func(tmp, update, compare map[string]interface{}, bnil bool) {
  114. defer func() {
  115. <-mpool
  116. }()
  117. if !bnil && compare != nil {
  118. subscopeclass, _ := compare["subscopeclass"].([]interface{})
  119. if subscopeclass != nil {
  120. //str := ","
  121. m1 := map[string]bool{}
  122. newclass := []string{}
  123. for _, sc := range subscopeclass {
  124. sclass, _ := sc.(string)
  125. if !m1[sclass] {
  126. m1[sclass] = true
  127. //str += sclass + ","
  128. newclass = append(newclass, sclass)
  129. }
  130. }
  131. update["s_subscopeclass"] = strings.Join(newclass, ",")
  132. update["subscopeclass"] = newclass
  133. }
  134. //处理中标企业
  135. winner, _ := compare["winner"].(string)
  136. m1 := map[string]bool{}
  137. if winner != "" {
  138. m1[winner] = true
  139. }
  140. package1 := compare["package"]
  141. if package1 != nil {
  142. packageM, _ := package1.(map[string]interface{})
  143. for _, p := range packageM {
  144. pm, _ := p.(map[string]interface{})
  145. pw, _ := pm["winner"].(string)
  146. if pw != "" {
  147. m1[pw] = true
  148. }
  149. }
  150. }
  151. compare = nil
  152. if len(m1) > 0 {
  153. //str := ","
  154. winnerarr := []string{}
  155. for k, _ := range m1 {
  156. //str += k + ","
  157. winnerarr = append(winnerarr, k)
  158. }
  159. update["s_winner"] = strings.Join(winnerarr, ",")
  160. }
  161. }
  162. //------------------对比结束
  163. //处理key descript
  164. // if bkey == "" {
  165. // DealInfo(&tmp, &update)
  166. // }
  167. //同时保存到elastic
  168. for tk, tv := range update {
  169. tmp[tk] = tv
  170. }
  171. //对projectscope字段的索引处理
  172. ps, _ := tmp["projectscope"].(string)
  173. if ps == "" {
  174. tmp["projectscope"] = "" //= tmp["detail"]
  175. }
  176. if len(ps) > ESLEN {
  177. tmp["projectscope"] = string(([]rune(ps))[:4000])
  178. }
  179. if s_budget := fmt.Sprint(tmp["budget"]); s_budget == "" || s_budget == "<nil>" || s_budget == "null" {
  180. tmp["budget"] = nil
  181. } else if sbd, ok := tmp["budget"].(string); ok {
  182. tmp["budget"] = ObjToMoney([]interface{}{sbd, sbd})[0]
  183. }
  184. if s_bidamount := fmt.Sprint(tmp["bidamount"]); s_bidamount == "" || s_bidamount == "<nil>" || s_bidamount == "null" {
  185. tmp["bidamount"] = nil
  186. } else if sbd, ok := tmp["bidamount"].(string); ok {
  187. tmp["bidamount"] = ObjToMoney([]interface{}{sbd, sbd})[0]
  188. }
  189. // for k1, _ := range tmp {
  190. // if strings.HasSuffix(k1, "_b") || k1 == "contenthtml" {
  191. // delete(tmp, k1)
  192. // }
  193. // }
  194. //go IS.Add("bidding")
  195. UpdatesLock.Lock()
  196. if qutil.IntAll(update["extracttype"]) != -1 {
  197. newTmp := map[string]interface{}{}
  198. for _, v := range biddingIndexFields {
  199. if tmp[v] != nil {
  200. if "projectinfo" == v {
  201. mp, _ := tmp[v].(map[string]interface{})
  202. if mp != nil {
  203. newmap := map[string]interface{}{}
  204. for _, v1 := range projectinfoFields {
  205. if mp[v1] != nil {
  206. newmap[v1] = mp[v1]
  207. }
  208. }
  209. newTmp[v] = newmap
  210. }
  211. } else {
  212. if v == "detail" {
  213. detail, _ := tmp[v].(string)
  214. newTmp[v] = FilterDetail(detail)
  215. } else {
  216. newTmp[v] = tmp[v]
  217. }
  218. }
  219. } else if v == "budget" || v == "bidamount" {
  220. newTmp[v] = nil
  221. }
  222. }
  223. arrEs = append(arrEs, newTmp)
  224. }
  225. if len(update) > 0 {
  226. arr = append(arr, []map[string]interface{}{
  227. map[string]interface{}{
  228. "_id": tmp["_id"],
  229. },
  230. map[string]interface{}{
  231. "$set": update,
  232. },
  233. })
  234. }
  235. if len(arr) >= BulkSize-1 {
  236. mgo.UpdateBulkAll(db, c, arr...)
  237. arr = [][]map[string]interface{}{}
  238. }
  239. if len(arrEs) >= BulkSize-1 {
  240. tmps := arrEs
  241. elastic.BulkSave(index, itype, &tmps, true)
  242. if len(multiIndex) == 2 {
  243. elastic.BulkSave(multiIndex[0], multiIndex[1], &tmps, true)
  244. }
  245. arrEs = []map[string]interface{}{}
  246. }
  247. UpdatesLock.Unlock()
  248. }(tmp, update, compare, bnil)
  249. if n%100 == 0 {
  250. log.Println("current:", n)
  251. }
  252. tmp = make(map[string]interface{})
  253. }
  254. UpdatesLock.Lock()
  255. if len(arr) > 0 {
  256. mgo.UpdateBulkAll(db, c, arr...)
  257. }
  258. if len(arrEs) > 0 {
  259. tmps := arrEs
  260. elastic.BulkSave(index, itype, &tmps, true)
  261. if len(multiIndex) == 2 {
  262. elastic.BulkSave(multiIndex[0], multiIndex[1], &tmps, true)
  263. }
  264. }
  265. UpdatesLock.Unlock()
  266. log.Println(mapInfo, "create bidding index...over", n)
  267. }