base_from_ths_ds.go 4.5 KB

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