package data_manage

import (
	"eta/eta_api/global"
	"eta/eta_api/utils"
	"time"
)

// MultipleGraphConfigEdbMapping 指标与多图配置的关系表
type MultipleGraphConfigEdbMapping struct {
	Id                    int       `orm:"column(id);pk" gorm:"primaryKey"`
	MultipleGraphConfigId int       `description:"多图配置id"`
	EdbInfoId             int       `description:"指标id"`
	Source                int       `description:"来源,1:曲线图,2:相关性图;3:滚动相关性图1;4:滚动相关性图2;10:区间计算指标"`
	ModifyTime            time.Time `description:"最近一次修改时间"`
	CreateTime            time.Time `description:"添加时间"`
}

// AddMultipleGraphConfigEdbMapping 新增多图配置
func AddMultipleGraphConfigEdbMapping(item *MultipleGraphConfigEdbMapping) (err error) {
	o := global.DbMap[utils.DbNameIndex]
	// 表格信息入库
	err = o.Create(item).Error
	return
}

// Update 更新 基础信息
func (item *MultipleGraphConfigEdbMapping) Update(cols []string) (err error) {
	o := global.DbMap[utils.DbNameIndex]
	err = o.Select(cols).Updates(item).Error
	return
}

// GetMultipleGraphConfigEdbMappingByIdAndSource 根据配置id和来源获取关联关系
func GetMultipleGraphConfigEdbMappingByIdAndSource(configId, source int) (item *MultipleGraphConfigEdbMapping, err error) {
	o := global.DbMap[utils.DbNameIndex]
	sql := `SELECT * FROM multiple_graph_config_edb_mapping WHERE multiple_graph_config_id = ? AND source = ? `
	err = o.Raw(sql, configId, source).Find(&item).Error

	return
}

// GetMultipleGraphConfigEdbMappingListById 根据配置id获取所有关联关系
func GetMultipleGraphConfigEdbMappingListById(configId int) (items []*MultipleGraphConfigEdbMapping, err error) {
	o := global.DbMap[utils.DbNameIndex]
	sql := `SELECT * FROM multiple_graph_config_edb_mapping WHERE multiple_graph_config_id = ? `
	err = o.Raw(sql, configId).Find(&items).Error

	return
}

func GetMultipleGraphConfigEdbMappingListByIdAndSource(configId, source int) (items []*MultipleGraphConfigEdbMapping, err error) {
	o := global.DbMap[utils.DbNameIndex]
	sql := `SELECT * FROM multiple_graph_config_edb_mapping WHERE multiple_graph_config_id = ? AND source = ? `
	err = o.Raw(sql, configId, source).Find(&items).Error

	return
}

// AddMultipleGraphConfigEdbMappingList 新增多图配置
func AddMultipleGraphConfigEdbMappingList(items []*MultipleGraphConfigEdbMapping) (err error) {
	o := global.DbMap[utils.DbNameIndex]
	err = o.CreateInBatches(items, utils.MultiAddNum).Error
	return
}

func DeleteMultipleGraphConfigEdbMappingByEdbIds(configId, source int, edbIds []int) (err error) {
	o := global.DbMap[utils.DbNameIndex]
	sql := `DELETE FROM multiple_graph_config_edb_mapping WHERE multiple_graph_config_id = ? AND source = ? AND edb_info_id IN (` + utils.GetOrmInReplace(len(edbIds)) + `) `
	err = o.Exec(sql, configId, source, edbIds).Error
	return
}

func DeleteMultipleGraphConfigByChartInfoId(chartInfoId, configId, source int) (err error) {
	o := global.DbMap[utils.DbNameIndex]
	to := o.Begin()
	defer func() {
		if err != nil {
			_ = to.Rollback()
		} else {
			_ = to.Commit()
		}
	}()
	sql := ` DELETE FROM multiple_graph_config WHERE multiple_graph_config_id = ?`
	err = to.Exec(sql, configId).Error
	if err != nil {
		return
	}
	sql = ` DELETE FROM multiple_graph_config_chart_mapping WHERE chart_info_id=? and multiple_graph_config_id = ? and	source=?`
	err = to.Exec(sql, chartInfoId, configId, source).Error
	if err != nil {
		return
	}
	sql = ` DELETE FROM  multiple_graph_config_edb_mapping WHERE multiple_graph_config_id = ? and	source=? `
	err = to.Exec(sql, configId, source).Error
	return
}