gatewaygroup.go 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. package repository
  2. import (
  3. "context"
  4. "errors"
  5. "fmt"
  6. "github.com/go-nunu/nunu-layout-advanced/internal/model"
  7. "gorm.io/gorm"
  8. )
  9. type GatewayGroupRepository interface {
  10. GetGatewayGroup(ctx context.Context, id int64) (*model.GatewayGroup, error)
  11. AddGatewayGroup(ctx context.Context, req *model.GatewayGroup) error
  12. EditGatewayGroup(ctx context.Context, req *model.GatewayGroup) error
  13. DeleteGatewayGroup(ctx context.Context, req *model.GatewayGroup) error
  14. GetGatewayGroupWhereHostIdNull(ctx context.Context,operator int, count int) (int, error)
  15. GetGatewayGroupByHostId(ctx context.Context, hostId int64) (*[]model.GatewayGroup, error)
  16. GetGatewayGroupByRuleId(ctx context.Context, ruleId int64) (*model.GatewayGroup, error)
  17. }
  18. func NewGatewayGroupRepository(
  19. repository *Repository,
  20. ) GatewayGroupRepository {
  21. return &gatewayGroupRepository{
  22. Repository: repository,
  23. }
  24. }
  25. type gatewayGroupRepository struct {
  26. *Repository
  27. }
  28. func (r *gatewayGroupRepository) GetGatewayGroup(ctx context.Context, id int64) (*model.GatewayGroup, error) {
  29. var gatewayGroup model.GatewayGroup
  30. return &gatewayGroup, nil
  31. }
  32. func (r *gatewayGroupRepository) AddGatewayGroup(ctx context.Context, req *model.GatewayGroup) error {
  33. if err := r.DB(ctx).Create(req).Error; err != nil {
  34. return err
  35. }
  36. return nil
  37. }
  38. func (r *gatewayGroupRepository) EditGatewayGroup(ctx context.Context, req *model.GatewayGroup) error {
  39. if err := r.DB(ctx).Model(&model.GatewayGroup{}).Where("rule_id = ?", req.RuleId).Updates(req).Error; err != nil {
  40. return err
  41. }
  42. return nil
  43. }
  44. func (r *gatewayGroupRepository) DeleteGatewayGroup(ctx context.Context, req *model.GatewayGroup) error {
  45. if err := r.DB(ctx).Model(&model.GatewayGroup{}).Where("id = ?", req.Id).Delete(req).Error; err != nil {
  46. return err
  47. }
  48. return nil
  49. }
  50. func (r *gatewayGroupRepository) GetGatewayGroupWhereHostIdNull(ctx context.Context,operator int, count int) (int, error) {
  51. var id int
  52. subQuery := r.DB(ctx).Model(&model.GateWayGroupIp{}).
  53. Select("gateway_group_id").
  54. Group("gateway_group_id").
  55. Having("COUNT(*) = ?", count)
  56. err := r.DB(ctx).Model(&model.GatewayGroup{}).
  57. Where("operator = ?", operator).
  58. Where("id IN (?)", subQuery).
  59. Where("host_id = ?", 0).
  60. Select("rule_id").First(&id).Error
  61. if err != nil {
  62. if errors.Is(err, gorm.ErrRecordNotFound){
  63. return 0, fmt.Errorf("库存不足,请联系客服补充网关组库存")
  64. }
  65. return 0, err
  66. }
  67. return id, nil
  68. }
  69. func (r *gatewayGroupRepository) GetGatewayGroupByHostId(ctx context.Context, hostId int64) (*[]model.GatewayGroup, error) {
  70. res := []model.GatewayGroup{}
  71. if err := r.DB(ctx).Where("host_id = ?", hostId).Find(&res).Error; err != nil {
  72. return nil, err
  73. }
  74. return &res, nil
  75. }
  76. func (r *gatewayGroupRepository) GetGatewayGroupByRuleId(ctx context.Context, ruleId int64) (*model.GatewayGroup, error) {
  77. res := model.GatewayGroup{}
  78. if err := r.DB(ctx).Where("rule_id = ?", ruleId).Find(&res).Error; err != nil {
  79. return nil, err
  80. }
  81. return &res, nil
  82. }