factor_edb_series_chart_mapping.go 6.1 KB

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