package edb_refresh

import (
	"errors"
	"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.NewOrmUsingDB("data")
	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.NewOrmUsingDB("data")
	_, 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.NewOrmUsingDB("data")
	_, 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.NewOrmUsingDB("data")
	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
}

// GetAllListBySourceList
// @Description: 根据来源列表获取所有默认刷新配置列表
// @author: Roc
// @datetime 2024-01-10 14:54:54
// @param source int
// @param subSource int
// @return list []*EdbRefreshDefaultConfig
// @return err error
func GetAllListBySourceList(source, subSource int) (list []*EdbRefreshDefaultConfig, err error) {
	o := orm.NewOrmUsingDB("data")
	sql := `SELECT * FROM edb_refresh_default_config
         WHERE source = ? AND sub_source = ? ORDER BY id ASC `
	_, err = o.Raw(sql, source, subSource).QueryRows(&list)

	return
}

// SaveEdbRefreshDefaultConfig
// @Description: 保存配置项
// @author: Roc
// @datetime 2024-01-08 10:20:00
// @param source int
// @param subSource int
// @param frequency string
// @param newList []*EdbRefreshDefaultConfig
// @return err error
func SaveEdbRefreshDefaultConfig(source, subSource int, frequency string, newList []*EdbRefreshDefaultConfig) (err error) {
	o, err := orm.NewOrmUsingDB("data").Begin()
	if err != nil {
		return
	}
	defer func() {
		if err != nil {
			_ = o.Rollback()
			return
		}
		_ = o.Commit()
	}()

	// 先删除
	sql := `DELETE FROM edb_refresh_default_config WHERE source =? AND sub_source =? AND frequency =?`
	_, err = o.Raw(sql, source, subSource, frequency).Exec()

	// 再写入
	_, err = o.InsertMulti(len(newList), newList)

	return

}
