ai_predict_model_index.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. package data_manage
  2. import (
  3. "eta/eta_api/models/data_manage"
  4. "eta/eta_api/utils"
  5. "fmt"
  6. "github.com/beego/beego/v2/client/orm"
  7. "github.com/rdlucklib/rdluck_tools/paging"
  8. "strings"
  9. "time"
  10. )
  11. // AiPredictModelIndex AI预测模型标的
  12. type AiPredictModelIndex struct {
  13. AiPredictModelIndexId int `orm:"column(ai_predict_model_index_id);pk"`
  14. IndexName string `description:"标的名称"`
  15. IndexCode string `description:"自生成的指标编码"`
  16. ClassifyId int `description:"分类ID"`
  17. ModelFramework string `description:"模型框架"`
  18. PredictDate time.Time `description:"预测日期"`
  19. PredictValue float64 `description:"预测值"`
  20. PredictFrequency string `description:"预测频度"`
  21. DirectionAccuracy string `description:"方向准确度"`
  22. AbsoluteDeviation string `description:"绝对偏差"`
  23. ExtraConfig string `description:"模型参数"`
  24. Sort int `description:"排序"`
  25. SysUserId int `description:"创建人ID"`
  26. SysUserRealName string `description:"创建人姓名"`
  27. LeftMin string `description:"图表左侧最小值"`
  28. LeftMax string `description:"图表左侧最大值"`
  29. CreateTime time.Time `description:"创建时间"`
  30. ModifyTime time.Time `description:"修改时间"`
  31. }
  32. func (m *AiPredictModelIndex) TableName() string {
  33. return "ai_predict_model_index"
  34. }
  35. type AiPredictModelIndexCols struct {
  36. PrimaryId string
  37. IndexName string
  38. IndexCode string
  39. ClassifyId string
  40. ModelFramework string
  41. PredictDate string
  42. PredictValue string
  43. DirectionAccuracy string
  44. AbsoluteDeviation string
  45. ExtraConfig string
  46. Sort string
  47. SysUserId string
  48. SysUserRealName string
  49. LeftMin string
  50. LeftMax string
  51. CreateTime string
  52. ModifyTime string
  53. }
  54. func (m *AiPredictModelIndex) Cols() AiPredictModelIndexCols {
  55. return AiPredictModelIndexCols{
  56. PrimaryId: "ai_predict_model_index_id",
  57. IndexName: "index_name",
  58. IndexCode: "index_code",
  59. ClassifyId: "classify_id",
  60. ModelFramework: "model_framework",
  61. PredictDate: "predict_date",
  62. PredictValue: "predict_value",
  63. DirectionAccuracy: "direction_accuracy",
  64. AbsoluteDeviation: "absolute_deviation",
  65. ExtraConfig: "extra_config",
  66. Sort: "sort",
  67. SysUserId: "sys_user_id",
  68. SysUserRealName: "sys_user_real_name",
  69. LeftMin: "left_min",
  70. LeftMax: "left_max",
  71. CreateTime: "create_time",
  72. ModifyTime: "modify_time",
  73. }
  74. }
  75. func (m *AiPredictModelIndex) Create() (err error) {
  76. o := orm.NewOrmUsingDB("data")
  77. id, err := o.Insert(m)
  78. if err != nil {
  79. return
  80. }
  81. m.AiPredictModelIndexId = int(id)
  82. return
  83. }
  84. func (m *AiPredictModelIndex) CreateMulti(items []*AiPredictModelIndex) (err error) {
  85. if len(items) == 0 {
  86. return
  87. }
  88. o := orm.NewOrmUsingDB("data")
  89. _, err = o.InsertMulti(len(items), items)
  90. return
  91. }
  92. func (m *AiPredictModelIndex) Update(cols []string) (err error) {
  93. o := orm.NewOrmUsingDB("data")
  94. _, err = o.Update(m, cols...)
  95. return
  96. }
  97. func (m *AiPredictModelIndex) Remove() (err error) {
  98. o := orm.NewOrmUsingDB("data")
  99. sql := fmt.Sprintf(`DELETE FROM %s WHERE %s = ? LIMIT 1`, m.TableName(), m.Cols().PrimaryId)
  100. _, err = o.Raw(sql, m.AiPredictModelIndexId).Exec()
  101. return
  102. }
  103. func (m *AiPredictModelIndex) MultiRemove(ids []int) (err error) {
  104. if len(ids) == 0 {
  105. return
  106. }
  107. o := orm.NewOrmUsingDB("data")
  108. sql := fmt.Sprintf(`DELETE FROM %s WHERE %s IN (%s)`, m.TableName(), m.Cols().PrimaryId, utils.GetOrmInReplace(len(ids)))
  109. _, err = o.Raw(sql, ids).Exec()
  110. return
  111. }
  112. func (m *AiPredictModelIndex) RemoveByCondition(condition string, pars []interface{}) (err error) {
  113. if condition == "" {
  114. return
  115. }
  116. o := orm.NewOrmUsingDB("data")
  117. sql := fmt.Sprintf(`DELETE FROM %s WHERE %s`, m.TableName(), condition)
  118. _, err = o.Raw(sql, pars).Exec()
  119. return
  120. }
  121. func (m *AiPredictModelIndex) GetItemById(id int) (item *AiPredictModelIndex, err error) {
  122. o := orm.NewOrmUsingDB("data")
  123. sql := fmt.Sprintf(`SELECT * FROM %s WHERE %s = ? LIMIT 1`, m.TableName(), m.Cols().PrimaryId)
  124. err = o.Raw(sql, id).QueryRow(&item)
  125. return
  126. }
  127. func (m *AiPredictModelIndex) GetItemByCondition(condition string, pars []interface{}, orderRule string) (item *AiPredictModelIndex, err error) {
  128. o := orm.NewOrmUsingDB("data")
  129. order := ``
  130. if orderRule != "" {
  131. order = ` ORDER BY ` + orderRule
  132. }
  133. sql := fmt.Sprintf(`SELECT * FROM %s WHERE 1=1 %s %s LIMIT 1`, m.TableName(), condition, order)
  134. err = o.Raw(sql, pars).QueryRow(&item)
  135. return
  136. }
  137. func (m *AiPredictModelIndex) GetCountByCondition(condition string, pars []interface{}) (count int, err error) {
  138. o := orm.NewOrmUsingDB("data")
  139. sql := fmt.Sprintf(`SELECT COUNT(1) FROM %s WHERE 1=1 %s`, m.TableName(), condition)
  140. err = o.Raw(sql, pars).QueryRow(&count)
  141. return
  142. }
  143. func (m *AiPredictModelIndex) GetItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string) (items []*AiPredictModelIndex, err error) {
  144. o := orm.NewOrmUsingDB("data")
  145. fields := strings.Join(fieldArr, ",")
  146. if len(fieldArr) == 0 {
  147. fields = `*`
  148. }
  149. order := fmt.Sprintf(`ORDER BY %s DESC`, m.Cols().CreateTime)
  150. if orderRule != "" {
  151. order = ` ORDER BY ` + orderRule
  152. }
  153. sql := fmt.Sprintf(`SELECT %s FROM %s WHERE 1=1 %s %s`, fields, m.TableName(), condition, order)
  154. _, err = o.Raw(sql, pars).QueryRows(&items)
  155. return
  156. }
  157. func (m *AiPredictModelIndex) GetPageItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string, startSize, pageSize int) (items []*AiPredictModelIndex, err error) {
  158. o := orm.NewOrmUsingDB("data")
  159. fields := strings.Join(fieldArr, ",")
  160. if len(fieldArr) == 0 {
  161. fields = `*`
  162. }
  163. order := fmt.Sprintf(`ORDER BY %s DESC`, m.Cols().CreateTime)
  164. if orderRule != "" {
  165. order = ` ORDER BY ` + orderRule
  166. }
  167. sql := fmt.Sprintf(`SELECT %s FROM %s WHERE 1=1 %s %s LIMIT ?,?`, fields, m.TableName(), condition, order)
  168. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
  169. return
  170. }
  171. // AiPredictModelIndexItem AI预测模型标的信息
  172. type AiPredictModelIndexItem struct {
  173. IndexId int `description:"标的ID"`
  174. IndexName string `description:"标的名称"`
  175. IndexCode string `description:"自生成的指标编码"`
  176. ClassifyId int `description:"分类ID"`
  177. ClassifyName string `description:"分类名称"`
  178. ModelFramework string `description:"模型框架"`
  179. PredictDate string `description:"预测日期"`
  180. PredictValue float64 `description:"预测值"`
  181. PredictFrequency string `description:"预测频度"`
  182. DirectionAccuracy string `description:"方向准确度"`
  183. AbsoluteDeviation string `description:"绝对偏差"`
  184. ExtraConfig string `description:"模型参数"`
  185. SysUserId int `description:"创建人ID"`
  186. SysUserRealName string `description:"创建人姓名"`
  187. CreateTime string `description:"创建时间"`
  188. ModifyTime string `description:"修改时间"`
  189. }
  190. func (m *AiPredictModelIndex) Format2Item() (item *AiPredictModelIndexItem) {
  191. item = new(AiPredictModelIndexItem)
  192. item.IndexId = m.AiPredictModelIndexId
  193. item.IndexName = m.IndexName
  194. item.IndexCode = m.IndexCode
  195. item.ClassifyId = m.ClassifyId
  196. item.ModelFramework = m.ModelFramework
  197. item.PredictDate = utils.TimeTransferString(utils.FormatDate, m.PredictDate)
  198. item.PredictValue = m.PredictValue
  199. item.PredictFrequency = m.PredictFrequency
  200. item.DirectionAccuracy = m.DirectionAccuracy
  201. item.AbsoluteDeviation = m.AbsoluteDeviation
  202. item.ExtraConfig = m.ExtraConfig
  203. item.SysUserId = m.SysUserId
  204. item.SysUserRealName = m.SysUserRealName
  205. item.CreateTime = utils.TimeTransferString(utils.FormatDateTime, m.CreateTime)
  206. item.ModifyTime = utils.TimeTransferString(utils.FormatDateTime, m.ModifyTime)
  207. return
  208. }
  209. type AiPredictModelIndexPageListResp struct {
  210. Paging *paging.PagingItem
  211. List []*AiPredictModelIndexItem `description:"列表"`
  212. }
  213. // RemoveIndexAndData 删除标的及数据
  214. func (m *AiPredictModelIndex) RemoveIndexAndData(indexId int) (err error) {
  215. o := orm.NewOrmUsingDB("data")
  216. tx, e := o.Begin()
  217. if e != nil {
  218. err = fmt.Errorf("trans begin err: %v", e)
  219. return
  220. }
  221. defer func() {
  222. if err != nil {
  223. _ = tx.Rollback()
  224. return
  225. }
  226. _ = tx.Commit()
  227. }()
  228. sql := `DELETE FROM ai_predict_model_index WHERE ai_predict_model_index_id = ? LIMIT 1`
  229. _, e = tx.Raw(sql, indexId).Exec()
  230. if e != nil {
  231. err = fmt.Errorf("remove index err: %v", e)
  232. return
  233. }
  234. sql = ` DELETE FROM ai_predict_model_data WHERE ai_predict_model_index_id = ?`
  235. _, e = tx.Raw(sql, indexId).Exec()
  236. if e != nil {
  237. err = fmt.Errorf("remove index data err: %v", e)
  238. return
  239. }
  240. return
  241. }
  242. // UpdateAiPredictModelIndexSortByClassifyId 根据分类id更新排序
  243. func UpdateAiPredictModelIndexSortByClassifyId(classifyId, nowSort int, prevEdbInfoId int, updateSort string) (err error) {
  244. o := orm.NewOrmUsingDB("data")
  245. sql := ` UPDATE ai_predict_model_index SET sort = ` + updateSort + ` WHERE classify_id = ?`
  246. if prevEdbInfoId > 0 {
  247. sql += ` AND ( sort > ? or ( ai_predict_model_index_id > ` + fmt.Sprint(prevEdbInfoId) + ` and sort=` + fmt.Sprint(nowSort) + ` )) `
  248. } else {
  249. sql += ` AND ( sort > ? )`
  250. }
  251. _, err = o.Raw(sql, classifyId, nowSort).Exec()
  252. return
  253. }
  254. // GetFirstAiPredictModelIndexByClassifyId 获取当前分类下,且排序数相同 的排序第一条的数据
  255. func GetFirstAiPredictModelIndexByClassifyId(classifyId int) (item *AiPredictModelIndex, err error) {
  256. o := orm.NewOrmUsingDB("data")
  257. sql := ` SELECT * FROM ai_predict_model_index WHERE classify_id = ? order by sort asc,ai_predict_model_index_id asc limit 1`
  258. err = o.Raw(sql, classifyId).QueryRow(&item)
  259. return
  260. }
  261. type AiPredictModelImportData struct {
  262. Index *AiPredictModelIndex
  263. Data []*AiPredictModelData
  264. }
  265. // ImportIndexAndData 导入数据
  266. func (m *AiPredictModelIndex) ImportIndexAndData(createIndexes, updateIndexes []*AiPredictModelImportData, updateCols []string) (err error) {
  267. if len(createIndexes) == 0 && len(updateIndexes) == 0 {
  268. return
  269. }
  270. o := orm.NewOrmUsingDB("data")
  271. tx, e := o.Begin()
  272. if e != nil {
  273. err = fmt.Errorf("trans begin err: %v", e)
  274. return
  275. }
  276. defer func() {
  277. if err != nil {
  278. _ = tx.Rollback()
  279. return
  280. }
  281. _ = tx.Commit()
  282. }()
  283. if len(updateIndexes) > 0 {
  284. for _, v := range updateIndexes {
  285. // 更新指标
  286. _, e = tx.Update(v.Index, updateCols...)
  287. if e != nil {
  288. err = fmt.Errorf("update index err: %v", e)
  289. return
  290. }
  291. for _, d := range v.Data {
  292. d.AiPredictModelIndexId = v.Index.AiPredictModelIndexId
  293. d.IndexCode = v.Index.IndexCode
  294. d.DataTimestamp = d.DataTime.UnixNano() / 1e6
  295. }
  296. // 清空指标并新增
  297. sql := `DELETE FROM ai_predict_model_data WHERE ai_predict_model_index_id = ?`
  298. _, e = tx.Raw(sql, v.Index.AiPredictModelIndexId).Exec()
  299. if e != nil {
  300. err = fmt.Errorf("clear index data err: %v", e)
  301. return
  302. }
  303. _, e = tx.InsertMulti(utils.MultiAddNum, v.Data)
  304. if e != nil {
  305. err = fmt.Errorf("insert index data err: %v", e)
  306. return
  307. }
  308. }
  309. }
  310. if len(createIndexes) > 0 {
  311. for _, v := range createIndexes {
  312. indexId, e := tx.Insert(v.Index)
  313. if e != nil {
  314. err = fmt.Errorf("insert index err: %v", e)
  315. return
  316. }
  317. for _, d := range v.Data {
  318. d.AiPredictModelIndexId = int(indexId)
  319. d.IndexCode = v.Index.IndexCode
  320. d.DataTimestamp = d.DataTime.UnixNano() / 1e6
  321. }
  322. _, e = tx.InsertMulti(utils.MultiAddNum, v.Data)
  323. if e != nil {
  324. err = fmt.Errorf("insert index data err: %v", e)
  325. return
  326. }
  327. }
  328. }
  329. return
  330. }
  331. type AiPredictModelDetailResp struct {
  332. TableData []*AiPredictModelDataItem `description:"表格数据"`
  333. ChartView *data_manage.ChartInfoDetailResp `description:"月度预测数据图表"`
  334. DailyChartView *data_manage.ChartInfoDetailResp `description:"日度预测数据图表"`
  335. }
  336. type AiPredictModelIndexSaveReq struct {
  337. IndexId int `description:"指标ID"`
  338. MonthlyChart *AiPredictModelIndexSaveChart `description:"月度图表信息"`
  339. DailyChart *AiPredictModelIndexSaveChart `description:"日度图表信息"`
  340. }
  341. type AiPredictModelIndexSaveChart struct {
  342. LeftMin string `description:"图表左侧最小值"`
  343. LeftMax string `description:"图表左侧最大值"`
  344. Unit string `description:"单位"`
  345. }
  346. type AiPredictModelIndexExtraConfig struct {
  347. MonthlyChart struct {
  348. LeftMin string `description:"图表左侧最小值"`
  349. LeftMax string `description:"图表左侧最大值"`
  350. Unit string `description:"单位"`
  351. }
  352. DailyChart struct {
  353. LeftMin string `description:"图表左侧最小值"`
  354. LeftMax string `description:"图表左侧最大值"`
  355. Unit string `description:"单位"`
  356. PredictLegendName string `description:"预测图例的名称(通常为Predicted)"`
  357. }
  358. }