gatewayipadmin.go 3.9 KB

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