main.go 3.1 KB

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