biddingall.go 8.9 KB

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