package project import ( "app/models" "gorm.io/gorm" ) type Demand struct { models.Tree DocID string `json:"doc_id" gorm:"column:doc_id;type:varchar(255);default:''"` ProjectID string `json:"project_id" gorm:"column:project_id;type:varchar(255);not null"` Description string `json:"description" gorm:"column:description;type:text"` ReqID string `json:"req_id" gorm:"column:req_id;type:varchar(50)"` ParentReqID string `json:"parent_req_id" gorm:"column:parent_req_id;type:varchar(50)"` Priority string `json:"priority" gorm:"column:priority;type:varchar(20)"` Type string `json:"type" gorm:"column:type;type:varchar(50)"` Status string `json:"status" gorm:"column:status;type:varchar(20)"` } func (Demand) TableName() string { return "project_demands" } func (t *Demand) Descendants(db *gorm.DB) ([]Demand, error) { var descendants []Demand // 使用GORM递归获取所有后代节点 var getAllDescendants func(parentID string) error getAllDescendants = func(parentID string) error { var children []Demand if err := db.Where("parent_id = ?", parentID).Find(&children).Error; err != nil { return err } for _, child := range children { descendants = append(descendants, child) if err := getAllDescendants(child.ID); err != nil { return err } } return nil } // 从当前节点开始递归 if err := getAllDescendants(t.ID); err != nil { return nil, err } return descendants, nil }