@@ -17,9 +17,11 @@ import (
1717 "treehole_next/utils"
1818)
1919
20- type Hole struct {
20+ type Hole = HoleV1
21+
22+ type BaseHole struct {
2123 /// saved fields
22- ID int `json:"id" gorm:"primaryKey"`
24+ ID int `json:"id" gorm:"primaryKey; "`
2325 CreatedAt time.Time `json:"time_created" gorm:"not null;index:idx_hole_div_cre,priority:2,sort:desc"`
2426 UpdatedAt time.Time `json:"time_updated" gorm:"not null;index:idx_hole_div_upd,priority:2,sort:desc"`
2527 DeletedAt gorm.DeletedAt `json:"time_deleted,omitempty" gorm:"index"`
@@ -54,7 +56,7 @@ type Hole struct {
5456 Tags Tags `json:"tags" gorm:"many2many:hole_tags;constraint:OnUpdate:CASCADE,OnDelete:CASCADE;"`
5557
5658 // 楼层列表
57- Floors Floors `json:"-" gorm:"constraint:OnUpdate:CASCADE,OnDelete:CASCADE;"`
59+ Floors Floors `json:"-" gorm:"foreignKey:HoleID;references:ID; constraint:OnUpdate:CASCADE,OnDelete:CASCADE;"`
5860
5961 // 匿名映射表
6062 Mapping Users `json:"-" gorm:"many2many:anonyname_mapping;constraint:OnUpdate:CASCADE,OnDelete:CASCADE;"`
@@ -66,20 +68,26 @@ type Hole struct {
6668
6769 // 兼容旧版 id
6870 HoleID int `json:"hole_id" gorm:"-:all"`
71+ }
72+
73+
74+ type HoleV1 struct {
75+ BaseHole
6976
7077 // 返回给前端的楼层列表,包括首楼、尾楼和预加载的前 n 个楼层
7178 HoleFloor struct {
7279 FirstFloor * Floor `json:"first_floor"` // 首楼
7380 LastFloor * Floor `json:"last_floor"` // 尾楼
7481 Floors Floors `json:"prefetch"` // 预加载的楼层
7582 } `json:"floors" gorm:"-:all"`
83+
7684}
7785
78- func (hole * Hole ) GetID () int {
86+ func (hole * BaseHole ) GetID () int {
7987 return hole .ID
8088}
8189
82- func (hole * Hole ) CacheName () string {
90+ func (hole * BaseHole ) CacheName () string {
8391 return fmt .Sprintf ("hole_%d" , hole .ID )
8492}
8593
@@ -97,7 +105,7 @@ func IsHolesExist(tx *gorm.DB, holeID []int) bool {
97105
98106const HoleCacheExpire = time .Minute * 10
99107
100- func loadTags (holes Holes ) ( err error ) {
108+ func (holes Holes )loadTags () error {
101109 if len (holes ) == 0 {
102110 return nil
103111 }
@@ -107,7 +115,7 @@ func loadTags(holes Holes) (err error) {
107115 }
108116
109117 var holeTags HoleTags
110- err = DB .Where ("hole_id in ?" , holeIDs ).Find (& holeTags ).Error
118+ err : = DB .Where ("hole_id in ?" , holeIDs ).Find (& holeTags ).Error
111119 if err != nil {
112120 return err
113121 }
@@ -144,7 +152,7 @@ func loadTags(holes Holes) (err error) {
144152 return nil
145153}
146154
147- func loadFloors (holes Holes ) error {
155+ func (holes Holes ) loadFloors ( ) error {
148156 if len (holes ) == 0 {
149157 return nil
150158 }
@@ -288,12 +296,12 @@ func (holes Holes) Preprocess(c *fiber.Ctx) error {
288296}
289297
290298func UpdateHoleCache (holes Holes ) (err error ) {
291- err = loadFloors (holes )
299+ err = holes . loadFloors ()
292300 if err != nil {
293301 return
294302 }
295303
296- err = loadTags (holes )
304+ err = holes . loadTags ()
297305 if err != nil {
298306 return
299307 }
@@ -351,7 +359,7 @@ func (holes Holes) MakeQuerySet(offset common.CustomTime, size int, order string
351359// set hole.HoleFloor from hole.Floors or hole.HoleFloor.Floors
352360// if Floors is not empty, set HoleFloor.Floors from Floors, in case loading from database
353361// if Floors is empty, set HoleFloor.Floors from HoleFloor.Floors, in case loading from cache
354- func (hole * Hole ) SetHoleFloor () {
362+ func (hole * HoleV1 ) SetHoleFloor () {
355363 if len (hole .Floors ) != 0 {
356364 holeFloorSize := len (hole .Floors )
357365
@@ -380,7 +388,7 @@ func (hole *Hole) SetHoleFloor() {
380388 //hole.HoleFloor.LastFloor.SetDefaults(c)
381389}
382390
383- func (hole * Hole ) Create (tx * gorm.DB , user * User , tagNames []string , c * fiber.Ctx ) (err error ) {
391+ func (hole * HoleV1 ) Create (tx * gorm.DB , user * User , tagNames []string , c * fiber.Ctx ) (err error ) {
384392 // Create hole.Tags, in different sql session
385393 hole .Tags , err = FindOrCreateTags (tx , user , tagNames )
386394 if err != nil {
@@ -453,12 +461,12 @@ func (hole *Hole) Create(tx *gorm.DB, user *User, tagNames []string, c *fiber.Ct
453461 return utils .SetCache (hole .CacheName (), hole , HoleCacheExpire )
454462}
455463
456- func (hole * Hole ) AfterCreate (_ * gorm.DB ) (err error ) {
464+ func (hole * BaseHole ) AfterCreate (_ * gorm.DB ) (err error ) {
457465 hole .HoleID = hole .ID
458466 return nil
459467}
460468
461- func (hole * Hole ) AfterFind (_ * gorm.DB ) (err error ) {
469+ func (hole * BaseHole ) AfterFind (_ * gorm.DB ) (err error ) {
462470 hole .HoleID = hole .ID
463471 return nil
464472}
@@ -473,7 +481,7 @@ func (holes Holes) RemoveIf(delCondition func(*Hole) bool) Holes {
473481 return result
474482}
475483
476- func (hole * Hole ) HoleHook () {
484+ func (hole * HoleV1 ) HoleHook () {
477485 if hole == nil {
478486 return
479487 }
0 commit comments