init.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. package main
  2. import (
  3. util "app.yhyue.com/data_processing/common_utils"
  4. "encoding/json"
  5. "fmt"
  6. "github.com/spf13/viper"
  7. "go.uber.org/zap"
  8. "jygit.jydev.jianyu360.cn/data_processing/common_utils/log"
  9. "jygit.jydev.jianyu360.cn/data_processing/common_utils/mongodb"
  10. )
  11. var (
  12. MongoTool *mongodb.MongodbSim
  13. QyStypeMap map[string]string
  14. CompanyStatusMap map[string]string
  15. AddressMap map[string]*City
  16. AddressOldMap map[string]*City
  17. CollArr []string // std 程序需要读取的数据表
  18. )
  19. func init() {
  20. InitLog()
  21. err := InitConfig()
  22. if err != nil {
  23. log.Info("init", zap.Any("InitConfig", err))
  24. }
  25. InitMgo()
  26. InitQyStype()
  27. InitCompanyStatus()
  28. InitAddress()
  29. err = seg.LoadDictionary("dict.txt")
  30. if err != nil {
  31. log.Fatal("init", zap.Any("LoadDictionary err", err))
  32. return
  33. }
  34. //qyxy_std 需要处理的数据表
  35. CollArr = []string{"company_base", "company_employee", "company_history_name", "company_partner", "annual_report_base", "annual_report_website"}
  36. }
  37. func InitMgo() {
  38. MongoTool = &mongodb.MongodbSim{
  39. MongodbAddr: GF.Env.Addr,
  40. Size: GF.Env.Dbsize,
  41. DbName: GF.Env.Dbname,
  42. UserName: GF.Env.Username,
  43. Password: GF.Env.Password,
  44. }
  45. MongoTool.InitPool()
  46. }
  47. func InitConfig() (err error) {
  48. viper.SetConfigFile("config.toml") // 指定配置文件路径
  49. viper.SetConfigName("config") // 配置文件名称(无扩展名)
  50. viper.SetConfigType("toml") // 如果配置文件的名称中没有扩展名,则需要配置此项
  51. viper.AddConfigPath("./")
  52. viper.AddConfigPath("./conf/") // 还可以在工作目录中查找配置
  53. viper.AddConfigPath("../conf/") // 还可以在工作目录中查找配置
  54. err = viper.ReadInConfig() // 查找并读取配置文件
  55. if err != nil { // 处理读取配置文件的错误
  56. return
  57. }
  58. err = viper.Unmarshal(&GF)
  59. return err
  60. }
  61. func InitLog() {
  62. err := log.InitLog(
  63. log.Path("./logs/log.out"),
  64. //log.Path(""),
  65. log.Level("info"),
  66. log.Compress(true),
  67. log.MaxSize(10),
  68. log.MaxBackups(10),
  69. log.MaxAge(7),
  70. log.Format("json"),
  71. )
  72. if err != nil {
  73. fmt.Printf("InitLog failed: %v\n", err)
  74. }
  75. }
  76. func InitQyStype() {
  77. defer util.Catch()
  78. util.Debug("Init QyStype...")
  79. QyStypeMap = map[string]string{}
  80. qystype, _ := MongoTool.Find("qystype", nil, nil, nil, false, -1, -1)
  81. for _, tmp := range *qystype {
  82. name := util.ObjToString(tmp["name"])
  83. prename := util.ObjToString(tmp["prename"])
  84. QyStypeMap[name] = prename
  85. }
  86. }
  87. func InitCompanyStatus() {
  88. defer util.Catch()
  89. util.Debug("Init CompanyStatus...")
  90. CompanyStatusMap = map[string]string{}
  91. status, _ := MongoTool.Find("company_status", nil, nil, nil, false, -1, -1)
  92. for _, tmp := range *status {
  93. old_status := util.ObjToString(tmp["old"])
  94. new_status := util.ObjToString(tmp["new"])
  95. CompanyStatusMap[old_status] = new_status
  96. }
  97. }
  98. type City struct {
  99. Code string `json:"code"`
  100. Province string `json:"province"`
  101. City string `json:"city"`
  102. District string `json:"district"`
  103. }
  104. // CodeMap 区域code补全
  105. var CodeMap = map[int]string{
  106. 2: "0000",
  107. 4: "00",
  108. }
  109. func InitAddress() {
  110. defer util.Catch()
  111. util.Debug("Init Address...")
  112. AddressMap = map[string]*City{}
  113. AddressOldMap = map[string]*City{}
  114. sess := MongoTool.GetMgoConn()
  115. defer MongoTool.DestoryMongoConn(sess)
  116. result := sess.DB("mixdata").C("address_new_2020").Find(nil).Iter()
  117. count := 0
  118. for tmp := make(map[string]interface{}); result.Next(&tmp); count++ {
  119. if count%50000 == 0 {
  120. log.Info("InitAddress", zap.Int("current", count))
  121. }
  122. code := util.ObjToString(tmp["code"])
  123. codeLen := len(code)
  124. if codeLen > 6 {
  125. continue
  126. }
  127. if t_code := CodeMap[codeLen]; t_code != "" {
  128. code = code + t_code
  129. }
  130. remark := fmt.Sprint(tmp["Remarks"])
  131. city := &City{}
  132. tmpjson, err := json.Marshal(tmp)
  133. if err == nil {
  134. json.Unmarshal(tmpjson, city)
  135. }
  136. if remark == "已作废" {
  137. AddressOldMap[code] = city
  138. } else {
  139. AddressMap[code] = city
  140. }
  141. }
  142. log.Info("InitAddress", zap.Any("AddressMap", len(AddressMap)), zap.Int("AddressOldMap", len(AddressOldMap)))
  143. }