package edb_source

import (
	"context"
	jsoniter "github.com/json-iterator/go"
	"hongze/hongze_yb/global"
	"hongze/hongze_yb/utils"
	"strconv"
	"time"
)

func GetEdbSourceBySource(source int) (item *EdbSource, err error) {
	cacheKey := utils.HONGZEYB_EBD_SOURCE + strconv.Itoa(source)
	sourceInfo, _ := global.Redis.Get(context.TODO(), cacheKey).Result()
	if sourceInfo != "" {
		err = jsoniter.UnmarshalFromString(sourceInfo, &item)
		if err != nil {
			return
		}
		return
	} else {
		err = global.MYSQL["data"].Where("edb_source_id = ? ", source).First(&item).Error
		if err == nil && item.EdbSourceId > 0 {
			sourceInfo, err = jsoniter.MarshalToString(item)
			if err != nil {
				return
			}
			_ = global.Redis.SetEX(context.TODO(), cacheKey, sourceInfo, 24*time.Hour)
		}
	}
	return
}
