base_from_ths_hf_data.go 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. package models
  2. import (
  3. "eta_gn/eta_index_lib/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.NewOrm()
  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.NewOrm()
  62. _, err = o.InsertMulti(500, items)
  63. return
  64. }
  65. func (m *BaseFromThsHfData) Update(cols []string) (err error) {
  66. o := orm.NewOrm()
  67. _, err = o.Update(m, cols...)
  68. return
  69. }
  70. func (m *BaseFromThsHfData) 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.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.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 *BaseFromThsHfData) 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 *BaseFromThsHfData) GetItemById(id int) (item *BaseFromThsHfData, 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 *BaseFromThsHfData) GetItemByCondition(condition string, pars []interface{}, orderRule string) (item *BaseFromThsHfData, 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 *BaseFromThsHfData) 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 *BaseFromThsHfData) GetItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string) (items []*BaseFromThsHfData, 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 *BaseFromThsHfData) GetPageItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string, startSize, pageSize int) (items []*BaseFromThsHfData, 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. // 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. }
  163. func (m *BaseFromThsHfData) MultiInsertOrUpdate(inserts, updates []*BaseFromThsHfData) (err error) {
  164. o := orm.NewOrm()
  165. if len(inserts) > 0 {
  166. _, e := o.InsertMulti(600, inserts)
  167. if e != nil {
  168. err = fmt.Errorf("insert multi err: %s", e.Error())
  169. return
  170. }
  171. }
  172. if len(updates) > 0 {
  173. sql := fmt.Sprintf("UPDATE %s SET %s = ?, modify_time = NOW() WHERE %s = ?", m.TableName(), m.Cols().Value, m.Cols().UniqueCode)
  174. p, e := o.Raw(sql).Prepare()
  175. if e != nil {
  176. err = fmt.Errorf("prepare err: %s", e.Error())
  177. return
  178. }
  179. defer func() {
  180. _ = p.Close()
  181. }()
  182. for _, v := range updates {
  183. _, e = p.Exec(v.Value, v.UniqueCode)
  184. if e != nil {
  185. err = fmt.Errorf("update err: %s", e.Error())
  186. return
  187. }
  188. }
  189. }
  190. return
  191. }
  192. func (m *BaseFromThsHfData) GetIndexMinMax(indexCode string) (item *EdbInfoMaxAndMinInfo, err error) {
  193. o := orm.NewOrm()
  194. sql := fmt.Sprintf(`SELECT MIN(%s) AS min_date, MAX(%s) AS max_date, MIN(%s) AS min_value,MAX(%s) AS max_value FROM %s WHERE %s = ?`, m.Cols().DataTime, m.Cols().DataTime, m.Cols().Value, m.Cols().Value, m.TableName(), m.Cols().IndexCode)
  195. err = o.Raw(sql, indexCode).QueryRow(&item)
  196. if err != nil {
  197. return
  198. }
  199. // 获取最新值
  200. var lastVal float64
  201. sql = fmt.Sprintf(`SELECT %s AS latest_value FROM %s WHERE %s = ? ORDER BY %s DESC LIMIT 1`, m.Cols().Value, m.TableName(), m.Cols().IndexCode, m.Cols().DataTime)
  202. err = o.Raw(sql, indexCode).QueryRow(&lastVal)
  203. if err != nil {
  204. return
  205. }
  206. item.LatestValue = lastVal
  207. return
  208. }