ai_predict_model_data.go 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. package data_manage
  2. import (
  3. "database/sql"
  4. "eta/eta_api/global"
  5. "eta/eta_api/utils"
  6. "fmt"
  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" gorm:"primaryKey"`
  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 := global.DbMap[utils.DbNameIndex]
  64. err = o.Create(m).Error
  65. return
  66. }
  67. func (m *AiPredictModelData) CreateMulti(items []*AiPredictModelData) (err error) {
  68. if len(items) == 0 {
  69. return
  70. }
  71. o := global.DbMap[utils.DbNameIndex]
  72. err = o.CreateInBatches(items, utils.MultiAddNum).Error
  73. return
  74. }
  75. func (m *AiPredictModelData) Update(cols []string) (err error) {
  76. o := global.DbMap[utils.DbNameIndex]
  77. err = o.Select(cols).Updates(m).Error
  78. return
  79. }
  80. func (m *AiPredictModelData) Remove() (err error) {
  81. o := global.DbMap[utils.DbNameIndex]
  82. sqlRun := fmt.Sprintf(`DELETE FROM %s WHERE %s = ? LIMIT 1`, m.TableName(), m.Cols().PrimaryId)
  83. err = o.Exec(sqlRun, m.AiPredictModelDataId).Error
  84. return
  85. }
  86. func (m *AiPredictModelData) MultiRemove(ids []int) (err error) {
  87. if len(ids) == 0 {
  88. return
  89. }
  90. o := global.DbMap[utils.DbNameIndex]
  91. sqlRun := fmt.Sprintf(`DELETE FROM %s WHERE %s IN (%s)`, m.TableName(), m.Cols().PrimaryId, utils.GetOrmInReplace(len(ids)))
  92. err = o.Exec(sqlRun, ids).Error
  93. return
  94. }
  95. func (m *AiPredictModelData) RemoveByCondition(condition string, pars []interface{}) (err error) {
  96. if condition == "" {
  97. return
  98. }
  99. o := global.DbMap[utils.DbNameIndex]
  100. sqlRun := fmt.Sprintf(`DELETE FROM %s WHERE %s`, m.TableName(), condition)
  101. err = o.Exec(sqlRun, pars...).Error
  102. return
  103. }
  104. func (m *AiPredictModelData) GetItemById(id int) (item *AiPredictModelData, err error) {
  105. o := global.DbMap[utils.DbNameIndex]
  106. sqlRun := fmt.Sprintf(`SELECT * FROM %s WHERE %s = ? LIMIT 1`, m.TableName(), m.Cols().PrimaryId)
  107. err = o.Raw(sqlRun, id).First(&item).Error
  108. return
  109. }
  110. func (m *AiPredictModelData) GetItemByCondition(condition string, pars []interface{}, orderRule string) (item *AiPredictModelData, err error) {
  111. o := global.DbMap[utils.DbNameIndex]
  112. order := ``
  113. if orderRule != "" {
  114. order = ` ORDER BY ` + orderRule
  115. }
  116. sqlRun := fmt.Sprintf(`SELECT * FROM %s WHERE 1=1 %s %s LIMIT 1`, m.TableName(), condition, order)
  117. err = o.Raw(sqlRun, pars...).First(&item).Error
  118. return
  119. }
  120. func (m *AiPredictModelData) GetCountByCondition(condition string, pars []interface{}) (count int, err error) {
  121. o := global.DbMap[utils.DbNameIndex]
  122. sqlRun := fmt.Sprintf(`SELECT COUNT(1) FROM %s WHERE 1=1 %s`, m.TableName(), condition)
  123. err = o.Raw(sqlRun, pars...).Scan(&count).Error
  124. return
  125. }
  126. func (m *AiPredictModelData) GetItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string) (items []*AiPredictModelData, err error) {
  127. o := global.DbMap[utils.DbNameIndex]
  128. fields := strings.Join(fieldArr, ",")
  129. if len(fieldArr) == 0 {
  130. fields = `*`
  131. }
  132. order := fmt.Sprintf(`ORDER BY %s DESC`, m.Cols().CreateTime)
  133. if orderRule != "" {
  134. order = ` ORDER BY ` + orderRule
  135. }
  136. sqlRun := fmt.Sprintf(`SELECT %s FROM %s WHERE 1=1 %s %s`, fields, m.TableName(), condition, order)
  137. err = o.Raw(sqlRun, pars...).Find(&items).Error
  138. return
  139. }
  140. func (m *AiPredictModelData) GetPageItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string, startSize, pageSize int) (items []*AiPredictModelData, err error) {
  141. o := global.DbMap[utils.DbNameIndex]
  142. fields := strings.Join(fieldArr, ",")
  143. if len(fieldArr) == 0 {
  144. fields = `*`
  145. }
  146. order := fmt.Sprintf(`ORDER BY %s DESC`, m.Cols().CreateTime)
  147. if orderRule != "" {
  148. order = ` ORDER BY ` + orderRule
  149. }
  150. sqlRun := fmt.Sprintf(`SELECT %s FROM %s WHERE 1=1 %s %s LIMIT ?,?`, fields, m.TableName(), condition, order)
  151. pars = append(pars, startSize, pageSize)
  152. err = o.Raw(sqlRun, pars...).Find(&items).Error
  153. return
  154. }
  155. // AiPredictModelDataItem AI预测模型标的数据
  156. type AiPredictModelDataItem struct {
  157. DataId int `description:"ID"`
  158. IndexId int `description:"标的ID"`
  159. IndexCode string `description:"标的编码"`
  160. DataTime string `description:"数据日期"`
  161. Value *float64 `description:"实际值"`
  162. PredictValue *float64 `description:"预测值"`
  163. Direction string `description:"方向"`
  164. DeviationRate string `description:"偏差率"`
  165. DataTimestamp int64 `description:"数据日期时间戳" gorm:"column:data_timestamp"`
  166. }
  167. func (m *AiPredictModelData) Format2Item() (item *AiPredictModelDataItem) {
  168. item = new(AiPredictModelDataItem)
  169. item.DataId = m.AiPredictModelDataId
  170. item.IndexId = m.AiPredictModelIndexId
  171. item.IndexCode = m.IndexCode
  172. item.DataTime = utils.TimeTransferString(utils.FormatDate, m.DataTime)
  173. if m.Value.Valid {
  174. item.Value = &m.Value.Float64
  175. }
  176. if m.PredictValue.Valid {
  177. item.PredictValue = &m.PredictValue.Float64
  178. }
  179. item.Direction = m.Direction
  180. item.DeviationRate = m.DeviationRate
  181. item.DataTimestamp = m.DataTimestamp
  182. return
  183. }