gatewayipadmin.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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.Gatewayip{})
  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. if req.Comment != "" {
  56. trimmedName := strings.TrimSpace(req.Comment)
  57. // 使用 LIKE 进行模糊匹配
  58. query = query.Where("comment LIKE CONCAT('%', ?, '%')", trimmedName)
  59. }
  60. if req.Column != "" {
  61. if req.Column == "createTime" {
  62. query = query.Order("created_at" + " " + req.Order)
  63. }
  64. }
  65. if err := query.Count(&total).Error; err != nil {
  66. // 如果连计数都失败了,直接返回错误
  67. return nil, err
  68. }
  69. page := req.Current
  70. pageSize := req.PageSize
  71. if page <= 0 {
  72. page = 1
  73. }
  74. if pageSize <= 0 {
  75. pageSize = 10 // 默认每页 10 条
  76. } else if pageSize > 100 {
  77. pageSize = 100 // 每页最多 100 条
  78. }
  79. // 计算 offset (偏移量)
  80. // 例如,第 1 页,offset = (1-1)*10 = 0 (从第0条开始)
  81. // 第 2 页,offset = (2-1)*10 = 10 (从第10条开始)
  82. offset := (page - 1) * pageSize
  83. // 3. 执行最终的查询
  84. // 在所有条件都添加完毕后,再执行 .Find()
  85. result := query.Offset(offset).Limit(pageSize).Find(&res)
  86. if result.Error != nil {
  87. // 这里的错误可能是数据库连接问题等,而不是“未找到记录”
  88. return nil, result.Error
  89. }
  90. return &v1.PaginatedResponse[model.Gatewayip]{
  91. Records: res,
  92. Page: page,
  93. PageSize: pageSize,
  94. Total: total,
  95. TotalPages: int(math.Ceil(float64(total) / float64(pageSize))),
  96. }, nil
  97. }
  98. func (r *gatewayIpAdminRepository) AddGatewayIp(ctx context.Context,req model.Gatewayip) error {
  99. return r.DB(ctx).Create(&req).Error
  100. }
  101. func (r *gatewayIpAdminRepository) EditGatewayIp(ctx context.Context,req model.Gatewayip) error {
  102. return r.DB(ctx).Updates(&req).Error
  103. }
  104. func (r *gatewayIpAdminRepository) DeleteGatewayIp(ctx context.Context,id int64) error {
  105. return r.DB(ctx).Model(&model.Gatewayip{}).Where("id = ?", id).Delete(id).Error
  106. }
  107. func (r *gatewayIpAdminRepository) DeleteGatewayIps(ctx context.Context, ids []int64) error {
  108. return r.DB(ctx).Model(&model.Gatewayip{}).Where("id in ?", ids).Delete(ids).Error
  109. }