package models

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

// EdbInfoCalculateMapping 计算指标于基础指标，关系表
type EdbInfoCalculateMapping struct {
	EdbInfoCalculateMappingId int       `orm:"column(edb_info_calculate_mapping_id);pk"`
	EdbInfoId                 int       `description:"计算指标id"`
	Source                    int       `description:"计算指标来源"`
	SourceName                string    `description:"计算指标来源名称"`
	EdbCode                   string    `description:"计算指标编码"`
	FromEdbInfoId             int       `description:"基础指标id"`
	FromEdbCode               string    `description:"基础指标编码"`
	FromEdbName               string    `description:"基础指标名称"`
	FromSource                int       `description:"基础指标来源"`
	FromSourceName            string    `description:"基础指标来源名称"`
	MoveValue                 int       `description:"移动的值，小于0是提前，0是不变，大于0是滞后"`
	Sort                      int       `description:"计算指标名称排序"`
	CreateTime                time.Time `description:"创建时间"`
	ModifyTime                time.Time `description:"修改时间"`
	FromTag                   string    `description:"来源指标标签"`
	FromSubSource             int       `description:"渠道子数据库来源"`
}

// AddEdbInfoCalculateMappingMulti 批量添加指标关系表
func AddEdbInfoCalculateMappingMulti(items []*EdbInfoCalculateMapping) (err error) {
	o := orm.NewOrm()
	_, err = o.InsertMulti(len(items), items)
	return
}

// AddEdbInfoCalculateMappingMulti 批量添加指标关系表
func (e *EdbInfoCalculateMapping) Add() (err error) {
	o := orm.NewOrm()
	_, err = o.Insert(e)
	return
}

// EdbInfoCalculateMappingView
type EdbInfoCalculateMappingView struct {
	EdbInfoCalculateMappingId int       `orm:"column(edb_info_calculate_mapping_id);pk"`
	EdbInfoId                 int       `description:"计算指标id"`
	Source                    int       `description:"计算指标来源"`
	SourceName                string    `description:"计算指标来源名称"`
	EdbCode                   string    `description:"计算指标编码"`
	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:"结束日期"`
	CalculateFormula          string    `description:"N值"`
	MoveType                  int       `description:"移动方式:1:领先(默认),2:滞后"`
	MoveFrequency             string    `description:"移动频度"`
}

// GetEdbInfoCalculateMappingDetail 获取单条关联指标
func GetEdbInfoCalculateMappingDetail(edbInfoId int) (item *EdbInfoCalculateMappingView, err error) {
	o := orm.NewOrm()
	sql := ` SELECT a.*,b.start_date,b.end_date,b.calculate_formula,b.move_type,b.move_frequency FROM edb_info_calculate_mapping AS a
			INNER JOIN edb_info AS b ON a.edb_info_id=b.edb_info_id
			WHERE a.edb_info_id=? `
	err = o.Raw(sql, edbInfoId).QueryRow(&item)
	return
}

// GetEdbInfoCalculateMappingDetailByFromTag 获取单条关联指标
func GetEdbInfoCalculateMappingDetailByFromTag(edbInfoId int, fromTag string) (item *EdbInfoCalculateMappingView, err error) {
	o := orm.NewOrm()
	sql := ` SELECT a.*,b.start_date,b.end_date,b.calculate_formula,b.move_type,b.move_frequency FROM edb_info_calculate_mapping AS a
			INNER JOIN edb_info AS b ON a.edb_info_id=b.edb_info_id
			WHERE a.edb_info_id=? and a.from_tag=? `
	err = o.Raw(sql, edbInfoId, fromTag).QueryRow(&item)
	return
}

// EdbInfoCalculateDetail
type EdbInfoCalculateDetail struct {
	EdbInfoCalculateId int       `orm:"column(edb_info_calculate_id);pk"`
	EdbInfoId          int       `description:"指标id"`
	EdbCode            string    `description:"指标编码"`
	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:计算指标"`
}

// GetEdbInfoCalculateDetailList 获取关联指标列表
func GetEdbInfoCalculateDetailList(edbInfoId int) (list []*EdbInfoCalculateDetail, err error) {
	o := orm.NewOrm()
	sql := ` SELECT a.*,b.start_date,b.end_date,b.edb_type FROM edb_info_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
}

// GetEdbInfoByEdbCode 根据指标code获取指标信息
func GetEdbInfoCalculateByEdbCode(edbCode, fromEdbCode string) (item *EdbInfoCalculateMapping, err error) {
	o := orm.NewOrm()
	sql := ` SELECT * FROM edb_info_calculate_mapping WHERE edb_code=? AND from_edb_code=? `
	err = o.Raw(sql, edbCode, fromEdbCode).QueryRow(&item)
	return
}
