package edb_refresh

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

// EdbRefreshDefaultConfig
// @Description: 指标的默认刷新时间配置表
type EdbRefreshDefaultConfig struct {
	Id                  int       `orm:"column(id);pk"`
	Source              int       `description:"来源"`
	SubSource           int       `description:"来源名称"`
	Frequency           string    `description:"频度"`
	RefreshFrequency    string    `description:"刷新频率"`
	RefreshFrequencyDay int       `description:"具体刷新的日期"`
	RefreshTime         string    `description:"刷新时间"`
	RefreshAllData      int       `description:"是否刷新所有数据，0：否，1：刷新所有数据"`
	RefreshDataNum      int       `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 *EdbRefreshDefaultConfig) Add() (err error) {
	if m.Id > 0 {
		err = errors.New("该配置已存在")
		return
	}
	o := orm.NewOrm()
	lastId, err := o.Insert(m)
	if err != nil {
		return
	}
	m.Id = int(lastId)

	return
}

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

// GetListBySourceAndFrequency
// @Description: 根据来源和频度获取列表
// @author: Roc
// @datetime 2024-01-04 17:39:47
// @param source int
// @param subSource int
// @param frequency string
// @return list []*EdbRefreshDefaultConfig
// @return err error
func GetListBySourceAndFrequency(source, subSource int, frequency string) (list []*EdbRefreshDefaultConfig, err error) {
	o := orm.NewOrm()
	sql := `SELECT * FROM edb_refresh_default_config
         WHERE source = ? AND sub_source = ? AND frequency = ? ORDER BY id ASC `
	_, err = o.Raw(sql, source, subSource, frequency).QueryRows(&list)

	return
}

// GetListByCondition
// @Description: 根据条条件获取默认配置列表
// @author: Roc
// @datetime 2024-01-09 13:28:49
// @param condition string
// @param pars []interface{}
// @return list []*EdbRefreshDefaultConfig
// @return err error
func GetListByCondition(condition string, pars []interface{}) (list []*EdbRefreshDefaultConfig, err error) {
	o := orm.NewOrm()
	sql := `SELECT * FROM edb_refresh_default_config
         WHERE 1 = 1 `

	if condition != "" {
		sql += condition
	}
	sql += ` ORDER BY id ASC `
	_, err = o.Raw(sql, pars).QueryRows(&list)

	return
}

// GetDefaultRefreshEdbInfoListBySourceAndSubSource
// @Description: 根据来源获取默认刷新的指标列表
// @author: Roc
// @datetime 2024-01-09 16:22:49
// @param source int
// @param subSource int
// @return list []*data_manage.EdbInfo
// @return err error
func GetDefaultRefreshEdbInfoListBySourceAndSubSource(source, subSource int, frequencyList []string) (list []*EdbInfoListAndRefreshConfig, err error) {
	num := len(frequencyList)
	if num <= 0 {
		return
	}

	o := orm.NewOrm()
	sql := `SELECT a.* FROM edb_info a 
LEFT JOIN edb_refresh_mapping b ON a.edb_info_id = b.edb_info_id
WHERE a.source = ? AND a.sub_source = ? AND a.frequency IN (` + utils.GetOrmInReplace(num) + `)  AND b.edb_info_id is null`
	_, err = o.Raw(sql, source, subSource, frequencyList).QueryRows(&list)

	return
}
