package data_manage

import (
	"eta/eta_mobile/utils"
	"github.com/beego/beego/v2/client/orm"
	"time"
)

// PredictEdbConfCalculateMapping 预测基础指标规则 与 计算预测指标关联关系表
type PredictEdbConfCalculateMapping struct {
	PredictEdbConfCalculateMappingId int       `orm:"column(predict_edb_conf_calculate_mapping_id);pk"`
	EdbInfoId                        int       `description:"指标id"`
	ConfigId                         int       `description:"配置id"`
	FromEdbInfoId                    int       `description:"基础指标id"`
	FromEdbCode                      string    `description:"基础指标编码"`
	FromEdbName                      string    `description:"基础指标名称"`
	FromSource                       int       `description:"基础指标来源"`
	FromSourceName                   string    `description:"基础指标来源名称"`
	FromTag                          string    `description:"来源指标标签"`
	Sort                             int       `description:"计算指标名称排序"`
	CreateTime                       time.Time `description:"创建时间"`
	ModifyTime                       time.Time `description:"修改时间"`
}

// GetPredictEdbConfCalculateMappingListById 根据预测指标id获取预测指标配置的关联指标信息列表
func GetPredictEdbConfCalculateMappingListById(edbInfoId int) (items []*PredictEdbConf, err error) {
	o := orm.NewOrmUsingDB("data")
	sql := ` SELECT * FROM predict_edb_conf_calculate_mapping WHERE edb_info_id=? ORDER BY predict_edb_conf_calculate_mapping_id ASC`
	_, err = o.Raw(sql, edbInfoId).QueryRows(&items)
	return
}

// GetPredictEdbConfCalculateMappingListByConfigId 根据预测指标配置id获取预测指标配置的关联指标信息列表
func GetPredictEdbConfCalculateMappingListByConfigId(edbInfoId, configId int) (items []*PredictEdbConf, err error) {
	o := orm.NewOrmUsingDB("data")
	sql := ` SELECT * FROM predict_edb_conf_calculate_mapping WHERE edb_info_id=? AND config_id=? ORDER BY predict_edb_conf_calculate_mapping_id ASC`
	_, err = o.Raw(sql, edbInfoId, configId).QueryRows(&items)
	return
}

type PredictEdbConfCalculateMappingDetail struct {
	PredictEdbConfCalculateMappingId int       `orm:"column(predict_edb_conf_calculate_mapping_id);pk"`
	EdbInfoId                        int       `description:"指标id"`
	ConfigId                         int       `description:"配置id"`
	FromEdbInfoId                    int       `description:"基础指标id"`
	FromEdbCode                      string    `description:"基础指标编码"`
	FromEdbName                      string    `description:"基础指标名称"`
	FromSource                       int       `description:"基础指标来源"`
	FromSourceName                   string    `description:"基础指标来源名称"`
	FromTag                          string    `description:"来源指标标签"`
	Sort                             int       `description:"计算指标名称排序"`
	CreateTime                       time.Time `description:"创建时间"`
	ModifyTime                       time.Time `description:"修改时间"`
	StartDate                        string    `description:"开始日期"`
	EndDate                          string    `description:"结束日期"`
	EdbType                          int       `description:"指标类型:1:基础指标,2:计算指标"`
	EdbCode                          string    `description:"指标code"`
}

// GetPredictEdbConfCalculateMappingDetailListById 根据配置id获取 配置关联指标信息
func GetPredictEdbConfCalculateMappingDetailListById(edbInfoId int) (list []*PredictEdbConfCalculateMappingDetail, err error) {
	o := orm.NewOrmUsingDB("data")
	sql := ` SELECT a.predict_edb_conf_calculate_mapping_id,a.edb_info_id,a.from_edb_info_id,a.from_edb_code,a.from_source,a.from_source_name,a.sort,a.create_time,a.modify_time,a.from_tag,b.edb_name_source as from_edb_name,b.start_date,b.end_date,b.edb_type FROM predict_edb_conf_calculate_mapping AS a
			INNER JOIN edb_info AS b ON a.from_edb_info_id=b.edb_info_id
			WHERE a.edb_info_id=? ORDER BY sort ASC `

	_, err = o.Raw(sql, edbInfoId).QueryRows(&list)
	return
}

// GetPredictEdbConfCalculateMappingDetailListByConfigId 根据配置id和指标id获取 配置关联指标信息
func GetPredictEdbConfCalculateMappingDetailListByConfigId(edbInfoId, configId int) (list []*PredictEdbConfCalculateMappingDetail, err error) {
	o := orm.NewOrmUsingDB("data")
	sql := ` SELECT a.predict_edb_conf_calculate_mapping_id,a.edb_info_id,a.from_edb_info_id,a.from_edb_code,a.from_source,a.from_source_name,a.sort,a.create_time,a.modify_time,a.from_tag,b.edb_name_source as from_edb_name,b.start_date,b.end_date,b.edb_type FROM predict_edb_conf_calculate_mapping AS a
			INNER JOIN edb_info AS b ON a.from_edb_info_id=b.edb_info_id
			WHERE a.edb_info_id=? AND a.config_id=?  ORDER BY sort ASC `

	_, err = o.Raw(sql, edbInfoId, configId).QueryRows(&list)
	return
}

// GetPredictEdbConfCalculateMappingDetailListByEdbInfoId 根据 关联指标id列表 来 获取 相关联的配置关联指标信息
func GetPredictEdbConfCalculateMappingDetailListByEdbInfoId(fromEdbInfoIdList []int) (list []*PredictEdbConfCalculateMappingDetail, err error) {
	num := len(fromEdbInfoIdList)
	if num <= 0 {
		return
	}
	o := orm.NewOrmUsingDB("data")
	sql := ` SELECT a.predict_edb_conf_calculate_mapping_id,a.edb_info_id,a.from_edb_info_id,a.from_edb_code,a.from_source,a.from_source_name,a.sort,a.create_time,a.modify_time,a.from_tag,b.edb_name_source as from_edb_name,b.start_date,b.end_date,b.edb_type,b.edb_code FROM predict_edb_conf_calculate_mapping AS a
			INNER JOIN edb_info AS b ON a.from_edb_info_id=b.edb_info_id
			WHERE a.edb_info_id in (` + utils.GetOrmInReplace(num) + `) GROUP BY a.edb_info_id ORDER BY sort ASC `

	_, err = o.Raw(sql, fromEdbInfoIdList).QueryRows(&list)
	return
}
