factor_edb_series_mapping.go 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. package data_manage
  2. import (
  3. "eta/eta_api/utils"
  4. "fmt"
  5. "github.com/beego/beego/v2/client/orm"
  6. "strings"
  7. "time"
  8. )
  9. // FactorEdbSeriesMapping 因子指标系列-指标关联表
  10. type FactorEdbSeriesMapping struct {
  11. FactorEdbSeriesMappingId int `orm:"column(factor_edb_series_mapping_id);pk"`
  12. FactorEdbSeriesId int `description:"因子指标系列ID"`
  13. EdbInfoId int `description:"指标ID"`
  14. EdbCode string `description:"指标编码"`
  15. CalculateData string `description:"计算数据-JSON(如相关性矩阵等等)"`
  16. CreateTime time.Time `description:"创建时间"`
  17. ModifyTime time.Time `description:"修改时间"`
  18. }
  19. func (m *FactorEdbSeriesMapping) TableName() string {
  20. return "factor_edb_series_mapping"
  21. }
  22. type FactorEdbSeriesMappingCols struct {
  23. PrimaryId string
  24. FactorEdbSeriesId string
  25. EdbInfoId string
  26. EdbCode string
  27. CalculateData string
  28. CreateTime string
  29. ModifyTime string
  30. }
  31. func (m *FactorEdbSeriesMapping) Cols() FactorEdbSeriesMappingCols {
  32. return FactorEdbSeriesMappingCols{
  33. PrimaryId: "factor_edb_series_mapping_id",
  34. FactorEdbSeriesId: "factor_edb_series_id",
  35. EdbInfoId: "edb_info_id",
  36. EdbCode: "edb_code",
  37. CalculateData: "calculate_data",
  38. CreateTime: "create_time",
  39. ModifyTime: "modify_time",
  40. }
  41. }
  42. func (m *FactorEdbSeriesMapping) Create() (err error) {
  43. o := orm.NewOrmUsingDB("data")
  44. id, err := o.Insert(m)
  45. if err != nil {
  46. return
  47. }
  48. m.FactorEdbSeriesMappingId = int(id)
  49. return
  50. }
  51. func (m *FactorEdbSeriesMapping) CreateMulti(items []*FactorEdbSeriesMapping) (err error) {
  52. if len(items) == 0 {
  53. return
  54. }
  55. o := orm.NewOrmUsingDB("data")
  56. _, err = o.InsertMulti(len(items), items)
  57. return
  58. }
  59. func (m *FactorEdbSeriesMapping) Update(cols []string) (err error) {
  60. o := orm.NewOrmUsingDB("data")
  61. _, err = o.Update(m, cols...)
  62. return
  63. }
  64. func (m *FactorEdbSeriesMapping) Remove() (err error) {
  65. o := orm.NewOrmUsingDB("data")
  66. sql := fmt.Sprintf(`DELETE FROM %s WHERE %s = ? LIMIT 1`, m.TableName(), m.Cols().PrimaryId)
  67. _, err = o.Raw(sql, m.FactorEdbSeriesMappingId).Exec()
  68. return
  69. }
  70. func (m *FactorEdbSeriesMapping) MultiRemove(ids []int) (err error) {
  71. if len(ids) == 0 {
  72. return
  73. }
  74. o := orm.NewOrmUsingDB("data")
  75. sql := fmt.Sprintf(`DELETE FROM %s WHERE %s IN (%s)`, m.TableName(), m.Cols().PrimaryId, utils.GetOrmInReplace(len(ids)))
  76. _, err = o.Raw(sql, ids).Exec()
  77. return
  78. }
  79. func (m *FactorEdbSeriesMapping) RemoveByCondition(condition string, pars []interface{}) (err error) {
  80. if condition == "" {
  81. return
  82. }
  83. o := orm.NewOrmUsingDB("data")
  84. sql := fmt.Sprintf(`DELETE FROM %s WHERE %s`, m.TableName(), condition)
  85. _, err = o.Raw(sql, pars).Exec()
  86. return
  87. }
  88. func (m *FactorEdbSeriesMapping) GetItemById(id int) (item *FactorEdbSeriesMapping, err error) {
  89. o := orm.NewOrmUsingDB("data")
  90. sql := fmt.Sprintf(`SELECT * FROM %s WHERE %s = ? LIMIT 1`, m.TableName(), m.Cols().PrimaryId)
  91. err = o.Raw(sql, id).QueryRow(&item)
  92. return
  93. }
  94. func (m *FactorEdbSeriesMapping) GetItemByCondition(condition string, pars []interface{}, orderRule string) (item *FactorEdbSeriesMapping, err error) {
  95. o := orm.NewOrmUsingDB("data")
  96. order := ``
  97. if orderRule != "" {
  98. order = ` ORDER BY ` + orderRule
  99. }
  100. sql := fmt.Sprintf(`SELECT * FROM %s WHERE 1=1 %s %s LIMIT 1`, m.TableName(), condition, order)
  101. err = o.Raw(sql, pars).QueryRow(&item)
  102. return
  103. }
  104. func (m *FactorEdbSeriesMapping) GetCountByCondition(condition string, pars []interface{}) (count int, err error) {
  105. o := orm.NewOrmUsingDB("data")
  106. sql := fmt.Sprintf(`SELECT COUNT(1) FROM %s WHERE 1=1 %s`, m.TableName(), condition)
  107. err = o.Raw(sql, pars).QueryRow(&count)
  108. return
  109. }
  110. func (m *FactorEdbSeriesMapping) GetItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string) (items []*FactorEdbSeriesMapping, err error) {
  111. o := orm.NewOrmUsingDB("data")
  112. fields := strings.Join(fieldArr, ",")
  113. if len(fieldArr) == 0 {
  114. fields = `*`
  115. }
  116. order := fmt.Sprintf(`ORDER BY %s DESC`, m.Cols().CreateTime)
  117. if orderRule != "" {
  118. order = ` ORDER BY ` + orderRule
  119. }
  120. sql := fmt.Sprintf(`SELECT %s FROM %s WHERE 1=1 %s %s`, fields, m.TableName(), condition, order)
  121. _, err = o.Raw(sql, pars).QueryRows(&items)
  122. return
  123. }
  124. func (m *FactorEdbSeriesMapping) GetPageItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string, startSize, pageSize int) (items []*FactorEdbSeriesMapping, err error) {
  125. o := orm.NewOrmUsingDB("data")
  126. fields := strings.Join(fieldArr, ",")
  127. if len(fieldArr) == 0 {
  128. fields = `*`
  129. }
  130. order := fmt.Sprintf(`ORDER BY %s DESC`, m.Cols().CreateTime)
  131. if orderRule != "" {
  132. order = ` ORDER BY ` + orderRule
  133. }
  134. sql := fmt.Sprintf(`SELECT %s FROM %s WHERE 1=1 %s %s LIMIT ?,?`, fields, m.TableName(), condition, order)
  135. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
  136. return
  137. }
  138. // FactorEdbSeriesMappingItem 因子指标系列-指标关联信息
  139. type FactorEdbSeriesMappingItem struct {
  140. SeriesId int `description:"因子指标系列ID"`
  141. EdbInfoId int `description:"指标ID"`
  142. EdbCode string `description:"指标编码"`
  143. EdbName string `description:"指标名称"`
  144. EdbNameEn string `description:"指标名称-英文"`
  145. }
  146. func (m *FactorEdbSeriesMapping) Format2Item() (item *FactorEdbSeriesMappingItem) {
  147. item = new(FactorEdbSeriesMappingItem)
  148. item.SeriesId = m.FactorEdbSeriesId
  149. item.EdbInfoId = m.EdbInfoId
  150. item.EdbCode = m.EdbCode
  151. return
  152. }
  153. // FactorEdbSeriesMappingUpdateCalculate 更新计算数据
  154. //type FactorEdbSeriesMappingUpdateCalculate struct {
  155. // SeriesId int `description:"因子指标系列ID"`
  156. // EdbInfoId int `description:"指标ID"`
  157. // CalculateData string `description:"计算数据-JSON"`
  158. //}
  159. //
  160. //func (m *FactorEdbSeriesMapping) UpdateCalculateData(updates []*FactorEdbSeriesMappingUpdateCalculate) (err error) {
  161. // if len(updates) == 0 {
  162. // return
  163. // }
  164. // o := orm.NewOrm()
  165. // sql := fmt.Sprintf(`UPDATE %s SET %s = ? WHERE %s = ? AND %s = ?`, m.TableName(), m.Cols().CalculateData, m.Cols().FactorEdbSeriesId, m.Cols().EdbInfoId)
  166. // p, e := o.Raw(sql).Prepare()
  167. // if e != nil {
  168. // err = fmt.Errorf("sql prepare err: %v", e)
  169. // return
  170. // }
  171. // defer func() {
  172. // _ = p.Close()
  173. // }()
  174. // for _, v := range updates {
  175. // _, e = p.Exec(v.CalculateData, v.SeriesId, v.EdbInfoId)
  176. // if e != nil {
  177. // err = fmt.Errorf("update exec err: %v", e)
  178. // return
  179. // }
  180. // }
  181. // return
  182. //}
  183. // GetChartUsedFactorSeriesEdb 获取图表引用的系列指标
  184. func GetChartUsedFactorSeriesEdb(chartId int) (items []*FactorEdbSeriesMapping, err error) {
  185. o := orm.NewOrmUsingDB("data")
  186. chartOb := new(FactorEdbSeriesChartMapping)
  187. edbOb := new(FactorEdbSeriesMapping)
  188. sql := fmt.Sprintf(`SELECT b.* FROM %s AS a
  189. JOIN %s AS b ON a.%s = b.%s AND a.%s = b.%s
  190. WHERE a.%s = ? ORDER BY %s ASC`, chartOb.TableName(), edbOb.TableName(), chartOb.Cols().FactorEdbSeriesId, edbOb.Cols().FactorEdbSeriesId, chartOb.Cols().EdbInfoId, edbOb.Cols().EdbInfoId, chartOb.Cols().ChartInfoId, edbOb.Cols().FactorEdbSeriesId)
  191. _, err = o.Raw(sql, chartId).QueryRows(&items)
  192. return
  193. }