host.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. package repository
  2. import (
  3. "context"
  4. v1 "github.com/go-nunu/nunu-layout-advanced/api/v1"
  5. "github.com/go-nunu/nunu-layout-advanced/internal/model"
  6. "time"
  7. )
  8. type HostRepository interface {
  9. GetHost(ctx context.Context, id int64) (*model.Host, error)
  10. GetHostConfig(ctx context.Context, hostId int) ([]*model.Host, error)
  11. GetProductConfigOption(ctx context.Context, id []int) ([]v1.ProductConfigOption, error)
  12. GetProductConfigOptionSub(ctx context.Context, id []int) ([]v1.ProductConfigOptionSub, error)
  13. GetDomainById(ctx context.Context, id int) (string, error)
  14. // 获取指定用户指定套餐的到期时间
  15. GetExpireTime(ctx context.Context, uid int64, hostId int64) (string, error)
  16. // 获取指定到期时间
  17. GetAlmostExpired(ctx context.Context, hostId []int,addTime int64) ([]v1.GetAlmostExpireHostResponse, error)
  18. // 获取到期时间区间
  19. GetExpireTimeRange(ctx context.Context,startTime int64,endTime int64) (int64, error)
  20. // 获取指定套餐的到期时间
  21. GetExpireTimeByHostId(ctx context.Context, hostIds []int64) ([]v1.GetAlmostExpireHostResponse, error)
  22. }
  23. func NewHostRepository(
  24. repository *Repository,
  25. ) HostRepository {
  26. return &hostRepository{
  27. Repository: repository,
  28. }
  29. }
  30. type hostRepository struct {
  31. *Repository
  32. }
  33. func (r *hostRepository) GetHost(ctx context.Context, id int64) (*model.Host, error) {
  34. var host model.Host
  35. return &host, nil
  36. }
  37. func (r *hostRepository) GetHostConfig(ctx context.Context, hostId int) ([]*model.Host, error) {
  38. var res []*model.Host
  39. if err := r.DB(ctx).Where("relid = ?", hostId).Find(&res).Error; err != nil {
  40. return nil, err
  41. }
  42. return res, nil
  43. }
  44. func (r *hostRepository) GetProductConfigOption(ctx context.Context, id []int) ([]v1.ProductConfigOption, error) {
  45. var res []v1.ProductConfigOption
  46. if err := r.DB(ctx).Table("shd_product_config_options").Where("id IN ?", id).Find(&res).Error; err != nil {
  47. return nil, err
  48. }
  49. return res, nil
  50. }
  51. func (r *hostRepository) GetProductConfigOptionSub(ctx context.Context, id []int) ([]v1.ProductConfigOptionSub, error) {
  52. var res []v1.ProductConfigOptionSub
  53. if err := r.DB(ctx).Table("shd_product_config_options_sub").Where("id IN ?", id).Find(&res).Error; err != nil {
  54. return nil, err
  55. }
  56. return res, nil
  57. }
  58. func (r *hostRepository) GetDomainById(ctx context.Context, id int) (string, error) {
  59. var res string
  60. if err := r.DB(ctx).Table("shd_host").
  61. Where("id = ?", id).
  62. Pluck("domain", &res).Error; err != nil {
  63. return "", err
  64. }
  65. return res, nil
  66. }
  67. // 获取到期时间
  68. func (r *hostRepository) GetExpireTime(ctx context.Context, uid int64, hostId int64) (string, error) {
  69. var nextDueDate string
  70. err := r.DB(ctx).Table("shd_host").
  71. Select("nextduedate").
  72. Where("id = ?", hostId).
  73. Where("uid = ?", uid).
  74. Scan(&nextDueDate).Error
  75. if err != nil {
  76. return "", err
  77. }
  78. return nextDueDate, nil
  79. }
  80. // 获取指定到期时间
  81. func (r *hostRepository) GetAlmostExpired(ctx context.Context, hostId []int,addTime int64) ([]v1.GetAlmostExpireHostResponse, error) {
  82. var res []v1.GetAlmostExpireHostResponse
  83. expiredTime := time.Now().Unix() + addTime
  84. if err := r.DB(ctx).Table("shd_host").
  85. Where("id IN ?", hostId).
  86. Where("nextduedate < ?", expiredTime).
  87. Select("id", "nextduedate").
  88. Scan(&res).Error; err != nil {
  89. return nil, err
  90. }
  91. return res, nil
  92. }
  93. // 获取到期时间区间
  94. func (r *hostRepository) GetExpireTimeRange(ctx context.Context,startTime int64,endTime int64) (int64, error) {
  95. var res int64
  96. if err := r.DB(ctx).Table("shd_host").
  97. Where("nextduedate > ?", startTime).
  98. Where("nextduedate < ?", endTime).
  99. Count(&res).Error; err != nil {
  100. return 0, err
  101. }
  102. return res, nil
  103. }
  104. // 获取指定套餐的到期时间
  105. func (r *hostRepository) GetExpireTimeByHostId(ctx context.Context, hostIds []int64) ([]v1.GetAlmostExpireHostResponse, error) {
  106. var res []v1.GetAlmostExpireHostResponse
  107. if err := r.DB(ctx).Table("shd_host").
  108. Where("id IN ?", hostIds).
  109. Select("id,nextduedate").
  110. Scan(&res).Error; err != nil {
  111. return nil, err
  112. }
  113. return res, nil
  114. }