package logic

import (
	"errors"
	"hongze/hongze_edb_lib/models"
	"hongze/hongze_edb_lib/utils"
	"strconv"
	"time"
)

// RefreshSci 卓创（红桃3）刷新
func RefreshSci(edbInfo *models.EdbInfo, startDate string) (isHandling bool, errMsg string, err error) {
	cacheKey := utils.CACHE_EDB_DATA_REFRESH + strconv.Itoa(edbInfo.Source) + "_" + edbInfo.EdbCode
	if utils.Rc.IsExist(cacheKey) {
		isHandling = true
		return
	}
	utils.Rc.SetNX(cacheKey, 1, 1*time.Minute)
	defer func() {
		utils.Rc.Delete(cacheKey)
	}()

	err = models.RefreshEdbDataFromSci(edbInfo.EdbInfoId, edbInfo.EdbCode, startDate)
	if err != nil && err.Error() != utils.ErrNoRow() {
		errMsg = "刷新指标信息失败！"
		err = errors.New("刷新指标信息失败 RefreshEdbDataFromSci，Err:" + err.Error())
		return
	}
	// 更新指标最大最小值
	err, errMsg = models.UnifiedModifyEdbInfoMaxAndMinInfo(edbInfo)
	if err != nil {
		return
	}

	// 更新ES
	go UpdateEs(edbInfo.EdbInfoId)

	return
}
