main.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. package main
  2. import (
  3. _ "eta/eta_mini_ht_api/common/component"
  4. "eta/eta_mini_ht_api/common/component/config"
  5. logger "eta/eta_mini_ht_api/common/component/log"
  6. "eta/eta_mini_ht_api/common/contants"
  7. "eta/eta_mini_ht_api/common/exception"
  8. "eta/eta_mini_ht_api/domian/report"
  9. "eta/eta_mini_ht_api/models/eta"
  10. "eta/eta_mini_ht_api/models/ht"
  11. _ "eta/eta_mini_ht_api/routers"
  12. _ "eta/eta_mini_ht_api/task"
  13. "github.com/beego/beego/v2/server/web"
  14. "sync"
  15. "time"
  16. )
  17. func main() {
  18. htConfig := config.GetConfig(contants.HT).(*config.HTBizConfig)
  19. if web.BConfig.RunMode == "dev" {
  20. web.BConfig.WebConfig.DirectoryIndex = true
  21. web.BConfig.WebConfig.StaticDir["/swagger"] = "swagger"
  22. }
  23. //sysConfig := config.GetConfig(contants.HT).(*config.HTBizConfig)
  24. //web.ErrorHandler("*", exception.ControllerAdvice())
  25. web.BConfig.RecoverFunc = exception.PanicAdvice
  26. go func() {
  27. //内存数据预热预加载
  28. logger.Info("开始预加载数据")
  29. if htConfig.EnableTask() {
  30. //初始化研报库
  31. initReport()
  32. }
  33. }()
  34. logger.Info("初始化成功")
  35. web.Run()
  36. }
  37. func initReport() {
  38. var wg sync.WaitGroup
  39. wg.Add(2)
  40. logger.Info("开始初始化研报库")
  41. go func() {
  42. defer wg.Done()
  43. for {
  44. id, err := report.GetETALatestReportId()
  45. var etaReportList []eta.ETAReport
  46. etaReportList, err = eta.GetETAReports(id)
  47. if err != nil {
  48. logger.Error("获取ETA研报列表失败:%v", err)
  49. }
  50. if len(etaReportList) > 0 {
  51. err = report.InitETAReportList(etaReportList)
  52. if err != nil {
  53. logger.Error("同步ETA研报列表失败:%v", err)
  54. }
  55. } else {
  56. logger.Info(contants.TaskFormat, "同步ETA研报库结束")
  57. break
  58. }
  59. }
  60. }()
  61. go func() {
  62. defer wg.Done()
  63. for {
  64. id, err := report.GetHTLatestReportId()
  65. var htReportList []ht.HTReport
  66. htReportList, err = ht.GetHTReports(id)
  67. if err != nil {
  68. logger.Error("获取ETA研报列表失败:%v", err)
  69. }
  70. if len(htReportList) > 0 {
  71. for i := 0; i < len(htReportList); i++ {
  72. timestamp := int64(htReportList[i].PublishTime)
  73. t := time.UnixMilli(timestamp)
  74. htReportList[i].PublishedTime = t.Format(time.DateTime)
  75. plateId := htReportList[i].PlateId
  76. plate, err := ht.GetPermissionNameById(plateId)
  77. if err != nil || plate.ParentId == 0 {
  78. htReportList[i].PermissionName = htReportList[i].PlateName
  79. } else {
  80. PermissionName, err := getPermissionNameById(plate.ParentId)
  81. if err != nil {
  82. logger.Error("获取ETA研报列表失败:%v", err)
  83. htReportList[i].PermissionName = ""
  84. } else {
  85. htReportList[i].PermissionName = PermissionName
  86. }
  87. }
  88. }
  89. var stop bool
  90. stop, err = report.InitHTReportList(htReportList)
  91. if err != nil {
  92. logger.Error("同步ETA研报列表失败:%v", err)
  93. break
  94. }
  95. if stop {
  96. logger.Info(contants.TaskFormat, "同步HT研报库结束")
  97. break
  98. }
  99. } else {
  100. logger.Info(contants.TaskFormat, "同步HT研报库结束")
  101. break
  102. }
  103. }
  104. }()
  105. wg.Wait()
  106. logger.Info("初始化研报库完成")
  107. }
  108. func getPermissionNameById(id int) (name string, err error) {
  109. plate, err := ht.GetPermissionNameById(id)
  110. if err != nil {
  111. return
  112. }
  113. if plate.ParentId != 0 {
  114. return getPermissionNameById(plate.ParentId)
  115. } else {
  116. return plate.PlateName, nil
  117. }
  118. }