edb_trade_analysis.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. package models
  2. import (
  3. "eta/eta_index_lib/utils"
  4. "fmt"
  5. "github.com/shopspring/decimal"
  6. "time"
  7. )
  8. // EdbTradeAnalysis 持仓分析指标
  9. type EdbTradeAnalysis struct{}
  10. // GetSource 获取来源编码id
  11. func (obj EdbTradeAnalysis) GetSource() int {
  12. return utils.DATA_SOURCE_TRADE_ANALYSIS
  13. }
  14. // GetSourceName 获取来源名称
  15. func (obj EdbTradeAnalysis) GetSourceName() string {
  16. return utils.DATA_SOURCE_NAME_TRADE_ANALYSIS
  17. }
  18. // GetEdbType 获取指标类型
  19. func (obj EdbTradeAnalysis) GetEdbType() int {
  20. return utils.DEFAULT_EDB_TYPE
  21. }
  22. func (obj EdbTradeAnalysis) Refresh(edbInfo *EdbInfo, convertData []*EdbDataList) (err error) {
  23. if edbInfo == nil {
  24. err = fmt.Errorf("指标信息有误, EdbInfo: %v", edbInfo)
  25. return
  26. }
  27. // 真实数据的最大日期, 插入规则配置的日期
  28. var realDataMaxDate, edbDataInsertConfigDate time.Time
  29. var edbDataInsertConfig *EdbDataInsertConfig
  30. var isFindConfigDateRealData bool
  31. {
  32. conf, e := GetEdbDataInsertConfigByEdbId(edbInfo.EdbInfoId)
  33. if e != nil && e.Error() != utils.ErrNoRow() {
  34. err = fmt.Errorf("GetEdbDataInsertConfigByEdbId err: %v", e)
  35. return
  36. }
  37. edbDataInsertConfig = conf
  38. if edbDataInsertConfig != nil {
  39. edbDataInsertConfigDate = edbDataInsertConfig.Date
  40. }
  41. }
  42. // 获取已有数据
  43. dataOb := new(EdbDataTradeAnalysis)
  44. dataExists := make(map[string]*EdbDataTradeAnalysis)
  45. searchExistMap := make(map[string]*EdbInfoSearchData)
  46. {
  47. cond := fmt.Sprintf(" AND %s = ?", dataOb.Cols().EdbInfoId)
  48. pars := make([]interface{}, 0)
  49. pars = append(pars, edbInfo.EdbInfoId)
  50. //if queryDate != "" {
  51. // cond += fmt.Sprintf(" AND %s >= ?", dataOb.Cols().DataTime)
  52. // pars = append(pars, queryDate)
  53. //}
  54. list, e := dataOb.GetItemsByCondition(cond, pars, []string{}, "")
  55. if e != nil {
  56. err = fmt.Errorf("获取指标数据失败, %v", e)
  57. return
  58. }
  59. for _, v := range list {
  60. dataExists[v.DataTime.Format(utils.FormatDate)] = v
  61. searchExistMap[v.DataTime.Format(utils.FormatDate)] = &EdbInfoSearchData{
  62. EdbDataId: v.EdbDataId,
  63. EdbInfoId: v.EdbInfoId,
  64. DataTime: v.DataTime.Format(utils.FormatDate),
  65. Value: v.Value,
  66. EdbCode: v.EdbCode,
  67. DataTimestamp: v.DataTimestamp,
  68. }
  69. }
  70. }
  71. // 比对数据
  72. insertExist := make(map[string]bool)
  73. insertData := make([]*EdbDataTradeAnalysis, 0)
  74. updateData := make([]*EdbDataTradeAnalysis, 0)
  75. for _, v := range convertData {
  76. dataDate, e := time.ParseInLocation(utils.FormatDate, v.DataTime, time.Local)
  77. if e != nil {
  78. utils.FileLog.Info(fmt.Sprintf("持仓分析-数据日期转换失败, EdbCode: %s, DataDate: %s", edbInfo.EdbCode, v.DataTime))
  79. continue
  80. }
  81. strDate := v.DataTime
  82. // 手动插入数据的判断
  83. if realDataMaxDate.IsZero() || dataDate.After(realDataMaxDate) {
  84. realDataMaxDate = dataDate
  85. }
  86. if edbDataInsertConfigDate.IsZero() || dataDate.Equal(edbDataInsertConfigDate) {
  87. isFindConfigDateRealData = true
  88. }
  89. // 入库值
  90. saveVal := decimal.NewFromFloat(v.Value).Round(4).String()
  91. d, e := decimal.NewFromString(saveVal)
  92. if e != nil {
  93. utils.FileLog.Info(fmt.Sprintf("EdbDataTradeAnalysis NewFromString err: %v", e))
  94. continue
  95. }
  96. saveFloat, _ := d.Float64()
  97. // 更新
  98. exists := dataExists[strDate]
  99. if exists != nil {
  100. existVal := decimal.NewFromFloat(exists.Value).Round(4).String()
  101. if saveVal != existVal {
  102. exists.Value = saveFloat
  103. updateData = append(updateData, exists)
  104. }
  105. continue
  106. }
  107. // 新增
  108. if insertExist[strDate] {
  109. continue
  110. }
  111. insertExist[strDate] = true
  112. timestamp := dataDate.UnixNano() / 1e6
  113. insertData = append(insertData, &EdbDataTradeAnalysis{
  114. EdbInfoId: edbInfo.EdbInfoId,
  115. EdbCode: edbInfo.EdbCode,
  116. DataTime: dataDate,
  117. Value: saveFloat,
  118. CreateTime: time.Now(),
  119. ModifyTime: time.Now(),
  120. DataTimestamp: timestamp,
  121. })
  122. }
  123. // 批量新增/更新
  124. if len(insertData) > 0 {
  125. if e := dataOb.CreateMulti(insertData); e != nil {
  126. err = fmt.Errorf("批量新增指标数据失败, %v", e)
  127. return
  128. }
  129. }
  130. if len(updateData) > 0 {
  131. if e := dataOb.MultiUpdateValue(updateData); e != nil {
  132. err = fmt.Errorf("批量更新指标数据失败, %v", e)
  133. return
  134. }
  135. }
  136. // 处理手工数据补充的配置
  137. HandleConfigInsertEdbData(realDataMaxDate, edbDataInsertConfig, edbInfo.EdbInfoId, obj.GetSource(), 0, searchExistMap, isFindConfigDateRealData)
  138. return
  139. }
  140. func (obj EdbTradeAnalysis) UnifiedModifyEdbInfoMaxAndMinInfo(edbInfo *EdbInfo) (err error) {
  141. err, _ = UnifiedModifyEdbInfoMaxAndMinInfo(edbInfo)
  142. return
  143. }