biddingdata.go 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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] //extract
  117. v2 := tmp[k] //bidding
  118. if v2 == nil && v1 != nil {
  119. update[k] = v1
  120. } else if v2 != nil && v1 != nil {
  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. m1 := map[string]bool{}
  159. newclass := []string{}
  160. for _, sc := range subscopeclass {
  161. sclass, _ := sc.(string)
  162. if !m1[sclass] {
  163. m1[sclass] = true
  164. newclass = append(newclass, sclass)
  165. }
  166. }
  167. update["s_subscopeclass"] = strings.Join(newclass, ",")
  168. update["subscopeclass"] = newclass
  169. }
  170. //处理中标企业
  171. // winner, _ := compare["winner"].(string)
  172. // m1 := map[string]bool{}
  173. // if winner != "" {
  174. // m1[winner] = true
  175. // }
  176. // package1 := compare["package"]
  177. // if package1 != nil {
  178. // packageM, _ := package1.(map[string]interface{})
  179. // for _, p := range packageM {
  180. // pm, _ := p.(map[string]interface{})
  181. // pw, _ := pm["winner"].(string)
  182. // if pw != "" {
  183. // m1[pw] = true
  184. // }
  185. // }
  186. // }
  187. compare = nil
  188. // if len(m1) > 0 {
  189. // //str := ","
  190. // winnerarr := []string{}
  191. // for k, _ := range m1 {
  192. // //str += k + ","
  193. // winnerarr = append(winnerarr, k)
  194. // }
  195. // update["s_winner"] = strings.Join(winnerarr, ",")
  196. // }
  197. }
  198. //------------------对比结束
  199. //同时保存到elastic
  200. for tk, tv := range update {
  201. tmp[tk] = tv
  202. }
  203. if tmp["supervisorrate"] != nil { //临时处理supervisorrate抽取类型为string不生索引
  204. if _, ok := tmp["supervisorrate"].(string); ok { //supervisorrate数据为string类型
  205. delete(tmp, "supervisorrate")
  206. }
  207. }
  208. //对projectscope字段的索引处理
  209. ps, _ := tmp["projectscope"].(string)
  210. if len(ps) > ESLEN {
  211. tmp["projectscope"] = string(([]rune(ps))[:4000])
  212. }
  213. //对标的物为空处理
  214. if filetext := getFileText(tmp); len(filetext) > 0 { //attach_text
  215. tmp["filetext"] = filetext
  216. }
  217. if purchasing, ok := tmp["purchasing"].(string); ok && purchasing == "" {
  218. delete(tmp, "purchasing")
  219. }
  220. if purchasinglist, ok := tmp["purchasinglist"].([]interface{}); ok && len(purchasinglist) == 0 {
  221. delete(tmp, "purchasinglist")
  222. }
  223. // if s_budget := fmt.Sprint(tmp["budget"]); s_budget == "" || s_budget == "<nil>" || s_budget == "null" {
  224. // tmp["budget"] = nil
  225. // } else if sbd, ok := tmp["budget"].(string); ok {
  226. // tmp["budget"] = ObjToMoney([]interface{}{sbd, sbd})[0]
  227. // }
  228. // if s_bidamount := fmt.Sprint(tmp["bidamount"]); s_bidamount == "" || s_bidamount == "<nil>" || s_bidamount == "null" {
  229. // tmp["bidamount"] = nil
  230. // } else if sbd, ok := tmp["bidamount"].(string); ok {
  231. // tmp["bidamount"] = ObjToMoney([]interface{}{sbd, sbd})[0]
  232. // }
  233. if qutil.IntAll(update["extracttype"]) != -1 {
  234. newTmp := map[string]interface{}{}
  235. for _, v := range biddingIndexFields { // indexfield
  236. if tmp[v] != nil {
  237. if "projectinfo" == v {
  238. mp, _ := tmp[v].(map[string]interface{})
  239. if mp != nil {
  240. newmap := map[string]interface{}{}
  241. for _, v1 := range projectinfoFields {
  242. if mp[v1] != nil {
  243. newmap[v1] = fmt.Sprint(mp[v1])
  244. }
  245. }
  246. if len(newmap) > 0 {
  247. newTmp[v] = newmap
  248. }
  249. // attachments := mp["attachments"]
  250. // con := ""
  251. // if attachments != nil {
  252. // am, _ := attachments.(map[string]interface{})
  253. // if am != nil {
  254. // for _, v1 := range am {
  255. // vm, _ := v1.(map[string]interface{})
  256. // if vm != nil {
  257. // c, _ := vm["content"].(string)
  258. // con += c
  259. // }
  260. // }
  261. // }
  262. // }
  263. // if con != "" {
  264. // con = FilterDetailSpace(con)
  265. // newTmp["attachments"] = con
  266. // }
  267. }
  268. } else if v == "purchasinglist" { //标的物处理
  269. purchasinglist_new := []map[string]interface{}{}
  270. if pcl, _ := tmp[v].([]interface{}); len(pcl) > 0 {
  271. for _, ls := range pcl {
  272. lsm_new := make(map[string]interface{})
  273. lsm := ls.(map[string]interface{})
  274. for _, pf := range purchasinglistFields {
  275. if lsm[pf] != nil {
  276. lsm_new[pf] = lsm[pf]
  277. }
  278. }
  279. if lsm_new != nil && len(lsm_new) > 0 {
  280. purchasinglist_new = append(purchasinglist_new, lsm_new)
  281. }
  282. }
  283. }
  284. if len(purchasinglist_new) > 0 {
  285. newTmp[v] = purchasinglist_new
  286. }
  287. } else {
  288. if v == "detail" {
  289. detail, _ := tmp[v].(string)
  290. newTmp[v] = FilterDetail(detail)
  291. } else {
  292. newTmp[v] = tmp[v]
  293. }
  294. }
  295. }
  296. }
  297. UpdatesLock.Lock()
  298. arrEs = append(arrEs, newTmp)
  299. UpdatesLock.Unlock()
  300. }
  301. UpdatesLock.Lock()
  302. if len(arrEs) >= BulkSize-1 {
  303. tmps := arrEs
  304. elastic.BulkSave(index, itype, &tmps, true)
  305. arrEs = []map[string]interface{}{}
  306. }
  307. UpdatesLock.Unlock()
  308. }(tmp, update, compare, bnil)
  309. if n%1000 == 0 {
  310. log.Println("current:", n)
  311. }
  312. tmp = make(map[string]interface{})
  313. }
  314. for i := 0; i < thread; i++ {
  315. mpool <- true
  316. }
  317. UpdatesLock.Lock()
  318. if len(arrEs) > 0 {
  319. tmps := arrEs
  320. elastic.BulkSave(index, itype, &tmps, true)
  321. }
  322. UpdatesLock.Unlock()
  323. log.Println(mapInfo, "create bidding index...over", n)
  324. }