biddingall.go 11 KB

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