cciplist.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. package flexCdn
  2. import (
  3. "context"
  4. v1 "github.com/go-nunu/nunu-layout-advanced/api/v1"
  5. "github.com/go-nunu/nunu-layout-advanced/internal/repository"
  6. )
  7. type CcIpListRepository interface {
  8. GetCcIpList(ctx context.Context, serverId int64) ([]v1.CcIpListResponse, error)
  9. GetHttpWebId(ctx context.Context, serverId int64) (int64, error)
  10. GetIpListId(ctx context.Context, serverId int64,ipListType string) (int64,error)
  11. GetIpId(ctx context.Context, ipListId int64,ip string,sourceCategory string) (int64,error)
  12. GetCcIpCount(ctx context.Context,ipListId int64, ip string,sourceCategory string) (int64, error)
  13. }
  14. func NewCcIpListRepository(
  15. repository *repository.Repository,
  16. ) CcIpListRepository {
  17. return &ccIpListRepository{
  18. Repository: repository,
  19. }
  20. }
  21. type ccIpListRepository struct {
  22. *repository.Repository
  23. }
  24. func (r *ccIpListRepository) GetCcIpList(ctx context.Context, serverId int64) ([]v1.CcIpListResponse, error) {
  25. var req []v1.CcIpListResponse
  26. ipListId, err := r.GetIpListId(ctx, serverId,"white")
  27. if err != nil {
  28. return nil, err
  29. }
  30. return req, r.DBWithName(ctx,"cdn").Table("cloud_ip_items").Where("listId = ? AND sourceCategory = ? AND state = ?", ipListId, "cc", 1).Order("id desc").Select("value,type, reason").Scan(&req).Error
  31. }
  32. func (r *ccIpListRepository) GetHttpWebId(ctx context.Context, serverId int64) (int64, error) {
  33. var webId int64
  34. return webId, r.DBWithName(ctx,"cdn").Table("cloud_servers").Where("id = ?", serverId).Select("webId").Scan(&webId).Error
  35. }
  36. func (r *ccIpListRepository) GetIpListId(ctx context.Context, serverId int64,ipListType string) (int64,error) {
  37. var ipListId int64
  38. return ipListId, r.DBWithName(ctx,"cdn").Table("cloud_ip_lists").Where("serverId = ? AND type = ?", serverId,ipListType).Select("id").Scan(&ipListId).Error
  39. }
  40. func (r *ccIpListRepository) GetIpId(ctx context.Context, ipListId int64,ip string,sourceCategory string) (int64,error) {
  41. var ipId int64
  42. return ipId, r.DBWithName(ctx,"cdn").Table("cloud_ip_items").Where("listId = ? AND value = ? AND sourceCategory = ?", ipListId,ip,sourceCategory).Order("id desc").Limit(1).Select("id").Scan(&ipId).Error
  43. }
  44. func (r *ccIpListRepository) GetCcIpCount(ctx context.Context,ipListId int64, ip string,sourceCategory string) (int64, error) {
  45. var count int64
  46. return count, r.DBWithName(ctx,"cdn").Table("cloud_ip_items").Where("listId = ? AND value = ? AND sourceCategory = ? AND state = 1", ipListId,ip,sourceCategory).Count(&count).Error
  47. }