gatewayip.go 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. )
  8. type GatewayipService interface {
  9. GetGatewayip(ctx context.Context, id int64) (*model.Gatewayip, error)
  10. GetGatewayipOnlyIpByHostIdAll(ctx context.Context, hostId int64,uid int64) ([]string, error)
  11. GetGatewayipByHostIdFirst(ctx context.Context, hostId int64,uid int64) (string, error)
  12. }
  13. func NewGatewayipService(
  14. service *Service,
  15. gatewayipRepository repository.GatewayipRepository,
  16. host HostService,
  17. ) GatewayipService {
  18. return &gatewayipService{
  19. Service: service,
  20. gatewayipRepository: gatewayipRepository,
  21. host : host,
  22. }
  23. }
  24. type gatewayipService struct {
  25. *Service
  26. gatewayipRepository repository.GatewayipRepository
  27. host HostService
  28. }
  29. func (s *gatewayipService) GetGatewayip(ctx context.Context, id int64) (*model.Gatewayip, error) {
  30. return s.gatewayipRepository.GetGatewayip(ctx, id)
  31. }
  32. func (s *gatewayipService) AddIpWhereHostIdNull(ctx context.Context, hostId int64,uid int64) error {
  33. config, err := s.host.GetGlobalLimitConfig(ctx, int(hostId))
  34. if err != nil {
  35. return err
  36. }
  37. if err := s.gatewayipRepository.GetIpWhereHostIdNull(ctx, v1.GlobalLimitRequireResponse{
  38. HostId: int(hostId),
  39. Bps: config.Bps,
  40. MaxBytesMonth: config.MaxBytesMonth,
  41. IpCount: config.IpCount,
  42. Operator: config.Operator,
  43. NodeArea: config.NodeArea,
  44. ConfigMaxProtection: config.ConfigMaxProtection,
  45. IsBanUdp: config.IsBanUdp,
  46. }); err != nil {
  47. return err
  48. }
  49. return nil
  50. }
  51. func (s *gatewayipService) GetGatewayipOnlyIpByHostIdAll(ctx context.Context, hostId int64,uid int64) ([]string, error) {
  52. gatewayIps, err := s.gatewayipRepository.GetGatewayipOnlyIpByHostIdAll(ctx, hostId)
  53. if err != nil {
  54. return nil, err
  55. }
  56. if len(gatewayIps) == 0 {
  57. err = s.AddIpWhereHostIdNull(ctx, hostId,uid)
  58. if err != nil {
  59. return nil, err
  60. }
  61. gatewayIps, err = s.gatewayipRepository.GetGatewayipOnlyIpByHostIdAll(ctx, hostId)
  62. if err != nil {
  63. return nil, err
  64. }
  65. }
  66. return gatewayIps, nil
  67. }
  68. func (s *gatewayipService) GetGatewayipByHostIdFirst(ctx context.Context, hostId int64,uid int64) (string, error) {
  69. gatewayIps, err := s.gatewayipRepository.GetGatewayipByHostIdFirst(ctx, hostId)
  70. if err != nil {
  71. return "", err
  72. }
  73. if len(gatewayIps) == 0 {
  74. err = s.AddIpWhereHostIdNull(ctx, hostId,uid)
  75. if err != nil {
  76. return "", err
  77. }
  78. gatewayIps, err = s.gatewayipRepository.GetGatewayipByHostIdFirst(ctx, hostId)
  79. if err != nil {
  80. return "", err
  81. }
  82. }
  83. return gatewayIps, nil
  84. }