package data_stat

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

// EdbInfoUpdateLog 指标更新/刷新日志列表
type EdbInfoUpdateLog struct {
	Id                     uint64    `orm:"column(id);pk"`
	EdbInfoId              int       `description:"指标ID"`
	SourceName             string    `description:"来源名称"`
	Source                 int       `description:"来源id"`
	EdbCode                string    `description:"指标编码"`
	EdbName                string    `description:"指标名称"`
	EdbNameSource          string    `description:"指标名称来源"`
	Frequency              string    `description:"频率"`
	Unit                   string    `description:"单位"`
	StartDate              string    `description:"起始日期"`
	EndDate                string    `description:"终止日期"`
	SysUserId              int       `description:"创建人ID"`
	SysUserRealName        string    `description:"创建人姓名"`
	UniqueCode             string    `description:"指标唯一编码"`
	EdbCreateTime          time.Time `description:"指标创建时间"`
	EdbModifyTime          time.Time `description:"指标修改时间"`
	CreateTime             time.Time `description:"创建时间"`
	UpdateSysUserId        int       `description:"变更人ID"`
	UpdateSysUserRealName  string    `description:"变更人姓名"`
	LatestDate             string    `description:"数据最新日期"`
	LatestValue            float64   `description:"数据最新值"`
	TerminalCode           string    `description:"终端编码，用于配置在机器上"`
	ErDataUpdateDate       string    `description:"本次更新，数据发生变化的最早日期"`
	UpdateResult           int       `description:"本次刷新操作结果：1成功，2失败"`
	UpdateFailedReason     string    `description:"刷新失败原因"`
	DataUpdateResult       int       `description:"数据更新结果：1成功，2失败"`
	DataUpdateFailedReason string    `description:"数据未正常更新原因"`
	DataUpdateTime         string    `description:"数据更新时间"`
	IsSourceRefresh        int       `description:"是否为终端刷新到数据源的刷新操作：0否，1是"`
	UpdateType             int       `description:"变更类型，0:数据明细变更，1:基础信息变更, 2:新增指标"`
}

type EdbInfoUpdateLogItem struct {
	Id                    uint64 `orm:"column(id);pk"`
	EdbInfoId             int    `description:"指标ID"`
	SourceName            string `description:"来源名称"`
	Source                int    `description:"来源id"`
	EdbCode               string `description:"指标编码"`
	EdbName               string `description:"指标名称"`
	EdbNameSource         string `description:"指标名称来源"`
	Frequency             string `description:"频率"`
	Unit                  string `description:"单位"`
	StartDate             string `description:"起始日期"`
	EndDate               string `description:"终止日期"`
	SysUserId             int    `description:"创建人ID"`
	SysUserRealName       string `description:"创建人姓名"`
	UpdateSysUserId       int    `description:"变更人ID"`
	UpdateSysUserRealName string `description:"变更人姓名"`
	CreateTime            string
	UniqueCode            string  `description:"指标唯一编码"`
	LatestDate            string  `description:"数据最新日期"`
	LatestValue           float64 `description:"数据最新值"`
	TerminalCode          string  `description:"终端编码，用于配置在机器上"`
	DataUpdateTime        string  `description:"最近一次数据发生变化的时间"`
	ErDataUpdateDate      string  `description:"本次更新，数据发生变化的最早日期"`
	UpdateType            int     `description:"变更类型，0:数据明细变更，1:基础信息变更, 2:新增指标"`
}

func AddEdbUpdateLog(item *EdbInfoUpdateLog) (lastId int64, err error) {
	o := orm.NewOrmUsingDB("data")
	lastId, err = o.Insert(item)
	return
}

type GetEdbUpdateLogResp struct {
	List   []*EdbInfoUpdateLogItem
	Paging *paging.PagingItem
}

func GetEdbUpdateLogByCondition(condition string, pars []interface{}, orderStr string, pageSize, startSize int) (item []*EdbInfoUpdateLog, err error) {
	o := orm.NewOrmUsingDB("data")
	sql := ` SELECT * FROM edb_info_update_log 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
}

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