biddingdata.go 8.7 KB

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