package company

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

// GetByCompanyId 根据客户id获取信息
func GetByCompanyId(companyId int64) (item *model.Company, err error) {
	cacheKey := utils.HONGZEYB_ + "company:GetByCompanyId:" + strconv.Itoa(int(companyId))
	companyInfo, _ := global.Redis.Get(context.TODO(), cacheKey).Result()
	if companyInfo != "" {
		err = jsoniter.UnmarshalFromString(companyInfo, &item)
		if err != nil {
			return
		}
		item.CompanyID = uint64(companyId)
		return
	}
	item, err = model.GetByCompanyId(companyId)
	if err == nil && item.CompanyID > 0 {
		companyInfo, err = jsoniter.MarshalToString(item)
		if err != nil {
			return
		}
		_ = global.Redis.SetEX(context.TODO(), cacheKey, companyInfo, 2*time.Hour)
	}
	return
}
