gatewayip.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. package service
  2. import (
  3. "context"
  4. "encoding/json"
  5. v1 "github.com/go-nunu/nunu-layout-advanced/api/v1"
  6. "github.com/go-nunu/nunu-layout-advanced/internal/model"
  7. "github.com/go-nunu/nunu-layout-advanced/internal/repository"
  8. )
  9. type GatewayipService interface {
  10. GetGatewayip(ctx context.Context, id int64) (*model.Gatewayip, error)
  11. GetGatewayipOnlyIpByHostIdAll(ctx context.Context, hostId int64,uid int64) ([]string, error)
  12. GetGatewayipByHostIdFirst(ctx context.Context, hostId int64,uid int64) (string, error)
  13. AddIpWhereHostIdNull(ctx context.Context, hostId int64,uid int64) error
  14. }
  15. func NewGatewayipService(
  16. service *Service,
  17. gatewayipRepository repository.GatewayipRepository,
  18. host HostService,
  19. log LogService,
  20. ) GatewayipService {
  21. return &gatewayipService{
  22. Service: service,
  23. gatewayipRepository: gatewayipRepository,
  24. host : host,
  25. log : log,
  26. }
  27. }
  28. type gatewayipService struct {
  29. *Service
  30. gatewayipRepository repository.GatewayipRepository
  31. host HostService
  32. log LogService
  33. }
  34. func (s *gatewayipService) GetGatewayip(ctx context.Context, id int64) (*model.Gatewayip, error) {
  35. return s.gatewayipRepository.GetGatewayip(ctx, id)
  36. }
  37. func (s *gatewayipService) AddIpWhereHostIdNull(ctx context.Context, hostId int64,uid int64) error {
  38. config, err := s.host.GetGlobalLimitConfig(ctx, int(hostId))
  39. if err != nil {
  40. return err
  41. }
  42. ips, err := s.gatewayipRepository.GetIpWhereHostIdNull(ctx, v1.GlobalLimitRequireResponse{
  43. HostId: int(hostId),
  44. Bps: config.Bps,
  45. MaxBytesMonth: config.MaxBytesMonth,
  46. IpCount: config.IpCount,
  47. Operator: config.Operator,
  48. NodeArea: config.NodeArea,
  49. ConfigMaxProtection: config.ConfigMaxProtection,
  50. IsBanUdp: config.IsBanUdp,
  51. });
  52. if err != nil {
  53. return err
  54. }
  55. ipsJson, err := json.Marshal(ips)
  56. if err != nil {
  57. return err
  58. }
  59. if err = s.log.AddLog(ctx, &model.Log{
  60. Uid: uid,
  61. Api: "AddIpWhereHostIdNull",
  62. Message: "分配网关组IP",
  63. ExtraData: ipsJson,
  64. }); err != nil {
  65. return err
  66. }
  67. return nil
  68. }
  69. func (s *gatewayipService) GetGatewayipOnlyIpByHostIdAll(ctx context.Context, hostId int64,uid int64) ([]string, error) {
  70. gatewayIps, err := s.gatewayipRepository.GetGatewayipOnlyIpByHostIdAll(ctx, hostId)
  71. if err != nil {
  72. return nil, err
  73. }
  74. if len(gatewayIps) == 0 {
  75. err = s.AddIpWhereHostIdNull(ctx, hostId,uid)
  76. if err != nil {
  77. return nil, err
  78. }
  79. gatewayIps, err = s.gatewayipRepository.GetGatewayipOnlyIpByHostIdAll(ctx, hostId)
  80. if err != nil {
  81. return nil, err
  82. }
  83. }
  84. return gatewayIps, nil
  85. }
  86. func (s *gatewayipService) GetGatewayipByHostIdFirst(ctx context.Context, hostId int64,uid int64) (string, error) {
  87. gatewayIps, err := s.gatewayipRepository.GetGatewayipByHostIdFirst(ctx, hostId)
  88. if err != nil {
  89. return "", err
  90. }
  91. if len(gatewayIps) == 0 {
  92. err = s.AddIpWhereHostIdNull(ctx, hostId,uid)
  93. if err != nil {
  94. return "", err
  95. }
  96. gatewayIps, err = s.gatewayipRepository.GetGatewayipByHostIdFirst(ctx, hostId)
  97. if err != nil {
  98. return "", err
  99. }
  100. }
  101. return gatewayIps, nil
  102. }