base_from_ths_hf_data.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. // BaseFromThsHfData 同花顺高频数据-指标数据
  10. type BaseFromThsHfData struct {
  11. BaseFromThsHfDataId int `orm:"column(base_from_ths_hf_data_id);pk"`
  12. BaseFromThsHfIndexId int `description:"指标ID"`
  13. IndexCode string `description:"指标编码"`
  14. DataTime time.Time `description:"数据日期(至时分秒)"`
  15. Value float64 `description:"数据值"`
  16. UniqueCode string `description:"唯一编码"`
  17. CreateTime time.Time `description:"创建时间"`
  18. ModifyTime time.Time `description:"修改时间"`
  19. DataTimestamp int64 `description:"数据日期时间戳"`
  20. }
  21. func (m *BaseFromThsHfData) TableName() string {
  22. return "base_from_ths_hf_data"
  23. }
  24. type BaseFromThsHfDataCols struct {
  25. PrimaryId string
  26. BaseFromThsHfIndexId string
  27. IndexCode string
  28. DataTime string
  29. Value string
  30. UniqueCode string
  31. CreateTime string
  32. ModifyTime string
  33. DataTimestamp string
  34. }
  35. func (m *BaseFromThsHfData) Cols() BaseFromThsHfDataCols {
  36. return BaseFromThsHfDataCols{
  37. PrimaryId: "base_from_ths_hf_data_id",
  38. BaseFromThsHfIndexId: "base_from_ths_hf_index_id",
  39. IndexCode: "index_code",
  40. DataTime: "data_time",
  41. Value: "value",
  42. UniqueCode: "unique_code",
  43. CreateTime: "create_time",
  44. ModifyTime: "modify_time",
  45. DataTimestamp: "data_timestamp",
  46. }
  47. }
  48. func (m *BaseFromThsHfData) Create() (err error) {
  49. o := orm.NewOrmUsingDB("data")
  50. id, err := o.Insert(m)
  51. if err != nil {
  52. return
  53. }
  54. m.BaseFromThsHfDataId = int(id)
  55. return
  56. }
  57. func (m *BaseFromThsHfData) CreateMulti(items []*BaseFromThsHfData) (err error) {
  58. if len(items) == 0 {
  59. return
  60. }
  61. o := orm.NewOrmUsingDB("data")
  62. _, err = o.InsertMulti(len(items), items)
  63. return
  64. }
  65. func (m *BaseFromThsHfData) Update(cols []string) (err error) {
  66. o := orm.NewOrmUsingDB("data")
  67. _, err = o.Update(m, cols...)
  68. return
  69. }
  70. func (m *BaseFromThsHfData) Remove() (err error) {
  71. o := orm.NewOrmUsingDB("data")
  72. sql := fmt.Sprintf(`DELETE FROM %s WHERE %s = ? LIMIT 1`, m.TableName(), m.Cols().PrimaryId)
  73. _, err = o.Raw(sql, m.BaseFromThsHfDataId).Exec()
  74. return
  75. }
  76. func (m *BaseFromThsHfData) MultiRemove(ids []int) (err error) {
  77. if len(ids) == 0 {
  78. return
  79. }
  80. o := orm.NewOrmUsingDB("data")
  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 *BaseFromThsHfData) RemoveByCondition(condition string, pars []interface{}) (err error) {
  86. if condition == "" {
  87. return
  88. }
  89. o := orm.NewOrmUsingDB("data")
  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 *BaseFromThsHfData) GetItemById(id int) (item *BaseFromThsHfData, err error) {
  95. o := orm.NewOrmUsingDB("data")
  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 *BaseFromThsHfData) GetItemByCondition(condition string, pars []interface{}, orderRule string) (item *BaseFromThsHfData, err error) {
  101. o := orm.NewOrmUsingDB("data")
  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 *BaseFromThsHfData) GetCountByCondition(condition string, pars []interface{}) (count int, err error) {
  111. o := orm.NewOrmUsingDB("data")
  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 *BaseFromThsHfData) GetItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string) (items []*BaseFromThsHfData, err error) {
  117. o := orm.NewOrmUsingDB("data")
  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 *BaseFromThsHfData) GetPageItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string, startSize, pageSize int) (items []*BaseFromThsHfData, err error) {
  131. o := orm.NewOrmUsingDB("data")
  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. // BaseFromThsHfDataItem 同花顺高频数据信息
  145. type BaseFromThsHfDataItem struct {
  146. DataId int `description:"数据ID"`
  147. IndexId int `description:"指标ID"`
  148. IndexCode string `description:"指标编码"`
  149. DataTime string `description:"数据日期(至时分秒)"`
  150. Value float64 `description:"数据值"`
  151. UniqueCode string `description:"唯一编码"`
  152. }
  153. func (m *BaseFromThsHfData) Format2Item() (item *BaseFromThsHfDataItem) {
  154. item = new(BaseFromThsHfDataItem)
  155. item.DataId = m.BaseFromThsHfDataId
  156. item.IndexId = m.BaseFromThsHfIndexId
  157. item.IndexCode = m.IndexCode
  158. item.DataTime = utils.TimeTransferString(utils.FormatDateTime, m.DataTime)
  159. item.Value = m.Value
  160. item.UniqueCode = m.UniqueCode
  161. return
  162. }