base_from_ths.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. package models
  2. import (
  3. "eta/eta_index_lib/utils"
  4. "fmt"
  5. "strconv"
  6. "strings"
  7. "time"
  8. )
  9. type EdbDataFromThs struct {
  10. DataVol int64 `json:"dataVol"`
  11. Errmsg string `json:"errmsg"`
  12. Errorcode int64 `json:"errorcode"`
  13. Perf interface{} `json:"perf"`
  14. Tables []Tables `json:"tables"`
  15. }
  16. // Tables 表格数据
  17. type Tables struct {
  18. ID []string `json:"id"`
  19. Time []string `json:"time"`
  20. Value []float64 `json:"value"`
  21. }
  22. // 新增同花顺指标数据
  23. func AddEdbDataFromThs(edbCode string, item EdbDataFromThs) (err error) {
  24. //o := orm.NewOrm()
  25. if len(item.Tables) > 0 {
  26. table := item.Tables[0]
  27. dataLen := len(table.Time)
  28. var isAdd bool
  29. addSql := ` INSERT INTO %s(edb_info_id,edb_code,data_time,value,create_time,modify_time,data_timestamp) values `
  30. tableName := GetEdbDataTableName(utils.DATA_SOURCE_THS, utils.DATA_SUB_SOURCE_EDB)
  31. addSql = fmt.Sprintf(addSql, tableName)
  32. for i := 0; i < dataLen; i++ {
  33. eDate := table.Time[i]
  34. sValue := table.Value[i]
  35. dataTime, err := time.ParseInLocation(utils.FormatDate, eDate, time.Local)
  36. if err != nil {
  37. return err
  38. }
  39. timestamp := dataTime.UnixNano() / 1e6
  40. timeStr := fmt.Sprintf("%d", timestamp)
  41. addSql += GetAddSql("0", edbCode, eDate, timeStr, utils.SubFloatToString(sValue, 20))
  42. isAdd = true
  43. }
  44. if isAdd {
  45. addSql = strings.TrimRight(addSql, ",")
  46. _, err = o.Raw(addSql).Exec()
  47. if err != nil {
  48. return
  49. }
  50. }
  51. }
  52. return
  53. }
  54. // 刷新同花顺指标数据
  55. func RefreshEdbDataFromThs(edbInfoId int, edbCode, startDate string, item EdbDataFromThs) (err error) {
  56. //o := orm.NewOrm()
  57. source := utils.DATA_SOURCE_THS
  58. subSource := utils.DATA_SUB_SOURCE_EDB
  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 && !utils.IsErrNoRow(err) {
  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, subSource, 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, subSource, 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, subSource, 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. }