2017-06-09 18:14:55 +08:00
|
|
|
package models
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
2018-03-24 17:24:02 +08:00
|
|
|
|
2021-03-26 11:34:02 +08:00
|
|
|
"github.com/beego/beego/v2/client/orm"
|
2021-03-23 15:09:17 +08:00
|
|
|
"github.com/mindoc-org/mindoc/utils/filetil"
|
2017-06-09 18:14:55 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type AttachmentResult struct {
|
|
|
|
Attachment
|
2018-01-26 17:17:38 +08:00
|
|
|
IsExist bool
|
|
|
|
BookName string
|
|
|
|
DocumentName string
|
2017-06-09 18:14:55 +08:00
|
|
|
FileShortSize string
|
2018-01-26 17:17:38 +08:00
|
|
|
Account string
|
2017-06-09 18:14:55 +08:00
|
|
|
LocalHttpPath string
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewAttachmentResult() *AttachmentResult {
|
2018-01-26 17:17:38 +08:00
|
|
|
return &AttachmentResult{IsExist: false}
|
2017-06-09 18:14:55 +08:00
|
|
|
}
|
|
|
|
|
2018-01-26 17:17:38 +08:00
|
|
|
func (m *AttachmentResult) Find(id int) (*AttachmentResult, error) {
|
2017-06-09 18:14:55 +08:00
|
|
|
o := orm.NewOrm()
|
|
|
|
|
|
|
|
attach := NewAttachment()
|
|
|
|
|
2018-01-26 17:17:38 +08:00
|
|
|
err := o.QueryTable(m.TableNameWithPrefix()).Filter("attachment_id", id).One(attach)
|
2017-06-09 18:14:55 +08:00
|
|
|
|
|
|
|
if err != nil {
|
2018-01-26 17:17:38 +08:00
|
|
|
return m, err
|
2017-06-09 18:14:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
m.Attachment = *attach
|
|
|
|
|
2018-07-17 19:13:11 +08:00
|
|
|
if attach.BookId == 0 && attach.DocumentId > 0 {
|
|
|
|
blog := NewBlog()
|
2021-03-23 15:09:17 +08:00
|
|
|
if err := o.QueryTable(blog.TableNameWithPrefix()).Filter("blog_id", attach.DocumentId).One(blog, "blog_title"); err == nil {
|
2018-07-17 19:13:11 +08:00
|
|
|
m.BookName = blog.BlogTitle
|
2021-03-23 15:09:17 +08:00
|
|
|
} else {
|
2018-07-17 19:13:11 +08:00
|
|
|
m.BookName = "[文章不存在]"
|
|
|
|
}
|
2017-06-09 18:14:55 +08:00
|
|
|
} else {
|
2018-07-17 19:13:11 +08:00
|
|
|
book := NewBook()
|
2017-06-09 18:14:55 +08:00
|
|
|
|
2018-07-17 19:13:11 +08:00
|
|
|
if e := o.QueryTable(book.TableNameWithPrefix()).Filter("book_id", attach.BookId).One(book, "book_name"); e == nil {
|
|
|
|
m.BookName = book.BookName
|
|
|
|
} else {
|
|
|
|
m.BookName = "[不存在]"
|
|
|
|
}
|
|
|
|
doc := NewDocument()
|
2017-06-09 18:14:55 +08:00
|
|
|
|
2018-07-17 19:13:11 +08:00
|
|
|
if e := o.QueryTable(doc.TableNameWithPrefix()).Filter("document_id", attach.DocumentId).One(doc, "document_name"); e == nil {
|
|
|
|
m.DocumentName = doc.DocumentName
|
|
|
|
} else {
|
|
|
|
m.DocumentName = "[不存在]"
|
|
|
|
}
|
|
|
|
}
|
2017-06-09 18:14:55 +08:00
|
|
|
if attach.CreateAt > 0 {
|
|
|
|
member := NewMember()
|
2018-01-26 17:17:38 +08:00
|
|
|
if e := o.QueryTable(member.TableNameWithPrefix()).Filter("member_id", attach.CreateAt).One(member, "account"); e == nil {
|
2017-06-09 18:14:55 +08:00
|
|
|
m.Account = member.Account
|
|
|
|
}
|
|
|
|
}
|
2018-07-17 19:13:11 +08:00
|
|
|
|
2018-03-24 17:24:02 +08:00
|
|
|
m.FileShortSize = filetil.FormatBytes(int64(attach.FileSize))
|
2018-01-26 17:17:38 +08:00
|
|
|
m.LocalHttpPath = strings.Replace(m.FilePath, "\\", "/", -1)
|
2017-06-09 18:14:55 +08:00
|
|
|
|
2018-01-26 17:17:38 +08:00
|
|
|
return m, nil
|
|
|
|
}
|