factor_edb_series_calculate_data.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. // FactorEdbSeriesCalculateData 因子指标系列-指标计算数据表
  10. type FactorEdbSeriesCalculateData struct {
  11. FactorEdbSeriesCalculateDataId int `orm:"column(factor_edb_series_calculate_data_id);pk"`
  12. FactorEdbSeriesId int `description:"因子指标系列ID"`
  13. EdbInfoId int `description:"指标ID"`
  14. EdbCode string `description:"指标编码"`
  15. DataTime time.Time `description:"数据日期"`
  16. Value float64 `description:"数据值"`
  17. CreateTime time.Time `description:"创建时间"`
  18. ModifyTime time.Time `description:"修改时间"`
  19. DataTimestamp int64 `description:"数据日期时间戳"`
  20. }
  21. func (m *FactorEdbSeriesCalculateData) TableName() string {
  22. return "factor_edb_series_calculate_data"
  23. }
  24. type FactorEdbSeriesCalculateDataCols struct {
  25. PrimaryId string
  26. FactorEdbSeriesId string
  27. EdbInfoId string
  28. EdbCode string
  29. DataTime string
  30. Value string
  31. CreateTime string
  32. ModifyTime string
  33. DataTimestamp string
  34. }
  35. func (m *FactorEdbSeriesCalculateData) Cols() FactorEdbSeriesCalculateDataCols {
  36. return FactorEdbSeriesCalculateDataCols{
  37. PrimaryId: "factor_edb_series_calculate_data_id",
  38. FactorEdbSeriesId: "factor_edb_series_id",
  39. EdbInfoId: "edb_info_id",
  40. EdbCode: "edb_code",
  41. DataTime: "data_time",
  42. Value: "value",
  43. CreateTime: "create_time",
  44. ModifyTime: "modify_time",
  45. DataTimestamp: "data_timestamp",
  46. }
  47. }
  48. func (m *FactorEdbSeriesCalculateData) Create() (err error) {
  49. o := orm.NewOrm()
  50. id, err := o.Insert(m)
  51. if err != nil {
  52. return
  53. }
  54. m.FactorEdbSeriesCalculateDataId = int(id)
  55. return
  56. }
  57. func (m *FactorEdbSeriesCalculateData) CreateMulti(items []*FactorEdbSeriesCalculateData) (err error) {
  58. if len(items) == 0 {
  59. return
  60. }
  61. o := orm.NewOrm()
  62. _, err = o.InsertMulti(500, items)
  63. return
  64. }
  65. func (m *FactorEdbSeriesCalculateData) Update(cols []string) (err error) {
  66. o := orm.NewOrm()
  67. _, err = o.Update(m, cols...)
  68. return
  69. }
  70. func (m *FactorEdbSeriesCalculateData) Remove() (err error) {
  71. o := orm.NewOrm()
  72. sql := fmt.Sprintf(`DELETE FROM %s WHERE %s = ? LIMIT 1`, m.TableName(), m.Cols().PrimaryId)
  73. _, err = o.Raw(sql, m.FactorEdbSeriesCalculateDataId).Exec()
  74. return
  75. }
  76. func (m *FactorEdbSeriesCalculateData) MultiRemove(ids []int) (err error) {
  77. if len(ids) == 0 {
  78. return
  79. }
  80. o := orm.NewOrm()
  81. sql := fmt.Sprintf(`DELETE FROM %s WHERE %s IN (%s)`, m.TableName(), m.Cols().PrimaryId, utils.GetOrmInReplace(len(ids)))
  82. _, err = o.Raw(sql, ids).Exec()
  83. return
  84. }
  85. func (m *FactorEdbSeriesCalculateData) RemoveByCondition(condition string, pars []interface{}) (err error) {
  86. if condition == "" {
  87. return
  88. }
  89. o := orm.NewOrm()
  90. sql := fmt.Sprintf(`DELETE FROM %s WHERE %s`, m.TableName(), condition)
  91. _, err = o.Raw(sql, pars).Exec()
  92. return
  93. }
  94. func (m *FactorEdbSeriesCalculateData) GetItemById(id int) (item *FactorEdbSeriesCalculateData, 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 *FactorEdbSeriesCalculateData) GetItemByCondition(condition string, pars []interface{}, orderRule string) (item *FactorEdbSeriesCalculateData, 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 *FactorEdbSeriesCalculateData) 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 *FactorEdbSeriesCalculateData) GetItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string) (items []*FactorEdbSeriesCalculateData, 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 *FactorEdbSeriesCalculateData) GetPageItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string, startSize, pageSize int) (items []*FactorEdbSeriesCalculateData, 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. // FactorEdbSeriesCalculateDataItem 因子指标系列-计算数据信息
  145. type FactorEdbSeriesCalculateDataItem struct {
  146. DataId int `description:"数据ID"`
  147. FactorEdbSeriesId int `description:"因子指标系列ID"`
  148. EdbInfoId int `description:"指标ID"`
  149. EdbCode string `description:"指标编码"`
  150. DataTime string `description:"数据日期"`
  151. Value float64 `description:"数据值"`
  152. }
  153. func (m *FactorEdbSeriesCalculateData) Format2Item() (item *FactorEdbSeriesCalculateDataItem) {
  154. item = new(FactorEdbSeriesCalculateDataItem)
  155. item.DataId = m.FactorEdbSeriesCalculateDataId
  156. item.FactorEdbSeriesId = m.FactorEdbSeriesId
  157. item.EdbInfoId = m.EdbInfoId
  158. item.EdbCode = m.EdbCode
  159. return
  160. }
  161. // TransEdbSeriesCalculateData2EdbDataList 转换数据格式
  162. func TransEdbSeriesCalculateData2EdbDataList(items []*FactorEdbSeriesCalculateData) (list []*EdbInfoSearchData) {
  163. list = make([]*EdbInfoSearchData, 0)
  164. for _, v := range items {
  165. list = append(list, &EdbInfoSearchData{
  166. DataTime: v.DataTime.Format(utils.FormatDate),
  167. Value: v.Value,
  168. })
  169. }
  170. return
  171. }