123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191 |
- package main
- import (
- "eta/eta_mini_ht_api/api"
- _ "eta/eta_mini_ht_api/common/component"
- "eta/eta_mini_ht_api/common/component/cache"
- "eta/eta_mini_ht_api/common/component/config"
- logger "eta/eta_mini_ht_api/common/component/log"
- "eta/eta_mini_ht_api/common/contants"
- "eta/eta_mini_ht_api/common/exception"
- "eta/eta_mini_ht_api/common/utils/redis"
- "eta/eta_mini_ht_api/domian/report"
- "eta/eta_mini_ht_api/models/eta"
- "eta/eta_mini_ht_api/models/ht"
- merchantDao "eta/eta_mini_ht_api/models/merchant"
- _ "eta/eta_mini_ht_api/routers"
- _ "eta/eta_mini_ht_api/task"
- "github.com/beego/beego/v2/server/web"
- "sync"
- "time"
- )
- var (
- redisUtils = cache.GetInstance()
- htApi = api.GetInstance()
- )
- func main() {
- htConfig := config.GetConfig(contants.HT).(*config.HTBizConfig)
- if web.BConfig.RunMode == "dev" {
- web.BConfig.WebConfig.DirectoryIndex = true
- web.BConfig.WebConfig.StaticDir["/swagger"] = "swagger"
- }
- //web.ErrorHandler("*", exception.ControllerAdvice())
- web.BConfig.RecoverFunc = exception.PanicAdvice
- go func() {
- //内存数据预热预加载
- logger.Info("开始预加载数据")
- if htConfig.EnableTask() {
- //初始化研报库
- initReport()
- }
- //初始化第三方AccessToken
- initThirdPartyAccessToken()
- //初始化商户信息
- initMerchant(htConfig.GetMerchantId())
- //初始化上架产品
- }()
- logger.Info("初始化成功")
- web.Run()
- }
- func initThirdPartyAccessToken() {
- accessToken := redis.GenerateCAPAccessTokenKey()
- if redisUtils.GetString(accessToken) == "" {
- logger.Info("开始初始化CAP AccessToken")
- token, err := htApi.GetAccessToken()
- if err != nil {
- logger.Error("获取CAP AccessToken失败:%v", err)
- return
- }
- expireTime, err := time.Parse(time.DateTime, token.ExpiresAt)
- if err != nil {
- logger.Error("解析过期时间失败:%v", err)
- return
- }
- duration := expireTime.Sub(time.Now()).Seconds()
- _ = redisUtils.SetString(accessToken, token.AccessToken, int(duration)-5)
- //fmt.Printf(base64.StdEncoding.EncodeToString([]byte(token.AccessToken)))
- }
- }
- func initReport() {
- var wg sync.WaitGroup
- wg.Add(2)
- logger.Info("开始初始化研报库")
- go func() {
- defer wg.Done()
- for {
- id, err := report.GetETALatestReportId()
- var etaReportList []eta.ETAReport
- etaReportList, err = eta.GetETAReports(id)
- if err != nil {
- logger.Error("获取ETA研报列表失败:%v", err)
- }
- if len(etaReportList) > 0 {
- err = report.InitETAReportList(etaReportList)
- if err != nil {
- logger.Error("同步ETA研报列表失败:%v", err)
- }
- } else {
- logger.Info(contants.TaskFormat, "同步ETA研报库结束")
- break
- }
- }
- }()
- go func() {
- defer wg.Done()
- for {
- id, err := report.GetHTLatestReportId()
- var htReportList []ht.HTReport
- htReportList, err = ht.GetHTReports(id)
- if err != nil {
- logger.Error("获取ETA研报列表失败:%v", err)
- }
- if len(htReportList) > 0 {
- for i := 0; i < len(htReportList); i++ {
- timestamp := int64(htReportList[i].PublishTime)
- t := time.UnixMilli(timestamp)
- htReportList[i].PublishedTime = t.Format(time.DateTime)
- plateId := htReportList[i].PlateId
- var plate ht.HTPlate
- plate, err = ht.GetPermissionNameById(plateId)
- if err != nil || plate.ParentId == 0 {
- htReportList[i].PermissionName = htReportList[i].PlateName
- } else {
- var PermissionName string
- err = getPermissionNameById(plate.Id, &PermissionName)
- if err != nil {
- logger.Error("获取ETA研报列表失败:%v", err)
- htReportList[i].PermissionName = ""
- } else {
- htReportList[i].PermissionName = PermissionName
- }
- }
- }
- var stop bool
- stop, err = report.InitHTReportList(htReportList)
- if err != nil {
- logger.Error("同步ETA研报列表失败:%v", err)
- break
- }
- if stop {
- logger.Info(contants.TaskFormat, "同步HT研报库结束")
- break
- }
- } else {
- logger.Info(contants.TaskFormat, "同步HT研报库结束")
- break
- }
- }
- }()
- wg.Wait()
- logger.Info("初始化研报库完成")
- }
- func initMerchant(merchantId string) {
- merchantList, err := merchantDao.GetEnablerMerchants()
- if err != nil {
- logger.Error("加载商户信息失败:%v", err)
- }
- if len(merchantList) == 0 {
- logger.Warn("未配置商户信息,请配置商户信息")
- return
- }
- if merchantId != "" {
- find := false
- for _, merchant := range merchantList {
- if merchant.MerchantID == merchantId {
- err = redisUtils.SetString(redis.GenerateMerchantKey(), merchant.MerchantID, 0)
- if err != nil {
- logger.Error("ID添加失败:%v", err)
- return
- }
- find = true
- break
- }
- if !find {
- logger.Error("未找到启动的商户信息,请配置商户信息,商户ID:[" + merchantId + "]")
- }
- }
- } else {
- err = redisUtils.SetString(redis.GenerateMerchantKey(), merchantList[0].MerchantID, 0)
- if err != nil {
- logger.Error("redis商户ID添加失败:%v", err)
- return
- }
- }
- }
- func getPermissionNameById(id int, currentName *string) (err error) {
- plate, err := ht.GetPermissionNameById(id)
- if err != nil {
- logger.Error("查询海通板块品种名称失败:%v", err)
- return
- }
- if plate.ParentId != 0 {
- *currentName = plate.PlateName
- return getPermissionNameById(plate.ParentId, currentName)
- } else {
- return
- }
- }
|