edb_trade_analysis.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. package models
  2. import (
  3. "eta_gn/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. list, e := dataOb.GetItemsByCondition(cond, pars, []string{}, "")
  51. if e != nil {
  52. err = fmt.Errorf("获取指标数据失败, %v", e)
  53. return
  54. }
  55. for _, v := range list {
  56. dataExists[v.DataTime.Format(utils.FormatDate)] = v
  57. searchExistMap[v.DataTime.Format(utils.FormatDate)] = &EdbInfoSearchData{
  58. EdbDataId: v.EdbDataId,
  59. EdbInfoId: v.EdbInfoId,
  60. DataTime: v.DataTime.Format(utils.FormatDate),
  61. Value: v.Value,
  62. EdbCode: v.EdbCode,
  63. DataTimestamp: v.DataTimestamp,
  64. }
  65. }
  66. }
  67. // 比对数据
  68. insertExist := make(map[string]bool)
  69. insertData := make([]*EdbDataTradeAnalysis, 0)
  70. updateData := make([]*EdbDataTradeAnalysis, 0)
  71. for _, v := range convertData {
  72. dataDate, e := time.ParseInLocation(utils.FormatDate, v.DataTime, time.Local)
  73. if e != nil {
  74. utils.FileLog.Info(fmt.Sprintf("持仓分析-数据日期转换失败, EdbCode: %s, DataDate: %s", edbInfo.EdbCode, v.DataTime))
  75. continue
  76. }
  77. strDate := v.DataTime
  78. // 手动插入数据的判断
  79. if realDataMaxDate.IsZero() || dataDate.After(realDataMaxDate) {
  80. realDataMaxDate = dataDate
  81. }
  82. if edbDataInsertConfigDate.IsZero() || dataDate.Equal(edbDataInsertConfigDate) {
  83. isFindConfigDateRealData = true
  84. }
  85. // 入库值
  86. saveVal := decimal.NewFromFloat(v.Value).Round(4).String()
  87. d, e := decimal.NewFromString(saveVal)
  88. if e != nil {
  89. utils.FileLog.Info(fmt.Sprintf("EdbDataTradeAnalysis NewFromString err: %v", e))
  90. continue
  91. }
  92. saveFloat, _ := d.Float64()
  93. // 更新
  94. exists := dataExists[strDate]
  95. if exists != nil {
  96. existVal := decimal.NewFromFloat(exists.Value).Round(4).String()
  97. if saveVal != existVal {
  98. exists.Value = saveFloat
  99. updateData = append(updateData, exists)
  100. }
  101. continue
  102. }
  103. // 新增
  104. if insertExist[strDate] {
  105. continue
  106. }
  107. insertExist[strDate] = true
  108. timestamp := dataDate.UnixNano() / 1e6
  109. insertData = append(insertData, &EdbDataTradeAnalysis{
  110. EdbInfoId: edbInfo.EdbInfoId,
  111. EdbCode: edbInfo.EdbCode,
  112. DataTime: dataDate,
  113. Value: saveFloat,
  114. CreateTime: time.Now(),
  115. ModifyTime: time.Now(),
  116. DataTimestamp: timestamp,
  117. })
  118. }
  119. // 批量新增/更新
  120. if len(insertData) > 0 {
  121. if e := dataOb.CreateMulti(insertData); e != nil {
  122. err = fmt.Errorf("批量新增指标数据失败, %v", e)
  123. return
  124. }
  125. }
  126. if len(updateData) > 0 {
  127. if e := dataOb.MultiUpdateValue(updateData); e != nil {
  128. err = fmt.Errorf("批量更新指标数据失败, %v", e)
  129. return
  130. }
  131. }
  132. // 处理手工数据补充的配置
  133. HandleConfigInsertEdbData(realDataMaxDate, edbDataInsertConfig, edbInfo.EdbInfoId, obj.GetSource(), 0, searchExistMap, isFindConfigDateRealData)
  134. return
  135. }
  136. func (obj EdbTradeAnalysis) UnifiedModifyEdbInfoMaxAndMinInfo(edbInfo *EdbInfo) (err error) {
  137. err, _ = UnifiedModifyEdbInfoMaxAndMinInfo(edbInfo)
  138. return
  139. }