main.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. package main
  2. import (
  3. "eta/eta_mini_ht_api/api"
  4. _ "eta/eta_mini_ht_api/common/component"
  5. "eta/eta_mini_ht_api/common/component/cache"
  6. "eta/eta_mini_ht_api/common/component/config"
  7. logger "eta/eta_mini_ht_api/common/component/log"
  8. "eta/eta_mini_ht_api/common/contants"
  9. "eta/eta_mini_ht_api/common/exception"
  10. "eta/eta_mini_ht_api/common/utils/redis"
  11. "eta/eta_mini_ht_api/domian/report"
  12. "eta/eta_mini_ht_api/models/eta"
  13. "eta/eta_mini_ht_api/models/ht"
  14. merchantDao "eta/eta_mini_ht_api/models/merchant"
  15. _ "eta/eta_mini_ht_api/routers"
  16. _ "eta/eta_mini_ht_api/task"
  17. "github.com/beego/beego/v2/server/web"
  18. "sync"
  19. "time"
  20. )
  21. var (
  22. redisUtils = cache.GetInstance()
  23. htApi = api.GetInstance()
  24. )
  25. func main() {
  26. htConfig := config.GetConfig(contants.HT).(*config.HTBizConfig)
  27. if web.BConfig.RunMode == "dev" {
  28. web.BConfig.WebConfig.DirectoryIndex = true
  29. web.BConfig.WebConfig.StaticDir["/swagger"] = "swagger"
  30. }
  31. //web.ErrorHandler("*", exception.ControllerAdvice())
  32. web.BConfig.RecoverFunc = exception.PanicAdvice
  33. go func() {
  34. //内存数据预热预加载
  35. logger.Info("开始预加载数据")
  36. if htConfig.EnableTask() {
  37. //初始化研报库
  38. initReport()
  39. }
  40. //初始化第三方AccessToken
  41. initThirdPartyAccessToken()
  42. //初始化商户信息
  43. initMerchant(htConfig.GetMerchantId())
  44. }()
  45. logger.Info("初始化成功")
  46. web.Run()
  47. }
  48. func initThirdPartyAccessToken() {
  49. accessToken := redis.GenerateCAPAccessTokenKey()
  50. if redisUtils.GetString(accessToken) == "" {
  51. logger.Info("开始初始化CAP AccessToken")
  52. token, err := htApi.GetAccessToken()
  53. if err != nil {
  54. logger.Error("获取CAP AccessToken失败:%v", err)
  55. return
  56. }
  57. expireTime, err := time.Parse(time.DateTime, token.ExpiresAt)
  58. if err != nil {
  59. logger.Error("解析过期时间失败:%v", err)
  60. return
  61. }
  62. duration := expireTime.Sub(time.Now()).Seconds()
  63. _ = redisUtils.SetString(accessToken, token.AccessToken, int(duration)-5)
  64. //fmt.Printf(base64.StdEncoding.EncodeToString([]byte(token.AccessToken)))
  65. }
  66. }
  67. func initReport() {
  68. var wg sync.WaitGroup
  69. wg.Add(2)
  70. logger.Info("开始初始化研报库")
  71. go func() {
  72. defer wg.Done()
  73. for {
  74. id, err := report.GetETALatestReportId()
  75. var etaReportList []eta.ETAReport
  76. etaReportList, err = eta.GetETAReports(id)
  77. if err != nil {
  78. logger.Error("获取ETA研报列表失败:%v", err)
  79. }
  80. if len(etaReportList) > 0 {
  81. err = report.InitETAReportList(etaReportList)
  82. if err != nil {
  83. logger.Error("同步ETA研报列表失败:%v", err)
  84. }
  85. } else {
  86. logger.Info(contants.TaskFormat, "同步ETA研报库结束")
  87. break
  88. }
  89. }
  90. }()
  91. go func() {
  92. defer wg.Done()
  93. for {
  94. id, err := report.GetHTLatestReportId()
  95. var htReportList []ht.HTReport
  96. htReportList, err = ht.GetHTReports(id)
  97. if err != nil {
  98. logger.Error("获取ETA研报列表失败:%v", err)
  99. }
  100. if len(htReportList) > 0 {
  101. for i := 0; i < len(htReportList); i++ {
  102. timestamp := int64(htReportList[i].PublishTime)
  103. t := time.UnixMilli(timestamp)
  104. htReportList[i].PublishedTime = t.Format(time.DateTime)
  105. plateId := htReportList[i].PlateId
  106. var plate ht.HTPlate
  107. plate, err = ht.GetPermissionNameById(plateId)
  108. if err != nil || plate.ParentId == 0 {
  109. htReportList[i].PermissionName = htReportList[i].PlateName
  110. } else {
  111. var PermissionName string
  112. err = getPermissionNameById(plate.Id, &PermissionName)
  113. if err != nil {
  114. logger.Error("获取ETA研报列表失败:%v", err)
  115. htReportList[i].PermissionName = ""
  116. } else {
  117. htReportList[i].PermissionName = PermissionName
  118. }
  119. }
  120. }
  121. var stop bool
  122. stop, err = report.InitHTReportList(htReportList)
  123. if err != nil {
  124. logger.Error("同步ETA研报列表失败:%v", err)
  125. break
  126. }
  127. if stop {
  128. logger.Info(contants.TaskFormat, "同步HT研报库结束")
  129. break
  130. }
  131. } else {
  132. logger.Info(contants.TaskFormat, "同步HT研报库结束")
  133. break
  134. }
  135. }
  136. }()
  137. wg.Wait()
  138. logger.Info("初始化研报库完成")
  139. }
  140. func initMerchant(merchantId string) {
  141. merchantList, err := merchantDao.GetEnablerMerchants()
  142. if err != nil {
  143. logger.Error("加载商户信息失败:%v", err)
  144. }
  145. if len(merchantList) == 0 {
  146. logger.Warn("未配置商户信息,请配置商户信息")
  147. }
  148. if merchantId != "" {
  149. find := false
  150. for _, merchant := range merchantList {
  151. if merchant.MerchantID == merchantId {
  152. err = redisUtils.SetString(redis.GenerateMerchantKey(), merchant.MerchantID, 0)
  153. if err != nil {
  154. logger.Error("redis商户ID添加失败:%v", err)
  155. return
  156. }
  157. find = true
  158. break
  159. }
  160. if !find {
  161. logger.Error("未找到启动的商户信息,请配置商户信息,商户ID:[" + merchantId + "]")
  162. }
  163. }
  164. } else {
  165. err = redisUtils.SetString(redis.GenerateMerchantKey(), merchantList[0].MerchantID, -1)
  166. if err != nil {
  167. logger.Error("redis商户ID添加失败:%v", err)
  168. return
  169. }
  170. }
  171. }
  172. func getPermissionNameById(id int, currentName *string) (err error) {
  173. plate, err := ht.GetPermissionNameById(id)
  174. if err != nil {
  175. logger.Error("查询海通板块品种名称失败:%v", err)
  176. return
  177. }
  178. if plate.ParentId != 0 {
  179. *currentName = plate.PlateName
  180. return getPermissionNameById(plate.ParentId, currentName)
  181. } else {
  182. return
  183. }
  184. }