gatewayipadmin.go 4.2 KB

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