attachment_result.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. package models
  2. import (
  3. "github.com/astaxie/beego/orm"
  4. "github.com/lifei6671/mindoc/utils"
  5. "strings"
  6. )
  7. type AttachmentResult struct {
  8. Attachment
  9. IsExist bool
  10. BookName string
  11. DocumentName string
  12. FileShortSize string
  13. Account string
  14. LocalHttpPath string
  15. }
  16. func NewAttachmentResult() *AttachmentResult {
  17. return &AttachmentResult{IsExist: false}
  18. }
  19. func (m *AttachmentResult) Find(id int) (*AttachmentResult, error) {
  20. o := orm.NewOrm()
  21. attach := NewAttachment()
  22. err := o.QueryTable(m.TableNameWithPrefix()).Filter("attachment_id", id).One(attach)
  23. if err != nil {
  24. return m, err
  25. }
  26. m.Attachment = *attach
  27. book := NewBook()
  28. if e := o.QueryTable(book.TableNameWithPrefix()).Filter("book_id", attach.BookId).One(book, "book_name"); e == nil {
  29. m.BookName = book.BookName
  30. } else {
  31. m.BookName = "[不存在]"
  32. }
  33. doc := NewDocument()
  34. if e := o.QueryTable(doc.TableNameWithPrefix()).Filter("document_id", attach.DocumentId).One(doc, "document_name"); e == nil {
  35. m.DocumentName = doc.DocumentName
  36. } else {
  37. m.DocumentName = "[不存在]"
  38. }
  39. if attach.CreateAt > 0 {
  40. member := NewMember()
  41. if e := o.QueryTable(member.TableNameWithPrefix()).Filter("member_id", attach.CreateAt).One(member, "account"); e == nil {
  42. m.Account = member.Account
  43. }
  44. }
  45. m.FileShortSize = utils.FormatBytes(int64(attach.FileSize))
  46. m.LocalHttpPath = strings.Replace(m.FilePath, "\\", "/", -1)
  47. return m, nil
  48. }