main.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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 test() {
  26. // pk, _ := auth.ParsePublicKeyFromPEM()
  27. // data := web_hook.AccountOpenInfoReq{
  28. // MobileTel: "18267183251",
  29. // //DealMobileTel string `json:"deal_mobile_tel"`
  30. // ClientName: "陈晗",
  31. // IdKind: 1,
  32. // IdNo: "330501199101080013",
  33. // AccountStatus: "success",
  34. // //ErrorCode int `json:"error_code"`
  35. // IdBeginDate: "2021-03-10",
  36. // IdEndDate: "2029-03-10",
  37. // ErrorMessage: "",
  38. // Timestamp: time.Now().Unix(),
  39. // }
  40. // bytes, _ := json.Marshal(data)
  41. // entry, _ := auth.EncryptWithRSA(pk, bytes)
  42. // fmt.Println(base64.StdEncoding.EncodeToString(entry))
  43. //}
  44. func main() {
  45. htConfig := config.GetConfig(contants.HT).(*config.HTBizConfig)
  46. if web.BConfig.RunMode == "dev" {
  47. web.BConfig.WebConfig.DirectoryIndex = true
  48. web.BConfig.WebConfig.StaticDir["/swagger"] = "swagger"
  49. }
  50. //web.ErrorHandler("*", exception.ControllerAdvice())
  51. web.BConfig.RecoverFunc = exception.PanicAdvice
  52. go func() {
  53. //内存数据预热预加载
  54. logger.Info("开始预加载数据")
  55. if htConfig.EnableTask() {
  56. //初始化研报库
  57. initReport()
  58. }
  59. //初始化第三方AccessToken
  60. initThirdPartyAccessToken()
  61. //初始化商户信息
  62. initMerchant(htConfig.GetMerchantId())
  63. //初始化上架产品
  64. }()
  65. logger.Info("初始化成功")
  66. //test()
  67. web.Run()
  68. }
  69. func initThirdPartyAccessToken() {
  70. accessToken := redis.GenerateCAPAccessTokenKey()
  71. if redisUtils.GetString(accessToken) == "" {
  72. logger.Info("开始初始化CAP AccessToken")
  73. token, err := htApi.GetAccessToken()
  74. if err != nil {
  75. logger.Error("获取CAP AccessToken失败:%v", err)
  76. return
  77. }
  78. expireTime, err := time.Parse(time.DateTime, token.ExpiresAt)
  79. if err != nil {
  80. logger.Error("解析过期时间失败:%v", err)
  81. return
  82. }
  83. duration := expireTime.Sub(time.Now()).Seconds()
  84. _ = redisUtils.SetString(accessToken, token.AccessToken, int(duration)-5)
  85. //fmt.Printf(base64.StdEncoding.EncodeToString([]byte(token.AccessToken)))
  86. }
  87. }
  88. func initReport() {
  89. var wg sync.WaitGroup
  90. wg.Add(2)
  91. logger.Info("开始初始化研报库")
  92. go func() {
  93. defer wg.Done()
  94. for {
  95. id, err := report.GetETALatestReportId()
  96. var etaReportList []eta.ETAReport
  97. etaReportList, err = eta.GetETAReports(id)
  98. if err != nil {
  99. logger.Error("获取ETA研报列表失败:%v", err)
  100. }
  101. if len(etaReportList) > 0 {
  102. err = report.InitETAReportList(etaReportList)
  103. if err != nil {
  104. logger.Error("同步ETA研报列表失败:%v", err)
  105. }
  106. } else {
  107. logger.Info(contants.TaskFormat, "同步ETA研报库结束")
  108. break
  109. }
  110. }
  111. }()
  112. go func() {
  113. defer wg.Done()
  114. for {
  115. id, err := report.GetHTLatestReportId()
  116. var htReportList []ht.HTReport
  117. htReportList, err = ht.GetHTReports(id)
  118. if err != nil {
  119. logger.Error("获取ETA研报列表失败:%v", err)
  120. }
  121. if len(htReportList) > 0 {
  122. for i := 0; i < len(htReportList); i++ {
  123. timestamp := int64(htReportList[i].PublishTime)
  124. t := time.UnixMilli(timestamp)
  125. htReportList[i].PublishedTime = t.Format(time.DateTime)
  126. plateId := htReportList[i].PlateId
  127. var plate ht.HTPlate
  128. plate, err = ht.GetPermissionNameById(plateId)
  129. if err != nil || plate.ParentId == 0 {
  130. htReportList[i].PermissionName = htReportList[i].PlateName
  131. } else {
  132. var PermissionName string
  133. err = getPermissionNameById(plate.Id, &PermissionName)
  134. if err != nil {
  135. logger.Error("获取ETA研报列表失败:%v", err)
  136. htReportList[i].PermissionName = ""
  137. } else {
  138. htReportList[i].PermissionName = PermissionName
  139. }
  140. }
  141. }
  142. var stop bool
  143. stop, err = report.InitHTReportList(htReportList)
  144. if err != nil {
  145. logger.Error("同步ETA研报列表失败:%v", err)
  146. break
  147. }
  148. if stop {
  149. logger.Info(contants.TaskFormat, "同步HT研报库结束")
  150. break
  151. }
  152. } else {
  153. logger.Info(contants.TaskFormat, "同步HT研报库结束")
  154. break
  155. }
  156. }
  157. }()
  158. wg.Wait()
  159. logger.Info("初始化研报库完成")
  160. }
  161. func initMerchant(merchantId string) {
  162. merchantList, err := merchantDao.GetEnablerMerchants()
  163. if err != nil {
  164. logger.Error("加载商户信息失败:%v", err)
  165. }
  166. if len(merchantList) == 0 {
  167. logger.Warn("未配置商户信息,请配置商户信息")
  168. return
  169. }
  170. if merchantId != "" {
  171. find := false
  172. for _, merchant := range merchantList {
  173. if merchant.MerchantID == merchantId {
  174. err = redisUtils.SetString(redis.GenerateMerchantKey(), merchant.MerchantID, 0)
  175. if err != nil {
  176. logger.Error("ID添加失败:%v", err)
  177. return
  178. }
  179. find = true
  180. break
  181. }
  182. if !find {
  183. logger.Error("未找到启动的商户信息,请配置商户信息,商户ID:[" + merchantId + "]")
  184. }
  185. }
  186. } else {
  187. err = redisUtils.SetString(redis.GenerateMerchantKey(), merchantList[0].MerchantID, 0)
  188. if err != nil {
  189. logger.Error("redis商户ID添加失败:%v", err)
  190. return
  191. }
  192. }
  193. }
  194. func getPermissionNameById(id int, currentName *string) (err error) {
  195. plate, err := ht.GetPermissionNameById(id)
  196. if err != nil {
  197. logger.Error("查询海通板块品种名称失败:%v", err)
  198. return
  199. }
  200. if plate.ParentId != 0 {
  201. *currentName = plate.PlateName
  202. return getPermissionNameById(plate.ParentId, currentName)
  203. } else {
  204. return
  205. }
  206. }