gatewaygroup.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. 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,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("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) GetGatewayGroupList(ctx context.Context,req v1.SearchGatewayGroupParams) (*v1.PaginatedResponse[model.GatewayGroup], error) {
  81. var res []model.GatewayGroup
  82. var total int64
  83. query := r.db.WithContext(ctx).Model(&model.GatewayGroup{})
  84. if req.Name != "" {
  85. // 去除后所有的空白字符(包括空格、制表符\t、换行符等)
  86. trimmedName := strings.TrimSpace(req.Name)
  87. // 使用 LIKE 进行模糊匹配
  88. query = query.Where("name LIKE CONCAT('%', ?, '%')", trimmedName)
  89. }
  90. // 如果 HostId 被提供了,添加一个精确匹配条件
  91. if req.HostId != 0 {
  92. query = query.Where("host_id = ?", req.HostId)
  93. }
  94. // 如果 RuleId 被提供了
  95. if req.RuleId != 0 {
  96. query = query.Where("rule_id = ?", req.RuleId)
  97. }
  98. // 如果 Operator 被提供了
  99. if req.Operator != 0 {
  100. query = query.Where("operator = ?", req.Operator)
  101. }
  102. if req.Column != "" {
  103. if req.Column == "createTime" {
  104. query = query.Order("created_at" + " " + req.Order)
  105. }
  106. }
  107. if err := query.Count(&total).Error; err != nil {
  108. // 如果连计数都失败了,直接返回错误
  109. return nil, err
  110. }
  111. page := req.Current
  112. pageSize := req.PageSize
  113. if page <= 0 {
  114. page = 1
  115. }
  116. if pageSize <= 0 {
  117. pageSize = 10 // 默认每页 10 条
  118. } else if pageSize > 100 {
  119. pageSize = 100 // 每页最多 100 条
  120. }
  121. // 计算 offset (偏移量)
  122. // 例如,第 1 页,offset = (1-1)*10 = 0 (从第0条开始)
  123. // 第 2 页,offset = (2-1)*10 = 10 (从第10条开始)
  124. offset := (page - 1) * pageSize
  125. // 3. 执行最终的查询
  126. // 在所有条件都添加完毕后,再执行 .Find()
  127. result := query.Offset(offset).Limit(pageSize).Find(&res)
  128. if result.Error != nil {
  129. // 这里的错误可能是数据库连接问题等,而不是“未找到记录”
  130. return nil, result.Error
  131. }
  132. return &v1.PaginatedResponse[model.GatewayGroup]{
  133. Records: res,
  134. Page: page,
  135. PageSize: pageSize,
  136. Total: total,
  137. TotalPages: int(math.Ceil(float64(total) / float64(pageSize))),
  138. }, nil
  139. }
  140. func (r *gatewayGroupRepository) EditGatewayGroupById(ctx context.Context, req *model.GatewayGroup) error {
  141. if err := r.DB(ctx).Model(&model.GatewayGroup{}).Where("id = ?", req.Id).Updates(req).Error; err != nil {
  142. return err
  143. }
  144. return nil
  145. }