k3cloud_fetch_data.go 1.2 KB
package redis

import (
	"fmt"
	"github.com/go-redis/redis"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/constant"
	"time"
)

// 获取每日设备运行数据
func GetLastFetchTime(args ...string) (time.Time, error) {
	client := GetRedis()
	key := K3CloudFetchTimeKey()
	result := client.Get(key)
	t, err := result.Int()
	if len(args) > 0 {
		key = args[0]
	}
	if err == redis.Nil {
		if _, err := client.Set(key, time.Now().Unix(), 0).Result(); err != nil {
			return time.Time{}, err
		}
	}
	fetchTime := time.Unix(int64(t), 0)
	if fetchTime.IsZero() {
		return time.Time{}, fmt.Errorf("zerotime retry")
	}
	if _, err := client.Set(key, time.Now().Unix(), 0).Result(); err != nil {
		return time.Time{}, err
	}
	return fetchTime, nil
}

func K3CloudFetchTimeKey() string {
	str := fmt.Sprintf("%v:k3clound-data:last-fetch:%v-%v", constant.CACHE_PREFIX, constant.MANUFACTURE_DEFAULT_COMPANYID, constant.MANUFACTURE_DEFAULT_ORGID)
	return str
}

func MaterialK3cloudFetchTimeKey() string {
	str := fmt.Sprintf("%v:material-k3clound-data:last-fetch:%v-%v", constant.CACHE_PREFIX, constant.MANUFACTURE_DEFAULT_COMPANYID, constant.MANUFACTURE_DEFAULT_ORGID)
	return str
}