gatewayip.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. package repository
  2. import (
  3. "context"
  4. "fmt"
  5. v1 "github.com/go-nunu/nunu-layout-advanced/api/v1"
  6. "github.com/go-nunu/nunu-layout-advanced/internal/model"
  7. "gorm.io/gorm"
  8. "gorm.io/gorm/clause"
  9. )
  10. type GatewayipRepository interface {
  11. GetGatewayip(ctx context.Context, id int64) (*model.Gatewayip, error)
  12. AddGatewayip(ctx context.Context, req model.Gatewayip) error
  13. EditGatewayip(ctx context.Context, req model.Gatewayip) error
  14. DeleteGatewayip(ctx context.Context, req model.Gatewayip) error
  15. GetGatewayipByHostIdFirst(ctx context.Context, hostId int64) (string, error)
  16. GetGatewayipByHostIdAll(ctx context.Context, hostId int64) (*model.Gatewayip, error)
  17. UpdateGatewayipByHostId(ctx context.Context, req model.Gatewayip) error
  18. DeleteGatewayipByHostId(ctx context.Context, hostId int64) error
  19. GetIpWhereHostIdNull(ctx context.Context,req v1.GlobalLimitRequireResponse) ([]string,error)
  20. CleanIPByHostId(ctx context.Context, hostId []int64) error
  21. GetGatewayipOnlyIpByHostIdAll(ctx context.Context, hostId int64) ([]string, error)
  22. }
  23. func NewGatewayipRepository(
  24. repository *Repository,
  25. ) GatewayipRepository {
  26. return &gatewayipRepository{
  27. Repository: repository,
  28. }
  29. }
  30. type gatewayipRepository struct {
  31. *Repository
  32. }
  33. func (r *gatewayipRepository) GetGatewayip(ctx context.Context, id int64) (*model.Gatewayip, error) {
  34. var req model.Gatewayip
  35. return &req, r.DB(ctx).Where("id = ?", id).First(&req).Error
  36. }
  37. func (r *gatewayipRepository) AddGatewayip(ctx context.Context, req model.Gatewayip) error {
  38. return r.DB(ctx).Create(&req).Error
  39. }
  40. func (r *gatewayipRepository) EditGatewayip(ctx context.Context, req model.Gatewayip) error {
  41. return r.DB(ctx).Model(&model.Gatewayip{}).Where("id = ?", req.Id).Updates(req).Error
  42. }
  43. func (r *gatewayipRepository) DeleteGatewayip(ctx context.Context, req model.Gatewayip) error {
  44. return r.DB(ctx).Model(&model.Gatewayip{}).Where("id = ?", req.Id).Delete(req).Error
  45. }
  46. func (r *gatewayipRepository) GetGatewayipByHostIdFirst(ctx context.Context, hostId int64) (string, error) {
  47. var req string
  48. return req, r.DB(ctx).Model(&model.Gatewayip{}).Where("host_id = ?", hostId).Pluck("ip", &req).Error
  49. }
  50. func (r *gatewayipRepository) GetGatewayipByHostIdAll(ctx context.Context, hostId int64) (*model.Gatewayip, error) {
  51. var req model.Gatewayip
  52. return &req, r.DB(ctx).Where("host_id = ?", hostId).Find(&req).Error
  53. }
  54. func (r *gatewayipRepository) UpdateGatewayipByHostId(ctx context.Context, req model.Gatewayip) error {
  55. return r.DB(ctx).Where("host_id = ?", req.HostId).Updates(&req).Error
  56. }
  57. func (r *gatewayipRepository) DeleteGatewayipByHostId(ctx context.Context, hostId int64) error {
  58. return r.DB(ctx).Model(&model.Gatewayip{}).Where("host_id = ?", hostId).Delete(&model.Gatewayip{}).Error
  59. }
  60. func (r *gatewayipRepository) GetIpWhereHostIdNull(ctx context.Context,req v1.GlobalLimitRequireResponse) ([]string,error) {
  61. if req.IpCount <= 0 {
  62. return nil, fmt.Errorf("套餐IP数量错误, 请联系客服")
  63. }
  64. if req.HostId <= 0 {
  65. return nil, fmt.Errorf("主机ID错误, 请联系客服")
  66. }
  67. var count int64
  68. err := r.DB(ctx).Model(&model.Gatewayip{}).Where("host_id = ?", req.HostId).Count(&count).Error
  69. if err != nil {
  70. return nil, err
  71. }
  72. if count >= int64(req.IpCount) {
  73. return nil, nil // IP数量已足够,无需操作
  74. }
  75. neededIpCount := int(int64(req.IpCount) - count)
  76. // 这个切片仍然需要是 model.Gatewayip 类型,因为它需要临时持有从数据库查出的完整对象
  77. var assignedIPs []model.Gatewayip
  78. // 使用事务保证操作的原子性
  79. err = r.DB(ctx).Transaction(func(tx *gorm.DB) error {
  80. // 步骤 1: 查询并锁定所需数量的可用IP对象
  81. // 我们仍然需要完整的对象,因为后续更新需要用到 ID
  82. err := tx.Model(&model.Gatewayip{}).
  83. Clauses(clause.Locking{Strength: "UPDATE"}).
  84. Where("operator = ?", req.Operator).
  85. Where("ban_udp = ?", req.IsBanUdp).
  86. Where("ban_overseas = ?", req.IsBanOverseas).
  87. Where("node_area = ?", req.NodeArea).
  88. Where("host_id IS NULL OR host_id = 0").
  89. Order("id ASC").
  90. Limit(neededIpCount).
  91. Find(&assignedIPs).Error
  92. if err != nil {
  93. return err
  94. }
  95. // 步骤 2: 检查库存
  96. if len(assignedIPs) < neededIpCount {
  97. return fmt.Errorf("IP库存不足, 需要 %d 个, 实际可用 %d 个, 请联系客服补充", neededIpCount, len(assignedIPs))
  98. }
  99. if len(assignedIPs) == 0 {
  100. return nil
  101. }
  102. // 步骤 3: 提取ID用于更新
  103. var idsToUpdate []int
  104. for _, ip := range assignedIPs {
  105. idsToUpdate = append(idsToUpdate, ip.Id)
  106. }
  107. // 步骤 4: 更新这些IP的 host_id
  108. updateResult := tx.Model(&model.Gatewayip{}).
  109. Where("id IN ?", idsToUpdate).
  110. Update("host_id", req.HostId)
  111. if updateResult.Error != nil {
  112. return updateResult.Error
  113. }
  114. if updateResult.RowsAffected != int64(len(idsToUpdate)) {
  115. return fmt.Errorf("IP分配异常: 期望更新 %d 条记录, 实际更新了 %d 条", len(idsToUpdate), updateResult.RowsAffected)
  116. }
  117. return nil
  118. })
  119. // 事务执行后,检查是否有错误
  120. if err != nil {
  121. return nil, err
  122. }
  123. // 如果事务成功,且分配了IP (assignedIPs不为空)
  124. // *** 核心改动点 ***
  125. // 创建一个新的字符串切片,用于存放最终要返回的IP地址
  126. var ipStrings []string
  127. if len(assignedIPs) > 0 {
  128. ipStrings = make([]string, 0, len(assignedIPs)) // 预分配容量以提高性能
  129. for _, ip := range assignedIPs {
  130. ipStrings = append(ipStrings, ip.Ip)
  131. }
  132. }
  133. // 返回IP地址字符串切片和 nil 错误
  134. return ipStrings, nil
  135. }
  136. func (r *gatewayipRepository) CleanIPByHostId(ctx context.Context, hostId []int64) error {
  137. return r.DB(ctx).Model(&model.Gatewayip{}).Where("host_id IN ?", hostId).Update("host_id", 0).Error
  138. }
  139. func (r *gatewayipRepository) GetGatewayipOnlyIpByHostIdAll(ctx context.Context, hostId int64) ([]string, error) {
  140. var req []string
  141. return req, r.DB(ctx).Model(&model.Gatewayip{}).Where("host_id = ?", hostId).Pluck("ip", &req).Error
  142. }