package models

import (
	"fmt"
	"github.com/beego/beego/v2/client/orm"
	"time"
)

type CalculateResidualAnalysisConfigMapping struct {
	CalculateResidualAnalysisConfigMappingId int       `orm:"column(calculate_residual_analysis_config_mapping_id);pk;auto" description:"自增id"`
	CalculateResidualAnalysisConfigId        int       `orm:"column(calculate_residual_analysis_config_id)" description:"残差分析配置id"`
	EdbInfoId                                int64     `orm:"column(edb_info_id)" description:"指标id"`
	ResidualType                             int       `orm:"column(residual_type)" description:"残差类型: 1-映射残差 2-拟合残差"`
	IndexType                                int       `orm:"column(index_type)" description:"指标类型:1-映射指标 2-残差指标 3-因变量指标 4-自变量指标"`
	CreateTime                               time.Time `orm:"column(create_time)" description:"创建时间"`
	ModifyTime                               time.Time `orm:"column(modify_time)" description:"修改时间"`
}

func init() {
	orm.RegisterModel(new(CalculateResidualAnalysisConfigMapping))
}

// GetConfigMappingListByConditionNotBase 查询非基础指标的配置映射
func GetConfigMappingListByConditionNotBase(edbInfoId int) (configMapping []CalculateResidualAnalysisConfigMapping, err error) {
	o := orm.NewOrm()
	sql := `
	SELECT
  		* 
	FROM
  		calculate_residual_analysis_config_mapping 
	WHERE
  		calculate_residual_analysis_config_id IN ( SELECT calculate_residual_analysis_config_id FROM calculate_residual_analysis_config_mapping WHERE edb_info_id = ? ) 
  		AND index_type != 3 
  		AND index_type != 4`
	_, err = o.Raw(sql, edbInfoId).QueryRows(&configMapping)
	if err != nil {
		return nil, fmt.Errorf("查询数据时出错: %v", err)
	}
	return configMapping, nil
}