main.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. }()
  46. logger.Info("初始化成功")
  47. web.Run()
  48. }
  49. func initThirdPartyAccessToken() {
  50. accessToken := redis.GenerateCAPAccessTokenKey()
  51. if redisUtils.GetString(accessToken) == "" {
  52. logger.Info("开始初始化CAP AccessToken")
  53. token, err := htApi.GetAccessToken()
  54. if err != nil {
  55. logger.Error("获取CAP AccessToken失败:%v", err)
  56. return
  57. }
  58. expireTime, err := time.Parse(time.DateTime, token.ExpiresAt)
  59. if err != nil {
  60. logger.Error("解析过期时间失败:%v", err)
  61. return
  62. }
  63. duration := expireTime.Sub(time.Now()).Seconds()
  64. _ = redisUtils.SetString(accessToken, token.AccessToken, int(duration)-5)
  65. //fmt.Printf(base64.StdEncoding.EncodeToString([]byte(token.AccessToken)))
  66. }
  67. }
  68. func initReport() {
  69. var wg sync.WaitGroup
  70. wg.Add(2)
  71. logger.Info("开始初始化研报库")
  72. go func() {
  73. defer wg.Done()
  74. for {
  75. id, err := report.GetETALatestReportId()
  76. var etaReportList []eta.ETAReport
  77. etaReportList, err = eta.GetETAReports(id)
  78. if err != nil {
  79. logger.Error("获取ETA研报列表失败:%v", err)
  80. }
  81. if len(etaReportList) > 0 {
  82. err = report.InitETAReportList(etaReportList)
  83. if err != nil {
  84. logger.Error("同步ETA研报列表失败:%v", err)
  85. }
  86. } else {
  87. logger.Info(contants.TaskFormat, "同步ETA研报库结束")
  88. break
  89. }
  90. }
  91. }()
  92. go func() {
  93. defer wg.Done()
  94. for {
  95. id, err := report.GetHTLatestReportId()
  96. var htReportList []ht.HTReport
  97. htReportList, err = ht.GetHTReports(id)
  98. if err != nil {
  99. logger.Error("获取ETA研报列表失败:%v", err)
  100. }
  101. if len(htReportList) > 0 {
  102. for i := 0; i < len(htReportList); i++ {
  103. timestamp := int64(htReportList[i].PublishTime)
  104. t := time.UnixMilli(timestamp)
  105. htReportList[i].PublishedTime = t.Format(time.DateTime)
  106. plateId := htReportList[i].PlateId
  107. var plate ht.HTPlate
  108. plate, err = ht.GetPermissionNameById(plateId)
  109. if err != nil || plate.ParentId == 0 {
  110. htReportList[i].PermissionName = htReportList[i].PlateName
  111. } else {
  112. var PermissionName string
  113. err = getPermissionNameById(plate.Id, &PermissionName)
  114. if err != nil {
  115. logger.Error("获取ETA研报列表失败:%v", err)
  116. htReportList[i].PermissionName = ""
  117. } else {
  118. htReportList[i].PermissionName = PermissionName
  119. }
  120. }
  121. }
  122. var stop bool
  123. stop, err = report.InitHTReportList(htReportList)
  124. if err != nil {
  125. logger.Error("同步ETA研报列表失败:%v", err)
  126. break
  127. }
  128. if stop {
  129. logger.Info(contants.TaskFormat, "同步HT研报库结束")
  130. break
  131. }
  132. } else {
  133. logger.Info(contants.TaskFormat, "同步HT研报库结束")
  134. break
  135. }
  136. }
  137. }()
  138. wg.Wait()
  139. logger.Info("初始化研报库完成")
  140. }
  141. func initMerchant(merchantId string) {
  142. merchantList, err := merchantDao.GetEnablerMerchants()
  143. if err != nil {
  144. logger.Error("加载商户信息失败:%v", err)
  145. }
  146. if len(merchantList) == 0 {
  147. logger.Warn("未配置商户信息,请配置商户信息")
  148. return
  149. }
  150. if merchantId != "" {
  151. find := false
  152. for _, merchant := range merchantList {
  153. if merchant.MerchantID == merchantId {
  154. err = redisUtils.SetString(redis.GenerateMerchantKey(), merchant.MerchantID, 0)
  155. if err != nil {
  156. logger.Error("ID添加失败:%v", err)
  157. return
  158. }
  159. find = true
  160. break
  161. }
  162. if !find {
  163. logger.Error("未找到启动的商户信息,请配置商户信息,商户ID:[" + merchantId + "]")
  164. }
  165. }
  166. } else {
  167. err = redisUtils.SetString(redis.GenerateMerchantKey(), merchantList[0].MerchantID, 0)
  168. if err != nil {
  169. logger.Error("redis商户ID添加失败:%v", err)
  170. return
  171. }
  172. }
  173. }
  174. func getPermissionNameById(id int, currentName *string) (err error) {
  175. plate, err := ht.GetPermissionNameById(id)
  176. if err != nil {
  177. logger.Error("查询海通板块品种名称失败:%v", err)
  178. return
  179. }
  180. if plate.ParentId != 0 {
  181. *currentName = plate.PlateName
  182. return getPermissionNameById(plate.ParentId, currentName)
  183. } else {
  184. return
  185. }
  186. }