main.go 6.0 KB

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