gameshield.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. package repository
  2. import (
  3. "context"
  4. "github.com/go-nunu/nunu-layout-advanced/internal/model"
  5. )
  6. type GameShieldRepository interface {
  7. GetGameShieldById(ctx context.Context, id int64) (*model.GameShield, error)
  8. GetGameShieldDuplicateName(ctx context.Context, appName string, uid int) (int64, error)
  9. AddGameShield(ctx context.Context, gameShield *model.GameShield) error
  10. UpdateGameShield(ctx context.Context, gameShield *model.GameShield) error
  11. DeleteGameShield(ctx context.Context, ruleId int) error
  12. GetGameShieldIsBuy(ctx context.Context, uid int64) (int64, error)
  13. GetGameShieldNextduedate(ctx context.Context, uid int64, productID string) (string, error)
  14. GetGameShieldExistingIps(ctx context.Context, ip string) ([]string, error)
  15. GetGameShieldNameByDunName(ctx context.Context, appName string) (string, error)
  16. GetGameShieldIdByDunName(ctx context.Context, id int64) (string, error)
  17. GetGameShieldRuleIdByAppName(ctx context.Context, appName string) (int, error)
  18. }
  19. func NewGameShieldRepository(
  20. repository *Repository,
  21. ) GameShieldRepository {
  22. return &gameShieldRepository{
  23. Repository: repository,
  24. }
  25. }
  26. type gameShieldRepository struct {
  27. *Repository
  28. }
  29. func (r *gameShieldRepository) GetGameShieldById(ctx context.Context, id int64) (*model.GameShield, error) {
  30. var res model.GameShield
  31. if err := r.DB(ctx).Where("id = ?", id).First(&res).Error; err != nil {
  32. return nil, err
  33. }
  34. return &res, nil
  35. }
  36. func (r *gameShieldRepository) GetGameShieldDuplicateName(ctx context.Context, appName string, uid int) (int64, error) {
  37. var count int64
  38. if err := r.DB(ctx).Model(&model.GameShield{}).Where("app_name = ?", appName).Where("uid = ?", uid).Count(&count).Error; err != nil {
  39. return 0, err
  40. }
  41. return count, nil
  42. }
  43. func (r *gameShieldRepository) AddGameShield(ctx context.Context, gameShield *model.GameShield) error {
  44. if err := r.DB(ctx).Create(gameShield).Error; err != nil {
  45. return err
  46. }
  47. return nil
  48. }
  49. func (r *gameShieldRepository) UpdateGameShield(ctx context.Context, gameShield *model.GameShield) error {
  50. if err := r.DB(ctx).Where("rule_id", gameShield.RuleId).Updates(gameShield).Error; err != nil {
  51. return err
  52. }
  53. return nil
  54. }
  55. func (r *gameShieldRepository) DeleteGameShield(ctx context.Context, ruleId int) error {
  56. if err := r.DB(ctx).Where("rule_id = ?", ruleId).Delete(&model.GameShield{}).Error; err != nil {
  57. return err
  58. }
  59. return nil
  60. }
  61. func (r *gameShieldRepository) GetGameShieldIsBuy(ctx context.Context, uid int64) (int64, error) {
  62. var count int64
  63. if err := r.DB(ctx).Table("shd_host").
  64. Where("domainstatus = ?", "Active").
  65. Where("productid = ?", 67).
  66. Where("uid = ?", uid).
  67. Count(&count).Error; err != nil {
  68. return 0, err
  69. }
  70. return count, nil
  71. }
  72. func (r *gameShieldRepository) GetGameShieldNextduedate(ctx context.Context, uid int64, productID string) (string, error) {
  73. var nextDueDate string
  74. err := r.DB(ctx).Table("shd_host").
  75. Select("nextduedate").
  76. Where("id = ?", productID).
  77. Where("uid = ?", uid).
  78. Scan(&nextDueDate).Error
  79. if err != nil {
  80. return "", err
  81. }
  82. return nextDueDate, nil
  83. }
  84. func (r *gameShieldRepository) GetGameShieldExistingIps(ctx context.Context, ip string) ([]string, error) {
  85. var res []string
  86. if err := r.DB(ctx).Model(&model.GameShield{}).
  87. Where("app_ip LIKE ?", ip+"%").
  88. Pluck("app_ip", &res).Error; err != nil {
  89. return nil, err
  90. }
  91. return res, nil
  92. }
  93. func (r *gameShieldRepository) GetGameShieldNameByDunName(ctx context.Context, appName string) (string, error) {
  94. var res string
  95. if err := r.DB(ctx).Model(&model.GameShield{}).
  96. Where("app_name = ?", appName).
  97. Pluck("dun_name", &res).Error; err != nil {
  98. return "", err
  99. }
  100. return res, nil
  101. }
  102. func (r *gameShieldRepository) GetGameShieldIdByDunName(ctx context.Context, id int64) (string, error) {
  103. var res string
  104. if err := r.DB(ctx).Model(&model.GameShield{}).
  105. Where("id = ?", id).
  106. Pluck("dun_name", &res).Error; err != nil {
  107. return "", err
  108. }
  109. return res, nil
  110. }
  111. func (r *gameShieldRepository) GetGameShieldRuleIdByAppName(ctx context.Context, appName string) (int, error) {
  112. var res int
  113. if err := r.DB(ctx).Model(&model.GameShield{}).
  114. Where("app_name = ?", appName).
  115. Pluck("rule_id", &res).Error; err != nil {
  116. return 0, err
  117. }
  118. return res, nil
  119. }