gatewaygroupip.go 4.9 KB

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