1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798 |
- 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"
- )
- type GatewayipService interface {
- GetGatewayip(ctx context.Context, id int64) (*model.Gatewayip, error)
- GetGatewayipOnlyIpByHostIdAll(ctx context.Context, hostId int64,uid int64) ([]string, error)
- GetGatewayipByHostIdFirst(ctx context.Context, hostId int64,uid int64) (string, error)
- }
- func NewGatewayipService(
- service *Service,
- gatewayipRepository repository.GatewayipRepository,
- host HostService,
- ) GatewayipService {
- return &gatewayipService{
- Service: service,
- gatewayipRepository: gatewayipRepository,
- host : host,
- }
- }
- type gatewayipService struct {
- *Service
- gatewayipRepository repository.GatewayipRepository
- host HostService
- }
- func (s *gatewayipService) GetGatewayip(ctx context.Context, id int64) (*model.Gatewayip, error) {
- return s.gatewayipRepository.GetGatewayip(ctx, id)
- }
- func (s *gatewayipService) AddIpWhereHostIdNull(ctx context.Context, hostId int64,uid int64) error {
- config, err := s.host.GetGlobalLimitConfig(ctx, int(hostId))
- if err != nil {
- return err
- }
- if err := s.gatewayipRepository.GetIpWhereHostIdNull(ctx, v1.GlobalLimitRequireResponse{
- HostId: int(hostId),
- Bps: config.Bps,
- MaxBytesMonth: config.MaxBytesMonth,
- IpCount: config.IpCount,
- Operator: config.Operator,
- NodeArea: config.NodeArea,
- ConfigMaxProtection: config.ConfigMaxProtection,
- IsBanUdp: config.IsBanUdp,
- }); err != nil {
- return err
- }
- return nil
- }
- func (s *gatewayipService) GetGatewayipOnlyIpByHostIdAll(ctx context.Context, hostId int64,uid int64) ([]string, error) {
- gatewayIps, err := s.gatewayipRepository.GetGatewayipOnlyIpByHostIdAll(ctx, hostId)
- if err != nil {
- return nil, err
- }
- if len(gatewayIps) == 0 {
- err = s.AddIpWhereHostIdNull(ctx, hostId,uid)
- if err != nil {
- return nil, err
- }
- gatewayIps, err = s.gatewayipRepository.GetGatewayipOnlyIpByHostIdAll(ctx, hostId)
- if err != nil {
- return nil, err
- }
- }
- return gatewayIps, nil
- }
- func (s *gatewayipService) GetGatewayipByHostIdFirst(ctx context.Context, hostId int64,uid int64) (string, error) {
- gatewayIps, err := s.gatewayipRepository.GetGatewayipByHostIdFirst(ctx, hostId)
- if err != nil {
- return "", err
- }
- if len(gatewayIps) == 0 {
- err = s.AddIpWhereHostIdNull(ctx, hostId,uid)
- if err != nil {
- return "", err
- }
- gatewayIps, err = s.gatewayipRepository.GetGatewayipByHostIdFirst(ctx, hostId)
- if err != nil {
- return "", err
- }
- }
- return gatewayIps, nil
- }
|