2026-08-09 10:43:45 +08:00
|
|
|
|
package data
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
2026-08-09 12:41:08 +08:00
|
|
|
|
"bsm/full/module/base/initial/internal/impl"
|
|
|
|
|
|
"bsm/full/module/base/initial/internal/models"
|
|
|
|
|
|
pb "bsm/full/module/base/initial/pb"
|
2026-09-22 21:15:34 +08:00
|
|
|
|
"git.apinb.com/bsm-sdk/core/errcode"
|
2026-08-09 10:43:45 +08:00
|
|
|
|
"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()
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-09-22 21:15:34 +08:00
|
|
|
|
// 缓存键只包含实际参与查询的条件(show_town 在现有模型下无对应列,不参与查询)
|
|
|
|
|
|
cacheKey := impl.RedisService.BuildKey("areas", key, val)
|
2026-08-09 10:43:45 +08:00
|
|
|
|
|
|
|
|
|
|
err := impl.RedisService.Get(cacheKey, &areas)
|
|
|
|
|
|
if err == nil {
|
|
|
|
|
|
return areas, nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-09-22 21:15:34 +08:00
|
|
|
|
// 按模型真实列查询(InitialAreas 没有 enabled/show_town/sort_order 列)
|
|
|
|
|
|
err = impl.DBService.Where(key, val).
|
|
|
|
|
|
Order("name ASC").
|
2026-08-09 10:43:45 +08:00
|
|
|
|
Find(&areas).Error
|
2026-09-22 21:15:34 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
|
// 查询失败不写缓存,避免空结果污染缓存
|
|
|
|
|
|
return nil, errcode.ErrDB
|
|
|
|
|
|
}
|
2026-08-09 10:43:45 +08:00
|
|
|
|
|
|
|
|
|
|
err = impl.RedisService.Set(cacheKey, areas, vars.DefaultTTL)
|
|
|
|
|
|
return areas, err
|
|
|
|
|
|
}
|