package data_stat

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

// EdbInfoUpdateStat 指标更新/刷新 汇总表（数据源明细表）
type EdbInfoUpdateStat 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:"记录创建时间"`
	LatestDate             string    `description:"数据最新日期"`
	LatestValue            float64   `description:"数据最新值"`
	TerminalCode           string    `description:"终端编码，用于配置在机器上"`
	ErDataUpdateDate       string    `description:"本次更新，数据发生变化的最早日期"`
	DataUpdateResult       int       `description:"数据更新结果：1成功，2失败"`
	DataUpdateFailedReason string    `description:"数据未正常更新原因"`
	DataUpdateTime         string    `description:"数据更新时间"`
	UpdateResult           int       `description:"本次刷新操作结果：1成功，2失败"`
	UpdateFailedReason     string    `description:"刷新失败原因"`
	UpdateTime             string    `description:"刷新时间"`
	ModifyTime             time.Time `description:"修改时间"`
	IsAdd                  int       `description:"是否是当日新增的指标：1是，2否"`
	NeedRefresh            int       `description:"今日是否需要定时刷新，1需要，0不需要"`
	HasRefresh             int       `description:"今日是否发起刷新服务，1是，0否"`
}

// Add 新增
func (r *EdbInfoUpdateStat) Add(list []*EdbInfoUpdateStat) (err error) {
	o := orm.NewOrm()
	_, err = o.InsertMulti(len(list), list)
	return
}

// UpdateEdbUpdateStatMulti 批量更新
func UpdateEdbUpdateStatMulti(list []*EdbInfoUpdateStat) (err error) {
	o := orm.NewOrm()
	sql := `UPDATE edb_info_update_stat 
SET 
edb_name = ?,
edb_name_source = ?,
frequency = ?,
unit = ?,
start_date = ?,
end_date = ?,
edb_modify_time = ?,
latest_date = ?,
latest_value = ?,
terminal_code = ?,
modify_time = ?,
er_data_update_date = ?,
is_add = ?,
need_refresh = ?,
has_refresh = ?,
data_update_result = ?,
data_update_failed_reason = ?,
data_update_time = ?, 
update_result = ?,
update_failed_reason = ?,
update_time = ?
WHERE
	id = ?`
	p, err := o.Raw(sql).Prepare()
	if err != nil {
		return
	}
	defer func() {
		_ = p.Close() // 别忘记关闭 statement
	}()
	for _, v := range list {
		_, err = p.Exec(v.EdbName, v.EdbNameSource, v.Frequency, v.Unit, v.StartDate, v.EndDate, v.EdbModifyTime, v.LatestDate, v.LatestValue, v.TerminalCode, v.ModifyTime, v.ErDataUpdateDate, v.IsAdd, v.NeedRefresh, v.HasRefresh, v.DataUpdateResult, v.DataUpdateFailedReason, v.DataUpdateTime, v.UpdateResult, v.UpdateFailedReason, v.UpdateTime, v.Id)
		if err != nil {
			return
		}
	}
	return
}

// GetEdbUpdateStatByCondition 获取指标更新汇总记录
func GetEdbUpdateStatByCondition(condition string, pars []interface{}) (item []*EdbInfoUpdateStat, err error) {
	o := orm.NewOrm()
	sql := ` SELECT * FROM edb_info_update_stat WHERE 1=1 `
	if condition != "" {
		sql += condition
	}
	_, err = o.Raw(sql, pars).QueryRows(&item)
	return
}
