main.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. package main
  2. import (
  3. "fmt"
  4. "log"
  5. qu "app.yhyue.com/moapp/jybase/common"
  6. "app.yhyue.com/moapp/jybase/mongodb"
  7. "app.yhyue.com/moapp/jybase/mysql"
  8. "github.com/gogf/gf/v2/util/gconv"
  9. )
  10. var config map[string]interface{}
  11. var Mgo *mongodb.MongodbSim
  12. var TidbCall *mysql.Mysql
  13. var TidbBi *mysql.Mysql
  14. func InitMongo(mgs map[string]interface{}) {
  15. Mgo = &mongodb.MongodbSim{
  16. MongodbAddr: qu.ObjToString(mgs["address"]),
  17. Size: qu.IntAll(mgs["size"]),
  18. DbName: qu.ObjToString(mgs["dbName"]),
  19. ReplSet: qu.ObjToString(mgs["replSet"]),
  20. UserName: qu.ObjToString(mgs["userName"]),
  21. Password: qu.ObjToString(mgs["password"]),
  22. }
  23. Mgo.InitPool()
  24. log.Println("初始化 mongodb")
  25. }
  26. func InitMysql(mys map[string]interface{}) {
  27. TidbBi = &mysql.Mysql{
  28. Address: qu.ObjToString(mys["address"]),
  29. UserName: qu.ObjToString(mys["userName"]),
  30. PassWord: qu.ObjToString(mys["passWord"]),
  31. DBName: qu.ObjToString(mys["dbName"]),
  32. MaxOpenConns: qu.IntAll(mys["maxOpenConns"]),
  33. MaxIdleConns: qu.IntAll(mys["maxIdleConns"]),
  34. }
  35. TidbBi.Init()
  36. log.Println("初始化 mysql")
  37. }
  38. func InitTidb(mys map[string]interface{}) {
  39. TidbCall = &mysql.Mysql{
  40. Address: qu.ObjToString(mys["address"]),
  41. UserName: qu.ObjToString(mys["userName"]),
  42. PassWord: qu.ObjToString(mys["passWord"]),
  43. DBName: qu.ObjToString(mys["dbName"]),
  44. MaxOpenConns: qu.IntAll(mys["maxOpenConns"]),
  45. MaxIdleConns: qu.IntAll(mys["maxIdleConns"]),
  46. }
  47. TidbCall.Init()
  48. log.Println("初始化 Tidb")
  49. }
  50. func init() {
  51. qu.ReadConfig(&config)
  52. //
  53. mgs, _ := config["mongodb"].(map[string]interface{})
  54. InitMongo(mgs)
  55. mys, _ := config["tidb1"].(map[string]interface{})
  56. InitMysql(mys)
  57. tbs, _ := config["tidb2"].(map[string]interface{})
  58. InitTidb(tbs)
  59. }
  60. func main() {
  61. //
  62. do1()
  63. //
  64. do2()
  65. }
  66. //电销线索刷库
  67. func do1() {
  68. i := 0
  69. for {
  70. count := TidbBi.CountBySql(`SELECT count(1) FROM dwd_f_crm_clue_info where company_nature is null`)
  71. if count == 0 {
  72. log.Println("find no data end")
  73. return
  74. }
  75. TidbBi.SelectByBath(10, func(l *[]map[string]interface{}) bool {
  76. for _, v := range *l {
  77. i++
  78. id := gconv.Int64(v["id"])
  79. query := map[string]interface{}{
  80. "id": id,
  81. }
  82. cluename := gconv.String(v["cluename"])
  83. update := getCompanyType(cluename)
  84. ok := TidbBi.Update("dwd_f_crm_clue_info", query, update)
  85. if !ok {
  86. log.Println("crm clue info update err", query, update)
  87. }
  88. }
  89. if i%5000 == 0 {
  90. log.Println(fmt.Sprintf("current --- %d ", i))
  91. }
  92. return true
  93. }, `SELECT * FROM dwd_f_crm_clue_info where company_nature is null limit 500`)
  94. }
  95. }
  96. //合力易捷刷库
  97. func do2() {
  98. i := 0
  99. for {
  100. count := TidbCall.CountBySql(`SELECT count(1) FROM customer where company_nature is null`)
  101. if count == 0 {
  102. log.Println("find no data end")
  103. return
  104. }
  105. TidbCall.SelectByBath(10, func(l *[]map[string]interface{}) bool {
  106. for _, v := range *l {
  107. i++
  108. id := gconv.String(v["unique_id"])
  109. query := map[string]interface{}{
  110. "unique_id": id,
  111. }
  112. company := gconv.String(v["company"])
  113. update := getCompanyType(company)
  114. ok := TidbCall.Update("customer", query, update)
  115. if !ok {
  116. log.Println("customer info update err", query, update)
  117. }
  118. }
  119. if i%5000 == 0 {
  120. log.Println(fmt.Sprintf("current --- %d ", i))
  121. }
  122. return true
  123. }, `SELECT * FROM customer where company_nature is null limit 500`)
  124. }
  125. }
  126. //公司性质、公司核验
  127. func getCompanyType(name string) map[string]interface{} {
  128. data := map[string]interface{}{
  129. "company_nature": 0,
  130. "company_verification": 0,
  131. }
  132. if c := TidbBi.CountBySql(`select count(1) from group_company_name where company_name=?`, name); c > 0 {
  133. data["company_nature"] = 1
  134. }
  135. if c := Mgo.Count("qyxy_std", map[string]interface{}{"company_name": name}); c > 0 {
  136. data["company_verification"] = 1
  137. }
  138. return data
  139. }