host.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. func NewHostRepository(
  20. repository *Repository,
  21. ) HostRepository {
  22. return &hostRepository{
  23. Repository: repository,
  24. }
  25. }
  26. type hostRepository struct {
  27. *Repository
  28. }
  29. func (r *hostRepository) GetHost(ctx context.Context, id int64) (*model.Host, error) {
  30. var host model.Host
  31. return &host, nil
  32. }
  33. func (r *hostRepository) GetHostConfig(ctx context.Context, hostId int) ([]*model.Host, error) {
  34. var res []*model.Host
  35. if err := r.DB(ctx).Where("relid = ?", hostId).Find(&res).Error; err != nil {
  36. return nil, err
  37. }
  38. return res, nil
  39. }
  40. func (r *hostRepository) GetProductConfigOption(ctx context.Context, id []int) ([]v1.ProductConfigOption, error) {
  41. var res []v1.ProductConfigOption
  42. if err := r.DB(ctx).Table("shd_product_config_options").Where("id IN ?", id).Find(&res).Error; err != nil {
  43. return nil, err
  44. }
  45. return res, nil
  46. }
  47. func (r *hostRepository) GetProductConfigOptionSub(ctx context.Context, id []int) ([]v1.ProductConfigOptionSub, error) {
  48. var res []v1.ProductConfigOptionSub
  49. if err := r.DB(ctx).Table("shd_product_config_options_sub").Where("id IN ?", id).Find(&res).Error; err != nil {
  50. return nil, err
  51. }
  52. return res, nil
  53. }
  54. func (r *hostRepository) GetDomainById(ctx context.Context, id int) (string, error) {
  55. var res string
  56. if err := r.DB(ctx).Table("shd_host").
  57. Where("id = ?", id).
  58. Pluck("domain", &res).Error; err != nil {
  59. return "", err
  60. }
  61. return res, nil
  62. }
  63. // 获取到期时间
  64. func (r *hostRepository) GetExpireTime(ctx context.Context, uid int64, hostId int64) (string, error) {
  65. var nextDueDate string
  66. err := r.DB(ctx).Table("shd_host").
  67. Select("nextduedate").
  68. Where("id = ?", hostId).
  69. Where("uid = ?", uid).
  70. Scan(&nextDueDate).Error
  71. if err != nil {
  72. return "", err
  73. }
  74. return nextDueDate, nil
  75. }
  76. // 获取指定到期时间
  77. func (r *hostRepository) GetAlmostExpired(ctx context.Context, hostId []int,addTime int64) ([]v1.GetAlmostExpireHostResponse, error) {
  78. var res []v1.GetAlmostExpireHostResponse
  79. expiredTime := time.Now().Unix() + addTime
  80. if err := r.DB(ctx).Table("shd_host").
  81. Where("id IN ?", hostId).
  82. Where("nextduedate < ?", expiredTime).
  83. Find(&res).Error; err != nil {
  84. return nil, err
  85. }
  86. return res, nil
  87. }