47 lines
1.2 KiB
Go
47 lines
1.2 KiB
Go
package data
|
||
|
||
import (
|
||
"bsm/full/module/base/initial/internal/impl"
|
||
"bsm/full/module/base/initial/internal/models"
|
||
pb "bsm/full/module/base/initial/pb"
|
||
"git.apinb.com/bsm-sdk/core/errcode"
|
||
"git.apinb.com/bsm-sdk/core/vars"
|
||
)
|
||
|
||
func GetAreasByCache(in *pb.AreasRequest) ([]*models.InitialAreas, error) {
|
||
var areas []*models.InitialAreas
|
||
|
||
var (
|
||
key string
|
||
val any
|
||
)
|
||
|
||
if in.GetCountryCode() != "" {
|
||
key = "country_code = ?"
|
||
val = in.GetCountryCode()
|
||
} else if in.GetCountryId() != 0 {
|
||
key = "country_id = ?"
|
||
val = in.GetCountryId()
|
||
}
|
||
|
||
// 缓存键只包含实际参与查询的条件(show_town 在现有模型下无对应列,不参与查询)
|
||
cacheKey := impl.RedisService.BuildKey("areas", key, val)
|
||
|
||
err := impl.RedisService.Get(cacheKey, &areas)
|
||
if err == nil {
|
||
return areas, nil
|
||
}
|
||
|
||
// 按模型真实列查询(InitialAreas 没有 enabled/show_town/sort_order 列)
|
||
err = impl.DBService.Where(key, val).
|
||
Order("name ASC").
|
||
Find(&areas).Error
|
||
if err != nil {
|
||
// 查询失败不写缓存,避免空结果污染缓存
|
||
return nil, errcode.ErrDB
|
||
}
|
||
|
||
err = impl.RedisService.Set(cacheKey, areas, vars.DefaultTTL)
|
||
return areas, err
|
||
}
|