base_from_ths.go 4.6 KB

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