base_from_wind.go 4.9 KB

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