gatewaygroup.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. 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("rule_id = ?", req.RuleId).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,operator int, count int) (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(*) = ?", count)
  60. err := r.DB(ctx).Model(&model.GatewayGroup{}).
  61. Where("operator = ?", operator).
  62. Where("id IN (?)", subQuery).
  63. Where("host_id = ?", 0).
  64. Select("rule_id").First(&id).Error
  65. if err != nil {
  66. if errors.Is(err, gorm.ErrRecordNotFound){
  67. return 0, fmt.Errorf("库存不足,请联系客服补充网关组库存")
  68. }
  69. return 0, err
  70. }
  71. return id, nil
  72. }
  73. func (r *gatewayGroupRepository) GetGatewayGroupByHostId(ctx context.Context, hostId int64) (*[]model.GatewayGroup, error) {
  74. res := []model.GatewayGroup{}
  75. if err := r.DB(ctx).Where("host_id = ?", hostId).Find(&res).Error; err != nil {
  76. return nil, err
  77. }
  78. return &res, nil
  79. }
  80. func (r *gatewayGroupRepository) GetGatewayGroupByRuleId(ctx context.Context, ruleId int64) (*model.GatewayGroup, error) {
  81. res := model.GatewayGroup{}
  82. if err := r.DB(ctx).Where("rule_id = ?", ruleId).Find(&res).Error; err != nil {
  83. return nil, err
  84. }
  85. return &res, nil
  86. }
  87. func (r *gatewayGroupRepository) GetGatewayGroupList(ctx context.Context,req v1.SearchGatewayGroupParams) (*v1.PaginatedResponse[model.GatewayGroup], error) {
  88. var res []model.GatewayGroup
  89. var total int64
  90. query := r.db.WithContext(ctx).Model(&model.GatewayGroup{})
  91. if req.Name != "" {
  92. // 使用 LIKE 进行模糊匹配
  93. query = query.Where("name LIKE ?", fmt.Sprintf("%%%s%%", req.Name))
  94. }
  95. // 如果 HostId 被提供了,添加一个精确匹配条件
  96. if req.HostId != 0 {
  97. query = query.Where("host_id = ?", req.HostId)
  98. }
  99. // 如果 RuleId 被提供了
  100. if req.RuleId != 0 {
  101. query = query.Where("rule_id = ?", req.RuleId)
  102. }
  103. // 如果 Operator 被提供了
  104. if req.Operator != 0 {
  105. query = query.Where("operator = ?", req.Operator)
  106. }
  107. if req.Column != "" {
  108. if req.Column == "createTime" {
  109. query = query.Order("created_at" + " " + req.Order)
  110. }
  111. }
  112. if err := query.Count(&total).Error; err != nil {
  113. // 如果连计数都失败了,直接返回错误
  114. return nil, err
  115. }
  116. page := req.Current
  117. pageSize := req.PageSize
  118. if page <= 0 {
  119. page = 1
  120. }
  121. if pageSize <= 0 {
  122. pageSize = 10 // 默认每页 10 条
  123. } else if pageSize > 100 {
  124. pageSize = 100 // 每页最多 100 条
  125. }
  126. // 计算 offset (偏移量)
  127. // 例如,第 1 页,offset = (1-1)*10 = 0 (从第0条开始)
  128. // 第 2 页,offset = (2-1)*10 = 10 (从第10条开始)
  129. offset := (page - 1) * pageSize
  130. // 3. 执行最终的查询
  131. // 在所有条件都添加完毕后,再执行 .Find()
  132. result := query.Offset(offset).Limit(pageSize).Find(&res)
  133. if result.Error != nil {
  134. // 这里的错误可能是数据库连接问题等,而不是“未找到记录”
  135. return nil, result.Error
  136. }
  137. return &v1.PaginatedResponse[model.GatewayGroup]{
  138. Records: res,
  139. Page: page,
  140. PageSize: pageSize,
  141. Total: total,
  142. TotalPages: int(math.Ceil(float64(total) / float64(pageSize))),
  143. }, nil
  144. }
  145. func (r *gatewayGroupRepository) EditGatewayGroupById(ctx context.Context, req *model.GatewayGroup) error {
  146. if err := r.DB(ctx).Model(&model.GatewayGroup{}).Where("id = ?", req.Id).Updates(req).Error; err != nil {
  147. return err
  148. }
  149. return nil
  150. }