document_tree.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. package models
  2. import (
  3. "github.com/astaxie/beego/orm"
  4. )
  5. type DocumentTree struct {
  6. DocumentId int `json:"id"`
  7. DocumentName string `json:"text"`
  8. ParentId interface{} `json:"parent"`
  9. Identify string `json:"identify"`
  10. Version int64 `json:"version"`
  11. State *DocumentSelected `json:"state,omitempty"`
  12. }
  13. type DocumentSelected struct {
  14. Selected bool `json:"selected"`
  15. Opened bool `json:"opened"`
  16. }
  17. func (m *Document) FindDocumentTree(book_id int) ([]*DocumentTree,error){
  18. o := orm.NewOrm()
  19. trees := make([]*DocumentTree,0)
  20. var docs []*Document
  21. count ,err := o.QueryTable(m).Filter("book_id",book_id).OrderBy("order_sort","document_id").All(&docs,"document_id","version","document_name","parent_id","identify")
  22. if err != nil {
  23. return trees,err
  24. }
  25. trees = make([]*DocumentTree,count)
  26. for index,item := range docs {
  27. tree := &DocumentTree{}
  28. if index == 0{
  29. tree.State = &DocumentSelected{ Selected: true, Opened: true }
  30. }
  31. tree.DocumentId = item.DocumentId
  32. tree.Identify = item.Identify
  33. tree.Version = item.Version
  34. if item.ParentId > 0 {
  35. tree.ParentId = item.ParentId
  36. }else{
  37. tree.ParentId = "#"
  38. }
  39. tree.DocumentName = item.DocumentName
  40. trees[index] = tree
  41. }
  42. return trees,nil
  43. }