main.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. package main
  2. import (
  3. "fmt"
  4. "log"
  5. "time"
  6. elastic "es"
  7. "mongodb"
  8. common "qfw/util"
  9. "github.com/robfig/cron"
  10. )
  11. var (
  12. Mgo *mongodb.MongodbSim
  13. Es elastic.Es
  14. cfg = new(Config)
  15. SEXhs = common.SimpleEncrypt{Key: "topJYBX2019"}
  16. mode = flag.Int("m", 1, "")
  17. )
  18. func init() {
  19. common.ReadConfig(&cfg)
  20. Mgo = mongodb.NewMgo(cfg.Db.Address, cfg.Db.DbName, cfg.Db.DbSize)
  21. Es = elastic.NewEs(cfg.Es.Version, cfg.Es.Address, cfg.Es.DbSize, cfg.Es.UserName, cfg.Es.Password)
  22. }
  23. func FindData() {
  24. session := Mgo.GetMgoConn()
  25. query := map[string]interface{}{
  26. "createtime": map[string]interface{}{
  27. "$gte": time.Now().Unix() - 86400,
  28. },
  29. "$or": []map[string]interface{}{
  30. map[string]interface{}{
  31. "projectId": "",
  32. },
  33. map[string]interface{}{
  34. "projectId": map[string]interface{}{
  35. "$exists": 0,
  36. },
  37. },
  38. }}
  39. defer Mgo.DestoryMongoConn(session)
  40. iter := session.DB(cfg.Db.DbName).C("usermail").Find(&query).Iter()
  41. thisData := map[string]interface{}{}
  42. for {
  43. if !iter.Next(&thisData) {
  44. break
  45. }
  46. id := common.ObjToString(thisData["id"])
  47. query := `{"query": {"bool": {"must": [{"term": {"ids": "%s"}}],"must_not": [],"should": []}}}`
  48. querys := fmt.Sprintf(query, id)
  49. projectId := ""
  50. data := Es.Get("projectset", "projectset", querys)
  51. if data != nil && *data != nil && len(*data) > 0 {
  52. projectId = common.ObjToString((*data)[0]["_id"])
  53. log.Println("projectId", projectId)
  54. } else {
  55. log.Println("ES未查到项目id", id)
  56. }
  57. //
  58. if projectId != "" {
  59. ok := Mgo.UpdateById("usermail", thisData["_id"], map[string]interface{}{"$set": map[string]interface{}{"projectId": projectId}})
  60. if ok {
  61. log.Println("项目id更新成功 ", id, projectId)
  62. } else {
  63. log.Println("项目id更新失败! ", id, projectId)
  64. }
  65. }
  66. thisData = map[string]interface{}{}
  67. }
  68. }
  69. func main() {
  70. flag.Parse()
  71. if *mode == 1 {
  72. FindHistory()
  73. } else {
  74. FindData()
  75. c := cron.New()
  76. c.AddFunc(cfg.CornExp, func() {
  77. FindData()
  78. })
  79. c.Start()
  80. select {}
  81. }
  82. }
  83. func FindHistory() {
  84. session := Mgo.GetMgoConn()
  85. query := map[string]interface{}{
  86. "$or": []map[string]interface{}{
  87. map[string]interface{}{
  88. "projectId": "",
  89. },
  90. map[string]interface{}{
  91. "projectId": map[string]interface{}{
  92. "$exists": 0,
  93. },
  94. },
  95. }}
  96. defer Mgo.DestoryMongoConn(session)
  97. iter := session.DB(cfg.Db.DbName).C("usermail").Find(&query).Iter()
  98. thisData := map[string]interface{}{}
  99. for {
  100. if !iter.Next(&thisData) {
  101. break
  102. }
  103. id := common.ObjToString(thisData["id"])
  104. query := `{"query": {"bool": {"must": [{"term": {"ids": "%s"}}],"must_not": [],"should": []}}}`
  105. querys := fmt.Sprintf(query, id)
  106. projectId := ""
  107. data := Es.Get("projectset", "projectset", querys)
  108. if data != nil && *data != nil && len(*data) > 0 {
  109. projectId = common.ObjToString((*data)[0]["_id"])
  110. log.Println("projectId", projectId)
  111. } else {
  112. log.Println("ES未查到项目id", id)
  113. }
  114. //
  115. if projectId != "" {
  116. ok := Mgo.UpdateById("usermail", thisData["_id"], map[string]interface{}{"$set": map[string]interface{}{"projectId": projectId}})
  117. if ok {
  118. log.Println("项目id更新成功 ", id, projectId)
  119. } else {
  120. log.Println("项目id更新失败! ", id, projectId)
  121. }
  122. }
  123. thisData = map[string]interface{}{}
  124. }
  125. }