factor_edb_series_chart_mapping.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. package models
  2. import (
  3. "eta/eta_index_lib/utils"
  4. "fmt"
  5. "github.com/beego/beego/v2/client/orm"
  6. "strings"
  7. "time"
  8. )
  9. const (
  10. FactorEdbSeriesChartCalculateTypeCorrelation = 1 // 相关性计算
  11. FactorEdbSeriesChartCalculateTypeRange = 2 // 区间计算
  12. )
  13. // FactorEdbSeriesChartMapping 因子指标系列-图表关联
  14. type FactorEdbSeriesChartMapping struct {
  15. FactorEdbSeriesChartMappingId int `orm:"column(factor_edb_series_chart_mapping_id);pk"`
  16. ChartInfoId int `description:"图表ID"`
  17. Source int `description:"图表来源, 同chart_info表source"`
  18. CalculateType int `description:"计算方式: 1-相关性"`
  19. CalculatePars string `description:"计算参数-JSON(如计算窗口等)"`
  20. CalculateData string `description:"计算数据-JSON(如相关性矩阵等)"`
  21. FactorEdbSeriesId int `description:"因子指标系列ID"`
  22. EdbInfoId int `description:"指标ID"`
  23. EdbUsed int `description:"指标是否使用: 0-否; 1-是"`
  24. CreateTime time.Time `description:"创建时间"`
  25. ModifyTime time.Time `description:"修改时间"`
  26. }
  27. func (m *FactorEdbSeriesChartMapping) TableName() string {
  28. return "factor_edb_series_chart_mapping"
  29. }
  30. type MultipleFactorSeriesChartMappingCols struct {
  31. PrimaryId string
  32. ChartInfoId string
  33. Source string
  34. CalculateType string
  35. CalculatePars string
  36. CalculateData string
  37. FactorEdbSeriesId string
  38. EdbInfoId string
  39. EdbUsed string
  40. CreateTime string
  41. ModifyTime string
  42. }
  43. func (m *FactorEdbSeriesChartMapping) Cols() MultipleFactorSeriesChartMappingCols {
  44. return MultipleFactorSeriesChartMappingCols{
  45. PrimaryId: "factor_edb_series_chart_mapping_id",
  46. ChartInfoId: "chart_info_id",
  47. Source: "source",
  48. CalculateType: "calculate_type",
  49. CalculatePars: "calculate_pars",
  50. CalculateData: "calculate_data",
  51. FactorEdbSeriesId: "factor_edb_series_id",
  52. EdbInfoId: "edb_info_id",
  53. EdbUsed: "edb_used",
  54. CreateTime: "create_time",
  55. ModifyTime: "modify_time",
  56. }
  57. }
  58. func (m *FactorEdbSeriesChartMapping) Create() (err error) {
  59. o := orm.NewOrm()
  60. id, err := o.Insert(m)
  61. if err != nil {
  62. return
  63. }
  64. m.FactorEdbSeriesChartMappingId = int(id)
  65. return
  66. }
  67. func (m *FactorEdbSeriesChartMapping) CreateMulti(items []*FactorEdbSeriesChartMapping) (err error) {
  68. if len(items) == 0 {
  69. return
  70. }
  71. o := orm.NewOrm()
  72. _, err = o.InsertMulti(len(items), items)
  73. return
  74. }
  75. func (m *FactorEdbSeriesChartMapping) Update(cols []string) (err error) {
  76. o := orm.NewOrm()
  77. _, err = o.Update(m, cols...)
  78. return
  79. }
  80. func (m *FactorEdbSeriesChartMapping) Remove() (err error) {
  81. o := orm.NewOrm()
  82. sql := fmt.Sprintf(`DELETE FROM %s WHERE %s = ? LIMIT 1`, m.TableName(), m.Cols().PrimaryId)
  83. _, err = o.Raw(sql, m.FactorEdbSeriesChartMappingId).Exec()
  84. return
  85. }
  86. func (m *FactorEdbSeriesChartMapping) MultiRemove(ids []int) (err error) {
  87. if len(ids) == 0 {
  88. return
  89. }
  90. o := orm.NewOrm()
  91. sql := fmt.Sprintf(`DELETE FROM %s WHERE %s IN (%s)`, m.TableName(), m.Cols().PrimaryId, utils.GetOrmInReplace(len(ids)))
  92. _, err = o.Raw(sql, ids).Exec()
  93. return
  94. }
  95. func (m *FactorEdbSeriesChartMapping) GetItemById(id int) (item *FactorEdbSeriesChartMapping, err error) {
  96. o := orm.NewOrm()
  97. sql := fmt.Sprintf(`SELECT * FROM %s WHERE %s = ? LIMIT 1`, m.TableName(), m.Cols().PrimaryId)
  98. err = o.Raw(sql, id).QueryRow(&item)
  99. return
  100. }
  101. func (m *FactorEdbSeriesChartMapping) GetItemByCondition(condition string, pars []interface{}, orderRule string) (item *FactorEdbSeriesChartMapping, err error) {
  102. o := orm.NewOrm()
  103. order := ``
  104. if orderRule != "" {
  105. order = ` ORDER BY ` + orderRule
  106. }
  107. sql := fmt.Sprintf(`SELECT * FROM %s WHERE 1=1 %s %s LIMIT 1`, m.TableName(), condition, order)
  108. err = o.Raw(sql, pars).QueryRow(&item)
  109. return
  110. }
  111. func (m *FactorEdbSeriesChartMapping) GetCountByCondition(condition string, pars []interface{}) (count int, err error) {
  112. o := orm.NewOrm()
  113. sql := fmt.Sprintf(`SELECT COUNT(1) FROM %s WHERE 1=1 %s`, m.TableName(), condition)
  114. err = o.Raw(sql, pars).QueryRow(&count)
  115. return
  116. }
  117. func (m *FactorEdbSeriesChartMapping) GetItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string) (items []*FactorEdbSeriesChartMapping, err error) {
  118. o := orm.NewOrm()
  119. fields := strings.Join(fieldArr, ",")
  120. if len(fieldArr) == 0 {
  121. fields = `*`
  122. }
  123. order := fmt.Sprintf(`ORDER BY %s DESC`, m.Cols().CreateTime)
  124. if orderRule != "" {
  125. order = ` ORDER BY ` + orderRule
  126. }
  127. sql := fmt.Sprintf(`SELECT %s FROM %s WHERE 1=1 %s %s`, fields, m.TableName(), condition, order)
  128. _, err = o.Raw(sql, pars).QueryRows(&items)
  129. return
  130. }
  131. func (m *FactorEdbSeriesChartMapping) GetPageItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string, startSize, pageSize int) (items []*FactorEdbSeriesChartMapping, err error) {
  132. o := orm.NewOrm()
  133. fields := strings.Join(fieldArr, ",")
  134. if len(fieldArr) == 0 {
  135. fields = `*`
  136. }
  137. order := fmt.Sprintf(`ORDER BY %s DESC`, m.Cols().CreateTime)
  138. if orderRule != "" {
  139. order = ` ORDER BY ` + orderRule
  140. }
  141. sql := fmt.Sprintf(`SELECT %s FROM %s WHERE 1=1 %s %s LIMIT ?,?`, fields, m.TableName(), condition, order)
  142. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
  143. return
  144. }
  145. // GetDistinctSeriesIdByChartId 获取图表关联的系列ID
  146. func (m *FactorEdbSeriesChartMapping) GetDistinctSeriesIdByChartId(chartId int) (seriesIds []int, err error) {
  147. o := orm.NewOrm()
  148. sql := fmt.Sprintf(`SELECT DISTINCT %s FROM %s WHERE %s = ?`, m.Cols().FactorEdbSeriesId, m.TableName(), m.Cols().ChartInfoId)
  149. _, err = o.Raw(sql, chartId).QueryRows(&seriesIds)
  150. return
  151. }
  152. // FactorEdbSeriesChartCalculateCorrelationReq 图表相关性计算参数
  153. type FactorEdbSeriesChartCalculateCorrelationReq struct {
  154. BaseEdbInfoId int `description:"标的指标ID"`
  155. LeadValue int `description:"领先期数"`
  156. LeadUnit string `description:"频度"`
  157. CalculateValue int `description:"计算窗口"`
  158. CalculateUnit string `description:"计算频度"`
  159. }