123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138 |
- package service
- import (
- "context"
- v1 "github.com/go-nunu/nunu-layout-advanced/api/v1"
- "github.com/go-nunu/nunu-layout-advanced/internal/model"
- "github.com/go-nunu/nunu-layout-advanced/internal/repository"
- "github.com/spf13/cast"
- "strings"
- )
- type HostService interface {
- GetHost(ctx context.Context, id int64) (*model.Host, error)
- GetGameShieldConfig(ctx context.Context, hostId int) (v1.GameShieldHostBackendConfigResponse, error)
- }
- func NewHostService(
- service *Service,
- hostRepository repository.HostRepository,
- ) HostService {
- return &hostService{
- Service: service,
- hostRepository: hostRepository,
- }
- }
- const (
- ConfigOnlineDevices = "在线设备数"
- ConfigRuleEntries = "规则条目"
- ConfigMaxBandwidth = "高带宽转发规则条目"
- ConfigSourceMachines = "支持源机"
- )
- type hostService struct {
- *Service
- hostRepository repository.HostRepository
- }
- func (s *hostService) GetHost(ctx context.Context, id int64) (*model.Host, error) {
- return s.hostRepository.GetHost(ctx, id)
- }
- func (s *hostService) GetHostConfig(ctx context.Context, hostId int) ([]map[string]string, error) {
- configOptions, err := s.hostRepository.GetHostConfig(ctx, hostId)
- if err != nil {
- return nil, err
- }
- // 2. 收集ID和建立映射关系
- var configIDs []int
- var optionIDs []int
- optionMap := make(map[int]int)
- for _, item := range configOptions {
- configIDs = append(configIDs, item.Configid)
- optionIDs = append(optionIDs, item.Optionid)
- optionMap[item.Configid] = item.Optionid
- }
- // 3. 获取配置和选项数据
- var configs []v1.ProductConfigOption
- if len(configIDs) > 0 {
- configs, err = s.hostRepository.GetProductConfigOption(ctx, configIDs)
- if err != nil {
- return nil, err
- }
- }
- var options []v1.ProductConfigOptionSub
- if len(optionIDs) > 0 {
- options, err = s.hostRepository.GetProductConfigOptionSub(ctx, optionIDs)
- if err != nil {
- return nil, err
- }
- }
- // 4. 转换选项为关联数组
- optionsByID := make(map[int]v1.ProductConfigOptionSub)
- for _, option := range options {
- optionsByID[option.ID] = option
- }
- // 5. 构建结果数据
- var data []map[string]string
- for _, config := range configs {
- optionID := optionMap[config.ID]
- var optionName string
- if opt, ok := optionsByID[optionID]; ok {
- optionName = opt.OptionName
- }
- data = append(data, map[string]string{
- "config_name": config.OptionName,
- "option_name": optionName,
- })
- }
- return data, nil
- }
- func (s *hostService) tidyGetGameShieldConfig(ctx context.Context, configName string, optionName string) (map[string]int, error) {
- // 根据配置名称去除相应的单位后缀
- switch configName {
- case ConfigOnlineDevices, ConfigRuleEntries:
- optionName = strings.TrimSuffix(optionName, "个")
- case ConfigMaxBandwidth:
- optionName = strings.TrimSuffix(optionName, "条")
- }
- // 转换为整数并返回
- return map[string]int{configName: cast.ToInt(optionName)}, nil
- }
- func (s *hostService) GetGameShieldConfig(ctx context.Context, hostId int) (v1.GameShieldHostBackendConfigResponse, error) {
- baseData, err := s.GetHostConfig(ctx, hostId)
- if err != nil {
- return v1.GameShieldHostBackendConfigResponse{}, err
- }
- var data v1.GameShieldHostBackendConfigResponse
- for _, item := range baseData {
- configName := item["config_name"]
- optionName := item["option_name"]
- res, err := s.tidyGetGameShieldConfig(ctx, configName, optionName)
- if err != nil {
- return v1.GameShieldHostBackendConfigResponse{}, err
- }
- // 使用switch直接设置对应字段
- switch configName {
- case ConfigOnlineDevices:
- data.OnlineDevicesCount = int64(res[configName])
- case ConfigRuleEntries:
- data.RuleEntriesCount = int64(res[configName])
- case ConfigMaxBandwidth:
- data.MaxBandwidthCount = int64(res[configName])
- case ConfigSourceMachines:
- data.SourceMachinesCount = int64(res[configName])
- }
- }
- return data, nil
- }
|