package data_stat

import (
	"eta/eta_api/models/data_manage"
	"eta/eta_api/models/data_stat"
	"eta/eta_api/models/system"
	"fmt"
	"time"
)

// AddEdbInfoUpdateLog 添加指标编辑/刷新日志
func AddEdbInfoUpdateLog(edbInfoId int, updateResult int, updateFailedReason string, sysUser *system.Admin, updateType int) (err error) {
	var edbInfo *data_manage.EdbInfo
	if edbInfoId > 0 {
		// 获取指标详情
		edbInfo, err = data_manage.GetEdbInfoById(edbInfoId)
		if err != nil {
			err = fmt.Errorf("指标不存在")
			return
		}
		nowTime := time.Now()
		log := new(data_stat.EdbInfoUpdateLog)
		log.EdbInfoId = edbInfo.EdbInfoId
		log.SourceName = edbInfo.SourceName
		log.Source = edbInfo.Source
		log.EdbCode = edbInfo.EdbCode
		log.EdbName = edbInfo.EdbName
		log.EdbNameSource = edbInfo.SourceIndexName
		log.Frequency = edbInfo.Frequency
		log.Unit = edbInfo.Unit
		log.StartDate = edbInfo.StartDate
		log.EndDate = edbInfo.EndDate
		log.SysUserId = edbInfo.SysUserId
		log.SysUserRealName = edbInfo.SysUserRealName
		log.UpdateSysUserId = sysUser.AdminId
		log.UpdateSysUserRealName = sysUser.RealName
		log.UniqueCode = edbInfo.UniqueCode
		log.EdbCreateTime = edbInfo.CreateTime
		log.EdbModifyTime = edbInfo.ModifyTime
		log.CreateTime = nowTime
		log.LatestDate = edbInfo.LatestDate
		log.LatestValue = edbInfo.LatestValue
		log.TerminalCode = edbInfo.TerminalCode
		log.UpdateResult = updateResult
		log.UpdateFailedReason = updateFailedReason
		log.DataUpdateTime = edbInfo.DataUpdateTime
		log.ErDataUpdateDate = edbInfo.ErDataUpdateDate
		log.UpdateType = updateType
		_, err = data_stat.AddEdbUpdateLog(log)
		if err != nil {
			err = fmt.Errorf("新增指标更新日志失败，Err: %s", err)
			return
		}
	}
	return
}
