edb_data_lt.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. package data
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "github.com/rdlucklib/rdluck_tools/http"
  6. "hongze/hz_crm_api/models/data_manage"
  7. "hongze/hz_crm_api/utils"
  8. "reflect"
  9. "strings"
  10. "time"
  11. )
  12. type EdbDataFromLt struct {
  13. Close map[int64]interface{} `json:"CLOSE"`
  14. }
  15. // GetEdbDataByLt 获取路透社指标
  16. func GetEdbDataByLt(edbCode, startDate, endDate string) (searchItem *data_manage.EdbInfoSearch, err error) {
  17. // 获取路透社数据
  18. ltDataList, err := QueryEdbDataByLt(edbCode, startDate, endDate)
  19. if err != nil {
  20. return
  21. }
  22. searchItem = new(data_manage.EdbInfoSearch)
  23. searchItem.EdbCode = edbCode
  24. if len(ltDataList) > 0 {
  25. var isAdd bool
  26. addSql := ` INSERT INTO edb_data_lt(edb_info_id,edb_code,data_time,value,create_time,modify_time,status,data_timestamp) values `
  27. nowStr := time.Now().Format(utils.FormatDateTime)
  28. dataList := make([]*data_manage.EdbInfoSearchData, 0)
  29. for timestampInt, edbValue := range ltDataList {
  30. dataTime := time.Unix(timestampInt/1000, 0)
  31. //校验数据类型对不对
  32. valType := reflect.TypeOf(edbValue)
  33. if valType == nil {
  34. continue
  35. }
  36. if valType.String() != "float64" {
  37. continue
  38. }
  39. sValue := edbValue.(float64)
  40. eDate := dataTime.Format(utils.FormatDate)
  41. if err != nil {
  42. return nil, err
  43. }
  44. timestamp := dataTime.UnixNano() / 1e6
  45. timeStr := fmt.Sprintf("%d", timestamp)
  46. addSql += "("
  47. addSql += "0," + "'" + edbCode + "'" + "," + "'" + eDate + "'" + "," + utils.SubFloatToString(sValue, 20) + "," + "'" + nowStr + "'" +
  48. "," + "'" + nowStr + "'" + "," + "0" + "," + "'" + timeStr + "'"
  49. addSql += "),"
  50. isAdd = true
  51. }
  52. if isAdd {
  53. addSql = strings.TrimRight(addSql, ",")
  54. err = data_manage.AddEdbDataLtBySql(addSql)
  55. if err != nil {
  56. utils.FileLogData.Info("AddEdbDataLtBySql Err:%s", err.Error())
  57. return
  58. }
  59. }
  60. size := utils.EDB_DATA_LIMIT
  61. dataList, err := data_manage.GetEdbDataAllByEdbCode(edbCode, utils.DATA_SOURCE_LT, size)
  62. if err != nil {
  63. utils.FileLogData.Info("GetEdbDataLtByCode Err:%s", err.Error())
  64. return searchItem, err
  65. }
  66. edbInfoMaxAndMinInfo, err := data_manage.GetEdbInfoMaxAndMinInfo(utils.DATA_SOURCE_LT, edbCode)
  67. if err != nil {
  68. return searchItem, err
  69. }
  70. searchItem.DataList = dataList
  71. searchItem.StartDate = edbInfoMaxAndMinInfo.MinDate
  72. searchItem.EndDate = edbInfoMaxAndMinInfo.MaxDate
  73. }
  74. if searchItem.DataList == nil {
  75. searchItem.DataList = make([]*data_manage.EdbInfoSearchData, 0)
  76. }
  77. return
  78. }
  79. //func init() {
  80. // fmt.Println("aa")
  81. // searchInfo, err := GetEdbDataByLt("OMA-1Mdubsw-A", "2021-01-01", "2021-12-28")
  82. // if err != nil {
  83. // fmt.Println("err:", err)
  84. // } else {
  85. // fmt.Println(searchInfo)
  86. // }
  87. //}
  88. // RefreshEdbDataByLt 刷新路透社指标数据
  89. func RefreshEdbDataByLt(edbInfoId int, edbCode, startDate, endDate string) (err error) {
  90. // 获取路透社数据
  91. //ltDataList,err := QueryEdbDataByLt(edbCode, startDate, endDate)
  92. //if err != nil{
  93. // return
  94. //}
  95. //if item.Errorcode != 0 {
  96. // err = errors.New(string(body))
  97. // return
  98. //}
  99. //if len(item.Tables) > 0 {
  100. // table := item.Tables[0]
  101. // dataLen := len(table.Time)
  102. // addSql := ` INSERT INTO edb_data_lt(edb_info_id,edb_code,data_time,value,create_time,modify_time,status,data_timestamp) values `
  103. // nowStr := time.Now().Format(utils.FormatDateTime)
  104. // var isAdd bool
  105. // for i := 0; i < dataLen; i++ {
  106. // eDate := table.Time[i]
  107. // sValue := table.Value[i]
  108. //
  109. // count, err := data_manage.GetEdbDataLtByCodeAndDate(edbCode, eDate)
  110. // if err != nil && err.Error() != utils.ErrNoRow() {
  111. // return err
  112. // }
  113. // if count <= 0 {
  114. // dataTime, err := time.Parse(utils.FormatDate, eDate)
  115. // if err != nil {
  116. //
  117. // }
  118. // timestamp := dataTime.UnixNano() / 1e6
  119. // timeStr := fmt.Sprintf("%d", timestamp)
  120. //
  121. // addSql += "("
  122. // addSql += strconv.Itoa(edbInfoId) + "," + "'" + edbCode + "'" + "," + "'" + eDate + "'" + "," + utils.SubFloatToString(sValue, 30) + "," + "'" + nowStr + "'" +
  123. // "," + "'" + nowStr + "'" + "," + "1"
  124. // addSql += "," + "'" + timeStr + "'"
  125. // addSql += "),"
  126. // isAdd = true
  127. // } else {
  128. // err = data_manage.ModifyEdbDataLt(int64(edbInfoId), eDate, sValue)
  129. // if err != nil {
  130. // return err
  131. // }
  132. // }
  133. // }
  134. //
  135. // if isAdd {
  136. // addSql = strings.TrimRight(addSql, ",")
  137. // err = data_manage.AddEdbDataLtBySql(addSql)
  138. // if err != nil {
  139. // fmt.Println("AddEdbDataLt Err", err.Error())
  140. // return
  141. // }
  142. // }
  143. //}
  144. return
  145. }
  146. // QueryEdbDataByLt 获取路透社数据
  147. func QueryEdbDataByLt(edbCode, startDate, endDate string) (dataList map[int64]interface{}, err error) {
  148. dataList = make(map[int64]interface{})
  149. ltUrl := utils.Hz_Data_LT_Url + `edbInfo/ek?EdbCode=%s&StartDate=%s&EndDate=%s`
  150. ltUrl = fmt.Sprintf(ltUrl, edbCode, startDate, endDate)
  151. utils.FileLogData.Info("ltUrl:%s", ltUrl)
  152. body, err := http.Get(ltUrl)
  153. utils.FileLogData.Info("lt result:%s", string(body))
  154. if err != nil {
  155. return
  156. }
  157. //fmt.Println(string(body))
  158. item := new(EdbDataFromLt)
  159. err = json.Unmarshal(body, &item)
  160. if err != nil {
  161. return
  162. }
  163. dataList = item.Close
  164. return
  165. }