base_from_wind_wsd.go 4.5 KB

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