base_from_ths.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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, utils.DATA_SUB_SOURCE_EDB)
  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. subSource := utils.DATA_SUB_SOURCE_EDB
  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. if len(item.Tables) > 0 {
  74. var condition string
  75. var pars []interface{}
  76. condition += " AND edb_info_id=? "
  77. pars = append(pars, edbInfoId)
  78. if startDate != "" {
  79. condition += " AND data_time>=? "
  80. pars = append(pars, startDate)
  81. }
  82. existList, err := GetEdbDataByCondition(source, subSource, condition, pars)
  83. if err != nil {
  84. return err
  85. }
  86. existMap := make(map[string]*EdbInfoSearchData)
  87. for _, v := range existList {
  88. existMap[v.DataTime] = v
  89. }
  90. table := item.Tables[0]
  91. dataLen := len(table.Time)
  92. addSql := ` INSERT INTO edb_data_ths(edb_info_id,edb_code,data_time,value,create_time,modify_time,data_timestamp) values `
  93. var isAdd bool
  94. addMap := make(map[string]string)
  95. edbInfoIdStr := strconv.Itoa(edbInfoId)
  96. for i := 0; i < dataLen; i++ {
  97. eDate := table.Time[i]
  98. sValue := table.Value[i]
  99. sValueStr := utils.SubFloatToString(sValue, 30)
  100. dataTime, err := time.ParseInLocation(utils.FormatDate, eDate, time.Local)
  101. if err != nil {
  102. return err
  103. }
  104. if findItem, ok := existMap[eDate]; !ok {
  105. if _, addOk := addMap[eDate]; !addOk {
  106. timestamp := dataTime.UnixNano() / 1e6
  107. timeStr := fmt.Sprintf("%d", timestamp)
  108. addSql += GetAddSql(edbInfoIdStr, edbCode, eDate, timeStr, sValueStr)
  109. isAdd = true
  110. addMap[eDate] = sValueStr
  111. }
  112. } else {
  113. if findItem != nil && utils.SubFloatToString(findItem.Value, 30) != utils.SubFloatToString(sValue, 30) {
  114. err = ModifyEdbDataById(source, subSource, findItem.EdbDataId, sValueStr)
  115. if err != nil {
  116. return err
  117. }
  118. }
  119. }
  120. // 下面代码主要目的是处理掉手动插入的数据判断
  121. {
  122. if realDataMaxDate.IsZero() || dataTime.After(realDataMaxDate) {
  123. realDataMaxDate = dataTime
  124. }
  125. if edbDataInsertConfigDate.IsZero() || dataTime.Equal(edbDataInsertConfigDate) {
  126. isFindConfigDateRealData = true
  127. }
  128. }
  129. }
  130. // 处理手工数据补充的配置
  131. HandleConfigInsertEdbData(realDataMaxDate, edbDataInsertConfig, edbInfoId, source, subSource, existMap, isFindConfigDateRealData)
  132. if isAdd {
  133. addSql = strings.TrimRight(addSql, ",")
  134. _, err = o.Raw(addSql).Exec()
  135. if err != nil {
  136. return err
  137. }
  138. }
  139. }
  140. return
  141. }