package edb_refresh

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

// EdbRefreshMapping
// @Description: 指标刷新时间配置关系表
type EdbRefreshMapping struct {
	EdbRefreshMappingId int       `orm:"column(edb_refresh_mapping_id);pk"`
	Source              int       `description:"来源"`
	SubSource           int       `description:"来源名称"`
	EdbInfoId           int       `description:"指标id，如果是数据源（钢联、有色）的，那么就是数据源里面的id"`
	EdbRefreshConfigId  int       `description:"刷新配置id"`
	SysUserId           int       `description:"操作人id"`
	SysUserRealName     string    `description:"操作人真实姓名"`
	ModifyTime          time.Time `description:"最晚一次的更新时间"`
	CreateTime          time.Time `description:"添加时间"`
}

// Add
// @Description: 添加
// @author: Roc
// @receiver m
// @datetime 2023-12-14 16:11:10
// @param cols []string
// @return err error
func (m *EdbRefreshMapping) Add() (err error) {
	if m.EdbRefreshMappingId > 0 {
		err = errors.New("该配置已存在")
		return
	}
	o := orm.NewOrm()
	lastId, err := o.Insert(m)
	if err != nil {
		return
	}
	m.EdbRefreshMappingId = int(lastId)

	return
}

// Update
// @Description: 更新
// @author: Roc
// @receiver m
// @datetime 2023-12-14 16:11:10
// @param cols []string
// @return err error
func (m *EdbRefreshMapping) Update(cols []string) (err error) {
	o := orm.NewOrm()
	_, err = o.Update(m, cols...)
	return
}

// Delete
// @Description: 删除
// @author: Roc
// @receiver m
// @datetime 2023-12-14 16:11:10
// @return err error
func (m *EdbRefreshMapping) Delete() (err error) {
	o := orm.NewOrm()
	_, err = o.Delete(m)
	return
}

type EdbInfoListAndRefreshConfig struct {
	EdbInfoId          int       `orm:"column(edb_info_id);pk"`
	SourceName         string    `description:"来源名称"`
	Source             int       `description:"来源id"`
	SubSource          int       `description:"子数据来源:0:经济数据库,1:日期序列"`
	SubSourceName      string    `description:"子数据来源名称"`
	EdbCode            string    `description:"指标编码"`
	EdbName            string    `description:"指标名称"`
	Frequency          string    `description:"频率"`
	Unit               string    `description:"单位"`
	StartDate          time.Time `description:"起始日期"`
	EndDate            time.Time `description:"终止日期"`
	ClassifyId         int       `description:"分类id"`
	UniqueCode         string    `description:"指标唯一编码"`
	CalculateFormula   string    `description:"计算公式"`
	ModifyTime         string    `description:"更新时间"`
	NoUpdate           int8      `description:"是否停止更新，0：继续更新；1：停止更新"`
	EdbRefreshConfigId int       `description:"刷新配置id"`
	DataRefreshNum     int       `description:"刷新的期数"`
}

// GetConfigRefreshEdbInfoListBySourceAndSubSource
// @Description:  根据来源和配置id列表获取指标列表
// @author: Roc
// @datetime 2024-01-09 17:28:06
// @param sourceList []int
// @param configIdList []int
// @return list []*data_manage.EdbInfoList
// @return err error
func GetConfigRefreshEdbInfoListBySourceAndSubSource(sourceList, configIdList []int) (list []*EdbInfoListAndRefreshConfig, err error) {
	num := len(configIdList)
	if num <= 0 {
		return
	}

	var pars []interface{}

	o := orm.NewOrm()
	sql := `SELECT a.*,b.edb_refresh_config_id FROM edb_info a 
 JOIN edb_refresh_mapping b ON a.edb_info_id = b.edb_info_id
WHERE b.edb_refresh_config_id IN (` + utils.GetOrmInReplace(num) + `) `
	pars = append(pars, configIdList)

	sourceNum := len(sourceList)
	if sourceNum > 0 {
		sql += ` AND b.source not in (` + utils.GetOrmInReplace(sourceNum) + `) `
		pars = append(pars, sourceList)
	}
	_, err = o.Raw(sql, pars).QueryRows(&list)

	return
}
