edb_data_lt.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. package data_manage
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "github.com/beego/beego/v2/client/orm"
  6. "github.com/rdlucklib/rdluck_tools/http"
  7. "hongze/hz_crm_api/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. to, err := o.Begin()
  64. if err != nil {
  65. return
  66. }
  67. defer func() {
  68. if err != nil {
  69. _ = to.Rollback()
  70. } else {
  71. _ = to.Commit()
  72. }
  73. }()
  74. //获取指标所有数据
  75. dataList := make([]*EdbDataBase, 0)
  76. dataTableName := GetEdbDataTableName(source)
  77. sql := `SELECT * FROM %s WHERE edb_info_id=? `
  78. sql = fmt.Sprintf(sql, dataTableName)
  79. _, err = to.Raw(sql, edbInfoId).QueryRows(&dataList)
  80. if err != nil {
  81. return err
  82. }
  83. dataMap := make(map[string]string)
  84. for _, v := range dataList {
  85. dataMap[v.DataTime] = v.Value
  86. }
  87. edbInfoIdStr := strconv.Itoa(edbInfoId)
  88. addSql := ` INSERT INTO edb_data_lt(edb_info_id,edb_code,data_time,value,create_time,modify_time,status,data_timestamp) values `
  89. var isAdd bool
  90. for timestampInt, edbValue := range ltDataList {
  91. dataTime := time.Unix(timestampInt/1000, 0)
  92. //校验数据类型对不对
  93. valType := reflect.TypeOf(edbValue)
  94. if valType == nil {
  95. continue
  96. }
  97. if valType.String() != "float64" {
  98. continue
  99. }
  100. sValue := edbValue.(float64)
  101. eDate := dataTime.Format(utils.FormatDate)
  102. if err != nil {
  103. return err
  104. }
  105. saveValue := utils.SubFloatToString(sValue, 30)
  106. if existVal, ok := dataMap[eDate]; !ok {
  107. timestamp := dataTime.UnixNano() / 1e6
  108. timeStr := fmt.Sprintf("%d", timestamp)
  109. addSql += GetAddSql(edbInfoIdStr, edbCode, eDate, timeStr, saveValue)
  110. isAdd = true
  111. } else {
  112. if existVal != saveValue {
  113. sql := ` UPDATE %s SET value=?,modify_time=NOW() WHERE edb_info_id=? AND data_time=? `
  114. sql = fmt.Sprintf(sql, dataTableName)
  115. _, err = to.Raw(sql, sValue, edbInfoId, eDate).Exec()
  116. if err != nil {
  117. return err
  118. }
  119. }
  120. }
  121. }
  122. if isAdd {
  123. addSql = strings.TrimRight(addSql, ",")
  124. _, err = to.Raw(addSql).Exec()
  125. if err != nil {
  126. fmt.Println("RefreshAllEdbDataByLt add Err", err.Error())
  127. return
  128. }
  129. }
  130. return
  131. }
  132. type EdbDataFromLt struct {
  133. Close map[int64]interface{} `json:"CLOSE"`
  134. }
  135. // QueryEdbDataByLt 获取路透社数据
  136. func QueryEdbDataByLt(edbCode, startDate, endDate string) (dataList map[int64]interface{}, err error) {
  137. dataList = make(map[int64]interface{})
  138. ltUrl := utils.Hz_Data_LT_Url + `edbInfo/ek?EdbCode=%s&StartDate=%s&EndDate=%s`
  139. ltUrl = fmt.Sprintf(ltUrl, edbCode, startDate, endDate)
  140. utils.FileLogData.Info("ltUrl:%s", ltUrl)
  141. body, err := http.Get(ltUrl)
  142. utils.FileLogData.Info("lt result:%s", string(body))
  143. if err != nil {
  144. return
  145. }
  146. //fmt.Println(string(body))
  147. item := new(EdbDataFromLt)
  148. err = json.Unmarshal(body, &item)
  149. if err != nil {
  150. return
  151. }
  152. dataList = item.Close
  153. return
  154. }