gatewaygroup.go 5.4 KB

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