biddingdata.go 7.3 KB

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