edb_data_wind.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. "strconv"
  9. "strings"
  10. "time"
  11. )
  12. type EdbDataFromWind struct {
  13. Close map[string]float64 `json:"CLOSE"`
  14. Dt map[string]int64 `json:"DT"`
  15. ErrMsg string
  16. }
  17. // 万得
  18. func GetEdbDataByWind(edbCode, startDate, endDate string) (searchItem *data_manage.EdbInfoSearch, err error) {
  19. defer func() {
  20. if err != nil {
  21. fmt.Println("GetEdbDataByWind Err:" + err.Error())
  22. }
  23. }()
  24. thsUrl := utils.Hz_Data_Url + `edbInfo/wind?EdbCode=%s&StartDate=%s&EndDate=%s`
  25. thsUrl = fmt.Sprintf(thsUrl, edbCode, startDate, endDate)
  26. utils.FileLog.Info("thsUrl:%s", thsUrl)
  27. body, err := http.Get(thsUrl)
  28. fmt.Println("GetEdbDataByThs body:")
  29. fmt.Println(string(body))
  30. utils.FileLog.Info("wind %s", string(body))
  31. if err != nil {
  32. return
  33. }
  34. item := new(EdbDataFromWind)
  35. err = json.Unmarshal(body, &item)
  36. if err != nil {
  37. return
  38. }
  39. var isAdd bool
  40. addSql := ` INSERT INTO edb_data_wind(edb_info_id,edb_code,data_time,value,create_time,modify_time,status,data_timestamp) values `
  41. nowStr := time.Now().Format(utils.FormatDateTime)
  42. for k, v := range item.Dt {
  43. timeStr := fmt.Sprintf("%d", v)
  44. v = v / 1000
  45. t := time.Unix(v, 0)
  46. dateTime := t.Format(utils.FormatDate)
  47. val := item.Close[k]
  48. addSql += "("
  49. addSql += "0," + "'" + edbCode + "'" + "," + "'" + dateTime + "'" + "," + utils.SubFloatToString(val, 20) + "," + "'" + nowStr + "'" +
  50. "," + "'" + nowStr + "'" + "," + "0" + "," + "'" + timeStr + "'"
  51. addSql += "),"
  52. isAdd = true
  53. }
  54. searchItem = new(data_manage.EdbInfoSearch)
  55. searchItem.EdbCode = edbCode
  56. if isAdd {
  57. addSql = strings.TrimRight(addSql, ",")
  58. err = data_manage.AddEdbDataWindBySql(addSql)
  59. if err != nil {
  60. utils.FileLogData.Info("GetEdbDataByPb AddEdbDataPbBySql Err:%s", err.Error())
  61. return searchItem, err
  62. }
  63. }
  64. size := utils.EDB_DATA_LIMIT
  65. dataList, err := data_manage.GetEdbDataWindByCode(edbCode, size)
  66. if err != nil {
  67. fmt.Println("GetEdbDataWindByCode Err", err.Error())
  68. return searchItem, err
  69. }
  70. minDate, maxDate, err := data_manage.GetEdbDataWindMaxOrMinDate(edbCode)
  71. if err != nil {
  72. fmt.Println("GetEdbDataWindByCode Err", err.Error())
  73. return searchItem, err
  74. }
  75. searchItem.DataList = dataList
  76. searchItem.StartDate = minDate
  77. searchItem.EndDate = maxDate
  78. if searchItem.DataList == nil {
  79. searchItem.DataList = make([]*data_manage.EdbInfoSearchData, 0)
  80. }
  81. return
  82. }
  83. func RefreshEdbDataByWind(edbInfoId int, edbCode, startDate, endDate string) (err error) {
  84. defer func() {
  85. if err != nil {
  86. fmt.Println("GetEdbDataByWind Err:" + err.Error())
  87. }
  88. }()
  89. fmt.Println("startDate,endDate:", startDate, endDate)
  90. thsUrl := utils.Hz_Data_Url + `edbInfo/wind?EdbCode=%s&StartDate=%s&EndDate=%s`
  91. thsUrl = fmt.Sprintf(thsUrl, edbCode, startDate, endDate)
  92. utils.FileLog.Info("thsUrl:%s", thsUrl)
  93. body, err := http.Get(thsUrl)
  94. fmt.Println("GetEdbDataByThs body:")
  95. fmt.Println(string(body))
  96. utils.FileLog.Info("wind result:" + string(body))
  97. if err != nil {
  98. return
  99. }
  100. item := new(EdbDataFromWind)
  101. err = json.Unmarshal(body, &item)
  102. if err != nil {
  103. return
  104. }
  105. addSql := ` INSERT INTO edb_data_wind(edb_info_id,edb_code,data_time,value,create_time,modify_time,status,data_timestamp) values `
  106. nowStr := time.Now().Format(utils.FormatDateTime)
  107. var isAdd bool
  108. for k, v := range item.Dt {
  109. timeStr := fmt.Sprintf("%d", v)
  110. v = v / 1000
  111. t := time.Unix(v, 0)
  112. dateTime := t.Format(utils.FormatDate)
  113. val := item.Close[k]
  114. count, err := data_manage.GetEdbDataWindByCodeAndDate(edbCode, dateTime)
  115. if err != nil && err.Error() != utils.ErrNoRow() {
  116. return err
  117. }
  118. if count <= 0 {
  119. isAdd = true
  120. fmt.Println(dateTime)
  121. addSql += "("
  122. addSql += strconv.Itoa(edbInfoId) + "," + "'" + edbCode + "'" + "," + "'" + dateTime + "'" + "," + utils.SubFloatToString(val, 30) + "," + "'" + nowStr + "'" +
  123. "," + "'" + nowStr + "'" + "," + "1"
  124. addSql += "," + "'" + timeStr + "'"
  125. addSql += "),"
  126. } else {
  127. err = data_manage.ModifyEdbDataWind(int64(edbInfoId), dateTime, val)
  128. if err != nil {
  129. return err
  130. }
  131. }
  132. }
  133. if isAdd {
  134. addSql = strings.TrimRight(addSql, ",")
  135. err = data_manage.AddEdbDataWindBySql(addSql)
  136. if err != nil {
  137. fmt.Println("AddEdbDataWind Err", err.Error())
  138. return
  139. }
  140. }
  141. return
  142. }