ai_predict_model_index.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  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. SearchText string `description:"搜索结果(含高亮)"`
  190. }
  191. func (m *AiPredictModelIndex) Format2Item() (item *AiPredictModelIndexItem) {
  192. item = new(AiPredictModelIndexItem)
  193. item.IndexId = m.AiPredictModelIndexId
  194. item.IndexName = m.IndexName
  195. item.IndexCode = m.IndexCode
  196. item.ClassifyId = m.ClassifyId
  197. item.ModelFramework = m.ModelFramework
  198. item.PredictDate = utils.TimeTransferString(utils.FormatDate, m.PredictDate)
  199. item.PredictValue = m.PredictValue
  200. item.PredictFrequency = m.PredictFrequency
  201. item.DirectionAccuracy = m.DirectionAccuracy
  202. item.AbsoluteDeviation = m.AbsoluteDeviation
  203. item.ExtraConfig = m.ExtraConfig
  204. item.SysUserId = m.SysUserId
  205. item.SysUserRealName = m.SysUserRealName
  206. item.CreateTime = utils.TimeTransferString(utils.FormatDateTime, m.CreateTime)
  207. item.ModifyTime = utils.TimeTransferString(utils.FormatDateTime, m.ModifyTime)
  208. return
  209. }
  210. type AiPredictModelIndexPageListResp struct {
  211. Paging *paging.PagingItem
  212. List []*AiPredictModelIndexItem `description:"列表"`
  213. }
  214. // RemoveIndexAndData 删除标的及数据
  215. func (m *AiPredictModelIndex) RemoveIndexAndData(indexId int) (err error) {
  216. o := orm.NewOrmUsingDB("data")
  217. tx, e := o.Begin()
  218. if e != nil {
  219. err = fmt.Errorf("trans begin err: %v", e)
  220. return
  221. }
  222. defer func() {
  223. if err != nil {
  224. _ = tx.Rollback()
  225. return
  226. }
  227. _ = tx.Commit()
  228. }()
  229. sql := `DELETE FROM ai_predict_model_index WHERE ai_predict_model_index_id = ? LIMIT 1`
  230. _, e = tx.Raw(sql, indexId).Exec()
  231. if e != nil {
  232. err = fmt.Errorf("remove index err: %v", e)
  233. return
  234. }
  235. sql = ` DELETE FROM ai_predict_model_data WHERE ai_predict_model_index_id = ?`
  236. _, e = tx.Raw(sql, indexId).Exec()
  237. if e != nil {
  238. err = fmt.Errorf("remove index data err: %v", e)
  239. return
  240. }
  241. return
  242. }
  243. // UpdateAiPredictModelIndexSortByClassifyId 根据分类id更新排序
  244. func UpdateAiPredictModelIndexSortByClassifyId(classifyId, nowSort int, prevEdbInfoId int, updateSort string) (err error) {
  245. o := orm.NewOrmUsingDB("data")
  246. sql := ` UPDATE ai_predict_model_index SET sort = ` + updateSort + ` WHERE classify_id = ?`
  247. if prevEdbInfoId > 0 {
  248. sql += ` AND ( sort > ? or ( ai_predict_model_index_id > ` + fmt.Sprint(prevEdbInfoId) + ` and sort=` + fmt.Sprint(nowSort) + ` )) `
  249. } else {
  250. sql += ` AND ( sort > ? )`
  251. }
  252. _, err = o.Raw(sql, classifyId, nowSort).Exec()
  253. return
  254. }
  255. // GetFirstAiPredictModelIndexByClassifyId 获取当前分类下,且排序数相同 的排序第一条的数据
  256. func GetFirstAiPredictModelIndexByClassifyId(classifyId int) (item *AiPredictModelIndex, err error) {
  257. o := orm.NewOrmUsingDB("data")
  258. sql := ` SELECT * FROM ai_predict_model_index WHERE classify_id = ? order by sort asc,ai_predict_model_index_id asc limit 1`
  259. err = o.Raw(sql, classifyId).QueryRow(&item)
  260. return
  261. }
  262. type AiPredictModelImportData struct {
  263. Index *AiPredictModelIndex
  264. Data []*AiPredictModelData
  265. }
  266. // ImportIndexAndData 导入数据
  267. func (m *AiPredictModelIndex) ImportIndexAndData(createIndexes, updateIndexes []*AiPredictModelImportData, updateCols []string) (err error) {
  268. if len(createIndexes) == 0 && len(updateIndexes) == 0 {
  269. return
  270. }
  271. o := orm.NewOrmUsingDB("data")
  272. tx, e := o.Begin()
  273. if e != nil {
  274. err = fmt.Errorf("trans begin err: %v", e)
  275. return
  276. }
  277. defer func() {
  278. if err != nil {
  279. _ = tx.Rollback()
  280. return
  281. }
  282. _ = tx.Commit()
  283. }()
  284. if len(updateIndexes) > 0 {
  285. for _, v := range updateIndexes {
  286. // 更新指标
  287. _, e = tx.Update(v.Index, updateCols...)
  288. if e != nil {
  289. err = fmt.Errorf("update index err: %v", e)
  290. return
  291. }
  292. for _, d := range v.Data {
  293. d.AiPredictModelIndexId = v.Index.AiPredictModelIndexId
  294. d.IndexCode = v.Index.IndexCode
  295. d.DataTimestamp = d.DataTime.UnixNano() / 1e6
  296. }
  297. // 清空指标并新增
  298. sql := `DELETE FROM ai_predict_model_data WHERE ai_predict_model_index_id = ?`
  299. _, e = tx.Raw(sql, v.Index.AiPredictModelIndexId).Exec()
  300. if e != nil {
  301. err = fmt.Errorf("clear index data err: %v", e)
  302. return
  303. }
  304. _, e = tx.InsertMulti(utils.MultiAddNum, v.Data)
  305. if e != nil {
  306. err = fmt.Errorf("insert index data err: %v", e)
  307. return
  308. }
  309. }
  310. }
  311. if len(createIndexes) > 0 {
  312. for _, v := range createIndexes {
  313. indexId, e := tx.Insert(v.Index)
  314. if e != nil {
  315. err = fmt.Errorf("insert index err: %v", e)
  316. return
  317. }
  318. v.Index.AiPredictModelIndexId = int(indexId)
  319. for _, d := range v.Data {
  320. d.AiPredictModelIndexId = int(indexId)
  321. d.IndexCode = v.Index.IndexCode
  322. d.DataTimestamp = d.DataTime.UnixNano() / 1e6
  323. }
  324. _, e = tx.InsertMulti(utils.MultiAddNum, v.Data)
  325. if e != nil {
  326. err = fmt.Errorf("insert index data err: %v", e)
  327. return
  328. }
  329. }
  330. }
  331. return
  332. }
  333. type AiPredictModelDetailResp struct {
  334. TableData []*AiPredictModelDataItem `description:"表格数据"`
  335. ChartView *data_manage.ChartInfoDetailResp `description:"月度预测数据图表"`
  336. DailyChartView *data_manage.ChartInfoDetailResp `description:"日度预测数据图表"`
  337. }
  338. type AiPredictModelIndexSaveReq struct {
  339. IndexId int `description:"指标ID"`
  340. MonthlyChart *AiPredictModelIndexSaveChart `description:"月度图表信息"`
  341. DailyChart *AiPredictModelIndexSaveChart `description:"日度图表信息"`
  342. }
  343. type AiPredictModelIndexSaveChart struct {
  344. LeftMin string `description:"图表左侧最小值"`
  345. LeftMax string `description:"图表左侧最大值"`
  346. Unit string `description:"单位"`
  347. }
  348. type AiPredictModelIndexExtraConfig struct {
  349. MonthlyChart struct {
  350. LeftMin string `description:"图表左侧最小值"`
  351. LeftMax string `description:"图表左侧最大值"`
  352. Unit string `description:"单位"`
  353. }
  354. DailyChart struct {
  355. LeftMin string `description:"图表左侧最小值"`
  356. LeftMax string `description:"图表左侧最大值"`
  357. Unit string `description:"单位"`
  358. PredictLegendName string `description:"预测图例的名称(通常为Predicted)"`
  359. }
  360. }