biddingdata.go 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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. var indexfield = []string{
  13. "_id",
  14. "s_winner",
  15. "winner",
  16. "buyerclass",
  17. "title",
  18. "detail",
  19. "area",
  20. "site",
  21. "bidopendate",
  22. "bidopentime",
  23. "buyer",
  24. "city",
  25. "comeintime",
  26. "href",
  27. "infoformat",
  28. "projectcode",
  29. "projectname",
  30. "publishtime",
  31. "s_sha",
  32. "spidercode",
  33. "subtype",
  34. "toptype",
  35. "agency",
  36. "budget",
  37. "bidamount",
  38. "s_subscopeclass",
  39. "projectscope",
  40. "bidstatus",
  41. "projectinfo",
  42. "buyertel",
  43. "buyerperson",
  44. "projectid",
  45. "buyerclass",
  46. "district",
  47. "topscopeclass",
  48. "attachments",
  49. }
  50. //招标数据表和抽取表一一对应开始更新
  51. func biddingDataTask(data []byte, mapInfo map[string]interface{}) {
  52. defer qutil.Catch()
  53. thread := 40
  54. var mpool = make(chan bool, thread)
  55. q, _ := mapInfo["query"].(map[string]interface{})
  56. if q == nil {
  57. q = map[string]interface{}{
  58. "_id": bson.M{
  59. "$gt": qutil.StringTOBsonId(mapInfo["gtid"].(string)),
  60. "$lte": qutil.StringTOBsonId(mapInfo["lteid"].(string)),
  61. },
  62. }
  63. }
  64. //bidding库
  65. session := mgo.GetMgoConn()
  66. defer mgo.DestoryMongoConn(session)
  67. //extract库
  68. extractsession := extractmgo.GetMgoConn()
  69. defer extractmgo.DestoryMongoConn(extractsession)
  70. //连接信息
  71. c, _ := mapInfo["coll"].(string)
  72. if c == "" {
  73. c, _ = bidding["collect"].(string)
  74. }
  75. extractc, _ := bidding["extractcollect"].(string)
  76. db, _ := bidding["db"].(string)
  77. extractdb, _ := bidding["extractdb"].(string)
  78. index, _ := bidding["index"].(string)
  79. itype, _ := bidding["type"].(string)
  80. count, _ := session.DB(db).C(c).Find(&q).Count()
  81. fields := strings.Split(bidding["fields"].(string), ",")
  82. //线程池
  83. UpdatesLock := sync.Mutex{}
  84. log.Println("查询语句:", q, "同步总数:", count, "elastic库:", index)
  85. //查询招标数据
  86. query := session.DB(db).C(c).Find(q).Select(bson.M{
  87. "projectinfo.attachment": 0,
  88. "contenthtml": 0,
  89. }).Sort("_id").Iter()
  90. //查询抽取结果
  91. extractquery := extractsession.DB(extractdb).C(extractc).Find(q).Sort("_id").Iter()
  92. n := 0
  93. //更新数组
  94. arrEs := []map[string]interface{}{}
  95. //对比两张表数据,减少查询次数
  96. var compare bson.M
  97. bnil := false
  98. for tmp := make(map[string]interface{}); query.Next(tmp); n++ {
  99. update := map[string]interface{}{}
  100. //对比方法----------------
  101. for {
  102. if compare == nil {
  103. compare = make(bson.M)
  104. if !extractquery.Next(compare) {
  105. break
  106. }
  107. }
  108. if compare != nil {
  109. //对比
  110. cid := qutil.BsonIdToSId(compare["_id"])
  111. tid := qutil.BsonIdToSId(tmp["_id"])
  112. if cid == tid {
  113. bnil = false
  114. //更新bidding表,生成索引
  115. for _, k := range fields {
  116. v1 := compare[k]
  117. v2 := tmp[k]
  118. if v2 == nil && v1 != nil {
  119. update[k] = v1
  120. } else if v2 != nil && v1 != nil {
  121. //update[k+"_b"] = v2
  122. update[k] = v1
  123. } else if v2 != nil && v1 == nil {
  124. update[k] = v2
  125. }
  126. }
  127. if qutil.IntAll(compare["repeat"]) == 1 {
  128. update["extracttype"] = -1
  129. //} else if qutil.IntAll(tmp["extracttype"]) == -1 {
  130. } else {
  131. update["extracttype"] = 1
  132. }
  133. break
  134. } else {
  135. if cid < tid {
  136. bnil = false
  137. compare = nil
  138. continue
  139. } else {
  140. bnil = true
  141. break
  142. }
  143. }
  144. } else {
  145. bnil = false
  146. break
  147. }
  148. }
  149. //下面可以多线程跑的--->
  150. //处理分类
  151. mpool <- true
  152. go func(tmp, update, compare map[string]interface{}, bnil bool) {
  153. defer func() {
  154. <-mpool
  155. }()
  156. if !bnil && compare != nil {
  157. subscopeclass, _ := compare["subscopeclass"].([]interface{})
  158. if subscopeclass != nil {
  159. //str := ","
  160. m1 := map[string]bool{}
  161. newclass := []string{}
  162. for _, sc := range subscopeclass {
  163. sclass, _ := sc.(string)
  164. if !m1[sclass] {
  165. m1[sclass] = true
  166. //str += sclass + ","
  167. newclass = append(newclass, sclass)
  168. }
  169. }
  170. update["s_subscopeclass"] = strings.Join(newclass, ",")
  171. update["subscopeclass"] = newclass
  172. }
  173. //处理中标企业
  174. // winner, _ := compare["winner"].(string)
  175. // m1 := map[string]bool{}
  176. // if winner != "" {
  177. // m1[winner] = true
  178. // }
  179. // package1 := compare["package"]
  180. // if package1 != nil {
  181. // packageM, _ := package1.(map[string]interface{})
  182. // for _, p := range packageM {
  183. // pm, _ := p.(map[string]interface{})
  184. // pw, _ := pm["winner"].(string)
  185. // if pw != "" {
  186. // m1[pw] = true
  187. // }
  188. // }
  189. // }
  190. compare = nil
  191. // if len(m1) > 0 {
  192. // //str := ","
  193. // winnerarr := []string{}
  194. // for k, _ := range m1 {
  195. // //str += k + ","
  196. // winnerarr = append(winnerarr, k)
  197. // }
  198. // update["s_winner"] = strings.Join(winnerarr, ",")
  199. // }
  200. }
  201. //------------------对比结束
  202. //同时保存到elastic
  203. for tk, tv := range update {
  204. tmp[tk] = tv
  205. }
  206. //对projectscope字段的索引处理
  207. ps, _ := tmp["projectscope"].(string)
  208. if ps == "" {
  209. tmp["projectscope"] = "" //= tmp["detail"]
  210. }
  211. if len(ps) > ESLEN {
  212. tmp["projectscope"] = string(([]rune(ps))[:4000])
  213. }
  214. // if s_budget := fmt.Sprint(tmp["budget"]); s_budget == "" || s_budget == "<nil>" || s_budget == "null" {
  215. // tmp["budget"] = nil
  216. // } else if sbd, ok := tmp["budget"].(string); ok {
  217. // tmp["budget"] = ObjToMoney([]interface{}{sbd, sbd})[0]
  218. // }
  219. // if s_bidamount := fmt.Sprint(tmp["bidamount"]); s_bidamount == "" || s_bidamount == "<nil>" || s_bidamount == "null" {
  220. // tmp["bidamount"] = nil
  221. // } else if sbd, ok := tmp["bidamount"].(string); ok {
  222. // tmp["bidamount"] = ObjToMoney([]interface{}{sbd, sbd})[0]
  223. // }
  224. if qutil.IntAll(update["extracttype"]) != -1 {
  225. newTmp := map[string]interface{}{}
  226. for _, v := range indexfield {
  227. if tmp[v] != nil {
  228. if "projectinfo" == v {
  229. mp, _ := tmp[v].(map[string]interface{})
  230. if mp != nil {
  231. newmap := map[string]interface{}{}
  232. for _, v1 := range projectinfoFields {
  233. if mp[v1] != nil {
  234. newmap[v1] = mp[v1]
  235. }
  236. }
  237. newTmp[v] = newmap
  238. // attachments := mp["attachments"]
  239. // con := ""
  240. // if attachments != nil {
  241. // am, _ := attachments.(map[string]interface{})
  242. // if am != nil {
  243. // for _, v1 := range am {
  244. // vm, _ := v1.(map[string]interface{})
  245. // if vm != nil {
  246. // c, _ := vm["content"].(string)
  247. // con += c
  248. // }
  249. // }
  250. // }
  251. // }
  252. // if con != "" {
  253. // con = FilterDetailSpace(con)
  254. // newTmp["attachments"] = con
  255. // }
  256. }
  257. } else {
  258. if v == "detail" {
  259. detail, _ := tmp[v].(string)
  260. newTmp[v] = FilterDetail(detail)
  261. } else {
  262. newTmp[v] = tmp[v]
  263. }
  264. }
  265. } else if v == "budget" || v == "bidamount" {
  266. newTmp[v] = nil
  267. }
  268. }
  269. UpdatesLock.Lock()
  270. arrEs = append(arrEs, newTmp)
  271. UpdatesLock.Unlock()
  272. }
  273. UpdatesLock.Lock()
  274. if len(arrEs) >= BulkSize-1 {
  275. tmps := arrEs
  276. elastic.BulkSave(index, itype, &tmps, false)
  277. arrEs = []map[string]interface{}{}
  278. }
  279. UpdatesLock.Unlock()
  280. }(tmp, update, compare, bnil)
  281. if n%1000 == 0 {
  282. log.Println("current:", n)
  283. }
  284. tmp = make(map[string]interface{})
  285. }
  286. for i := 0; i < thread; i++ {
  287. mpool <- true
  288. }
  289. UpdatesLock.Lock()
  290. if len(arrEs) > 0 {
  291. tmps := arrEs
  292. log.Println(tmps[0])
  293. elastic.BulkSave(index, itype, &tmps, false)
  294. }
  295. UpdatesLock.Unlock()
  296. log.Println(mapInfo, "create bidding index...over", n)
  297. }