package models

import (
	sql2 "database/sql"
	"eta/eta_index_lib/global"
	"eta/eta_index_lib/utils"
	"time"
)

type EdbDataCalculateStl struct {
	EdbDataId     int       `gorm:"column:edb_data_id;type:int(11);primaryKey;not null;"`
	EdbInfoId     int       `gorm:"column:edb_info_id;type:int(11);comment:指标id;default:NULL;"`       // 指标id
	EdbCode       string    `gorm:"column:edb_code;type:varchar(190);comment:指标编码;default:NULL;"`     // 指标编码
	DataTime      time.Time `gorm:"column:data_time;type:date;comment:数据日期;default:NULL;"`            // 数据日期
	Value         float64   `gorm:"column:value;type:double;comment:数据值;default:NULL;" json:"value"`  // 数据值
	CreateTime    time.Time `gorm:"column:create_time;type:datetime;comment:创建时间;default:NULL;"`      // 创建时间
	ModifyTime    time.Time `gorm:"column:modify_time;type:datetime;comment:修改时间;default:NULL;"`      // 修改时间
	DataTimestamp int64     `gorm:"column:data_timestamp;type:bigint(20);comment:数据日期时间戳;default:0;"` // 数据日期时间戳
}

type CalculateStlConfigMapping struct {
	Id                   int       `gorm:"column:id;type:int(10) UNSIGNED;primaryKey;not null;"`
	CalculateStlConfigId int       `gorm:"column:calculate_stl_config_id;type:int(10) UNSIGNED;comment:stl趋势分解配置id;not null;" `                // stl趋势分解配置id
	EdbInfoId            int       `gorm:"column:edb_info_id;type:int(11);comment:指标id;not null;"`                                             // 指标id
	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
	CreateTime           time.Time `gorm:"column:create_time;type:datetime;comment:关系建立时间;not null;"`                                          // 关系建立时间
	ModifyTime           time.Time `gorm:"column:modify_time;type:datetime;comment:更新时间;default:NULL;" `                                       // 更新时间
}

type CalculateStlConfig struct {
	CalculateStlConfigId int       `gorm:"column:calculate_stl_config_id;type:int(10) UNSIGNED;primaryKey;not null;"`
	Config               string    `gorm:"column:config;type:text;comment:计算参数配置;not null;"`            // 计算参数配置
	SysUserId            int       `gorm:"column:sys_user_id;type:int(11);comment:操作人id;not null;"`     // 操作人id
	ModifyTime           time.Time `gorm:"column:modify_time;type:datetime;comment:修改时间;default:NULL;"` // 修改时间
	CreateTime           time.Time `gorm:"column:create_time;type:datetime;comment:创建时间;default:NULL;"` // 创建时间
}

func GetRelationCalculateStlConfigMappingByEdbInfoId(edbInfoId int) (items []*CalculateStlConfigMapping, err error) {
	o := global.DEFAULT_DB
	sql := `SELECT calculate_stl_config_id FROM calculate_stl_config_mapping WHERE edb_info_id =? LIMIT 1`
	var confId int
	err = o.Raw(sql, edbInfoId).First(&confId).Error
	if err != nil {
		return
	}
	sql = `SELECT * FROM calculate_stl_config_mapping WHERE calculate_stl_config_id =?`
	err = o.Raw(sql, confId).Find(&items).Error

	return
}

// GetCalculateStlConfigMappingByConfigId 根据配置文件id获取配置文件映射信息
func GetCalculateStlConfigMappingByConfigId(configId int) (items []*CalculateStlConfigMapping, err error) {
	sql := `SELECT * FROM calculate_stl_config_mapping WHERE calculate_stl_config_id=?`
	err = global.DEFAULT_DB.Raw(sql, configId).Find(&items).Error

	return
}

// GetCalculateStlConfigMappingIdByEdbInfoId 获取配置文件id
func GetCalculateStlConfigMappingIdByEdbInfoId(edbInfoId int) (configId int, err error) {
	var configIdNull sql2.NullInt64
	sql := `SELECT calculate_stl_config_id FROM calculate_stl_config_mapping WHERE edb_info_id=? LIMIT 1`
	err = global.DEFAULT_DB.Raw(sql, edbInfoId).Scan(&configIdNull).Error
	if err != nil {
		return
	}
	if configIdNull.Valid {
		configId = int(configIdNull.Int64)
	}

	return
}

func DeleteAndInsertEdbDataCalculateStl(edbCode string, dataList []*EdbDataCalculateStl) (err error) {
	tx := global.DEFAULT_DB.Begin()
	if err != nil {
		return
	}
	defer func() {
		if err != nil {
			tx.Rollback()
		} else {
			tx.Commit()
		}
	}()
	sql := `DELETE FROM edb_data_calculate_stl WHERE edb_code =?`
	err = tx.Exec(sql, edbCode).Error
	if err != nil {
		return
	}
	err = tx.CreateInBatches(dataList, utils.MultiAddNum).Error

	return
}
func (c *CalculateStlConfig) Update(cols []string) (err error) {
	err = global.DEFAULT_DB.Model(c).Select(cols).Updates(c).Error

	return
}

func GetCalculateStlConfigById(id int) (item *CalculateStlConfig, err error) {
	sql := "SELECT * FROM calculate_stl_config WHERE calculate_stl_config_id =?"
	err = global.DEFAULT_DB.Raw(sql, id).First(&item).Error

	return
}