edb_data_lt.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. package data_manage
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "github.com/rdlucklib/rdluck_tools/http"
  6. "github.com/beego/beego/v2/client/orm"
  7. "hongze/hongze_task/utils"
  8. "reflect"
  9. "strconv"
  10. "strings"
  11. "time"
  12. )
  13. type EdbDataLt struct {
  14. EdbDataId int `orm:"column(edb_data_id);pk"`
  15. EdbInfoId int
  16. EdbCode string
  17. DataTime string
  18. Value float64
  19. Status int
  20. CreateTime time.Time
  21. ModifyTime time.Time
  22. DataTimestamp int64
  23. }
  24. // AddEdbDataLtBySql 执行添加数据的sql
  25. func AddEdbDataLtBySql(sqlStr string) (err error) {
  26. o := orm.NewOrmUsingDB("data")
  27. _, err = o.Raw(sqlStr).Exec()
  28. return
  29. }
  30. // ModifyEdbDataLt 修改路透社数据
  31. func ModifyEdbDataLt(edbInfoId int64, dataTime string, value float64) (err error) {
  32. o := orm.NewOrmUsingDB("data")
  33. sql := ` UPDATE edb_data_lt SET value=?,modify_time=NOW() WHERE edb_info_id=? AND data_time=? `
  34. _, err = o.Raw(sql, value, edbInfoId, dataTime).Exec()
  35. return
  36. }
  37. func GetEdbDataLtMaxOrMinDate(edbCode string) (min_date, max_date string, err error) {
  38. o := orm.NewOrmUsingDB("data")
  39. sql := ` SELECT MIN(data_time) AS min_date,MAX(data_time) AS max_date FROM edb_data_lt WHERE edb_code=? `
  40. err = o.Raw(sql, edbCode).QueryRow(&min_date, &max_date)
  41. return
  42. }
  43. func GetEdbDataLtByCodeAndDate(edbCode string, startDate string) (count int, err error) {
  44. o := orm.NewOrmUsingDB("data")
  45. sql := ` SELECT COUNT(1) AS count FROM edb_data_lt WHERE edb_code=? AND data_time=? `
  46. err = o.Raw(sql, edbCode, startDate).QueryRow(&count)
  47. return
  48. }
  49. func GetEdbDataLtByCode(edbCode string, size int) (items []*EdbInfoSearchData, err error) {
  50. o := orm.NewOrmUsingDB("data")
  51. sql := ` SELECT * FROM edb_data_lt WHERE edb_code=? ORDER BY data_time DESC LIMIT ? `
  52. _, err = o.Raw(sql, edbCode, size).QueryRows(&items)
  53. return
  54. }
  55. //刷新所有数据
  56. func RefreshAllEdbDataByLt(edbInfoId, source int, edbCode, startDate, endDate string) (err error) {
  57. // 获取路透社数据
  58. ltDataList, err := QueryEdbDataByLt(edbCode, startDate, endDate)
  59. if err != nil {
  60. return
  61. }
  62. o := orm.NewOrmUsingDB("data")
  63. tx,err:=o.Begin()
  64. defer func() {
  65. if err != nil {
  66. tx.Rollback()
  67. } else {
  68. tx.Commit()
  69. }
  70. }()
  71. //获取指标所有数据
  72. dataList := make([]*EdbDataBase, 0)
  73. dataTableName := GetEdbDataTableName(source)
  74. sql := `SELECT * FROM %s WHERE edb_info_id=? `
  75. sql = fmt.Sprintf(sql, dataTableName)
  76. _, err = o.Raw(sql, edbInfoId).QueryRows(&dataList)
  77. if err != nil {
  78. return err
  79. }
  80. dataMap := make(map[string]string)
  81. for _, v := range dataList {
  82. dataMap[v.DataTime] = v.Value
  83. }
  84. edbInfoIdStr := strconv.Itoa(edbInfoId)
  85. addSql := ` INSERT INTO edb_data_lt(edb_info_id,edb_code,data_time,value,create_time,modify_time,status,data_timestamp) values `
  86. var isAdd bool
  87. for timestampInt, edbValue := range ltDataList {
  88. dataTime := time.Unix(timestampInt/1000, 0)
  89. //校验数据类型对不对
  90. valType := reflect.TypeOf(edbValue)
  91. if valType == nil {
  92. continue
  93. }
  94. if valType.String() != "float64" {
  95. continue
  96. }
  97. sValue := edbValue.(float64)
  98. eDate := dataTime.Format(utils.FormatDate)
  99. if err != nil {
  100. return err
  101. }
  102. saveValue := utils.SubFloatToString(sValue, 30)
  103. if existVal, ok := dataMap[eDate]; !ok {
  104. timestamp := dataTime.UnixNano() / 1e6
  105. timeStr := fmt.Sprintf("%d", timestamp)
  106. addSql += GetAddSql(edbInfoIdStr, edbCode, eDate, timeStr, saveValue)
  107. isAdd = true
  108. } else {
  109. if existVal != saveValue {
  110. sql := ` UPDATE %s SET value=?,modify_time=NOW() WHERE edb_info_id=? AND data_time=? `
  111. sql = fmt.Sprintf(sql, dataTableName)
  112. _, err = tx.Raw(sql, sValue, edbInfoId, eDate).Exec()
  113. if err != nil {
  114. return err
  115. }
  116. }
  117. }
  118. }
  119. if isAdd {
  120. addSql = strings.TrimRight(addSql, ",")
  121. _, err = tx.Raw(addSql).Exec()
  122. if err != nil {
  123. fmt.Println("RefreshAllEdbDataByLt add Err", err.Error())
  124. return
  125. }
  126. }
  127. return
  128. }
  129. type EdbDataFromLt struct {
  130. Close map[int64]interface{} `json:"CLOSE"`
  131. }
  132. // QueryEdbDataByLt 获取路透社数据
  133. func QueryEdbDataByLt(edbCode, startDate, endDate string) (dataList map[int64]interface{}, err error) {
  134. dataList = make(map[int64]interface{})
  135. ltUrl := utils.Hz_Data_LT_Url + `edbInfo/ek?EdbCode=%s&StartDate=%s&EndDate=%s`
  136. ltUrl = fmt.Sprintf(ltUrl, edbCode, startDate, endDate)
  137. utils.FileLog.Info("ltUrl:%s", ltUrl)
  138. body, err := http.Get(ltUrl)
  139. utils.FileLog.Info("lt result:%s", string(body))
  140. if err != nil {
  141. return
  142. }
  143. //fmt.Println(string(body))
  144. item := new(EdbDataFromLt)
  145. err = json.Unmarshal(body, &item)
  146. if err != nil {
  147. return
  148. }
  149. dataList = item.Close
  150. return
  151. }