country.go 5.21 KB
Newer Older
chenweijian's avatar
chenweijian 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 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 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
package res_m

import (
	"gorm.io/gorm"
	"hilo-user/_const/enum/res_e"
	"hilo-user/domain"
	"hilo-user/myerr"
	"hilo-user/resource/mysql"
)

type ResCountry struct {
	mysql.Entity
	*domain.Model     `gorm:"-"`
	Name              mysql.Str
	ShortName         mysql.Str
	Code              mysql.Str
	Icon              mysql.Str
	Icon2             mysql.Str
	Lang              mysql.Str
	IsCommon          mysql.UserYesNo
	IsDefault         mysql.UserYesNo
	Status            mysql.UserYesNo
	StandardShortName mysql.Str
	AreaCode          mysql.Str
	AreaCodeName      mysql.Str
}

type ResLanguage struct {
	mysql.Entity
	*domain.Model `gorm:"-"`
	Name          mysql.Str
	Remark        mysql.Str
	ResCountryId  mysql.ID
}

type ResMsgTranslate struct {
	mysql.Entity
	*domain.Model `gorm:"-"`
	Language      mysql.Str
	Title         mysql.Str
	Content       mysql.Str
	IconUrl       mysql.Str
	MsgType       mysql.Type
	Type          uint32
	//跳转类型 0:无跳转 1:网页跳转, 2:跳转到用户 3:跳转到群组
	ActionType mysql.Type
	ActionUrl  mysql.Str
}

// 复制消息并替换图片和URL
func (rmt *ResMsgTranslate) CopyRecord(db *gorm.DB, newType uint64, iconUrl, actionUrl string) error {
	return db.Exec("REPLACE INTO res_msg_translate (language,title,content,?,msg_type,?,action_type,?) "+
		"SELECT language,title,content,icon_url,msg_type,10001,action_type,action_url FROM res_msg_translate "+
		"WHERE msg_type = ? and type = 39", iconUrl, newType, actionUrl, rmt.MsgType, rmt.Type).Error
}

//获取默认的国家
func GetCountryDefault(model *domain.Model) (*ResCountry, error) {
	var resCountry ResCountry
	if err := model.Db.Where(&ResCountry{
		IsDefault: mysql.USER,
	}).First(&resCountry).Error; err != nil {
		return nil, myerr.WrapErr(err)
	}
	return &resCountry, nil
}

// 获取所有国家的icon信息
func GetAllCountries(model *domain.Model) (map[string]string, error) {
	var countrys []ResCountry
	if err := model.Db.Model(&ResCountry{}).Where(&ResCountry{
		Status: mysql.USER,
	}).Find(&countrys).Error; err != nil {
		return nil, myerr.WrapErr(err)
	}

	result := make(map[string]string, 0)
	for _, i := range countrys {
		result[i.Name] = i.Icon
	}
	return result, nil
}

func GetAllCountryByFilter(model *domain.Model, shortNames []string) ([]ResCountry, error) {
	var countrys []ResCountry
	if err := model.Db.Model(&ResCountry{}).Where(&ResCountry{
		Status: mysql.USER,
	}).Find(&countrys).Error; err != nil {
		return nil, myerr.WrapErr(err)
	}
	countryMap := map[string]ResCountry{}

	//转成map
	for i, _ := range countrys {
		countryMap[countrys[i].ShortName] = countrys[i]
	}

	results := make([]ResCountry, 0, len(shortNames))
	for i, _ := range shortNames {
		if country, flag := countryMap[shortNames[i]]; flag {
			results = append(results, country)
		}
	}
	return results, nil
}

//通过standardShortName获取国家
func GetCountryByStandardShortName(model *domain.Model, standardShortName mysql.Str) (*ResCountry, error) {
	var resCountry ResCountry
	if err := model.Db.Where(&ResCountry{
		StandardShortName: standardShortName,
		Status:            mysql.USER,
	}).First(&resCountry).Error; err != nil {
		if err == gorm.ErrRecordNotFound {
			return nil, nil
		} else {
			return nil, myerr.WrapErr(err)
		}
	}
	resCountry.Model = model
	return &resCountry, nil
}

//通过shortName获取国家
func GetCountryByShortName(model *domain.Model, shortName mysql.Str) (*ResCountry, error) {
	var resCountry ResCountry
	err := model.Db.Where(&ResCountry{
		ShortName: shortName,
	}).First(&resCountry).Error
	return &resCountry, myerr.WrapErr(err)
}

//通过name获取国家
func GetCountryByName(model *domain.Model, name mysql.Str) (*ResCountry, error) {
	var resCountry ResCountry
	err := model.Db.WithContext(model.Context).Where(&ResCountry{
		Name: name,
	}).First(&resCountry).Error
	return &resCountry, myerr.WrapErr(err)
}

//通过code获取国家
func GetCountryByCode(model *domain.Model, code mysql.Str) (*ResCountry, error) {
	if code == "" {
		return nil, nil
	}
	var resCountry ResCountry
	err := model.Db.Where(&ResCountry{
		Code: code,
	}).First(&resCountry).Error
	if err == nil {
		return &resCountry, myerr.WrapErr(err)
	} else if err == gorm.ErrRecordNotFound {
		return nil, nil
	} else {
		return nil, myerr.WrapErr(err)
	}
}

//通过id获取国家
func GetCountryById(model *domain.Model, id mysql.ID) (*ResCountry, error) {
	var resCountry ResCountry
	err := model.Db.First(&resCountry, id).Error
	return &resCountry, myerr.WrapErr(err)
}

//通过语言,找到国家
func GetCountryByLanguage(model *domain.Model, name mysql.Str) (*ResCountry, error) {
	if name == "" {
		return nil, nil
	}
	var language ResLanguage
	err := model.Db.Where(&ResLanguage{
		Name: name,
	}).First(&language).Error
	if err == nil {
		return GetCountryById(model, language.ResCountryId)
	} else if err == gorm.ErrRecordNotFound {
		return nil, nil
	} else {
		return nil, myerr.WrapErr(err)
	}
}

//通过国家,找到对应的语言
func GetLangeByCountry(db *gorm.DB, country mysql.Str) (string, error) {
	var r ResCountry
	err := db.Where(&ResCountry{
		Name: country,
	}).First(&r).Error
	if err == nil {
		return r.Lang, nil
	} else if err == gorm.ErrRecordNotFound {
		return res_e.DEFAULT_LANG, nil
	} else {
		return "", myerr.WrapErr(err)
	}
}