package excel

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

// ExcelEdbMapping excel与指标的关系表
type ExcelEdbMapping struct {
	ExcelEdbMappingId int       `orm:"column(excel_edb_mapping_id);pk"`
	ExcelInfoId       int       `description:"excel的id"`
	Source            int       `description:"表格来源，1：excel插件的表格，2：自定义表格，3：混合表格，4：自定义分析，默认：1"`
	EdbInfoId         int       `description:"计算指标id"`
	CreateTime        time.Time `description:"创建时间"`
	ModifyTime        time.Time `description:"修改时间"`
}

// AddExcelEdbMappingMulti 批量添加excel与指标的关系
func AddExcelEdbMappingMulti(items []*ExcelEdbMapping) (err error) {
	o := orm.NewOrm()
	_, err = o.InsertMulti(len(items), items)
	return
}

// Add 添加excel与指标的关系
func (e *ExcelEdbMapping) Add() (err error) {
	o := orm.NewOrm()
	_, err = o.Insert(e)
	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) {
	o := orm.NewOrm()
	sql := ` SELECT *  FROM excel_edb_mapping WHERE 1=1 AND edb_info_id = ? `

	err = o.Raw(sql, edbInfoId).QueryRow(&item)
	return
}
