base_from_ths.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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 EdbDataFromThs struct {
  11. DataVol int64 `json:"dataVol"`
  12. Errmsg string `json:"errmsg"`
  13. Errorcode int64 `json:"errorcode"`
  14. Perf interface{} `json:"perf"`
  15. Tables []Tables `json:"tables"`
  16. }
  17. // Tables 表格数据
  18. type Tables struct {
  19. ID []string `json:"id"`
  20. Time []string `json:"time"`
  21. Value []float64 `json:"value"`
  22. }
  23. // 新增同花顺指标数据
  24. func AddEdbDataFromThs(edbCode string, item EdbDataFromThs) (err error) {
  25. o := orm.NewOrm()
  26. if len(item.Tables) > 0 {
  27. table := item.Tables[0]
  28. dataLen := len(table.Time)
  29. var isAdd bool
  30. addSql := ` INSERT INTO %s(edb_info_id,edb_code,data_time,value,create_time,modify_time,data_timestamp) values `
  31. tableName := GetEdbDataTableName(utils.DATA_SOURCE_THS)
  32. addSql = fmt.Sprintf(addSql, tableName)
  33. for i := 0; i < dataLen; i++ {
  34. eDate := table.Time[i]
  35. sValue := table.Value[i]
  36. dataTime, err := time.ParseInLocation(utils.FormatDate, eDate, time.Local)
  37. if err != nil {
  38. return err
  39. }
  40. timestamp := dataTime.UnixNano() / 1e6
  41. timeStr := fmt.Sprintf("%d", timestamp)
  42. addSql += GetAddSql("0", edbCode, eDate, timeStr, utils.SubFloatToString(sValue, 20))
  43. isAdd = true
  44. }
  45. if isAdd {
  46. addSql = strings.TrimRight(addSql, ",")
  47. _, err = o.Raw(addSql).Exec()
  48. if err != nil {
  49. return
  50. }
  51. }
  52. }
  53. return
  54. }
  55. // 刷新同花顺指标数据
  56. func RefreshEdbDataFromThs(edbInfoId int, edbCode, startDate string, item EdbDataFromThs) (err error) {
  57. o := orm.NewOrm()
  58. source := utils.DATA_SOURCE_THS
  59. // 真实数据的最大日期 , 插入规则配置的日期
  60. var realDataMaxDate, edbDataInsertConfigDate time.Time
  61. var edbDataInsertConfig *EdbDataInsertConfig
  62. var isFindConfigDateRealData bool //是否找到配置日期的实际数据的值
  63. {
  64. edbDataInsertConfig, err = GetEdbDataInsertConfigByEdbId(edbInfoId)
  65. if err != nil && err.Error() != utils.ErrNoRow() {
  66. return
  67. }
  68. if edbDataInsertConfig != nil {
  69. edbDataInsertConfigDate = edbDataInsertConfig.Date
  70. }
  71. }
  72. if len(item.Tables) > 0 {
  73. var condition string
  74. var pars []interface{}
  75. condition += " AND edb_info_id=? "
  76. pars = append(pars, edbInfoId)
  77. if startDate != "" {
  78. condition += " AND data_time>=? "
  79. pars = append(pars, startDate)
  80. }
  81. existList, err := GetEdbDataByCondition(source, condition, pars)
  82. if err != nil {
  83. return err
  84. }
  85. existMap := make(map[string]*EdbInfoSearchData)
  86. for _, v := range existList {
  87. existMap[v.DataTime] = v
  88. }
  89. table := item.Tables[0]
  90. dataLen := len(table.Time)
  91. addSql := ` INSERT INTO edb_data_ths(edb_info_id,edb_code,data_time,value,create_time,modify_time,data_timestamp) values `
  92. var isAdd bool
  93. addMap := make(map[string]string)
  94. edbInfoIdStr := strconv.Itoa(edbInfoId)
  95. for i := 0; i < dataLen; i++ {
  96. eDate := table.Time[i]
  97. sValue := table.Value[i]
  98. sValueStr := utils.SubFloatToString(sValue, 30)
  99. dataTime, err := time.ParseInLocation(utils.FormatDate, eDate, time.Local)
  100. if err != nil {
  101. return err
  102. }
  103. if findItem, ok := existMap[eDate]; !ok {
  104. if _, addOk := addMap[eDate]; !addOk {
  105. timestamp := dataTime.UnixNano() / 1e6
  106. timeStr := fmt.Sprintf("%d", timestamp)
  107. addSql += GetAddSql(edbInfoIdStr, edbCode, eDate, timeStr, sValueStr)
  108. isAdd = true
  109. addMap[eDate] = sValueStr
  110. }
  111. } else {
  112. if findItem != nil && utils.SubFloatToString(findItem.Value, 30) != utils.SubFloatToString(sValue, 30) {
  113. err = ModifyEdbDataById(source, findItem.EdbDataId, sValueStr)
  114. if err != nil {
  115. return err
  116. }
  117. }
  118. }
  119. // 下面代码主要目的是处理掉手动插入的数据判断
  120. {
  121. if realDataMaxDate.IsZero() || dataTime.After(realDataMaxDate) {
  122. realDataMaxDate = dataTime
  123. }
  124. if edbDataInsertConfigDate.IsZero() || dataTime.Equal(edbDataInsertConfigDate) {
  125. isFindConfigDateRealData = true
  126. }
  127. }
  128. }
  129. // 处理手工数据补充的配置
  130. HandleConfigInsertEdbData(realDataMaxDate, edbDataInsertConfig, edbInfoId, source, existMap, isFindConfigDateRealData)
  131. if isAdd {
  132. addSql = strings.TrimRight(addSql, ",")
  133. _, err = o.Raw(addSql).Exec()
  134. if err != nil {
  135. return err
  136. }
  137. }
  138. }
  139. return
  140. }