123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118 |
- package service
- import (
- "context"
- "encoding/json"
- 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)
- AddIpWhereHostIdNull(ctx context.Context, hostId int64,uid int64) error
- }
- func NewGatewayipService(
- service *Service,
- gatewayipRepository repository.GatewayipRepository,
- host HostService,
- log LogService,
- ) GatewayipService {
- return &gatewayipService{
- Service: service,
- gatewayipRepository: gatewayipRepository,
- host : host,
- log : log,
- }
- }
- type gatewayipService struct {
- *Service
- gatewayipRepository repository.GatewayipRepository
- host HostService
- log LogService
- }
- 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
- }
- ips, 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,
- });
- if err != nil {
- return err
- }
- ipsJson, err := json.Marshal(ips)
- if err != nil {
- return err
- }
- if err = s.log.AddLog(ctx, &model.Log{
- Uid: uid,
- Api: "AddIpWhereHostIdNull",
- Message: "分配网关组IP",
- ExtraData: ipsJson,
- }); 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
- }
|