base_from_ths.go 4.7 KB

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