edb_data_wind.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. "strconv"
  9. "strings"
  10. "time"
  11. )
  12. type EdbDataWind struct {
  13. EdbDataId int `orm:"column(edb_data_id);pk"`
  14. EdbInfoId int
  15. EdbCode string
  16. DataTime string
  17. Value float64
  18. Status int
  19. CreateTime time.Time
  20. ModifyTime time.Time
  21. DataTimestamp int64
  22. }
  23. func AddEdbDataWind(items []*EdbDataWind) (err error) {
  24. o := orm.NewOrmUsingDB("data")
  25. _, err = o.InsertMulti(1, items)
  26. return
  27. }
  28. func AddEdbDataWindBySql(sqlStr string) (err error) {
  29. o := orm.NewOrmUsingDB("data")
  30. _, err = o.Raw(sqlStr).Exec()
  31. return
  32. }
  33. func DeleteEdbDataWind(edbCode string) (err error) {
  34. o := orm.NewOrmUsingDB("data")
  35. sql := `DELETE FROM edb_data_wind WHERE edb_code=? `
  36. _, err = o.Raw(sql, edbCode).Exec()
  37. return
  38. }
  39. func ModifyEdbDataWind(edbInfoId int64, dataTime string, value float64) (err error) {
  40. o := orm.NewOrmUsingDB("data")
  41. sql := ` UPDATE edb_data_wind SET value=?,modify_time=NOW() WHERE edb_info_id=? AND data_time=? `
  42. _, err = o.Raw(sql, value, edbInfoId, dataTime).Exec()
  43. return
  44. }
  45. func GetEdbDataWindByCode(edbCode string, size int) (items []*EdbInfoSearchData, err error) {
  46. o := orm.NewOrmUsingDB("data")
  47. sql := ` SELECT * FROM edb_data_wind WHERE edb_code=? ORDER BY data_time DESC LIMIT ? `
  48. _, err = o.Raw(sql, edbCode, size).QueryRows(&items)
  49. return
  50. }
  51. func GetEdbDataWindMaxOrMinDate(edbCode string) (min_date, max_date string, err error) {
  52. o := orm.NewOrmUsingDB("data")
  53. sql := ` SELECT MIN(data_time) AS min_date,MAX(data_time) AS max_date FROM edb_data_wind WHERE edb_code=? `
  54. err = o.Raw(sql, edbCode).QueryRow(&min_date, &max_date)
  55. return
  56. }
  57. func GetEdbDataWindByCodeAndDate(edbCode string, startDate string) (count int, err error) {
  58. o := orm.NewOrmUsingDB("data")
  59. sql := ` SELECT COUNT(1) AS count FROM edb_data_wind WHERE edb_code=? AND data_time=? `
  60. err = o.Raw(sql, edbCode, startDate).QueryRow(&count)
  61. return
  62. }
  63. type EdbDataFromWind struct {
  64. Close map[string]float64 `json:"CLOSE"`
  65. Dt map[string]int64 `json:"DT"`
  66. ErrMsg string
  67. }
  68. func RefreshAllEdbDataByWind(edbInfoId, source int, edbCode, startDate, endDate string) (err error) {
  69. o := orm.NewOrmUsingDB("data")
  70. to, err := o.Begin()
  71. if err != nil {
  72. return
  73. }
  74. defer func() {
  75. if err != nil {
  76. _ = to.Rollback()
  77. } else {
  78. _ = to.Commit()
  79. }
  80. }()
  81. fmt.Println("wind start:", time.Now())
  82. thsUrl := utils.Hz_Data_Url + `edbInfo/wind?EdbCode=%s&StartDate=%s&EndDate=%s`
  83. thsUrl = fmt.Sprintf(thsUrl, edbCode, startDate, endDate)
  84. utils.FileLog.Info("thsUrl:%s", thsUrl)
  85. body, err := http.Get(thsUrl)
  86. utils.FileLog.Info("wind %s", string(body))
  87. fmt.Println("wind end:", time.Now())
  88. if err != nil {
  89. return
  90. }
  91. item := new(EdbDataFromWind)
  92. err = json.Unmarshal(body, &item)
  93. if err != nil {
  94. return
  95. }
  96. //获取指标所有数据
  97. dataList := make([]*EdbDataBase, 0)
  98. dataTableName := GetEdbDataTableName(source)
  99. sql := `SELECT * FROM %s WHERE edb_info_id=? `
  100. sql = fmt.Sprintf(sql, dataTableName)
  101. _, err = to.Raw(sql, edbInfoId).QueryRows(&dataList)
  102. if err != nil {
  103. return err
  104. }
  105. dataMap := make(map[string]string)
  106. for _, v := range dataList {
  107. dataMap[v.DataTime] = v.Value
  108. }
  109. addSql := ` INSERT INTO edb_data_wind(edb_info_id,edb_code,data_time,value,create_time,modify_time,status,data_timestamp) values `
  110. var isAdd bool
  111. edbInfoIdStr := strconv.Itoa(edbInfoId)
  112. existDataMap := make(map[string]string)
  113. fmt.Println("start for:", time.Now())
  114. for k, v := range item.Dt {
  115. timeStr := fmt.Sprintf("%d", v)
  116. v = v / 1000
  117. t := time.Unix(v, 0)
  118. dateTime := t.Format(utils.FormatDate)
  119. val := item.Close[k]
  120. saveVal := utils.SubFloatToString(val, 30)
  121. if existVal, ok := dataMap[dateTime]; !ok {
  122. if _, existOk := existDataMap[dateTime]; !existOk {
  123. isAdd = true
  124. addSql += GetAddSql(edbInfoIdStr, edbCode, dateTime, timeStr, saveVal)
  125. }
  126. existDataMap[dateTime] = dateTime
  127. } else {
  128. if existVal != saveVal {
  129. sql := ` UPDATE %s SET value=?,modify_time=NOW() WHERE edb_info_id=? AND data_time=? `
  130. sql = fmt.Sprintf(sql, dataTableName)
  131. _, err = to.Raw(sql, saveVal, edbInfoId, dateTime).Exec()
  132. if err != nil {
  133. return err
  134. }
  135. }
  136. }
  137. }
  138. fmt.Println("end for:", time.Now())
  139. if isAdd {
  140. addSql = strings.TrimRight(addSql, ",")
  141. _, err = to.Raw(addSql).Exec()
  142. if err != nil {
  143. fmt.Println("RefreshAllEdbDataByWind add Err", err.Error())
  144. return
  145. }
  146. }
  147. return
  148. }