gatewaygroupip.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. package service
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. v1 "github.com/go-nunu/nunu-layout-advanced/api/v1"
  7. "github.com/go-nunu/nunu-layout-advanced/internal/model"
  8. "github.com/go-nunu/nunu-layout-advanced/internal/repository"
  9. )
  10. type GateWayGroupIpService interface {
  11. GetGateWayGroupIp(ctx context.Context, id int64) (*model.GateWayGroupIp, error)
  12. GetGateWayGroupIpByGatewayGroupId(ctx context.Context, gatewayGroupId int) (*[]model.GateWayGroupIp, error)
  13. AddGateWayGroupIp(ctx context.Context, req *v1.GateWayGroupIpRequest) error
  14. EditGateWayGroupIp(ctx context.Context, req *v1.GateWayGroupIpRequest) error
  15. DeleteGateWayGroupIp(ctx context.Context, req *v1.DeleteGateWayGroupIpRequest) error
  16. GetGateWayGroupIpAdmin(ctx context.Context,req *v1.SearchGatewayGroupIpParams) (*v1.PaginatedResponse[model.GateWayGroupIp], error)
  17. }
  18. func NewGateWayGroupIpService(
  19. service *Service,
  20. gateWayGroupIpRepository repository.GateWayGroupIpRepository,
  21. request RequestService,
  22. ) GateWayGroupIpService {
  23. return &gateWayGroupIpService{
  24. Service: service,
  25. gateWayGroupIpRepository: gateWayGroupIpRepository,
  26. request: request,
  27. }
  28. }
  29. type gateWayGroupIpService struct {
  30. *Service
  31. gateWayGroupIpRepository repository.GateWayGroupIpRepository
  32. request RequestService
  33. }
  34. func (s *gateWayGroupIpService) GetGateWayGroupIp(ctx context.Context, id int64) (*model.GateWayGroupIp, error) {
  35. res, err := s.gateWayGroupIpRepository.GetGateWayGroupIp(ctx, id)
  36. if err != nil {
  37. return nil, err
  38. }
  39. return res, nil
  40. }
  41. func (s *gateWayGroupIpService) GetGateWayGroupIpByGatewayGroupId(ctx context.Context, gatewayGroupId int) (*[]model.GateWayGroupIp, error) {
  42. res, err := s.gateWayGroupIpRepository.GetGateWayGroupIpByGatewayGroupId(ctx, gatewayGroupId)
  43. if err != nil {
  44. return nil, err
  45. }
  46. return &res, nil
  47. }
  48. func (s *gateWayGroupIpService) AddGateWayGroupIp(ctx context.Context, req *v1.GateWayGroupIpRequest) error {
  49. if err := s.sendIp(ctx, req.Ip, "add"); err != nil {
  50. return err
  51. }
  52. if err := s.gateWayGroupIpRepository.AddGateWayGroupIp(ctx, &model.GateWayGroupIp{
  53. GatewayGroupId: req.GatewayGroupId,
  54. Ip: req.Ip,
  55. Tag: req.Tag,
  56. Comment: req.Comment,
  57. OriginPlace: req.OriginPlace,
  58. }); err != nil {
  59. return err
  60. }
  61. return nil
  62. }
  63. func (s *gateWayGroupIpService) EditGateWayGroupIp(ctx context.Context, req *v1.GateWayGroupIpRequest) error {
  64. oldIp, err := s.GetGateWayGroupIp(ctx, int64(req.Id))
  65. if err != nil {
  66. return err
  67. }
  68. if oldIp.Ip != req.Ip {
  69. if err := s.sendIp(ctx, oldIp.Ip, "delete"); err != nil {
  70. return err
  71. }
  72. if err := s.sendIp(ctx, req.Ip, "add"); err != nil {
  73. return err
  74. }
  75. }
  76. if err := s.gateWayGroupIpRepository.EditGateWayGroupIp(ctx, &model.GateWayGroupIp{
  77. Id: req.Id,
  78. GatewayGroupId: req.GatewayGroupId,
  79. Ip: req.Ip,
  80. Tag: req.Tag,
  81. Comment: req.Comment,
  82. OriginPlace: req.OriginPlace,
  83. }); err != nil {
  84. return err
  85. }
  86. return nil
  87. }
  88. func (s *gateWayGroupIpService) DeleteGateWayGroupIp(ctx context.Context, req *v1.DeleteGateWayGroupIpRequest) error {
  89. oldIp, err := s.GetGateWayGroupIp(ctx, int64(req.Id))
  90. if err != nil {
  91. return err
  92. }
  93. if err := s.sendIp(ctx, oldIp.Ip, "delete"); err != nil {
  94. return err
  95. }
  96. if err := s.gateWayGroupIpRepository.DeleteGateWayGroupIp(ctx, &model.GateWayGroupIp{
  97. Id: req.Id,
  98. }); err != nil {
  99. return err
  100. }
  101. return nil
  102. }
  103. func (s *gateWayGroupIpService) GetGateWayGroupIpAdmin(ctx context.Context,req *v1.SearchGatewayGroupIpParams) (*v1.PaginatedResponse[model.GateWayGroupIp], error) {
  104. res, err := s.gateWayGroupIpRepository.GetGatewayGroupIpList(ctx, *req)
  105. if err != nil {
  106. return nil, err
  107. }
  108. return res, nil
  109. }
  110. func (s *gateWayGroupIpService) sendIp(ctx context.Context, ip string, action string) error {
  111. var apiUrl string
  112. switch action {
  113. case "add":
  114. apiUrl = ""
  115. case "delete":
  116. apiUrl = ""
  117. }
  118. formData := map[string]interface{}{
  119. "ip": ip,
  120. }
  121. resBody, err := s.request.Request(ctx, formData, apiUrl, "", "")
  122. if err != nil {
  123. return err
  124. }
  125. var res v1.GeneralResponse[string]
  126. err = json.Unmarshal(resBody, &res)
  127. if err != nil {
  128. return fmt.Errorf("反序列化响应 JSON 失败 (内容: %s): %w", string(resBody), err)
  129. }
  130. if res.Code != 0 {
  131. return fmt.Errorf("API 错误: code %d, msg '%s'", res.Code, res.Message)
  132. }
  133. return nil
  134. }