biddingall.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  1. package main
  2. import (
  3. "mongodb"
  4. "regexp"
  5. "time"
  6. //"fmt"
  7. "log"
  8. qutil "qfw/util"
  9. elastic "qfw/util/elastic"
  10. "reflect"
  11. "strings"
  12. "sync"
  13. "gopkg.in/mgo.v2/bson"
  14. )
  15. //对字段处理 bidamount budget
  16. //招标数据表和抽取表一一对应开始更新
  17. /*
  18. 注意:
  19. 1、biddingall任务跑历史数据生成索引并更新bidding表
  20. 2、调用biddingall任务时config.json中indexfields配置要有purchasing、purchasinglist、filetext
  21. */
  22. var RegSpace = regexp.MustCompile("[\\s\u3000\u2003\u00a0]+")
  23. func biddingAllTask(data []byte, mapInfo map[string]interface{}) {
  24. defer qutil.Catch()
  25. thread := 40
  26. var mpool = make(chan bool, thread)
  27. q, _ := mapInfo["query"].(map[string]interface{})
  28. if q == nil {
  29. q = map[string]interface{}{
  30. "_id": bson.M{
  31. "$gt": mongodb.StringTOBsonId(mapInfo["gtid"].(string)),
  32. "$lte": mongodb.StringTOBsonId(mapInfo["lteid"].(string)),
  33. },
  34. }
  35. } else {
  36. idMap := q["_id"].(map[string]interface{})
  37. tmpQ := map[string]interface{}{}
  38. for c, id := range idMap {
  39. if idStr, ok := id.(string); ok && id != "" {
  40. tmpQ[c] = mongodb.StringTOBsonId(idStr)
  41. }
  42. }
  43. q["_id"] = tmpQ
  44. }
  45. //bidding库
  46. session := mgo.GetMgoConn()
  47. defer mgo.DestoryMongoConn(session)
  48. //extract库
  49. extractsession := extractmgo.GetMgoConn()
  50. defer extractmgo.DestoryMongoConn(extractsession)
  51. //连接信息
  52. c, _ := mapInfo["coll"].(string)
  53. if c == "" {
  54. c, _ = bidding["collect"].(string)
  55. }
  56. extractc, _ := bidding["extractcollect"].(string)
  57. db, _ := bidding["db"].(string)
  58. extractdb, _ := bidding["extractdb"].(string)
  59. index, _ := bidding["index"].(string)
  60. itype, _ := bidding["type"].(string)
  61. count, _ := session.DB(db).C(c).Find(&q).Count()
  62. fields := strings.Split(bidding["fields"].(string), ",")
  63. //线程池
  64. UpdatesLock := sync.Mutex{}
  65. log.Println("查询语句:", q, "同步总数:", count, "elastic库:", index)
  66. //查询招标数据
  67. query := session.DB(db).C(c).Find(q).Select(bson.M{
  68. "projectinfo.attachment": 0,
  69. "contenthtml": 0,
  70. }).Sort("_id").Iter()
  71. //查询抽取结果
  72. extractquery := extractsession.DB(extractdb).C(extractc).Find(q).Sort("_id").Iter()
  73. n := 0
  74. //更新数组
  75. arr := [][]map[string]interface{}{}
  76. arrEs := []map[string]interface{}{}
  77. //对比两张表数据,减少查询次数
  78. var compare map[string]interface{}
  79. bnil := false
  80. for tmp := make(map[string]interface{}); query.Next(tmp); n++ {
  81. // if qutil.IntAll(tmp["dataging"]) == 1 { //dataging=1不生索引
  82. // tmp = make(map[string]interface{})
  83. // continue
  84. // }
  85. //if qutil.IntAll(tmp["repeat"]) != 0 {
  86. // esQ := `{"query": {"bool": {"must": [{"term": {"id": "`+ mongodb.BsonIdToSId(tmp["_id"]) +`"}}]}}}`
  87. // esData := elastic.Get(index, itype, esQ)
  88. // if len(*esData) > 0 {
  89. // elastic.DelById(index, itype, mongodb.BsonIdToSId(tmp["_id"]))
  90. // }
  91. //}
  92. // if sensitive := qutil.ObjToString(tmp["sensitive"]); sensitive != "" { //bidding中有敏感词,不生索引
  93. // tmp = make(map[string]interface{})
  94. // continue
  95. // }
  96. update := map[string]interface{}{}
  97. del := map[string]interface{}{} //记录extract没有值而bidding中有值的字段
  98. //对比方法----------------
  99. for {
  100. if compare == nil {
  101. compare = make(map[string]interface{})
  102. if !extractquery.Next(compare) {
  103. break
  104. }
  105. }
  106. if compare != nil {
  107. //对比
  108. cid := mongodb.BsonIdToSId(compare["_id"])
  109. tid := mongodb.BsonIdToSId(tmp["_id"])
  110. if cid == tid {
  111. bnil = false
  112. //更新bidding表,生成索引;bidding表modifyinfo中的字段不更新
  113. modifyinfo := make(map[string]bool)
  114. if tmpmodifyinfo, ok := tmp["modifyinfo"].(map[string]interface{}); ok && tmpmodifyinfo != nil {
  115. for k, _ := range tmpmodifyinfo {
  116. modifyinfo[k] = true
  117. }
  118. }
  119. for _, k := range fields { //fields更新到mongo的字段
  120. v1 := compare[k] //extract
  121. v2 := tmp[k] //bidding
  122. if v2 == nil && v1 != nil {
  123. update[k] = v1
  124. } else if v2 != nil && v1 != nil && !modifyinfo[k] {
  125. update[k] = v1
  126. } else if v2 != nil && v1 == nil && !modifyinfo[k] { //
  127. if k == "s_subscopeclass" && del["subscopeclass"] == nil {
  128. continue
  129. } else if k == "s_topscopeclass" && del["topscopeclass"] == nil {
  130. continue
  131. }
  132. del[k] = 1
  133. //qutil.Debug("抽取结果没有值,bidding有值:field--", k, "val--", v2)
  134. }
  135. }
  136. if qutil.IntAll(compare["repeat"]) == 1 {
  137. update["extracttype"] = -1
  138. } else {
  139. update["extracttype"] = 1
  140. }
  141. break
  142. } else {
  143. if cid < tid {
  144. bnil = false
  145. compare = nil
  146. continue
  147. } else {
  148. bnil = true
  149. break
  150. }
  151. }
  152. } else {
  153. bnil = false
  154. break
  155. }
  156. }
  157. //下面可以多线程跑的--->
  158. //处理分类
  159. mpool <- true
  160. _id := tmp["_id"]
  161. go func(tmp, update, compare, del map[string]interface{}, bnil bool) {
  162. defer func() {
  163. <-mpool
  164. }()
  165. if !bnil && compare != nil {
  166. subscopeclass, _ := compare["subscopeclass"].([]interface{}) //subscopeclass
  167. if subscopeclass != nil {
  168. m1 := map[string]bool{}
  169. newclass := []string{}
  170. for _, sc := range subscopeclass {
  171. sclass, _ := sc.(string)
  172. if !m1[sclass] {
  173. m1[sclass] = true
  174. newclass = append(newclass, sclass)
  175. }
  176. }
  177. update["s_subscopeclass"] = strings.Join(newclass, ",")
  178. update["subscopeclass"] = newclass
  179. }
  180. topscopeclass, _ := compare["topscopeclass"].([]interface{}) //topscopeclass
  181. if topscopeclass != nil {
  182. m2 := map[string]bool{}
  183. newclass := []string{}
  184. for _, tc := range topscopeclass {
  185. tclass, _ := tc.(string)
  186. tclass = reg_letter.ReplaceAllString(tclass, "") // 去除字母
  187. if !m2[tclass] {
  188. m2[tclass] = true
  189. newclass = append(newclass, tclass)
  190. }
  191. }
  192. update["s_topscopeclass"] = strings.Join(newclass, ",")
  193. }
  194. //处理中标企业
  195. // winner, _ := compare["winner"].(string)
  196. // m1 := map[string]bool{}
  197. // if winner != "" {
  198. // m1[winner] = true
  199. // }
  200. // package1 := compare["package"]
  201. // if package1 != nil {
  202. // packageM, _ := package1.(map[string]interface{})
  203. // for _, p := range packageM {
  204. // pm, _ := p.(map[string]interface{})
  205. // pw, _ := pm["winner"].(string)
  206. // if pw != "" {
  207. // m1[pw] = true
  208. // }
  209. // }
  210. // }
  211. compare = nil
  212. // if len(m1) > 0 {
  213. // //str := ","
  214. // winnerarr := []string{}
  215. // for k, _ := range m1 {
  216. // //str += k + ","
  217. // winnerarr = append(winnerarr, k)
  218. // }
  219. // update["s_winner"] = strings.Join(winnerarr, ",")
  220. // }
  221. }
  222. //------------------对比结束
  223. //同时保存到elastic
  224. for tk, tv := range update {
  225. tmp[tk] = tv
  226. }
  227. if tmp["s_winner"] != "" {
  228. sWinnerarr := strings.Split(qutil.ObjToString(tmp["s_winner"]), ",")
  229. var cid []string
  230. for _, w := range sWinnerarr {
  231. if w != "" {
  232. ent, _ := mgostandard.FindOne("qyxy_historyname", map[string]interface{}{"company_name": w})
  233. if len(*ent) > 0 {
  234. cid = append(cid, qutil.ObjToString((*ent)["company_id"]))
  235. }else {
  236. ent, _ = mgostandard.FindOne("qyxy_std", map[string]interface{}{"company_name": w})
  237. if len(*ent) > 0 {
  238. cid = append(cid, qutil.ObjToString((*ent)["_id"]))
  239. }
  240. }
  241. }
  242. }
  243. if len(cid) > 0 {
  244. tmp["entidlist"] = cid
  245. update["entidlist"] = cid
  246. tmp_up := []map[string]interface{}{}
  247. tmp_up = append(tmp_up, map[string]interface{}{"_id": tmp["_id"]})
  248. tmp_up = append(tmp_up, map[string]interface{}{"$set": map[string]interface{}{"entidlist": cid}})
  249. UpdataMgoCache <- tmp_up
  250. }
  251. }
  252. //对projectscope字段的索引处理
  253. ps, _ := tmp["projectscope"].(string)
  254. if len(ps) > ESLEN {
  255. tmp["projectscope"] = string(([]rune(ps))[:4000])
  256. }
  257. //对标的物为空处理
  258. filetext := getFileText(tmp)
  259. filetextS := filetext
  260. if len([]rune(filetextS)) > 10 { //attach_text
  261. tmp["filetext"] = filetext
  262. }
  263. if purchasing, ok := tmp["purchasing"].(string); ok && purchasing == "" {
  264. delete(tmp, "purchasing")
  265. }
  266. if purchasinglist, ok := tmp["purchasinglist"].([]interface{}); ok && len(purchasinglist) == 0 {
  267. delete(tmp, "purchasinglist")
  268. }
  269. //数据为空处理
  270. for _, f := range []string{"bidstatus", "city", "district", "channel"} {
  271. if fVal, ok := tmp[f].(string); ok && fVal == "" {
  272. delete(tmp, f)
  273. }
  274. }
  275. //预算和中标金额
  276. // if s_budget := fmt.Sprint(tmp["budget"]); s_budget == "" || s_budget == "<nil>" || s_budget == "null" {
  277. // tmp["budget"] = nil
  278. // } else if sbd, ok := tmp["budget"].(string); ok {
  279. // tmp["budget"] = ObjToMoney([]interface{}{sbd, sbd})[0]
  280. // }
  281. // if s_bidamount := fmt.Sprint(tmp["bidamount"]); s_bidamount == "" || s_bidamount == "<nil>" || s_bidamount == "null" {
  282. // tmp["bidamount"] = nil
  283. // } else if sbd, ok := tmp["bidamount"].(string); ok {
  284. // tmp["bidamount"] = ObjToMoney([]interface{}{sbd, sbd})[0]
  285. // }
  286. //go IS.Add("bidding")
  287. UpdatesLock.Lock()
  288. if qutil.IntAll(update["extracttype"]) != -1 {
  289. newTmp := map[string]interface{}{}
  290. for field, ftype := range biddingIndexFieldsMap { //
  291. if tmp[field] != nil && del[field] == nil { //
  292. //qutil.Debug(field, tmp[field], reflect.TypeOf(tmp[field]).String(), ftype)
  293. if field == "projectinfo" {
  294. mp, _ := tmp[field].(map[string]interface{})
  295. if mp != nil {
  296. newmap := map[string]interface{}{}
  297. for k, ktype := range projectinfoFieldsMap {
  298. mpv := mp[k]
  299. if mpv != nil && reflect.TypeOf(mpv).String() == ktype {
  300. newmap[k] = mp[k]
  301. }
  302. }
  303. if len(newmap) > 0 {
  304. newTmp[field] = newmap
  305. }
  306. }
  307. } else if field == "purchasinglist" { //标的物处理
  308. purchasinglist_new := []map[string]interface{}{}
  309. if pcl, _ := tmp[field].([]interface{}); len(pcl) > 0 {
  310. for _, ls := range pcl {
  311. lsm_new := make(map[string]interface{})
  312. lsm := ls.(map[string]interface{})
  313. for pf, pftype := range purchasinglistFieldsMap {
  314. lsmv := lsm[pf]
  315. if lsmv != nil && reflect.TypeOf(lsmv).String() == pftype {
  316. lsm_new[pf] = lsm[pf]
  317. }
  318. }
  319. if lsm_new != nil && len(lsm_new) > 0 {
  320. purchasinglist_new = append(purchasinglist_new, lsm_new)
  321. }
  322. }
  323. }
  324. if len(purchasinglist_new) > 0 {
  325. newTmp[field] = purchasinglist_new
  326. }
  327. } else if field == "winnerorder" { //中标候选
  328. winnerorder_new := []map[string]interface{}{}
  329. if winnerorder, _ := tmp[field].([]interface{}); len(winnerorder) > 0 {
  330. for _, win := range winnerorder {
  331. winMap_new := make(map[string]interface{})
  332. winMap := win.(map[string]interface{})
  333. for wf, wftype := range winnerorderlistFieldsMap {
  334. wfv := winMap[wf]
  335. if wfv != nil && reflect.TypeOf(wfv).String() == wftype {
  336. if wf == "sort" && qutil.Int64All(wfv) > 100 {
  337. continue
  338. }
  339. winMap_new[wf] = winMap[wf]
  340. }
  341. }
  342. if winMap_new != nil && len(winMap_new) > 0 {
  343. winnerorder_new = append(winnerorder_new, winMap_new)
  344. }
  345. }
  346. }
  347. if len(winnerorder_new) > 0 {
  348. newTmp[field] = winnerorder_new
  349. }
  350. } else if field == "qualifies" {
  351. //项目资质
  352. qs := []string{}
  353. if q, _ := tmp[field].([]interface{}); len(q) > 0 {
  354. for _, v := range q {
  355. v1 := v.(map[string]interface{})
  356. qs = append(qs, qutil.ObjToString(v1["key"]))
  357. }
  358. }
  359. if len(qs) > 0 {
  360. newTmp[field] = strings.Join(qs, ",")
  361. }
  362. } else if field == "detail" { //过滤
  363. detail, _ := tmp[field].(string)
  364. if len([]rune(detail)) > detailLength {
  365. detail = detail[:detailLength]
  366. }
  367. newTmp[field] = FilterDetail(detail)
  368. } else if field == "_id" || field == "topscopeclass" { //不做处理
  369. newTmp[field] = tmp[field]
  370. } else if field == "publishtime" || field == "comeintime" {
  371. //字段类型不正确,特别处理
  372. if tmp[field] != nil && qutil.Int64All(tmp[field]) > 0 {
  373. newTmp[field] = qutil.Int64All(tmp[field])
  374. }
  375. } else if field == "entidlist" {
  376. newTmp[field] = tmp[field]
  377. } else { //其它字段判断数据类型,不正确舍弃
  378. if fieldval := tmp[field]; reflect.TypeOf(fieldval).String() != ftype {
  379. continue
  380. } else {
  381. newTmp[field] = fieldval
  382. }
  383. }
  384. }
  385. }
  386. // for _, v := range biddingIndexFields { //
  387. // if tmp[v] != nil && del[v] == nil { //
  388. // if "projectinfo" == v {
  389. // mp, _ := tmp[v].(map[string]interface{})
  390. // if mp != nil {
  391. // newmap := map[string]interface{}{}
  392. // for _, v1 := range projectinfoFields {
  393. // if mp[v1] != nil {
  394. // newmap[v1] = fmt.Sprint(mp[v1])
  395. // }
  396. // }
  397. // if len(newmap) > 0 {
  398. // newTmp[v] = newmap
  399. // }
  400. // }
  401. // } else if v == "purchasinglist" { //标的物处理
  402. // purchasinglist_new := []map[string]interface{}{}
  403. // if pcl, _ := tmp[v].([]interface{}); len(pcl) > 0 {
  404. // for _, ls := range pcl {
  405. // lsm_new := make(map[string]interface{})
  406. // lsm := ls.(map[string]interface{})
  407. // for _, pf := range purchasinglistFields {
  408. // if lsm[pf] != nil {
  409. // lsm_new[pf] = lsm[pf]
  410. // }
  411. // }
  412. // if lsm_new != nil && len(lsm_new) > 0 {
  413. // purchasinglist_new = append(purchasinglist_new, lsm_new)
  414. // }
  415. // }
  416. // }
  417. // if len(purchasinglist_new) > 0 {
  418. // newTmp[v] = purchasinglist_new
  419. // }
  420. // } else {
  421. // if v == "detail" {
  422. // detail, _ := tmp[v].(string)
  423. // if len([]rune(detail)) > detailLength {
  424. // detail = detail[:detailLength]
  425. // }
  426. // newTmp[v] = FilterDetail(detail)
  427. // } else {
  428. // newTmp[v] = tmp[v]
  429. // }
  430. // }
  431. // } /*else if v == "budget" || v == "bidamount" {
  432. // newTmp[v] = nil
  433. // }*/
  434. // }
  435. arrEs = append(arrEs, newTmp)
  436. }
  437. if len(update) > 0 {
  438. delete(update, "winnerorder") //winnerorder不需要更新到bindding表,删除
  439. queryId := map[string]interface{}{"_id": tmp["_id"]}
  440. set := map[string]interface{}{"$set": update}
  441. if len(del) > 0 { //删除的数据
  442. set["$unset"] = del
  443. }
  444. arr = append(arr, []map[string]interface{}{queryId, set})
  445. }
  446. if len(arr) >= BulkSize {
  447. mgo.UpdateBulkAll(db, c, arr...)
  448. arr = [][]map[string]interface{}{}
  449. }
  450. if len(arrEs) >= BulkSize {
  451. tmps := arrEs
  452. elastic.BulkSave(index, itype, &tmps, true)
  453. if len(multiIndex) == 2 {
  454. elastic.BulkSave(multiIndex[0], multiIndex[1], &tmps, true)
  455. }
  456. if other_index != "" && other_itype != "" { //备份库同时生索引
  457. bidding_other_es.BulkSave(other_index, other_itype, &tmps, true)
  458. }
  459. arrEs = []map[string]interface{}{}
  460. }
  461. UpdatesLock.Unlock()
  462. }(tmp, update, compare, del, bnil)
  463. if n%1000 == 0 {
  464. log.Println("current:", n, _id)
  465. }
  466. tmp = make(map[string]interface{})
  467. }
  468. for i := 0; i < thread; i++ {
  469. mpool <- true
  470. }
  471. UpdatesLock.Lock()
  472. //log.Println(db, c, index, itype, arr, arrEs)
  473. if len(arr) > 0 {
  474. mgo.UpdateBulkAll(db, c, arr...)
  475. }
  476. if len(arrEs) > 0 {
  477. tmps := arrEs
  478. elastic.BulkSave(index, itype, &tmps, true)
  479. if len(multiIndex) == 2 {
  480. elastic.BulkSave(multiIndex[0], multiIndex[1], &tmps, true)
  481. }
  482. if other_index != "" && other_itype != "" { //备份库同时生索引
  483. bidding_other_es.BulkSave(other_index, other_itype, &tmps, true)
  484. }
  485. }
  486. UpdatesLock.Unlock()
  487. log.Println(mapInfo, "create bidding index...over", n)
  488. }
  489. //更新extract表
  490. func UpdateExtract() {
  491. qutil.Debug("Update Extract...")
  492. extract := qutil.ObjToString(bidding["extractcollect"])
  493. arru := make([][]map[string]interface{}, 200)
  494. indexu := 0
  495. for {
  496. select {
  497. case v := <-UpdataMgoCache:
  498. arru[indexu] = v
  499. indexu++
  500. if indexu == 200 {
  501. SP <- true
  502. go func(arru [][]map[string]interface{}) {
  503. defer func() {
  504. <-SP
  505. }()
  506. extractmgo.UpdateBulk(extract, arru...)
  507. }(arru)
  508. arru = make([][]map[string]interface{}, 200)
  509. indexu = 0
  510. }
  511. case <-time.After(1000 * time.Millisecond):
  512. if indexu > 0 {
  513. SP <- true
  514. go func(arru [][]map[string]interface{}) {
  515. defer func() {
  516. <-SP
  517. }()
  518. extractmgo.UpdateBulk(extract, arru...)
  519. }(arru[:indexu])
  520. arru = make([][]map[string]interface{}, 200)
  521. indexu = 0
  522. }
  523. }
  524. }
  525. }