gatewaygroup.go 4.9 KB

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