ai_predict_model_data.go 6.7 KB

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