ai_predict_model_data.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. package data_manage
  2. import (
  3. "database/sql"
  4. "eta/eta_api/utils"
  5. "fmt"
  6. "github.com/beego/beego/v2/client/orm"
  7. "strings"
  8. "time"
  9. )
  10. // AiPredictModelData AI预测模型标的数据
  11. type AiPredictModelData struct {
  12. AiPredictModelDataId int `orm:"column(ai_predict_model_data_id);pk"`
  13. AiPredictModelIndexId int `description:"标的ID"`
  14. IndexCode string `description:"标的编码"`
  15. DataTime time.Time `description:"数据日期"`
  16. Value sql.NullFloat64 `description:"实际值"`
  17. PredictValue sql.NullFloat64 `description:"预测值"`
  18. Direction string `description:"方向"`
  19. DeviationRate string `description:"偏差率"`
  20. CreateTime time.Time `description:"创建时间"`
  21. ModifyTime time.Time `description:"修改时间"`
  22. DataTimestamp int64 `description:"数据日期时间戳"`
  23. }
  24. func (m *AiPredictModelData) TableName() string {
  25. return "ai_predict_model_data"
  26. }
  27. type AiPredictModelDataCols struct {
  28. PrimaryId string
  29. AiPredictModelIndexId string
  30. IndexCode string
  31. DataTime string
  32. Value string
  33. PredictValue string
  34. Direction string
  35. DeviationRate string
  36. CreateTime string
  37. ModifyTime string
  38. DataTimestamp string
  39. }
  40. func (m *AiPredictModelData) Cols() AiPredictModelDataCols {
  41. return AiPredictModelDataCols{
  42. PrimaryId: "ai_predict_model_data_id",
  43. AiPredictModelIndexId: "ai_predict_model_index_id",
  44. IndexCode: "index_code",
  45. DataTime: "data_time",
  46. Value: "value",
  47. PredictValue: "predict_value",
  48. Direction: "direction",
  49. DeviationRate: "deviation_rate",
  50. CreateTime: "create_time",
  51. ModifyTime: "modify_time",
  52. DataTimestamp: "data_timestamp",
  53. }
  54. }
  55. func (m *AiPredictModelData) Create() (err error) {
  56. o := orm.NewOrmUsingDB("data")
  57. id, err := o.Insert(m)
  58. if err != nil {
  59. return
  60. }
  61. m.AiPredictModelDataId = int(id)
  62. return
  63. }
  64. func (m *AiPredictModelData) CreateMulti(items []*AiPredictModelData) (err error) {
  65. if len(items) == 0 {
  66. return
  67. }
  68. o := orm.NewOrmUsingDB("data")
  69. _, err = o.InsertMulti(len(items), items)
  70. return
  71. }
  72. func (m *AiPredictModelData) Update(cols []string) (err error) {
  73. o := orm.NewOrmUsingDB("data")
  74. _, err = o.Update(m, cols...)
  75. return
  76. }
  77. func (m *AiPredictModelData) Remove() (err error) {
  78. o := orm.NewOrmUsingDB("data")
  79. sqlRun := fmt.Sprintf(`DELETE FROM %s WHERE %s = ? LIMIT 1`, m.TableName(), m.Cols().PrimaryId)
  80. _, err = o.Raw(sqlRun, m.AiPredictModelDataId).Exec()
  81. return
  82. }
  83. func (m *AiPredictModelData) MultiRemove(ids []int) (err error) {
  84. if len(ids) == 0 {
  85. return
  86. }
  87. o := orm.NewOrmUsingDB("data")
  88. sqlRun := fmt.Sprintf(`DELETE FROM %s WHERE %s IN (%s)`, m.TableName(), m.Cols().PrimaryId, utils.GetOrmInReplace(len(ids)))
  89. _, err = o.Raw(sqlRun, ids).Exec()
  90. return
  91. }
  92. func (m *AiPredictModelData) RemoveByCondition(condition string, pars []interface{}) (err error) {
  93. if condition == "" {
  94. return
  95. }
  96. o := orm.NewOrmUsingDB("data")
  97. sqlRun := fmt.Sprintf(`DELETE FROM %s WHERE %s`, m.TableName(), condition)
  98. _, err = o.Raw(sqlRun, pars).Exec()
  99. return
  100. }
  101. func (m *AiPredictModelData) GetItemById(id int) (item *AiPredictModelData, err error) {
  102. o := orm.NewOrmUsingDB("data")
  103. sqlRun := fmt.Sprintf(`SELECT * FROM %s WHERE %s = ? LIMIT 1`, m.TableName(), m.Cols().PrimaryId)
  104. err = o.Raw(sqlRun, id).QueryRow(&item)
  105. return
  106. }
  107. func (m *AiPredictModelData) GetItemByCondition(condition string, pars []interface{}, orderRule string) (item *AiPredictModelData, err error) {
  108. o := orm.NewOrmUsingDB("data")
  109. order := ``
  110. if orderRule != "" {
  111. order = ` ORDER BY ` + orderRule
  112. }
  113. sqlRun := fmt.Sprintf(`SELECT * FROM %s WHERE 1=1 %s %s LIMIT 1`, m.TableName(), condition, order)
  114. err = o.Raw(sqlRun, pars).QueryRow(&item)
  115. return
  116. }
  117. func (m *AiPredictModelData) GetCountByCondition(condition string, pars []interface{}) (count int, err error) {
  118. o := orm.NewOrmUsingDB("data")
  119. sqlRun := fmt.Sprintf(`SELECT COUNT(1) FROM %s WHERE 1=1 %s`, m.TableName(), condition)
  120. err = o.Raw(sqlRun, pars).QueryRow(&count)
  121. return
  122. }
  123. func (m *AiPredictModelData) GetItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string) (items []*AiPredictModelData, err error) {
  124. o := orm.NewOrmUsingDB("data")
  125. fields := strings.Join(fieldArr, ",")
  126. if len(fieldArr) == 0 {
  127. fields = `*`
  128. }
  129. order := fmt.Sprintf(`ORDER BY %s DESC`, m.Cols().CreateTime)
  130. if orderRule != "" {
  131. order = ` ORDER BY ` + orderRule
  132. }
  133. sqlRun := fmt.Sprintf(`SELECT %s FROM %s WHERE 1=1 %s %s`, fields, m.TableName(), condition, order)
  134. _, err = o.Raw(sqlRun, pars).QueryRows(&items)
  135. return
  136. }
  137. func (m *AiPredictModelData) GetPageItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string, startSize, pageSize int) (items []*AiPredictModelData, err error) {
  138. o := orm.NewOrmUsingDB("data")
  139. fields := strings.Join(fieldArr, ",")
  140. if len(fieldArr) == 0 {
  141. fields = `*`
  142. }
  143. order := fmt.Sprintf(`ORDER BY %s DESC`, m.Cols().CreateTime)
  144. if orderRule != "" {
  145. order = ` ORDER BY ` + orderRule
  146. }
  147. sqlRun := fmt.Sprintf(`SELECT %s FROM %s WHERE 1=1 %s %s LIMIT ?,?`, fields, m.TableName(), condition, order)
  148. _, err = o.Raw(sqlRun, pars, startSize, pageSize).QueryRows(&items)
  149. return
  150. }
  151. // AiPredictModelDataItem AI预测模型标的数据
  152. type AiPredictModelDataItem struct {
  153. DataId int `description:"ID"`
  154. IndexId int `description:"标的ID"`
  155. IndexCode string `description:"标的编码"`
  156. DataTime string `description:"数据日期"`
  157. Value *float64 `description:"实际值"`
  158. PredictValue *float64 `description:"预测值"`
  159. Direction string `description:"方向"`
  160. DeviationRate string `description:"偏差率"`
  161. DataTimestamp int64 `description:"数据日期时间戳"`
  162. }
  163. func (m *AiPredictModelData) Format2Item() (item *AiPredictModelDataItem) {
  164. item = new(AiPredictModelDataItem)
  165. item.DataId = m.AiPredictModelDataId
  166. item.IndexId = m.AiPredictModelIndexId
  167. item.IndexCode = m.IndexCode
  168. item.DataTime = utils.TimeTransferString(utils.FormatDate, m.DataTime)
  169. if m.Value.Valid {
  170. item.Value = &m.Value.Float64
  171. }
  172. if m.PredictValue.Valid {
  173. item.PredictValue = &m.PredictValue.Float64
  174. }
  175. item.Direction = m.Direction
  176. item.DeviationRate = m.DeviationRate
  177. item.DataTimestamp = m.DataTimestamp
  178. return
  179. }