base_from_wind.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. package models
  2. import (
  3. "eta/eta_index_lib/utils"
  4. "fmt"
  5. "github.com/beego/beego/v2/client/orm"
  6. "strconv"
  7. "strings"
  8. "time"
  9. )
  10. type EdbDataFromWind struct {
  11. Close map[string]float64 `json:"CLOSE"`
  12. Dt map[string]int64 `json:"DT"`
  13. ErrorCode map[string]int64 `json:"ErrorCode"`
  14. ErrMsg string
  15. }
  16. func AddEdbDataFromWind(edbCode string, item *EdbDataFromWind) (err error) {
  17. o := orm.NewOrm()
  18. var isAdd bool
  19. addSql := ` INSERT INTO edb_data_wind(edb_info_id,edb_code,data_time,value,create_time,modify_time,data_timestamp) values `
  20. for k, v := range item.Dt {
  21. timeStr := fmt.Sprintf("%d", v)
  22. v = v / 1000
  23. t := time.Unix(v, 0)
  24. dateTime := t.Format(utils.FormatDate)
  25. val := item.Close[k]
  26. saveVal := utils.SubFloatToString(val, 20)
  27. addSql += GetAddSql("0", edbCode, dateTime, timeStr, saveVal)
  28. isAdd = true
  29. }
  30. if isAdd {
  31. addSql = strings.TrimRight(addSql, ",")
  32. _, err = o.Raw(addSql).Exec()
  33. if err != nil {
  34. return
  35. }
  36. }
  37. return
  38. }
  39. // RefreshEdbDataFromWind 刷新wind指标数据
  40. func RefreshEdbDataFromWind(edbInfoId int, edbCode, startDate string, item *EdbDataFromWind) (err error) {
  41. o := orm.NewOrm()
  42. source := utils.DATA_SOURCE_WIND
  43. // 真实数据的最大日期 , 插入规则配置的日期
  44. var realDataMaxDate, edbDataInsertConfigDate time.Time
  45. var edbDataInsertConfig *EdbDataInsertConfig
  46. var isFindConfigDateRealData bool //是否找到配置日期的实际数据的值
  47. {
  48. edbDataInsertConfig, err = GetEdbDataInsertConfigByEdbId(edbInfoId)
  49. if err != nil && err.Error() != utils.ErrNoRow() {
  50. return
  51. }
  52. if edbDataInsertConfig != nil {
  53. edbDataInsertConfigDate = edbDataInsertConfig.Date
  54. }
  55. }
  56. var condition string
  57. var pars []interface{}
  58. condition += " AND edb_info_id=? "
  59. pars = append(pars, edbInfoId)
  60. var startDateTime time.Time
  61. if startDate != "" {
  62. condition += " AND data_time>=? "
  63. pars = append(pars, startDate)
  64. startDateTime, _ = time.ParseInLocation(utils.FormatDate, startDate, time.Local)
  65. }
  66. existList, err := GetEdbDataByCondition(source, condition, pars)
  67. existMap := make(map[string]*EdbInfoSearchData)
  68. for _, v := range existList {
  69. existMap[v.DataTime] = v
  70. }
  71. addSql := ` INSERT INTO edb_data_wind(edb_info_id,edb_code,data_time,value,create_time,modify_time,data_timestamp) values `
  72. var isAdd bool
  73. addMap := make(map[string]string)
  74. edbInfoIdStr := strconv.Itoa(edbInfoId)
  75. for k, v := range item.Dt {
  76. timeStr := fmt.Sprintf("%d", v)
  77. v = v / 1000
  78. t := time.Unix(v, 0)
  79. dateTime := t.Format(utils.FormatDate)
  80. t, _ = time.ParseInLocation(utils.FormatDate, dateTime, time.Local) // 这里的目的是为了处理成北京时间,所以多转一遍
  81. val := item.Close[k]
  82. saveVal := utils.SubFloatToString(val, 30)
  83. //如果传入的开始时间是空的,且当前数据日期早于传入的开始日期,那么需要判断下当前日期的数据是否存在
  84. if !startDateTime.IsZero() && t.Before(startDateTime) {
  85. tmpItem, tmpErr := GetEdbDataByDate(source, edbCode, dateTime)
  86. if tmpErr == nil && tmpItem != nil {
  87. existMap[tmpItem.DataTime] = tmpItem
  88. }
  89. }
  90. if findItem, ok := existMap[dateTime]; !ok {
  91. if _, existOk := addMap[dateTime]; !existOk {
  92. isAdd = true
  93. addSql += GetAddSql(edbInfoIdStr, edbCode, dateTime, timeStr, saveVal)
  94. addMap[dateTime] = saveVal
  95. }
  96. } else {
  97. if findItem != nil && utils.SubFloatToString(findItem.Value, 30) != saveVal {
  98. err = ModifyEdbDataById(source, findItem.EdbDataId, saveVal)
  99. if err != nil {
  100. return err
  101. }
  102. }
  103. }
  104. // 下面代码主要目的是处理掉手动插入的数据判断
  105. {
  106. if realDataMaxDate.IsZero() || t.After(realDataMaxDate) {
  107. realDataMaxDate = t
  108. }
  109. if edbDataInsertConfigDate.IsZero() || t.Equal(edbDataInsertConfigDate) {
  110. isFindConfigDateRealData = true
  111. }
  112. }
  113. }
  114. // 处理手工数据补充的配置
  115. HandleConfigInsertEdbData(realDataMaxDate, edbDataInsertConfig, edbInfoId, source, existMap, isFindConfigDateRealData)
  116. if isAdd {
  117. addSql = strings.TrimRight(addSql, ",")
  118. _, err = o.Raw(addSql).Exec()
  119. if err != nil {
  120. fmt.Println("RefreshEdbDataFromWind add Err", err.Error())
  121. return
  122. }
  123. }
  124. return
  125. }
  126. type ServerUrlCountGroup struct {
  127. ServerUrl string
  128. Total int
  129. }
  130. // GetServerUrlCountGroupByServerUrl 获取服务器地址分组总数
  131. func GetServerUrlCountGroupByServerUrl() (list []ServerUrlCountGroup, err error) {
  132. o := orm.NewOrm()
  133. sql := `select server_url,count(1) total from edb_info where source = 2 AND no_update=0 AND server_url != "" group by server_url; `
  134. _, err = o.Raw(sql).QueryRows(&list)
  135. return
  136. }