biddingdata.go 7.1 KB

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