refactor: reorganize modules and add Linux build tooling
This commit is contained in:
44
module/base/initial/internal/logic/data/areas.go
Normal file
44
module/base/initial/internal/logic/data/areas.go
Normal file
@@ -0,0 +1,44 @@
|
||||
package data
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
pb "bsm/full/module/base/initial/pb"
|
||||
"git.apinb.com/bsm-sdk/core/errcode"
|
||||
"git.apinb.com/bsm-sdk/core/printer"
|
||||
)
|
||||
|
||||
// 区域数据,默认级别:市
|
||||
func Areas(ctx context.Context, in *pb.AreasRequest) (reply *pb.AreasReply, err error) {
|
||||
// 参数检查 - 如果都没有提供,默认使用中国(ID=1)
|
||||
if in.GetCountryCode() == "" && in.GetCountryId() == 0 {
|
||||
in.CountryId = 1 // 默认中国
|
||||
}
|
||||
|
||||
// 查询条件:国家代码优先
|
||||
areas, err := GetAreasByCache(in)
|
||||
if err != nil {
|
||||
printer.Error("GetAreasByCache", err)
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
|
||||
// 转换为响应格式
|
||||
var data []*pb.AreasItem
|
||||
for _, area := range areas {
|
||||
data = append(data, &pb.AreasItem{
|
||||
Id: fmt.Sprintf("%d", area.Id),
|
||||
Pid: fmt.Sprintf("%d", area.Pid),
|
||||
Deep: area.Deep,
|
||||
Name: area.Name,
|
||||
PinyinPrefix: area.PinyinPrefix,
|
||||
Pinyin: area.Pinyin,
|
||||
ExtId: area.ExtId,
|
||||
ExtName: area.ExtName,
|
||||
})
|
||||
}
|
||||
|
||||
return &pb.AreasReply{
|
||||
Areas: data,
|
||||
}, nil
|
||||
}
|
||||
39
module/base/initial/internal/logic/data/areas_cache.go
Normal file
39
module/base/initial/internal/logic/data/areas_cache.go
Normal file
@@ -0,0 +1,39 @@
|
||||
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/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()
|
||||
}
|
||||
|
||||
cacheKey := impl.RedisService.BuildKey("areas", key, val, in.ShowTown)
|
||||
|
||||
err := impl.RedisService.Get(cacheKey, &areas)
|
||||
if err == nil {
|
||||
return areas, nil
|
||||
}
|
||||
|
||||
err = impl.DBService.Where("enabled = ?", true).Where(key, val).Where("show_town = ?", in.ShowTown).
|
||||
Order("sort_order ASC, name ASC").
|
||||
Find(&areas).Error
|
||||
|
||||
err = impl.RedisService.Set(cacheKey, areas, vars.DefaultTTL)
|
||||
return areas, err
|
||||
}
|
||||
43
module/base/initial/internal/logic/data/country.go
Normal file
43
module/base/initial/internal/logic/data/country.go
Normal file
@@ -0,0 +1,43 @@
|
||||
package data
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
pb "bsm/full/module/base/initial/pb"
|
||||
"git.apinb.com/bsm-sdk/core/errcode"
|
||||
"git.apinb.com/bsm-sdk/core/printer"
|
||||
)
|
||||
|
||||
func Country(ctx context.Context, in *pb.Empty) (reply *pb.CountryReply, err error) {
|
||||
// 查询启用的国家,按排序字段和名称排序
|
||||
countries, err := GetCountryByCache()
|
||||
if err != nil {
|
||||
printer.Error("GetCountryByCache", err)
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
|
||||
// 转换为响应格式
|
||||
var data []*pb.CountryItem
|
||||
for _, country := range countries {
|
||||
data = append(data, &pb.CountryItem{
|
||||
Id: uint32(country.Id),
|
||||
Iso2: country.Iso2,
|
||||
Iso3: country.Iso3,
|
||||
NumCode: country.NumCode,
|
||||
PhoneCode: country.PhoneCode,
|
||||
Currency: country.Currency,
|
||||
CurrencySymbol: country.CurrencySymbol,
|
||||
Region: country.Region,
|
||||
Timezones: country.Timezones,
|
||||
Translations: country.Translations,
|
||||
Name: country.Name,
|
||||
LocalName: country.LocalName,
|
||||
Enabled: country.Enabled,
|
||||
SortOrder: country.SortOrder,
|
||||
})
|
||||
}
|
||||
|
||||
return &pb.CountryReply{
|
||||
Countries: data,
|
||||
}, nil
|
||||
}
|
||||
25
module/base/initial/internal/logic/data/country_cache.go
Normal file
25
module/base/initial/internal/logic/data/country_cache.go
Normal file
@@ -0,0 +1,25 @@
|
||||
package data
|
||||
|
||||
import (
|
||||
"bsm/full/module/base/initial/internal/impl"
|
||||
"bsm/full/module/base/initial/internal/models"
|
||||
"git.apinb.com/bsm-sdk/core/vars"
|
||||
)
|
||||
|
||||
func GetCountryByCache() ([]*models.InitialCountry, error) {
|
||||
var country []*models.InitialCountry
|
||||
|
||||
cacheKey := impl.RedisService.BuildKey("country")
|
||||
|
||||
err := impl.RedisService.Get(cacheKey, &country)
|
||||
if err == nil {
|
||||
return country, nil
|
||||
}
|
||||
|
||||
err = impl.DBService.Where("enabled = ?", true).
|
||||
Order("sort_order ASC, name ASC").
|
||||
Find(&country).Error
|
||||
|
||||
err = impl.RedisService.Set(cacheKey, country, vars.DefaultTTL)
|
||||
return country, err
|
||||
}
|
||||
48
module/base/initial/internal/logic/data/datas.go
Normal file
48
module/base/initial/internal/logic/data/datas.go
Normal file
@@ -0,0 +1,48 @@
|
||||
package data
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
pb "bsm/full/module/base/initial/pb"
|
||||
"git.apinb.com/bsm-sdk/core/errcode"
|
||||
"git.apinb.com/bsm-sdk/core/printer"
|
||||
)
|
||||
|
||||
// 系统标签数据
|
||||
func Datas(ctx context.Context, in *pb.Empty) (reply *pb.DatasReply, err error) {
|
||||
// 查询系统数据,按数据类型和ID排序
|
||||
datas, err := GetDatasByCache()
|
||||
if err != nil {
|
||||
printer.Error("GetDatasByCache", err)
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
|
||||
// 转换为响应格式
|
||||
var data []*pb.DatasItem
|
||||
|
||||
// 添加默认的"全部"选项
|
||||
data = append(data, &pb.DatasItem{
|
||||
Id: 0,
|
||||
DataType: "default",
|
||||
Key: "all",
|
||||
Title: "全部",
|
||||
Remark: "默认全部选项",
|
||||
Icon: "",
|
||||
})
|
||||
|
||||
// 添加数据库中的数据
|
||||
for _, item := range datas {
|
||||
data = append(data, &pb.DatasItem{
|
||||
Id: int64(item.ID),
|
||||
DataType: item.DataType,
|
||||
Key: item.Key,
|
||||
Title: item.Title,
|
||||
Remark: item.Remark,
|
||||
Icon: item.Icon,
|
||||
})
|
||||
}
|
||||
|
||||
return &pb.DatasReply{
|
||||
Datas: data,
|
||||
}, nil
|
||||
}
|
||||
29
module/base/initial/internal/logic/data/datas_cache.go
Normal file
29
module/base/initial/internal/logic/data/datas_cache.go
Normal file
@@ -0,0 +1,29 @@
|
||||
package data
|
||||
|
||||
import (
|
||||
"bsm/full/module/base/initial/internal/impl"
|
||||
"bsm/full/module/base/initial/internal/models"
|
||||
"gorm.io/gorm"
|
||||
|
||||
"git.apinb.com/bsm-sdk/core/errcode"
|
||||
"git.apinb.com/bsm-sdk/core/vars"
|
||||
)
|
||||
|
||||
func GetDatasByCache() ([]*models.InitialDatas, error) {
|
||||
var datas []*models.InitialDatas
|
||||
|
||||
cacheKey := impl.RedisService.BuildKey("datas")
|
||||
|
||||
err := impl.RedisService.Get(cacheKey, &datas)
|
||||
if err == nil {
|
||||
return datas, nil
|
||||
}
|
||||
|
||||
err = impl.DBService.Order("data_type ASC, id ASC").Find(&datas).Error
|
||||
if err != nil && err != gorm.ErrRecordNotFound {
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
|
||||
err = impl.RedisService.Set(cacheKey, datas, vars.DefaultTTL)
|
||||
return datas, err
|
||||
}
|
||||
Reference in New Issue
Block a user