Skip to content

Commit

Permalink
feat: delete floors when purge holes
Browse files Browse the repository at this point in the history
  • Loading branch information
JingYiJun committed Nov 15, 2023
1 parent ab62fb6 commit 2ebc938
Showing 1 changed file with 81 additions and 7 deletions.
88 changes: 81 additions & 7 deletions apis/hole/purge_hole.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,92 @@ import (
"time"

"github.com/rs/zerolog/log"
"gorm.io/gorm"
"gorm.io/gorm/clause"

"treehole_next/config"
. "treehole_next/models"
)

func purgeHole() error {
return DB.
Where("no_purge = ?", false).
Where("division_id IN ?", config.Config.HolePurgeDivisions).
Where("updated_at < ?",
time.Now().AddDate(0, 0, -config.Config.HolePurgeDays),
).Delete(&Hole{}).Error
func purgeHole() (err error) {
const REASON = "purge_hole"
const DELETE_CONTENT = "该内容已被删除"

return DB.Transaction(func(tx *gorm.DB) (err error) {

// load holeIDs, lock for update
var holeIDs []int
err = tx.Model(&Hole{}).
Clauses(clause.Locking{Strength: "UPDATE"}).
Where("no_purge = ?", false).
Where("division_id IN ?", config.Config.HolePurgeDivisions).
Where("updated_at < ?",
time.Now().AddDate(0, 0, -config.Config.HolePurgeDays),
).Pluck("id", &holeIDs).Error
if err != nil {
return err
}

if len(holeIDs) == 0 {
return nil
}

/* delete all floors in hole of holeIOs */

// get floors, lock for update
var floors []Floor
err = tx.
Clauses(clause.Locking{Strength: "UPDATE"}).
Where("hole_id IN ?", holeIDs).
Find(&floors).Error
if err != nil {
return err
}
if len(floors) == 0 {
return nil
}

// generate floorHistory
var floorHistorySlice = make([]FloorHistory, 0, len(floors))
for i := range floors {
floorHistorySlice = append(floorHistorySlice, FloorHistory{
Content: floors[i].Content,
Reason: REASON,
FloorID: floors[i].ID,
UserID: 1,
})
}
err = tx.Create(&floorHistorySlice).Error
if err != nil {
return err
}

// delete floors
var floorIDs = make([]int, 0, len(floors))
for i := range floors {
floorIDs = append(floorIDs, floors[i].ID)
}

err = tx.Model(&Floor{}).
Where("id IN ?", floorIDs).
Updates(map[string]any{
"deleted": true,
"content": DELETE_CONTENT,
}).Error
if err != nil {
return err
}

/* delete all holes in holeIDs */
err = tx.
Where("hole_id IN ?", holeIDs).
Delete(&Hole{}).Error
if err != nil {
return err
}

return nil
})
}

func PurgeHole(ctx context.Context) {
Expand Down

0 comments on commit 2ebc938

Please sign in to comment.