|
@@ -2,8 +2,9 @@ package service
|
|
|
|
|
|
import (
|
|
|
"context"
|
|
|
+ "encoding/json"
|
|
|
+ "fmt"
|
|
|
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"
|
|
|
)
|
|
@@ -20,20 +21,27 @@ type GateWayGroupIpService interface {
|
|
|
func NewGateWayGroupIpService(
|
|
|
service *Service,
|
|
|
gateWayGroupIpRepository repository.GateWayGroupIpRepository,
|
|
|
+ request RequestService,
|
|
|
) GateWayGroupIpService {
|
|
|
return &gateWayGroupIpService{
|
|
|
Service: service,
|
|
|
gateWayGroupIpRepository: gateWayGroupIpRepository,
|
|
|
+ request: request,
|
|
|
}
|
|
|
}
|
|
|
|
|
|
type gateWayGroupIpService struct {
|
|
|
*Service
|
|
|
gateWayGroupIpRepository repository.GateWayGroupIpRepository
|
|
|
+ request RequestService
|
|
|
}
|
|
|
|
|
|
func (s *gateWayGroupIpService) GetGateWayGroupIp(ctx context.Context, id int64) (*model.GateWayGroupIp, error) {
|
|
|
- return s.gateWayGroupIpRepository.GetGateWayGroupIp(ctx, id)
|
|
|
+ res, err := s.gateWayGroupIpRepository.GetGateWayGroupIp(ctx, id)
|
|
|
+ if err != nil {
|
|
|
+ return nil, err
|
|
|
+ }
|
|
|
+ return res, nil
|
|
|
}
|
|
|
|
|
|
func (s *gateWayGroupIpService) GetGateWayGroupIpByGatewayGroupId(ctx context.Context, gatewayGroupId int) (*[]model.GateWayGroupIp, error) {
|
|
@@ -45,6 +53,9 @@ func (s *gateWayGroupIpService) GetGateWayGroupIpByGatewayGroupId(ctx context.Co
|
|
|
}
|
|
|
|
|
|
func (s *gateWayGroupIpService) AddGateWayGroupIp(ctx context.Context, req *v1.GateWayGroupIpRequest) error {
|
|
|
+ if err := s.sendIp(ctx, req.Ip, "add"); err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
if err := s.gateWayGroupIpRepository.AddGateWayGroupIp(ctx, &model.GateWayGroupIp{
|
|
|
GatewayGroupId: req.GatewayGroupId,
|
|
|
Ip: req.Ip,
|
|
@@ -58,6 +69,18 @@ func (s *gateWayGroupIpService) AddGateWayGroupIp(ctx context.Context, req *v1.G
|
|
|
}
|
|
|
|
|
|
func (s *gateWayGroupIpService) EditGateWayGroupIp(ctx context.Context, req *v1.GateWayGroupIpRequest) error {
|
|
|
+ oldIp, err := s.GetGateWayGroupIp(ctx, int64(req.Id))
|
|
|
+ if err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
+ if oldIp.Ip != req.Ip {
|
|
|
+ if err := s.sendIp(ctx, oldIp.Ip, "delete"); err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
+ if err := s.sendIp(ctx, req.Ip, "add"); err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
+ }
|
|
|
if err := s.gateWayGroupIpRepository.EditGateWayGroupIp(ctx, &model.GateWayGroupIp{
|
|
|
Id: req.Id,
|
|
|
GatewayGroupId: req.GatewayGroupId,
|
|
@@ -72,6 +95,13 @@ func (s *gateWayGroupIpService) EditGateWayGroupIp(ctx context.Context, req *v1.
|
|
|
}
|
|
|
|
|
|
func (s *gateWayGroupIpService) DeleteGateWayGroupIp(ctx context.Context, req *v1.DeleteGateWayGroupIpRequest) error {
|
|
|
+ oldIp, err := s.GetGateWayGroupIp(ctx, int64(req.Id))
|
|
|
+ if err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
+ if err := s.sendIp(ctx, oldIp.Ip, "delete"); err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
if err := s.gateWayGroupIpRepository.DeleteGateWayGroupIp(ctx, &model.GateWayGroupIp{
|
|
|
Id: req.Id,
|
|
|
}); err != nil {
|
|
@@ -87,4 +117,32 @@ func (s *gateWayGroupIpService) GetGateWayGroupIpAdmin(ctx context.Context,req *
|
|
|
}
|
|
|
return res, nil
|
|
|
|
|
|
+}
|
|
|
+
|
|
|
+func (s *gateWayGroupIpService) sendIp(ctx context.Context, ip string, action string) error {
|
|
|
+ var apiUrl string
|
|
|
+ switch action {
|
|
|
+ case "add":
|
|
|
+ apiUrl = ""
|
|
|
+ case "delete":
|
|
|
+
|
|
|
+ apiUrl = ""
|
|
|
+ }
|
|
|
+ formData := map[string]interface{}{
|
|
|
+ "ip": ip,
|
|
|
+ }
|
|
|
+ resBody, err := s.request.Request(ctx, formData, apiUrl, "", "")
|
|
|
+ if err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
+ var res v1.GeneralResponse[string]
|
|
|
+ err = json.Unmarshal(resBody, &res)
|
|
|
+ if err != nil {
|
|
|
+ return fmt.Errorf("反序列化响应 JSON 失败 (内容: %s): %w", string(resBody), err)
|
|
|
+ }
|
|
|
+ if res.Code != 0 {
|
|
|
+ return fmt.Errorf("API 错误: code %d, msg '%s'", res.Code, res.Message)
|
|
|
+ }
|
|
|
+ return nil
|
|
|
+
|
|
|
}
|