main.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. package main
  2. import (
  3. "context"
  4. "github.com/olivere/elastic/v7"
  5. "github.com/spf13/viper"
  6. "go.mongodb.org/mongo-driver/bson"
  7. "go.mongodb.org/mongo-driver/mongo/options"
  8. utils "jygit.jydev.jianyu360.cn/data_processing/common_utils"
  9. "strings"
  10. "jygit.jydev.jianyu360.cn/data_processing/common_utils/mongodb"
  11. "log"
  12. )
  13. var (
  14. MgoP *mongodb.MongodbSim
  15. MgoB *mongodb.MongodbSim
  16. GF GlobalConf
  17. )
  18. func InitConfig() (err error) {
  19. viper.SetConfigFile("config.toml") // 指定配置文件路径
  20. viper.SetConfigName("config") // 配置文件名称(无扩展名)
  21. viper.SetConfigType("toml") // 如果配置文件的名称中没有扩展名,则需要配置此项
  22. viper.AddConfigPath("./")
  23. viper.AddConfigPath("./conf/") // 还可以在工作目录中查找配置
  24. viper.AddConfigPath("../conf/") // 还可以在工作目录中查找配置
  25. err = viper.ReadInConfig() // 查找并读取配置文件
  26. if err != nil { // 处理读取配置文件的错误
  27. return
  28. }
  29. err = viper.Unmarshal(&GF)
  30. return err
  31. }
  32. func InitMgo() {
  33. MgoP = &mongodb.MongodbSim{
  34. MongodbAddr: GF.MongoP.Host,
  35. Size: 10,
  36. DbName: GF.MongoP.DB,
  37. UserName: GF.MongoP.Username,
  38. Password: GF.MongoP.Password,
  39. Direct: GF.MongoP.Direct,
  40. }
  41. MgoP.InitPool()
  42. MgoB = &mongodb.MongodbSim{
  43. MongodbAddr: GF.MongoB.Host,
  44. Size: 10,
  45. DbName: GF.MongoB.DB,
  46. UserName: GF.MongoB.Username,
  47. Password: GF.MongoB.Password,
  48. Direct: GF.MongoB.Direct,
  49. }
  50. MgoB.InitPool()
  51. }
  52. func main() {
  53. InitConfig()
  54. InitMgo()
  55. //InitEs()
  56. //sync()
  57. updateReview()
  58. log.Println("over")
  59. select {}
  60. }
  61. func sync() {
  62. sess := MgoP.GetMgoConn()
  63. defer MgoP.DestoryMongoConn(sess)
  64. ctx := context.Background()
  65. coll := sess.M.C.Database("qfw").Collection("projectset_20230904")
  66. find := options.Find().SetBatchSize(1000).SetSort(bson.D{bson.E{"_id", -1}}).SetProjection(bson.M{"_id": 1, "ids": 1})
  67. cur, err := coll.Find(ctx, bson.D{}, find)
  68. if err != nil {
  69. log.Println(err)
  70. }
  71. // 创建 Elasticsearch 客户端
  72. client, err := elastic.NewClient(
  73. elastic.SetURL(GF.ES.URL),
  74. elastic.SetBasicAuth(GF.ES.Username, GF.ES.Password),
  75. elastic.SetSniff(false),
  76. )
  77. if err != nil {
  78. log.Fatalf("创建 Elasticsearch 客户端失败:%s", err)
  79. }
  80. count := 0
  81. for tmp := make(map[string]interface{}); cur.Next(ctx); count++ {
  82. if cur != nil {
  83. cur.Decode(&tmp)
  84. }
  85. if count%10000 == 0 {
  86. log.Println("current:", count, tmp["_id"])
  87. }
  88. if ids, ok := tmp["ids"].([]interface{}); ok {
  89. projectID := mongodb.BsonIdToSId(tmp["_id"])
  90. for i := len(ids) - 1; i >= 0; i-- {
  91. idStr := utils.ObjToString(ids[i])
  92. if idStr != "" {
  93. if idStr > "5a862e7040d2d9bbe88e3b1f" {
  94. bidd, _ := MgoB.FindById("bidding", idStr, map[string]interface{}{"review_experts": 1})
  95. if len(*bidd) > 0 {
  96. if review_experts, ok := (*bidd)["review_experts"]; ok {
  97. update := map[string]interface{}{
  98. "review_experts": review_experts,
  99. }
  100. MgoP.UpdateById("projectset_20230904", projectID, map[string]interface{}{"$set": update})
  101. esUpdate := map[string]interface{}{
  102. "review_experts": review_experts,
  103. }
  104. client.Update().
  105. Index("projectset_v3").
  106. Id(projectID).
  107. Doc(esUpdate).
  108. Do(context.Background())
  109. break
  110. }
  111. }
  112. } else {
  113. bidd, _ := MgoB.FindById("bidding_back", idStr, map[string]interface{}{"review_experts": 1})
  114. if len(*bidd) > 0 {
  115. if review_experts, ok := (*bidd)["review_experts"]; ok {
  116. update := map[string]interface{}{
  117. "review_experts": review_experts,
  118. }
  119. MgoP.UpdateById("projectset_20230904", projectID, map[string]interface{}{"$set": update})
  120. esUpdate := map[string]interface{}{
  121. "review_experts": review_experts,
  122. }
  123. client.Update().
  124. Index("projectset_v3").
  125. Id(projectID).
  126. Doc(esUpdate).
  127. Do(context.Background())
  128. break
  129. }
  130. }
  131. }
  132. }
  133. }
  134. }
  135. tmp = make(map[string]interface{})
  136. }
  137. }
  138. func updateReview() {
  139. sess := MgoP.GetMgoConn()
  140. defer MgoP.DestoryMongoConn(sess)
  141. ctx := context.Background()
  142. coll := sess.M.C.Database("qfw_data").Collection("projectset")
  143. find := options.Find().SetBatchSize(1000).SetSort(bson.D{bson.E{"_id", 1}}).SetProjection(bson.M{"_id": 1, "review_experts": 1})
  144. cur, err := coll.Find(ctx, bson.D{}, find)
  145. if err != nil {
  146. log.Println(err)
  147. }
  148. count := 0
  149. for tmp := make(map[string]interface{}); cur.Next(ctx); count++ {
  150. if cur != nil {
  151. cur.Decode(&tmp)
  152. }
  153. if count%10000 == 0 {
  154. log.Println("current:", count, tmp["_id"])
  155. }
  156. projectID := mongodb.BsonIdToSId(tmp["_id"])
  157. if reviews, ok := tmp["review_experts"].([]interface{}); ok {
  158. if len(reviews) == 0 {
  159. continue
  160. } else {
  161. ds := make([]string, 0)
  162. for _, v := range reviews {
  163. ds = append(ds, utils.ObjToString(v))
  164. }
  165. dsStr := strings.Join(ds, ",")
  166. update := map[string]interface{}{
  167. "review_experts": dsStr,
  168. }
  169. //log.Println("---", projectID)
  170. MgoP.UpdateById("projectset", projectID, map[string]interface{}{"$set": update})
  171. }
  172. }
  173. tmp = make(map[string]interface{})
  174. }
  175. log.Println("over")
  176. }