biddingall.go 14 KB

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