package data_stat

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

// EdbSourceStat 数据源统计表
type EdbSourceStat struct {
	Id                uint64    `orm:"column(id);pk"`
	SourceName        string    `description:"来源名称"`
	Source            int       `description:"来源id"`
	TerminalCode      string    `description:"终端编码，用于配置在机器上"`
	EdbNum            int32     `description:"指标总数"`
	EdbNewNum         int32     `description:"今日新增指标数"`
	EdbDelNum         int32     `description:"今日删除指标数"`
	NeedRefreshNum    int       `description:"今日需刷新指标"`
	HasRefreshNum     int       `description:"今日发起刷新任务指标数"`
	UpdateSuccessNum  int       `description:"今日已更新成功指标数"`
	UpdateFailedNum   int       `description:"今日已更新失败指标数"`
	RefreshSuccessNum int       `description:"今日已刷新成功指标数"`
	RefreshFailedNum  int       `description:"今日已刷新失败指标数"`
	CreateTime        time.Time `description:"创建时间"`
	ModifyTime        time.Time `description:"修改时间"`
}

type EdbInfoSourceStatItem struct {
	Id                uint64 `orm:"column(id);pk"`
	SourceName        string `description:"来源名称"`
	Source            int    `description:"来源id"`
	TerminalCode      string `description:"终端编码，用于配置在机器上"`
	EdbNum            int32  `description:"指标总数"`
	EdbNewNum         int32  `description:"今日新增指标数"`
	EdbDelNum         int32  `description:"今日删除指标数"`
	NeedRefreshNum    int    `description:"今日需刷新指标"`
	HasRefreshNum     int    `description:"今日发起刷新任务指标数"`
	UpdateSuccessNum  int    `description:"今日已更新成功指标数"`
	UpdateFailedNum   int    `description:"今日已更新失败指标数"`
	RefreshSuccessNum int    `description:"今日已刷新成功指标数"`
	RefreshFailedNum  int    `description:"今日已刷新失败指标数"`
}

type GetEdbSourceStatResp struct {
	List   []*EdbInfoSourceStatItem
	Paging *paging.PagingItem
}

func GetEdbSourceStatByCondition(condition string, pars []interface{}, orderStr string, pageSize, startSize int) (item []*EdbSourceStat, err error) {
	o := orm.NewOrmUsingDB("data")
	sql := ` SELECT * FROM edb_source_stat WHERE 1=1 `
	if condition != "" {
		sql += condition
	}
	if orderStr != "" {
		sql += orderStr
	} else {
		sql += ` ORDER BY create_time DESC, id DESC `
	}

	sql += ` LIMIT ?,? `
	_, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&item)
	return
}

// GetEdbSourceStatCount
func GetEdbSourceStatCount(condition string, pars []interface{}) (count int, err error) {
	o := orm.NewOrmUsingDB("data")
	sql := ` SELECT count(*) FROM edb_source_stat WHERE 1=1 `
	if condition != "" {
		sql += condition
	}
	err = o.Raw(sql, pars).QueryRow(&count)
	return
}
