init.go 4.0 KB

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