country.go 1.73 KB
Newer Older
hujiebin's avatar
hujiebin committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
package res_c

import (
	"context"
	"git.hilo.cn/hilo-common/domain"
	"hilo-group/_const/redis_key"
	"hilo-group/domain/model/res_m"
	"time"
)

func GetCountryIconMap(model *domain.Model) (map[string]string, error) {
	m, err := GetAllCountryIcon(model)
	if err != nil || len(m) <= 0 {
		m, err = res_m.GetAllCountries(model)
		if err != nil {
			return nil, err
		}
		if len(m) <= 0 {
			return map[string]string{}, nil
		}
		SaveAllCountryIcon(model, m)
	}
	return m, nil
}

func GetAllCountryIcon(model *domain.Model) (map[string]string, error) {
	key := redis_key.GetCountryIconKey()
	return model.Redis.HGetAll(context.Background(), key).Result()
}

func SaveAllCountryIcon(model *domain.Model, icons map[string]string) (int64, error) {
	key := redis_key.GetCountryIconKey()
	ret, err := model.Redis.HSet(context.Background(), key, icons).Result()
	if err == nil {
		model.Redis.Expire(context.Background(), key, time.Minute*10)
	}
	return ret, err
}
chenweijian's avatar
chenweijian committed
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67

func GetCountryAreaMap(model *domain.Model) (map[string]string, error) {
	m, err := GetAllCountryArea(model)
	if err != nil || len(m) <= 0 {
		m, err = res_m.GetAllCountriesArea(model)
		if err != nil {
			return nil, err
		}
		if len(m) <= 0 {
			return map[string]string{}, nil
		}
		SaveAllCountryArea(model, m)
	}
	return m, nil
}

func GetAllCountryArea(model *domain.Model) (map[string]string, error) {
	key := redis_key.GetCountryAreaKey()
	return model.Redis.HGetAll(context.Background(), key).Result()
}

func SaveAllCountryArea(model *domain.Model, data map[string]string) (int64, error) {
	key := redis_key.GetCountryAreaKey()
	ret, err := model.Redis.HSet(context.Background(), key, data).Result()
	if err == nil {
		model.Redis.Expire(context.Background(), key, time.Minute*10)
	}
	return ret, err
}