host.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. package service
  2. import (
  3. "context"
  4. v1 "github.com/go-nunu/nunu-layout-advanced/api/v1"
  5. "github.com/go-nunu/nunu-layout-advanced/internal/model"
  6. "github.com/go-nunu/nunu-layout-advanced/internal/repository"
  7. "github.com/spf13/cast"
  8. "strings"
  9. )
  10. type HostService interface {
  11. GetHost(ctx context.Context, id int64) (*model.Host, error)
  12. GetGameShieldConfig(ctx context.Context, hostId int) (v1.GameShieldHostBackendConfigResponse, error)
  13. }
  14. func NewHostService(
  15. service *Service,
  16. hostRepository repository.HostRepository,
  17. ) HostService {
  18. return &hostService{
  19. Service: service,
  20. hostRepository: hostRepository,
  21. }
  22. }
  23. const (
  24. ConfigOnlineDevices = "在线设备数"
  25. ConfigRuleEntries = "规则条目"
  26. ConfigMaxBandwidth = "高带宽转发规则条目"
  27. ConfigSourceMachines = "支持源机"
  28. )
  29. type hostService struct {
  30. *Service
  31. hostRepository repository.HostRepository
  32. }
  33. func (s *hostService) GetHost(ctx context.Context, id int64) (*model.Host, error) {
  34. return s.hostRepository.GetHost(ctx, id)
  35. }
  36. func (s *hostService) GetHostConfig(ctx context.Context, hostId int) ([]map[string]string, error) {
  37. configOptions, err := s.hostRepository.GetHostConfig(ctx, hostId)
  38. if err != nil {
  39. return nil, err
  40. }
  41. // 2. 收集ID和建立映射关系
  42. var configIDs []int
  43. var optionIDs []int
  44. optionMap := make(map[int]int)
  45. for _, item := range configOptions {
  46. configIDs = append(configIDs, item.Configid)
  47. optionIDs = append(optionIDs, item.Optionid)
  48. optionMap[item.Configid] = item.Optionid
  49. }
  50. // 3. 获取配置和选项数据
  51. var configs []v1.ProductConfigOption
  52. if len(configIDs) > 0 {
  53. configs, err = s.hostRepository.GetProductConfigOption(ctx, configIDs)
  54. if err != nil {
  55. return nil, err
  56. }
  57. }
  58. var options []v1.ProductConfigOptionSub
  59. if len(optionIDs) > 0 {
  60. options, err = s.hostRepository.GetProductConfigOptionSub(ctx, optionIDs)
  61. if err != nil {
  62. return nil, err
  63. }
  64. }
  65. // 4. 转换选项为关联数组
  66. optionsByID := make(map[int]v1.ProductConfigOptionSub)
  67. for _, option := range options {
  68. optionsByID[option.ID] = option
  69. }
  70. // 5. 构建结果数据
  71. var data []map[string]string
  72. for _, config := range configs {
  73. optionID := optionMap[config.ID]
  74. var optionName string
  75. if opt, ok := optionsByID[optionID]; ok {
  76. optionName = opt.OptionName
  77. }
  78. data = append(data, map[string]string{
  79. "config_name": config.OptionName,
  80. "option_name": optionName,
  81. })
  82. }
  83. return data, nil
  84. }
  85. func (s *hostService) tidyGetGameShieldConfig(ctx context.Context, configName string, optionName string) (map[string]int, error) {
  86. // 根据配置名称去除相应的单位后缀
  87. switch configName {
  88. case ConfigOnlineDevices, ConfigRuleEntries:
  89. optionName = strings.TrimSuffix(optionName, "个")
  90. case ConfigMaxBandwidth:
  91. optionName = strings.TrimSuffix(optionName, "条")
  92. }
  93. // 转换为整数并返回
  94. return map[string]int{configName: cast.ToInt(optionName)}, nil
  95. }
  96. func (s *hostService) GetGameShieldConfig(ctx context.Context, hostId int) (v1.GameShieldHostBackendConfigResponse, error) {
  97. baseData, err := s.GetHostConfig(ctx, hostId)
  98. if err != nil {
  99. return v1.GameShieldHostBackendConfigResponse{}, err
  100. }
  101. var data v1.GameShieldHostBackendConfigResponse
  102. for _, item := range baseData {
  103. configName := item["config_name"]
  104. optionName := item["option_name"]
  105. res, err := s.tidyGetGameShieldConfig(ctx, configName, optionName)
  106. if err != nil {
  107. return v1.GameShieldHostBackendConfigResponse{}, err
  108. }
  109. // 使用switch直接设置对应字段
  110. switch configName {
  111. case ConfigOnlineDevices:
  112. data.OnlineDevicesCount = int64(res[configName])
  113. case ConfigRuleEntries:
  114. data.RuleEntriesCount = int64(res[configName])
  115. case ConfigMaxBandwidth:
  116. data.MaxBandwidthCount = int64(res[configName])
  117. case ConfigSourceMachines:
  118. data.SourceMachinesCount = int64(res[configName])
  119. }
  120. }
  121. return data, nil
  122. }