gatewaygroupip.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. package repository
  2. import (
  3. "context"
  4. v1 "github.com/go-nunu/nunu-layout-advanced/api/v1"
  5. "math"
  6. "strings"
  7. "github.com/go-nunu/nunu-layout-advanced/internal/model"
  8. )
  9. type GateWayGroupIpRepository interface {
  10. GetGateWayGroupIp(ctx context.Context, id int64) (*model.GateWayGroupIp, error)
  11. AddGateWayGroupIp(ctx context.Context, req *model.GateWayGroupIp) error
  12. EditGateWayGroupIp(ctx context.Context, req *model.GateWayGroupIp) error
  13. DeleteGateWayGroupIp(ctx context.Context, req *model.GateWayGroupIp) error
  14. GetGateWayGroupIpByGatewayGroupId(ctx context.Context, gatewayGroupId int) ([]model.GateWayGroupIp, error)
  15. GetGateWayGroupFirstIpByGatewayGroupId(ctx context.Context, gatewayGroupId int) (string, error)
  16. GetGateWayGroupAllIpByGatewayGroupId(ctx context.Context, gatewayGroupId int) ([]string, error)
  17. GetGatewayGroupIpList(ctx context.Context,req v1.SearchGatewayGroupIpParams) (*v1.PaginatedResponse[model.GateWayGroupIp], error)
  18. }
  19. func NewGateWayGroupIpRepository(
  20. repository *Repository,
  21. ) GateWayGroupIpRepository {
  22. return &gateWayGroupIpRepository{
  23. Repository: repository,
  24. }
  25. }
  26. type gateWayGroupIpRepository struct {
  27. *Repository
  28. }
  29. func (r *gateWayGroupIpRepository) GetGateWayGroupIp(ctx context.Context, id int64) (*model.GateWayGroupIp, error) {
  30. var gateWayGroupIp model.GateWayGroupIp
  31. return &gateWayGroupIp, nil
  32. }
  33. func (r *gateWayGroupIpRepository) AddGateWayGroupIp(ctx context.Context, req *model.GateWayGroupIp) error {
  34. if err := r.DB(ctx).Create(req).Error; err != nil {
  35. return err
  36. }
  37. return nil
  38. }
  39. func (r *gateWayGroupIpRepository) EditGateWayGroupIp(ctx context.Context, req *model.GateWayGroupIp) error {
  40. if err := r.DB(ctx).Model(&model.GateWayGroupIp{}).Where("id = ?", req.Id).Updates(req).Error; err != nil {
  41. return err
  42. }
  43. return nil
  44. }
  45. func (r *gateWayGroupIpRepository) DeleteGateWayGroupIp(ctx context.Context, req *model.GateWayGroupIp) error {
  46. if err := r.DB(ctx).Model(&model.GateWayGroupIp{}).Where("id = ?", req.Id).Delete(req).Error; err != nil {
  47. return err
  48. }
  49. return nil
  50. }
  51. func (r *gateWayGroupIpRepository) GetGateWayGroupIpByGatewayGroupId(ctx context.Context, gatewayGroupId int) ([]model.GateWayGroupIp, error) {
  52. var res []model.GateWayGroupIp
  53. if err := r.DB(ctx).Model(&model.GateWayGroupIp{}).Where("gateway_group_id = ?", gatewayGroupId).Find(&res).Error; err != nil {
  54. return nil, err
  55. }
  56. return res, nil
  57. }
  58. func (r *gateWayGroupIpRepository) GetGateWayGroupFirstIpByGatewayGroupId(ctx context.Context, gatewayGroupId int) (string, error) {
  59. var res string
  60. if err := r.DB(ctx).Model(&model.GateWayGroupIp{}).Where("gateway_group_id = ?", gatewayGroupId).Select("ip").First(&res).Error; err != nil {
  61. return "", err
  62. }
  63. return res, nil
  64. }
  65. func (r *gateWayGroupIpRepository) GetGateWayGroupAllIpByGatewayGroupId(ctx context.Context, gatewayGroupId int) ([]string, error) {
  66. var res []string
  67. if err := r.DB(ctx).Model(&model.GateWayGroupIp{}).Where("gateway_group_id = ?", gatewayGroupId).Select("ip").Find(&res).Error; err != nil {
  68. return nil, err
  69. }
  70. return res, nil
  71. }
  72. func (r *gateWayGroupIpRepository) GetGatewayGroupIpList(ctx context.Context,req v1.SearchGatewayGroupIpParams) (*v1.PaginatedResponse[model.GateWayGroupIp], error) {
  73. var res []model.GateWayGroupIp
  74. var total int64
  75. query := r.db.WithContext(ctx).Model(&model.GateWayGroupIp{})
  76. if req.Ip != "" {
  77. trimmedName := strings.TrimSpace(req.Ip)
  78. // 使用 LIKE 进行模糊匹配
  79. query = query.Where("ip LIKE CONCAT('%', ?, '%')", trimmedName)
  80. }
  81. // 如果 RuleId 被提供了
  82. if req.GatewayGroupId != 0 {
  83. query = query.Where("gateway_group_id = ?", req.GatewayGroupId)
  84. }
  85. // 如果 Operator 被提供了
  86. if req.Operator != 0 {
  87. query = query.Where("operator = ?", req.Operator)
  88. }
  89. if req.Column != "" {
  90. if req.Column == "createTime" {
  91. query = query.Order("created_at" + " " + req.Order)
  92. }
  93. }
  94. if err := query.Count(&total).Error; err != nil {
  95. // 如果连计数都失败了,直接返回错误
  96. return nil, err
  97. }
  98. page := req.Current
  99. pageSize := req.PageSize
  100. if page <= 0 {
  101. page = 1
  102. }
  103. if pageSize <= 0 {
  104. pageSize = 10 // 默认每页 10 条
  105. } else if pageSize > 100 {
  106. pageSize = 100 // 每页最多 100 条
  107. }
  108. // 计算 offset (偏移量)
  109. // 例如,第 1 页,offset = (1-1)*10 = 0 (从第0条开始)
  110. // 第 2 页,offset = (2-1)*10 = 10 (从第10条开始)
  111. offset := (page - 1) * pageSize
  112. // 3. 执行最终的查询
  113. // 在所有条件都添加完毕后,再执行 .Find()
  114. result := query.Offset(offset).Limit(pageSize).Find(&res)
  115. if result.Error != nil {
  116. // 这里的错误可能是数据库连接问题等,而不是“未找到记录”
  117. return nil, result.Error
  118. }
  119. return &v1.PaginatedResponse[model.GateWayGroupIp]{
  120. Records: res,
  121. Page: page,
  122. PageSize: pageSize,
  123. Total: total,
  124. TotalPages: int(math.Ceil(float64(total) / float64(pageSize))),
  125. }, nil
  126. }