edb_data_ths_hf.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. // EdbDataThsHf 同花顺高频指标数据
  10. type EdbDataThsHf struct {
  11. EdbDataId int `orm:"column(edb_data_id);pk"`
  12. EdbInfoId int `description:"指标ID"`
  13. EdbCode string `description:"指标编码"`
  14. DataTime time.Time `description:"数据日期"`
  15. Value float64 `description:"数据值"`
  16. CreateTime time.Time `description:"创建时间"`
  17. ModifyTime time.Time `description:"修改时间"`
  18. DataTimestamp int64 `description:"数据日期时间戳"`
  19. }
  20. func (m *EdbDataThsHf) TableName() string {
  21. return "edb_data_ths_hf"
  22. }
  23. type EdbDataThsHfCols struct {
  24. PrimaryId string
  25. EdbInfoId string
  26. EdbCode string
  27. DataTime string
  28. Value string
  29. CreateTime string
  30. ModifyTime string
  31. DataTimestamp string
  32. }
  33. func (m *EdbDataThsHf) Cols() EdbDataThsHfCols {
  34. return EdbDataThsHfCols{
  35. PrimaryId: "edb_data_id",
  36. EdbInfoId: "edb_info_id",
  37. EdbCode: "edb_code",
  38. DataTime: "data_time",
  39. Value: "value",
  40. CreateTime: "create_time",
  41. ModifyTime: "modify_time",
  42. DataTimestamp: "data_timestamp",
  43. }
  44. }
  45. func (m *EdbDataThsHf) Create() (err error) {
  46. o := orm.NewOrm()
  47. id, err := o.Insert(m)
  48. if err != nil {
  49. return
  50. }
  51. m.EdbDataId = int(id)
  52. return
  53. }
  54. func (m *EdbDataThsHf) CreateMulti(items []*EdbDataThsHf) (err error) {
  55. if len(items) == 0 {
  56. return
  57. }
  58. o := orm.NewOrm()
  59. _, err = o.InsertMulti(500, items)
  60. return
  61. }
  62. func (m *EdbDataThsHf) Update(cols []string) (err error) {
  63. o := orm.NewOrm()
  64. _, err = o.Update(m, cols...)
  65. return
  66. }
  67. func (m *EdbDataThsHf) Remove() (err error) {
  68. o := orm.NewOrm()
  69. sql := fmt.Sprintf(`DELETE FROM %s WHERE %s = ? LIMIT 1`, m.TableName(), m.Cols().PrimaryId)
  70. _, err = o.Raw(sql, m.EdbDataId).Exec()
  71. return
  72. }
  73. func (m *EdbDataThsHf) MultiRemove(ids []int) (err error) {
  74. if len(ids) == 0 {
  75. return
  76. }
  77. o := orm.NewOrm()
  78. sql := fmt.Sprintf(`DELETE FROM %s WHERE %s IN (%s)`, m.TableName(), m.Cols().PrimaryId, utils.GetOrmInReplace(len(ids)))
  79. _, err = o.Raw(sql, ids).Exec()
  80. return
  81. }
  82. func (m *EdbDataThsHf) RemoveByCondition(condition string, pars []interface{}) (err error) {
  83. if condition == "" {
  84. return
  85. }
  86. o := orm.NewOrm()
  87. sql := fmt.Sprintf(`DELETE FROM %s WHERE %s`, m.TableName(), condition)
  88. _, err = o.Raw(sql, pars).Exec()
  89. return
  90. }
  91. func (m *EdbDataThsHf) GetItemById(id int) (item *EdbDataThsHf, err error) {
  92. o := orm.NewOrm()
  93. sql := fmt.Sprintf(`SELECT * FROM %s WHERE %s = ? LIMIT 1`, m.TableName(), m.Cols().PrimaryId)
  94. err = o.Raw(sql, id).QueryRow(&item)
  95. return
  96. }
  97. func (m *EdbDataThsHf) GetItemByCondition(condition string, pars []interface{}, orderRule string) (item *EdbDataThsHf, err error) {
  98. o := orm.NewOrm()
  99. order := ``
  100. if orderRule != "" {
  101. order = ` ORDER BY ` + orderRule
  102. }
  103. sql := fmt.Sprintf(`SELECT * FROM %s WHERE 1=1 %s %s LIMIT 1`, m.TableName(), condition, order)
  104. err = o.Raw(sql, pars).QueryRow(&item)
  105. return
  106. }
  107. func (m *EdbDataThsHf) GetCountByCondition(condition string, pars []interface{}) (count int, err error) {
  108. o := orm.NewOrm()
  109. sql := fmt.Sprintf(`SELECT COUNT(1) FROM %s WHERE 1=1 %s`, m.TableName(), condition)
  110. err = o.Raw(sql, pars).QueryRow(&count)
  111. return
  112. }
  113. func (m *EdbDataThsHf) GetItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string) (items []*EdbDataThsHf, err error) {
  114. o := orm.NewOrm()
  115. fields := strings.Join(fieldArr, ",")
  116. if len(fieldArr) == 0 {
  117. fields = `*`
  118. }
  119. order := fmt.Sprintf(`ORDER BY %s DESC`, m.Cols().CreateTime)
  120. if orderRule != "" {
  121. order = ` ORDER BY ` + orderRule
  122. }
  123. sql := fmt.Sprintf(`SELECT %s FROM %s WHERE 1=1 %s %s`, fields, m.TableName(), condition, order)
  124. _, err = o.Raw(sql, pars).QueryRows(&items)
  125. return
  126. }
  127. func (m *EdbDataThsHf) GetPageItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string, startSize, pageSize int) (items []*EdbDataThsHf, err error) {
  128. o := orm.NewOrm()
  129. fields := strings.Join(fieldArr, ",")
  130. if len(fieldArr) == 0 {
  131. fields = `*`
  132. }
  133. order := fmt.Sprintf(`ORDER BY %s DESC`, m.Cols().CreateTime)
  134. if orderRule != "" {
  135. order = ` ORDER BY ` + orderRule
  136. }
  137. sql := fmt.Sprintf(`SELECT %s FROM %s WHERE 1=1 %s %s LIMIT ?,?`, fields, m.TableName(), condition, order)
  138. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
  139. return
  140. }
  141. // MultiUpdateValue 批量更新数据值
  142. func (m *EdbDataThsHf) MultiUpdateValue(items []*EdbDataThsHf) (err error) {
  143. if len(items) == 0 {
  144. return
  145. }
  146. o := orm.NewOrm()
  147. sql := fmt.Sprintf("UPDATE %s SET %s = ?, %s = NOW() WHERE %s = ?", m.TableName(), m.Cols().Value, m.Cols().ModifyTime, m.Cols().PrimaryId)
  148. p, e := o.Raw(sql).Prepare()
  149. if e != nil {
  150. err = fmt.Errorf("update sql prepare err: %v", e)
  151. return
  152. }
  153. defer func() {
  154. _ = p.Close()
  155. }()
  156. for _, v := range items {
  157. _, err = p.Exec(v.Value, v.EdbDataId)
  158. if err != nil {
  159. return
  160. }
  161. }
  162. return
  163. }