package cache

import (
	"eta/eta_api/models/company"
	"eta/eta_api/utils"
	"fmt"
	"strconv"
	"time"
)

func GetIntervalFromCrmConfig(key string) int {
	//redis 查询key
	sIntervals := utils.Rc.Get(key)
	var interval int
	var err error
	if sIntervals == nil {
		//redis没查到，去数据库查
		interval, err = company.GetConfigValueByCode(key)
		if err != nil {
			fmt.Println("GetConfigValueByCode Err:" + err.Error())
		}
		//插入redis
		if !utils.Rc.SetNX(key, interval, 2*time.Hour) {
			fmt.Println("setNX Err")
		}
		return interval
	} else {
		intervals := fmt.Sprintf("%s", sIntervals)
		interval, err = strconv.Atoi(intervals)
		if err != nil {
			fmt.Println("strconv err:", err.Error())
		}
		return interval
	}
}
