edb_data_calculate_stl.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. package models
  2. import (
  3. sql2 "database/sql"
  4. "eta/eta_index_lib/global"
  5. "eta/eta_index_lib/utils"
  6. "time"
  7. )
  8. type EdbDataCalculateStl struct {
  9. EdbDataId int `gorm:"column:edb_data_id;type:int(11);primaryKey;not null;"`
  10. EdbInfoId int `gorm:"column:edb_info_id;type:int(11);comment:指标id;default:NULL;"` // 指标id
  11. EdbCode string `gorm:"column:edb_code;type:varchar(190);comment:指标编码;default:NULL;"` // 指标编码
  12. DataTime time.Time `gorm:"column:data_time;type:date;comment:数据日期;default:NULL;"` // 数据日期
  13. Value float64 `gorm:"column:value;type:double;comment:数据值;default:NULL;" json:"value"` // 数据值
  14. CreateTime time.Time `gorm:"column:create_time;type:datetime;comment:创建时间;default:NULL;"` // 创建时间
  15. ModifyTime time.Time `gorm:"column:modify_time;type:datetime;comment:修改时间;default:NULL;"` // 修改时间
  16. DataTimestamp int64 `gorm:"column:data_timestamp;type:bigint(20);comment:数据日期时间戳;default:0;"` // 数据日期时间戳
  17. }
  18. type CalculateStlConfigMapping struct {
  19. Id int `gorm:"column:id;type:int(10) UNSIGNED;primaryKey;not null;"`
  20. CalculateStlConfigId int `gorm:"column:calculate_stl_config_id;type:int(10) UNSIGNED;comment:stl趋势分解配置id;not null;" ` // stl趋势分解配置id
  21. EdbInfoId int `gorm:"column:edb_info_id;type:int(11);comment:指标id;not null;"` // 指标id
  22. StlEdbType int `gorm:"column:stl_edb_type;type:tinyint(4);comment:stl指标类型: 1-Trend, 2-Seasonal, 3-Residual;default:NULL;"` // stl指标类型: 1-Trend, 2-Seasonal, 3-Residual
  23. CreateTime time.Time `gorm:"column:create_time;type:datetime;comment:关系建立时间;not null;"` // 关系建立时间
  24. ModifyTime time.Time `gorm:"column:modify_time;type:datetime;comment:更新时间;default:NULL;" ` // 更新时间
  25. }
  26. type CalculateStlConfig struct {
  27. CalculateStlConfigId int `gorm:"column:calculate_stl_config_id;type:int(10) UNSIGNED;primaryKey;not null;"`
  28. Config string `gorm:"column:config;type:text;comment:计算参数配置;not null;"` // 计算参数配置
  29. SysUserId int `gorm:"column:sys_user_id;type:int(11);comment:操作人id;not null;"` // 操作人id
  30. ModifyTime time.Time `gorm:"column:modify_time;type:datetime;comment:修改时间;default:NULL;"` // 修改时间
  31. CreateTime time.Time `gorm:"column:create_time;type:datetime;comment:创建时间;default:NULL;"` // 创建时间
  32. }
  33. func GetRelationCalculateStlConfigMappingByEdbInfoId(edbInfoId int) (items []*CalculateStlConfigMapping, err error) {
  34. o := global.DEFAULT_DB
  35. sql := `SELECT calculate_stl_config_id FROM calculate_stl_config_mapping WHERE edb_info_id =? LIMIT 1`
  36. var confId int
  37. err = o.Raw(sql, edbInfoId).First(&confId).Error
  38. if err != nil {
  39. return
  40. }
  41. sql = `SELECT * FROM calculate_stl_config_mapping WHERE calculate_stl_config_id =?`
  42. err = o.Raw(sql, confId).Find(&items).Error
  43. return
  44. }
  45. // GetCalculateStlConfigMappingByConfigId 根据配置文件id获取配置文件映射信息
  46. func GetCalculateStlConfigMappingByConfigId(configId int) (items []*CalculateStlConfigMapping, err error) {
  47. sql := `SELECT * FROM calculate_stl_config_mapping WHERE calculate_stl_config_id=?`
  48. err = global.DEFAULT_DB.Raw(sql, configId).Find(&items).Error
  49. return
  50. }
  51. // GetCalculateStlConfigMappingIdByEdbInfoId 获取配置文件id
  52. func GetCalculateStlConfigMappingIdByEdbInfoId(edbInfoId int) (configId int, err error) {
  53. var configIdNull sql2.NullInt64
  54. sql := `SELECT calculate_stl_config_id FROM calculate_stl_config_mapping WHERE edb_info_id=? LIMIT 1`
  55. err = global.DEFAULT_DB.Raw(sql, edbInfoId).Scan(&configIdNull).Error
  56. if err != nil {
  57. return
  58. }
  59. if configIdNull.Valid {
  60. configId = int(configIdNull.Int64)
  61. }
  62. return
  63. }
  64. func DeleteAndInsertEdbDataCalculateStl(edbCode string, dataList []*EdbDataCalculateStl) (err error) {
  65. tx := global.DEFAULT_DB.Begin()
  66. if err != nil {
  67. return
  68. }
  69. defer func() {
  70. if err != nil {
  71. tx.Rollback()
  72. } else {
  73. tx.Commit()
  74. }
  75. }()
  76. sql := `DELETE FROM edb_data_calculate_stl WHERE edb_code =?`
  77. err = tx.Exec(sql, edbCode).Error
  78. if err != nil {
  79. return
  80. }
  81. err = tx.CreateInBatches(dataList, utils.MultiAddNum).Error
  82. return
  83. }
  84. func (c *CalculateStlConfig) Update(cols []string) (err error) {
  85. err = global.DEFAULT_DB.Model(c).Select(cols).Updates(c).Error
  86. return
  87. }
  88. func GetCalculateStlConfigById(id int) (item *CalculateStlConfig, err error) {
  89. sql := "SELECT * FROM calculate_stl_config WHERE calculate_stl_config_id =?"
  90. err = global.DEFAULT_DB.Raw(sql, id).First(&item).Error
  91. return
  92. }