gatewaygroup.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. package repository
  2. import (
  3. "context"
  4. "errors"
  5. "fmt"
  6. v1 "github.com/go-nunu/nunu-layout-advanced/api/v1"
  7. "github.com/go-nunu/nunu-layout-advanced/internal/model"
  8. "gorm.io/gorm"
  9. "math"
  10. "strings"
  11. )
  12. type GatewayGroupRepository interface {
  13. GetGatewayGroup(ctx context.Context, id int64) (*model.GatewayGroup, error)
  14. AddGatewayGroup(ctx context.Context, req *model.GatewayGroup) error
  15. EditGatewayGroup(ctx context.Context, req *model.GatewayGroup) error
  16. DeleteGatewayGroup(ctx context.Context, id int) error
  17. GetGatewayGroupWhereHostIdNull(ctx context.Context, req v1.GlobalLimitRequireResponse) (int, error)
  18. GetGatewayGroupByHostId(ctx context.Context, hostId int64) (*model.GatewayGroup, error)
  19. GetGatewayGroupList(ctx context.Context,req v1.SearchGatewayGroupParams) (*v1.PaginatedResponse[model.GatewayGroup], error)
  20. EditGatewayGroupById(ctx context.Context, req *model.GatewayGroup) error
  21. }
  22. func NewGatewayGroupRepository(
  23. repository *Repository,
  24. ) GatewayGroupRepository {
  25. return &gatewayGroupRepository{
  26. Repository: repository,
  27. }
  28. }
  29. type gatewayGroupRepository struct {
  30. *Repository
  31. }
  32. func (r *gatewayGroupRepository) GetGatewayGroup(ctx context.Context, id int64) (*model.GatewayGroup, error) {
  33. var gatewayGroup model.GatewayGroup
  34. return &gatewayGroup, nil
  35. }
  36. func (r *gatewayGroupRepository) AddGatewayGroup(ctx context.Context, req *model.GatewayGroup) error {
  37. if err := r.DB(ctx).Create(req).Error; err != nil {
  38. return err
  39. }
  40. return nil
  41. }
  42. func (r *gatewayGroupRepository) EditGatewayGroup(ctx context.Context, req *model.GatewayGroup) error {
  43. if err := r.DB(ctx).Model(&model.GatewayGroup{}).Where("id = ?", req.Id).Updates(req).Error; err != nil {
  44. return err
  45. }
  46. return nil
  47. }
  48. func (r *gatewayGroupRepository) DeleteGatewayGroup(ctx context.Context, id int) error {
  49. if err := r.DB(ctx).Model(&model.GatewayGroup{}).Where("id = ?", id).Delete(&model.GatewayGroup{}).Error; err != nil {
  50. return err
  51. }
  52. return nil
  53. }
  54. func (r *gatewayGroupRepository) GetGatewayGroupWhereHostIdNull(ctx context.Context,req v1.GlobalLimitRequireResponse) (int, error) {
  55. var id int
  56. subQuery := r.DB(ctx).Model(&model.GateWayGroupIp{}).
  57. Select("gateway_group_id").
  58. Group("gateway_group_id").
  59. Having("COUNT(*) = ?", req.IpCount)
  60. err := r.DB(ctx).Model(&model.GatewayGroup{}).
  61. Where("operator = ?", req.Operator).
  62. Where("ban_udp", req.IsBanUdp).
  63. Where("ban_overseas", req.IsBanOverseas).
  64. Where("id IN (?)", subQuery).
  65. Where("host_id = ?", 0).
  66. Select("id").First(&id).Error
  67. if err != nil {
  68. if errors.Is(err, gorm.ErrRecordNotFound){
  69. return 0, fmt.Errorf("库存不足,请联系客服补充网关组库存")
  70. }
  71. return 0, err
  72. }
  73. return id, nil
  74. }
  75. func (r *gatewayGroupRepository) GetGatewayGroupByHostId(ctx context.Context, hostId int64) (*model.GatewayGroup, error) {
  76. res := model.GatewayGroup{}
  77. if err := r.DB(ctx).Where("host_id = ?", hostId).Find(&res).Error; err != nil {
  78. return nil, err
  79. }
  80. return &res, nil
  81. }
  82. func (r *gatewayGroupRepository) GetGatewayGroupList(ctx context.Context,req v1.SearchGatewayGroupParams) (*v1.PaginatedResponse[model.GatewayGroup], error) {
  83. var res []model.GatewayGroup
  84. var total int64
  85. query := r.Db.WithContext(ctx).Model(&model.GatewayGroup{})
  86. if req.Name != "" {
  87. // 去除后所有的空白字符(包括空格、制表符\t、换行符等)
  88. trimmedName := strings.TrimSpace(req.Name)
  89. // 使用 LIKE 进行模糊匹配
  90. query = query.Where("name LIKE CONCAT('%', ?, '%')", trimmedName)
  91. }
  92. // 如果 HostId 被提供了,添加一个精确匹配条件
  93. if req.HostId != 0 {
  94. query = query.Where("host_id = ?", req.HostId)
  95. }
  96. // 如果 RuleId 被提供了
  97. if req.RuleId != 0 {
  98. query = query.Where("rule_id = ?", req.RuleId)
  99. }
  100. // 如果 Operator 被提供了
  101. if req.Operator != 0 {
  102. query = query.Where("operator = ?", req.Operator)
  103. }
  104. if req.Column != "" {
  105. if req.Column == "createTime" {
  106. query = query.Order("created_at" + " " + req.Order)
  107. }
  108. }
  109. if err := query.Count(&total).Error; err != nil {
  110. // 如果连计数都失败了,直接返回错误
  111. return nil, err
  112. }
  113. page := req.Current
  114. pageSize := req.PageSize
  115. if page <= 0 {
  116. page = 1
  117. }
  118. if pageSize <= 0 {
  119. pageSize = 10 // 默认每页 10 条
  120. } else if pageSize > 100 {
  121. pageSize = 100 // 每页最多 100 条
  122. }
  123. // 计算 offset (偏移量)
  124. // 例如,第 1 页,offset = (1-1)*10 = 0 (从第0条开始)
  125. // 第 2 页,offset = (2-1)*10 = 10 (从第10条开始)
  126. offset := (page - 1) * pageSize
  127. // 3. 执行最终的查询
  128. // 在所有条件都添加完毕后,再执行 .Find()
  129. result := query.Offset(offset).Limit(pageSize).Find(&res)
  130. if result.Error != nil {
  131. // 这里的错误可能是数据库连接问题等,而不是“未找到记录”
  132. return nil, result.Error
  133. }
  134. return &v1.PaginatedResponse[model.GatewayGroup]{
  135. Records: res,
  136. Page: page,
  137. PageSize: pageSize,
  138. Total: total,
  139. TotalPages: int(math.Ceil(float64(total) / float64(pageSize))),
  140. }, nil
  141. }
  142. func (r *gatewayGroupRepository) EditGatewayGroupById(ctx context.Context, req *model.GatewayGroup) error {
  143. if err := r.DB(ctx).Model(&model.GatewayGroup{}).Where("id = ?", req.Id).Updates(req).Error; err != nil {
  144. return err
  145. }
  146. return nil
  147. }