ai_predict_model_index.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. package data_manage
  2. import (
  3. "eta/eta_api/global"
  4. "eta/eta_api/models/data_manage"
  5. "eta/eta_api/utils"
  6. "fmt"
  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" gorm:"primaryKey"`
  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 := global.DbMap[utils.DbNameIndex]
  77. err = o.Create(m).Error
  78. return
  79. }
  80. func (m *AiPredictModelIndex) CreateMulti(items []*AiPredictModelIndex) (err error) {
  81. if len(items) == 0 {
  82. return
  83. }
  84. o := global.DbMap[utils.DbNameIndex]
  85. err = o.CreateInBatches(items, utils.MultiAddNum).Error
  86. return
  87. }
  88. func (m *AiPredictModelIndex) Update(cols []string) (err error) {
  89. o := global.DbMap[utils.DbNameIndex]
  90. err = o.Select(cols).Updates(m).Error
  91. return
  92. }
  93. func (m *AiPredictModelIndex) Remove() (err error) {
  94. o := global.DbMap[utils.DbNameIndex]
  95. sql := fmt.Sprintf(`DELETE FROM %s WHERE %s = ? LIMIT 1`, m.TableName(), m.Cols().PrimaryId)
  96. err = o.Exec(sql, m.AiPredictModelIndexId).Error
  97. return
  98. }
  99. func (m *AiPredictModelIndex) MultiRemove(ids []int) (err error) {
  100. if len(ids) == 0 {
  101. return
  102. }
  103. o := global.DbMap[utils.DbNameIndex]
  104. sql := fmt.Sprintf(`DELETE FROM %s WHERE %s IN (%s)`, m.TableName(), m.Cols().PrimaryId, utils.GetOrmInReplace(len(ids)))
  105. err = o.Exec(sql, ids).Error
  106. return
  107. }
  108. func (m *AiPredictModelIndex) RemoveByCondition(condition string, pars []interface{}) (err error) {
  109. if condition == "" {
  110. return
  111. }
  112. o := global.DbMap[utils.DbNameIndex]
  113. sql := fmt.Sprintf(`DELETE FROM %s WHERE %s`, m.TableName(), condition)
  114. err = o.Exec(sql, pars...).Error
  115. return
  116. }
  117. func (m *AiPredictModelIndex) GetItemById(id int) (item *AiPredictModelIndex, err error) {
  118. o := global.DbMap[utils.DbNameIndex]
  119. sql := fmt.Sprintf(`SELECT * FROM %s WHERE %s = ? LIMIT 1`, m.TableName(), m.Cols().PrimaryId)
  120. err = o.Raw(sql, id).First(&item).Error
  121. return
  122. }
  123. func (m *AiPredictModelIndex) GetItemByCondition(condition string, pars []interface{}, orderRule string) (item *AiPredictModelIndex, err error) {
  124. o := global.DbMap[utils.DbNameIndex]
  125. order := ``
  126. if orderRule != "" {
  127. order = ` ORDER BY ` + orderRule
  128. }
  129. sql := fmt.Sprintf(`SELECT * FROM %s WHERE 1=1 %s %s LIMIT 1`, m.TableName(), condition, order)
  130. err = o.Raw(sql, pars...).First(&item).Error
  131. return
  132. }
  133. func (m *AiPredictModelIndex) GetCountByCondition(condition string, pars []interface{}) (count int, err error) {
  134. o := global.DbMap[utils.DbNameIndex]
  135. sql := fmt.Sprintf(`SELECT COUNT(1) FROM %s WHERE 1=1 %s`, m.TableName(), condition)
  136. err = o.Raw(sql, pars...).Scan(&count).Error
  137. return
  138. }
  139. func (m *AiPredictModelIndex) GetItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string) (items []*AiPredictModelIndex, err error) {
  140. o := global.DbMap[utils.DbNameIndex]
  141. fields := strings.Join(fieldArr, ",")
  142. if len(fieldArr) == 0 {
  143. fields = `*`
  144. }
  145. order := fmt.Sprintf(`ORDER BY %s DESC`, m.Cols().CreateTime)
  146. if orderRule != "" {
  147. order = ` ORDER BY ` + orderRule
  148. }
  149. sql := fmt.Sprintf(`SELECT %s FROM %s WHERE 1=1 %s %s`, fields, m.TableName(), condition, order)
  150. err = o.Raw(sql, pars...).Find(&items).Error
  151. return
  152. }
  153. func (m *AiPredictModelIndex) GetPageItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string, startSize, pageSize int) (items []*AiPredictModelIndex, err error) {
  154. o := global.DbMap[utils.DbNameIndex]
  155. fields := strings.Join(fieldArr, ",")
  156. if len(fieldArr) == 0 {
  157. fields = `*`
  158. }
  159. order := fmt.Sprintf(`ORDER BY %s DESC`, m.Cols().CreateTime)
  160. if orderRule != "" {
  161. order = ` ORDER BY ` + orderRule
  162. }
  163. sql := fmt.Sprintf(`SELECT %s FROM %s WHERE 1=1 %s %s LIMIT ?,?`, fields, m.TableName(), condition, order)
  164. pars = append(pars, startSize, pageSize)
  165. err = o.Raw(sql, pars...).Find(&items).Error
  166. return
  167. }
  168. // AiPredictModelIndexItem AI预测模型标的信息
  169. type AiPredictModelIndexItem struct {
  170. IndexId int `description:"标的ID"`
  171. IndexName string `description:"标的名称"`
  172. IndexCode string `description:"自生成的指标编码"`
  173. ClassifyId int `description:"分类ID"`
  174. ClassifyName string `description:"分类名称"`
  175. ModelFramework string `description:"模型框架"`
  176. PredictDate string `description:"预测日期"`
  177. PredictValue float64 `description:"预测值"`
  178. PredictFrequency string `description:"预测频度"`
  179. DirectionAccuracy string `description:"方向准确度"`
  180. AbsoluteDeviation string `description:"绝对偏差"`
  181. ExtraConfig string `description:"模型参数"`
  182. SysUserId int `description:"创建人ID"`
  183. SysUserRealName string `description:"创建人姓名"`
  184. CreateTime string `description:"创建时间"`
  185. ModifyTime string `description:"修改时间"`
  186. SearchText string `description:"搜索结果(含高亮)"`
  187. }
  188. func (m *AiPredictModelIndex) Format2Item() (item *AiPredictModelIndexItem) {
  189. item = new(AiPredictModelIndexItem)
  190. item.IndexId = m.AiPredictModelIndexId
  191. item.IndexName = m.IndexName
  192. item.IndexCode = m.IndexCode
  193. item.ClassifyId = m.ClassifyId
  194. item.ModelFramework = m.ModelFramework
  195. item.PredictDate = utils.TimeTransferString(utils.FormatDate, m.PredictDate)
  196. item.PredictValue = m.PredictValue
  197. item.PredictFrequency = m.PredictFrequency
  198. item.DirectionAccuracy = m.DirectionAccuracy
  199. item.AbsoluteDeviation = m.AbsoluteDeviation
  200. item.ExtraConfig = m.ExtraConfig
  201. item.SysUserId = m.SysUserId
  202. item.SysUserRealName = m.SysUserRealName
  203. item.CreateTime = utils.TimeTransferString(utils.FormatDateTime, m.CreateTime)
  204. item.ModifyTime = utils.TimeTransferString(utils.FormatDateTime, m.ModifyTime)
  205. return
  206. }
  207. type AiPredictModelIndexPageListResp struct {
  208. Paging *paging.PagingItem
  209. List []*AiPredictModelIndexItem `description:"列表"`
  210. }
  211. // RemoveIndexAndData 删除标的及数据
  212. func (m *AiPredictModelIndex) RemoveIndexAndData(indexId int) (err error) {
  213. o := global.DbMap[utils.DbNameIndex]
  214. tx := o.Begin()
  215. defer func() {
  216. if err != nil {
  217. _ = tx.Rollback()
  218. return
  219. }
  220. _ = tx.Commit()
  221. }()
  222. sql := `DELETE FROM ai_predict_model_index WHERE ai_predict_model_index_id = ? LIMIT 1`
  223. e := tx.Exec(sql, indexId).Error
  224. if e != nil {
  225. err = fmt.Errorf("remove index err: %v", e)
  226. return
  227. }
  228. sql = ` DELETE FROM ai_predict_model_data WHERE ai_predict_model_index_id = ?`
  229. e = tx.Exec(sql, indexId).Error
  230. if e != nil {
  231. err = fmt.Errorf("remove index data err: %v", e)
  232. return
  233. }
  234. return
  235. }
  236. // UpdateAiPredictModelIndexSortByClassifyId 根据分类id更新排序
  237. func UpdateAiPredictModelIndexSortByClassifyId(classifyId, nowSort int, prevEdbInfoId int, updateSort string) (err error) {
  238. o := global.DbMap[utils.DbNameIndex]
  239. sql := ` UPDATE ai_predict_model_index SET sort = ` + updateSort + ` WHERE classify_id = ?`
  240. if prevEdbInfoId > 0 {
  241. sql += ` AND ( sort > ? or ( ai_predict_model_index_id > ` + fmt.Sprint(prevEdbInfoId) + ` and sort=` + fmt.Sprint(nowSort) + ` )) `
  242. } else {
  243. sql += ` AND ( sort > ? )`
  244. }
  245. err = o.Exec(sql, classifyId, nowSort).Error
  246. return
  247. }
  248. // GetFirstAiPredictModelIndexByClassifyId 获取当前分类下,且排序数相同 的排序第一条的数据
  249. func GetFirstAiPredictModelIndexByClassifyId(classifyId int) (item *AiPredictModelIndex, err error) {
  250. o := global.DbMap[utils.DbNameIndex]
  251. sql := ` SELECT * FROM ai_predict_model_index WHERE classify_id = ? order by sort asc,ai_predict_model_index_id asc limit 1`
  252. err = o.Raw(sql, classifyId).First(&item).Error
  253. return
  254. }
  255. type AiPredictModelImportData struct {
  256. Index *AiPredictModelIndex
  257. Data []*AiPredictModelData
  258. }
  259. // ImportIndexAndData 导入数据
  260. func (m *AiPredictModelIndex) ImportIndexAndData(createIndexes, updateIndexes []*AiPredictModelImportData, updateCols []string) (err error) {
  261. if len(createIndexes) == 0 && len(updateIndexes) == 0 {
  262. return
  263. }
  264. o := global.DbMap[utils.DbNameIndex]
  265. tx := o.Begin()
  266. defer func() {
  267. if err != nil {
  268. _ = tx.Rollback()
  269. return
  270. }
  271. _ = tx.Commit()
  272. }()
  273. if len(updateIndexes) > 0 {
  274. for _, v := range updateIndexes {
  275. // 更新指标
  276. e := tx.Select(updateCols).Updates(v.Index).Error
  277. if e != nil {
  278. err = fmt.Errorf("update index err: %v", e)
  279. return
  280. }
  281. for _, d := range v.Data {
  282. d.AiPredictModelIndexId = v.Index.AiPredictModelIndexId
  283. d.IndexCode = v.Index.IndexCode
  284. d.DataTimestamp = d.DataTime.UnixNano() / 1e6
  285. }
  286. // 清空指标并新增
  287. sql := `DELETE FROM ai_predict_model_data WHERE ai_predict_model_index_id = ?`
  288. e = tx.Exec(sql, v.Index.AiPredictModelIndexId).Error
  289. if e != nil {
  290. err = fmt.Errorf("clear index data err: %v", e)
  291. return
  292. }
  293. e = tx.CreateInBatches(v.Data, utils.MultiAddNum).Error
  294. if e != nil {
  295. err = fmt.Errorf("insert index data err: %v", e)
  296. return
  297. }
  298. }
  299. }
  300. if len(createIndexes) > 0 {
  301. for _, v := range createIndexes {
  302. e := tx.Create(v.Index).Error
  303. if e != nil {
  304. err = fmt.Errorf("insert index err: %v", e)
  305. return
  306. }
  307. indexId := v.Index.AiPredictModelIndexId
  308. for _, d := range v.Data {
  309. d.AiPredictModelIndexId = int(indexId)
  310. d.IndexCode = v.Index.IndexCode
  311. d.DataTimestamp = d.DataTime.UnixNano() / 1e6
  312. }
  313. e = tx.CreateInBatches(v.Data, utils.MultiAddNum).Error
  314. if e != nil {
  315. err = fmt.Errorf("insert index data err: %v", e)
  316. return
  317. }
  318. }
  319. }
  320. return
  321. }
  322. type AiPredictModelDetailResp struct {
  323. TableData []*AiPredictModelDataItem `description:"表格数据"`
  324. ChartView *data_manage.ChartInfoDetailResp `description:"月度预测数据图表"`
  325. DailyChartView *data_manage.ChartInfoDetailResp `description:"日度预测数据图表"`
  326. }
  327. type AiPredictModelIndexSaveReq struct {
  328. IndexId int `description:"指标ID"`
  329. MonthlyChart *AiPredictModelIndexSaveChart `description:"月度图表信息"`
  330. DailyChart *AiPredictModelIndexSaveChart `description:"日度图表信息"`
  331. }
  332. type AiPredictModelIndexSaveChart struct {
  333. LeftMin string `description:"图表左侧最小值"`
  334. LeftMax string `description:"图表左侧最大值"`
  335. Unit string `description:"单位"`
  336. }
  337. type AiPredictModelIndexExtraConfig struct {
  338. MonthlyChart struct {
  339. LeftMin string `description:"图表左侧最小值"`
  340. LeftMax string `description:"图表左侧最大值"`
  341. Unit string `description:"单位"`
  342. }
  343. DailyChart struct {
  344. LeftMin string `description:"图表左侧最小值"`
  345. LeftMax string `description:"图表左侧最大值"`
  346. Unit string `description:"单位"`
  347. PredictLegendName string `description:"预测图例的名称(通常为Predicted)"`
  348. }
  349. }