package excel

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

// ExcelEdbMapping excel与指标的关系表
type ExcelEdbMapping struct {
	ExcelEdbMappingId int       `gorm:"primaryKey;autoIncrement;column:excel_edb_mapping_id"`
	ExcelInfoId       int       `gorm:"column:excel_info_id" description:"excel的id"`
	Source            int       `gorm:"column:source" description:"表格来源,1:excel插件的表格,2:自定义表格,3:混合表格,4:自定义分析,默认:1"`
	EdbInfoId         int       `gorm:"column:edb_info_id" description:"计算指标id"`
	CreateTime        time.Time `gorm:"column:create_time" description:"创建时间"`
	ModifyTime        time.Time `gorm:"column:modify_time" description:"修改时间"`
}

// AddExcelEdbMappingMulti 批量添加excel与指标的关系
func AddExcelEdbMappingMulti(items []*ExcelEdbMapping) (err error) {
	err = global.DEFAULT_DB.CreateInBatches(items, utils.MultiAddNum).Error

	return
}

// Add 添加excel与指标的关系
func (e *ExcelEdbMapping) Add() (err error) {
	err = global.DEFAULT_DB.Create(e).Error

	return
}

type ExcelEdbMappingItem struct {
	EdbInfoId        int    `description:"指标id"`
	UniqueCode       string `description:"唯一编码"`
	EdbName          string `description:"指标名称"`
	ClassifyId       int    `description:"分类id"`
	Frequency        string `description:"频度"`
	Unit             string `description:"单位"`
	CalculateFormula string `json:"-"`
	DateSequenceStr  string `description:"日期序列公式"`
	DataSequenceStr  string `description:"数据序列公式"`
}

// CalculateFormula 计算公式
type CalculateFormula struct {
	DateSequenceStr string `json:"DateSequenceStr"`
	DataSequenceStr string `json:"DataSequenceStr"`
}

// GetExcelEdbMappingByEdbInfoId 根据指标id获取配置关系
func GetExcelEdbMappingByEdbInfoId(edbInfoId int) (item *ExcelEdbMapping, err error) {
	sql := ` SELECT *  FROM excel_edb_mapping WHERE 1=1 AND edb_info_id = ? `
	err = global.DEFAULT_DB.Raw(sql, edbInfoId).First(&item).Error

	return
}