biddingall.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  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 := 10
  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. }
  236. }
  237. }
  238. if len(cid) > 0 {
  239. tmp["entidlist"] = cid
  240. update["entidlist"] = cid
  241. tmp_up := []map[string]interface{}{}
  242. tmp_up = append(tmp_up, map[string]interface{}{"_id": tmp["_id"]})
  243. tmp_up = append(tmp_up, map[string]interface{}{"$set": map[string]interface{}{"entidlist": cid}})
  244. UpdataMgoCache <- tmp_up
  245. }
  246. }
  247. //对projectscope字段的索引处理
  248. ps, _ := tmp["projectscope"].(string)
  249. if len(ps) > ESLEN {
  250. tmp["projectscope"] = string(([]rune(ps))[:4000])
  251. }
  252. //对标的物为空处理
  253. filetext := getFileText(tmp)
  254. filetextS := filetext
  255. if len([]rune(filetextS)) > 10 { //attach_text
  256. tmp["filetext"] = filetext
  257. }
  258. if purchasing, ok := tmp["purchasing"].(string); ok && purchasing == "" {
  259. delete(tmp, "purchasing")
  260. }
  261. if purchasinglist, ok := tmp["purchasinglist"].([]interface{}); ok && len(purchasinglist) == 0 {
  262. delete(tmp, "purchasinglist")
  263. }
  264. //数据为空处理
  265. for _, f := range []string{"bidstatus", "city", "district", "channel"} {
  266. if fVal, ok := tmp[f].(string); ok && fVal == "" {
  267. delete(tmp, f)
  268. }
  269. }
  270. //预算和中标金额
  271. // if s_budget := fmt.Sprint(tmp["budget"]); s_budget == "" || s_budget == "<nil>" || s_budget == "null" {
  272. // tmp["budget"] = nil
  273. // } else if sbd, ok := tmp["budget"].(string); ok {
  274. // tmp["budget"] = ObjToMoney([]interface{}{sbd, sbd})[0]
  275. // }
  276. // if s_bidamount := fmt.Sprint(tmp["bidamount"]); s_bidamount == "" || s_bidamount == "<nil>" || s_bidamount == "null" {
  277. // tmp["bidamount"] = nil
  278. // } else if sbd, ok := tmp["bidamount"].(string); ok {
  279. // tmp["bidamount"] = ObjToMoney([]interface{}{sbd, sbd})[0]
  280. // }
  281. //go IS.Add("bidding")
  282. UpdatesLock.Lock()
  283. if qutil.IntAll(update["extracttype"]) != -1 {
  284. newTmp := map[string]interface{}{}
  285. for field, ftype := range biddingIndexFieldsMap { //
  286. if tmp[field] != nil && del[field] == nil { //
  287. //qutil.Debug(field, tmp[field], reflect.TypeOf(tmp[field]).String(), ftype)
  288. if field == "projectinfo" {
  289. mp, _ := tmp[field].(map[string]interface{})
  290. if mp != nil {
  291. newmap := map[string]interface{}{}
  292. for k, ktype := range projectinfoFieldsMap {
  293. mpv := mp[k]
  294. if mpv != nil && reflect.TypeOf(mpv).String() == ktype {
  295. newmap[k] = mp[k]
  296. }
  297. }
  298. if len(newmap) > 0 {
  299. newTmp[field] = newmap
  300. }
  301. }
  302. } else if field == "purchasinglist" { //标的物处理
  303. purchasinglist_new := []map[string]interface{}{}
  304. if pcl, _ := tmp[field].([]interface{}); len(pcl) > 0 {
  305. for _, ls := range pcl {
  306. lsm_new := make(map[string]interface{})
  307. lsm := ls.(map[string]interface{})
  308. for pf, pftype := range purchasinglistFieldsMap {
  309. lsmv := lsm[pf]
  310. if lsmv != nil && reflect.TypeOf(lsmv).String() == pftype {
  311. lsm_new[pf] = lsm[pf]
  312. }
  313. }
  314. if lsm_new != nil && len(lsm_new) > 0 {
  315. purchasinglist_new = append(purchasinglist_new, lsm_new)
  316. }
  317. }
  318. }
  319. if len(purchasinglist_new) > 0 {
  320. newTmp[field] = purchasinglist_new
  321. }
  322. } else if field == "winnerorder" { //中标候选
  323. winnerorder_new := []map[string]interface{}{}
  324. if winnerorder, _ := tmp[field].([]interface{}); len(winnerorder) > 0 {
  325. for _, win := range winnerorder {
  326. winMap_new := make(map[string]interface{})
  327. winMap := win.(map[string]interface{})
  328. for wf, wftype := range winnerorderlistFieldsMap {
  329. wfv := winMap[wf]
  330. if wfv != nil && reflect.TypeOf(wfv).String() == wftype {
  331. if wf == "sort" && qutil.Int64All(wfv) > 100 {
  332. continue
  333. }
  334. winMap_new[wf] = winMap[wf]
  335. }
  336. }
  337. if winMap_new != nil && len(winMap_new) > 0 {
  338. winnerorder_new = append(winnerorder_new, winMap_new)
  339. }
  340. }
  341. }
  342. if len(winnerorder_new) > 0 {
  343. newTmp[field] = winnerorder_new
  344. }
  345. } else if field == "qualifies" {
  346. //项目资质
  347. qs := []string{}
  348. if q, _ := tmp[field].([]interface{}); len(q) > 0 {
  349. for _, v := range q {
  350. v1 := v.(map[string]interface{})
  351. qs = append(qs, qutil.ObjToString(v1["key"]))
  352. }
  353. }
  354. if len(qs) > 0 {
  355. newTmp[field] = strings.Join(qs, ",")
  356. }
  357. } else if field == "review_experts" {
  358. // 评审专家
  359. if arr, ok :=tmp["review_experts"].([]interface{}); ok && len(arr) > 0 {
  360. arr1 := qutil.ObjArrToStringArr(arr)
  361. newTmp[field] = strings.Join(arr1, ",")
  362. }
  363. } else if field == "detail" { //过滤
  364. detail, _ := tmp[field].(string)
  365. if len([]rune(detail)) > detailLength {
  366. detail = detail[:detailLength]
  367. }
  368. newTmp[field] = FilterDetail(detail)
  369. } else if field == "_id" || field == "topscopeclass" { //不做处理
  370. newTmp[field] = tmp[field]
  371. } else if field == "publishtime" || field == "comeintime" {
  372. //字段类型不正确,特别处理
  373. if tmp[field] != nil && qutil.Int64All(tmp[field]) > 0 {
  374. newTmp[field] = qutil.Int64All(tmp[field])
  375. }
  376. } else if field == "s" {
  377. newTmp[field] = tmp[field]
  378. } else { //其它字段判断数据类型,不正确舍弃
  379. if fieldval := tmp[field]; reflect.TypeOf(fieldval).String() != ftype {
  380. continue
  381. } else {
  382. if fieldval != "" {
  383. newTmp[field] = fieldval
  384. }
  385. }
  386. }
  387. }
  388. }
  389. arrEs = append(arrEs, newTmp)
  390. }
  391. if len(update) > 0 {
  392. delete(update, "winnerorder") //winnerorder不需要更新到bindding表,删除
  393. queryId := map[string]interface{}{"_id": tmp["_id"]}
  394. set := map[string]interface{}{"$set": update}
  395. if len(del) > 0 { //删除的数据
  396. set["$unset"] = del
  397. }
  398. arr = append(arr, []map[string]interface{}{queryId, set})
  399. }
  400. if len(arr) >= BulkSize {
  401. mgo.UpdateBulkAll(db, c, arr...)
  402. arr = [][]map[string]interface{}{}
  403. }
  404. if len(arrEs) >= BulkSize {
  405. tmps := arrEs
  406. elastic.BulkSave(index, itype, &tmps, true)
  407. if len(multiIndex) == 2 {
  408. elastic.BulkSave(multiIndex[0], multiIndex[1], &tmps, true)
  409. }
  410. if other_index != "" && other_itype != "" { //备份库同时生索引
  411. bidding_other_es.BulkSave(other_index, other_itype, &tmps, true)
  412. }
  413. arrEs = []map[string]interface{}{}
  414. }
  415. UpdatesLock.Unlock()
  416. }(tmp, update, compare, del, bnil)
  417. if n%1000 == 0 {
  418. log.Println("current:", n, _id)
  419. }
  420. tmp = make(map[string]interface{})
  421. }
  422. for i := 0; i < thread; i++ {
  423. mpool <- true
  424. }
  425. UpdatesLock.Lock()
  426. //log.Println(db, c, index, itype, arr, arrEs)
  427. if len(arr) > 0 {
  428. mgo.UpdateBulkAll(db, c, arr...)
  429. }
  430. if len(arrEs) > 0 {
  431. tmps := arrEs
  432. elastic.BulkSave(index, itype, &tmps, true)
  433. if len(multiIndex) == 2 {
  434. elastic.BulkSave(multiIndex[0], multiIndex[1], &tmps, true)
  435. }
  436. if other_index != "" && other_itype != "" { //备份库同时生索引
  437. bidding_other_es.BulkSave(other_index, other_itype, &tmps, true)
  438. }
  439. }
  440. UpdatesLock.Unlock()
  441. log.Println(mapInfo, "create bidding index...over", n)
  442. }
  443. //更新extract表
  444. func UpdateExtract() {
  445. qutil.Debug("Update Extract...")
  446. extract := qutil.ObjToString(bidding["extractcollect"])
  447. arru := make([][]map[string]interface{}, 200)
  448. indexu := 0
  449. for {
  450. select {
  451. case v := <-UpdataMgoCache:
  452. arru[indexu] = v
  453. indexu++
  454. if indexu == 200 {
  455. SP <- true
  456. go func(arru [][]map[string]interface{}) {
  457. defer func() {
  458. <-SP
  459. }()
  460. extractmgo.UpdateBulk(extract, arru...)
  461. }(arru)
  462. arru = make([][]map[string]interface{}, 200)
  463. indexu = 0
  464. }
  465. case <-time.After(1000 * time.Millisecond):
  466. if indexu > 0 {
  467. SP <- true
  468. go func(arru [][]map[string]interface{}) {
  469. defer func() {
  470. <-SP
  471. }()
  472. extractmgo.UpdateBulk(extract, arru...)
  473. }(arru[:indexu])
  474. arru = make([][]map[string]interface{}, 200)
  475. indexu = 0
  476. }
  477. }
  478. }
  479. }