historyrepair.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. // historyrepair 处理多线程重复数据问题
  2. package main
  3. import (
  4. "dbutil/mongo"
  5. "dbutil/redis"
  6. "log"
  7. qu "qfw/util"
  8. "go.mongodb.org/mongo-driver/bson"
  9. )
  10. func historyrepair(db, coll, datatype string, dbnum int) {
  11. sess := MongoTo.GetMgoConn()
  12. defer MongoTo.Close()
  13. field := ""
  14. if datatype == "winner" {
  15. field = "company_name"
  16. } else if datatype == "buyer" {
  17. field = "buyer_name"
  18. } else if datatype == "agency" {
  19. field = "agency_name"
  20. }
  21. it := sess.DB(db).C(coll).Find(bson.M{}).Select(bson.M{field: 1}).Iter()
  22. index := 0
  23. delnum := 0
  24. for tmp := map[string]interface{}{}; it.Next(&tmp); index++ {
  25. name := qu.ObjToString(tmp[field])
  26. id := mongo.BsonTOStringId(tmp["_id"])
  27. str, _ := redis.GetRedisStr(datatype, dbnum, name)
  28. if str != "" {
  29. MongoTo.DeleteById(coll, id)
  30. delnum++
  31. } else {
  32. redis.PutRedis(datatype, dbnum, name, id, -1)
  33. }
  34. tmp = map[string]interface{}{}
  35. if index%100 == 0 {
  36. log.Println(index, delnum)
  37. }
  38. }
  39. log.Println(index, delnum)
  40. }
  41. func historyrepairErr(db, coll, datatype string, dbnum int) {
  42. sess := MongoTo.GetMgoConn()
  43. defer MongoTo.Close()
  44. it := sess.DB(db).C(coll).Find(bson.M{}).Select(bson.M{"name": 1}).Iter()
  45. index := 0
  46. delnum := 0
  47. for tmp := map[string]interface{}{}; it.Next(&tmp); index++ {
  48. name := qu.ObjToString(tmp["name"])
  49. id := mongo.BsonTOStringId(tmp["_id"])
  50. str, _ := redis.GetRedisStr(datatype, dbnum, name)
  51. if str != "" {
  52. MongoTo.DeleteById(coll, id)
  53. delnum++
  54. } else {
  55. redis.PutRedis(datatype, dbnum, name, id, -1)
  56. }
  57. tmp = map[string]interface{}{}
  58. if index%100 == 0 {
  59. log.Println(index, delnum)
  60. }
  61. }
  62. log.Println(index, delnum)
  63. }