init.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "log"
  6. "mongodb"
  7. "qfw/util"
  8. "qfw/util/redis"
  9. )
  10. var (
  11. Sysconfig map[string]interface{}
  12. MongoTool *mongodb.MongodbSim
  13. MongoTool1 *mongodb.MongodbSim
  14. MysqlTool *Mysql
  15. DbSave string
  16. updatePool chan []map[string]interface{}
  17. updateSp chan bool
  18. saveSize int
  19. savePool chan map[string]interface{}
  20. saveSp chan bool
  21. QyStypeMap map[string]string
  22. CompanyStatusMap map[string]string
  23. AddressMap map[string]*City
  24. AddressOldMap map[string]*City
  25. lastId int64
  26. )
  27. func init() {
  28. util.ReadConfig(&Sysconfig)
  29. DbSave = util.ObjToString(Sysconfig["dbSave"])
  30. MongoTool = &mongodb.MongodbSim{
  31. MongodbAddr: util.ObjToString(Sysconfig["dbServer"]),
  32. Size: util.IntAll(Sysconfig["dbSize"]),
  33. DbName: util.ObjToString(Sysconfig["dbName"]),
  34. UserName: util.ObjToString(Sysconfig["uname"]),
  35. Password: util.ObjToString(Sysconfig["upwd"]),
  36. }
  37. MongoTool.InitPool()
  38. MongoTool1 = &mongodb.MongodbSim{
  39. MongodbAddr: "172.17.4.181:27001",
  40. Size: 10,
  41. DbName: "mixdata",
  42. }
  43. MongoTool1.InitPool()
  44. //MysqlTool = &Mysql{
  45. // Address: "172.17.4.181:3308",
  46. // UserName: "root",
  47. // PassWord: "root123",
  48. // DBName: "ic",
  49. //}
  50. //MysqlTool.Init()
  51. saveSize = 200
  52. updatePool = make(chan []map[string]interface{}, 5000)
  53. updateSp = make(chan bool, 5)
  54. savePool = make(chan map[string]interface{}, 5000)
  55. saveSp = make(chan bool, 5)
  56. lastId = 0
  57. InitQyStype()
  58. InitCompanyStatus()
  59. InitAddress()
  60. err := seg.LoadDictionary("dict.txt")
  61. if err != nil {
  62. log.Fatal("结巴分词出错...")
  63. return
  64. }
  65. redis.InitRedis1("qyxy_winner=127.0.0.1:8379", 3)
  66. }
  67. func InitQyStype() {
  68. defer util.Catch()
  69. util.Debug("Init QyStype...")
  70. QyStypeMap = map[string]string{}
  71. qystype, _ := MongoTool.Find("qystype", nil, nil, nil, false, -1, -1)
  72. for _, tmp := range *qystype {
  73. name := util.ObjToString(tmp["name"])
  74. prename := util.ObjToString(tmp["prename"])
  75. QyStypeMap[name] = prename
  76. }
  77. }
  78. func InitCompanyStatus() {
  79. defer util.Catch()
  80. util.Debug("Init CompanyStatus...")
  81. CompanyStatusMap = map[string]string{}
  82. status, _ := MongoTool.Find("company_status", nil, nil, nil, false, -1, -1)
  83. for _, tmp := range *status {
  84. old_status := util.ObjToString(tmp["old"])
  85. new_status := util.ObjToString(tmp["new"])
  86. CompanyStatusMap[old_status] = new_status
  87. }
  88. }
  89. type City struct {
  90. Code string `json:"code"`
  91. Province string `json:"province"`
  92. City string `json:"city"`
  93. District string `json:"district"`
  94. }
  95. // CodeMap 区域code补全
  96. var CodeMap = map[int]string{
  97. 2: "0000",
  98. 4: "00",
  99. }
  100. func InitAddress() {
  101. defer util.Catch()
  102. util.Debug("Init Address...")
  103. AddressMap = map[string]*City{}
  104. AddressOldMap = map[string]*City{}
  105. sess := MongoTool.GetMgoConn()
  106. defer MongoTool.DestoryMongoConn(sess)
  107. result := sess.DB("mixdata").C("address_new_2020").Find(nil).Iter()
  108. count := 0
  109. for tmp := make(map[string]interface{}); result.Next(&tmp); count++ {
  110. if count % 50000 == 0 {
  111. util.Debug("current---", count)
  112. }
  113. code := util.ObjToString(tmp["code"])
  114. codeLen := len(code)
  115. if codeLen > 6 {
  116. continue
  117. }
  118. if t_code := CodeMap[codeLen]; t_code != "" {
  119. code = code + t_code
  120. }
  121. remark := fmt.Sprint(tmp["Remarks"])
  122. city := &City{}
  123. tmpjson, err := json.Marshal(tmp)
  124. if err == nil {
  125. json.Unmarshal(tmpjson, city)
  126. }
  127. if remark == "已作废" {
  128. AddressOldMap[code] = city
  129. } else {
  130. AddressMap[code] = city
  131. }
  132. }
  133. util.Debug("Init Address end...", len(AddressMap), len(AddressOldMap))
  134. }