| 
									
										
										
										
											2024-05-09 13:33:27 +08:00
										 |  |  |  | // 数据库模型. | 
					
						
							| 
									
										
										
										
											2017-04-24 18:25:17 +08:00
										 |  |  |  | package models | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | import ( | 
					
						
							|  |  |  |  | 	"time" | 
					
						
							| 
									
										
										
										
											2017-05-12 19:14:29 +08:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  | 	"os" | 
					
						
							| 
									
										
										
										
											2017-06-05 13:41:47 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-24 17:24:02 +08:00
										 |  |  |  | 	"strings" | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-26 11:34:02 +08:00
										 |  |  |  | 	"github.com/beego/beego/v2/client/orm" | 
					
						
							| 
									
										
										
										
											2021-03-26 14:26:19 +08:00
										 |  |  |  | 	"github.com/beego/beego/v2/core/logs" | 
					
						
							| 
									
										
										
										
											2021-03-23 15:09:17 +08:00
										 |  |  |  | 	"github.com/mindoc-org/mindoc/conf" | 
					
						
							|  |  |  |  | 	"github.com/mindoc-org/mindoc/utils/filetil" | 
					
						
							| 
									
										
										
										
											2024-05-09 13:33:27 +08:00
										 |  |  |  | 	// "gorm.io/driver/sqlite" | 
					
						
							|  |  |  |  | 	// "gorm.io/gorm" | 
					
						
							|  |  |  |  | 	// "gorm.io/gorm/logger" | 
					
						
							|  |  |  |  | 	// "gorm.io/gorm/schema" | 
					
						
							| 
									
										
										
										
											2017-04-24 18:25:17 +08:00
										 |  |  |  | ) | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-05-09 13:33:27 +08:00
										 |  |  |  | // 定义全局的db对象,我们执行数据库操作主要通过他实现。 | 
					
						
							|  |  |  |  | // var _db *gorm.DB | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-24 18:25:17 +08:00
										 |  |  |  | // Attachment struct . | 
					
						
							|  |  |  |  | type Attachment struct { | 
					
						
							| 
									
										
										
										
											2017-05-12 19:14:29 +08:00
										 |  |  |  | 	AttachmentId int       `orm:"column(attachment_id);pk;auto;unique" json:"attachment_id"` | 
					
						
							| 
									
										
										
										
											2022-10-28 12:33:36 +08:00
										 |  |  |  | 	BookId       int       `orm:"column(book_id);type(int);description(所属book id)" json:"book_id"` | 
					
						
							|  |  |  |  | 	DocumentId   int       `orm:"column(document_id);type(int);null;description(所属文档id)" json:"doc_id"` | 
					
						
							|  |  |  |  | 	FileName     string    `orm:"column(file_name);size(255);description(文件名称)" json:"file_name"` | 
					
						
							|  |  |  |  | 	FilePath     string    `orm:"column(file_path);size(2000);description(文件路径)" json:"file_path"` | 
					
						
							|  |  |  |  | 	FileSize     float64   `orm:"column(file_size);type(float);description(文件大小 字节)" json:"file_size"` | 
					
						
							|  |  |  |  | 	HttpPath     string    `orm:"column(http_path);size(2000);description(文件路径)" json:"http_path"` | 
					
						
							|  |  |  |  | 	FileExt      string    `orm:"column(file_ext);size(50);description(文件后缀)" json:"file_ext"` | 
					
						
							|  |  |  |  | 	CreateTime   time.Time `orm:"type(datetime);column(create_time);auto_now_add;description(创建时间)" json:"create_time"` | 
					
						
							|  |  |  |  | 	CreateAt     int       `orm:"column(create_at);type(int);description(创建人id)" json:"create_at"` | 
					
						
							| 
									
										
										
										
											2024-07-05 15:31:34 +08:00
										 |  |  |  | 	ResourceType string    `orm:"-" json:"resource_type"` | 
					
						
							| 
									
										
										
										
											2017-04-24 18:25:17 +08:00
										 |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-28 12:33:36 +08:00
										 |  |  |  | // TableName 获取对应上传附件数据库表名. | 
					
						
							| 
									
										
										
										
											2017-04-24 18:25:17 +08:00
										 |  |  |  | func (m *Attachment) TableName() string { | 
					
						
							|  |  |  |  | 	return "attachment" | 
					
						
							|  |  |  |  | } | 
					
						
							| 
									
										
										
										
											2017-05-12 19:14:29 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-24 18:25:17 +08:00
										 |  |  |  | // TableEngine 获取数据使用的引擎. | 
					
						
							|  |  |  |  | func (m *Attachment) TableEngine() string { | 
					
						
							|  |  |  |  | 	return "INNODB" | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | func (m *Attachment) TableNameWithPrefix() string { | 
					
						
							|  |  |  |  | 	return conf.GetDatabasePrefix() + m.TableName() | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-12 19:14:29 +08:00
										 |  |  |  | func NewAttachment() *Attachment { | 
					
						
							| 
									
										
										
										
											2017-04-24 18:25:17 +08:00
										 |  |  |  | 	return &Attachment{} | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-12 19:14:29 +08:00
										 |  |  |  | func (m *Attachment) Insert() error { | 
					
						
							| 
									
										
										
										
											2017-04-24 18:25:17 +08:00
										 |  |  |  | 	o := orm.NewOrm() | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-12 19:14:29 +08:00
										 |  |  |  | 	_, err := o.Insert(m) | 
					
						
							| 
									
										
										
										
											2017-04-24 18:25:17 +08:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  | 	return err | 
					
						
							|  |  |  |  | } | 
					
						
							| 
									
										
										
										
											2017-04-27 18:19:37 +08:00
										 |  |  |  | func (m *Attachment) Update() error { | 
					
						
							|  |  |  |  | 	o := orm.NewOrm() | 
					
						
							| 
									
										
										
										
											2017-05-12 19:14:29 +08:00
										 |  |  |  | 	_, err := o.Update(m) | 
					
						
							| 
									
										
										
										
											2017-04-27 18:19:37 +08:00
										 |  |  |  | 	return err | 
					
						
							|  |  |  |  | } | 
					
						
							| 
									
										
										
										
											2017-04-24 18:25:17 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-12 19:14:29 +08:00
										 |  |  |  | func (m *Attachment) Delete() error { | 
					
						
							| 
									
										
										
										
											2017-04-24 18:25:17 +08:00
										 |  |  |  | 	o := orm.NewOrm() | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-05 13:41:47 +08:00
										 |  |  |  | 	_, err := o.Delete(m) | 
					
						
							| 
									
										
										
										
											2017-04-24 18:25:17 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-12 19:14:29 +08:00
										 |  |  |  | 	if err == nil { | 
					
						
							|  |  |  |  | 		if err1 := os.Remove(m.FilePath); err1 != nil { | 
					
						
							| 
									
										
										
										
											2021-03-23 21:55:50 +08:00
										 |  |  |  | 			logs.Error(err1) | 
					
						
							| 
									
										
										
										
											2017-05-12 19:14:29 +08:00
										 |  |  |  | 		} | 
					
						
							|  |  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-04-24 18:25:17 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-12 19:14:29 +08:00
										 |  |  |  | 	return err | 
					
						
							|  |  |  |  | } | 
					
						
							| 
									
										
										
										
											2017-04-24 18:25:17 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-12 19:14:29 +08:00
										 |  |  |  | func (m *Attachment) Find(id int) (*Attachment, error) { | 
					
						
							|  |  |  |  | 	if id <= 0 { | 
					
						
							|  |  |  |  | 		return m, ErrInvalidParameter | 
					
						
							|  |  |  |  | 	} | 
					
						
							|  |  |  |  | 	o := orm.NewOrm() | 
					
						
							| 
									
										
										
										
											2017-04-24 18:25:17 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-12 19:14:29 +08:00
										 |  |  |  | 	err := o.QueryTable(m.TableNameWithPrefix()).Filter("attachment_id", id).One(m) | 
					
						
							| 
									
										
										
										
											2017-04-24 18:25:17 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-12 19:14:29 +08:00
										 |  |  |  | 	return m, err | 
					
						
							|  |  |  |  | } | 
					
						
							| 
									
										
										
										
											2021-03-23 15:09:17 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-05-09 13:33:27 +08:00
										 |  |  |  | // 查询指定文档的附件列表 | 
					
						
							| 
									
										
										
										
											2018-07-17 19:13:11 +08:00
										 |  |  |  | func (m *Attachment) FindListByDocumentId(docId int) (attaches []*Attachment, err error) { | 
					
						
							| 
									
										
										
										
											2017-05-12 19:14:29 +08:00
										 |  |  |  | 	o := orm.NewOrm() | 
					
						
							| 
									
										
										
										
											2017-04-24 18:25:17 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-23 15:09:17 +08:00
										 |  |  |  | 	_, err = o.QueryTable(m.TableNameWithPrefix()).Filter("document_id", docId).Filter("book_id__gt", 0).OrderBy("-attachment_id").All(&attaches) | 
					
						
							| 
									
										
										
										
											2017-05-12 19:14:29 +08:00
										 |  |  |  | 	return | 
					
						
							|  |  |  |  | } | 
					
						
							| 
									
										
										
										
											2017-06-09 18:14:55 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-05-09 13:33:27 +08:00
										 |  |  |  | // 分页查询附件 | 
					
						
							| 
									
										
										
										
											2018-01-26 18:07:55 +08:00
										 |  |  |  | func (m *Attachment) FindToPager(pageIndex, pageSize int) (attachList []*AttachmentResult, totalCount int, err error) { | 
					
						
							| 
									
										
										
										
											2017-06-09 18:14:55 +08:00
										 |  |  |  | 	o := orm.NewOrm() | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-26 18:07:55 +08:00
										 |  |  |  | 	total, err := o.QueryTable(m.TableNameWithPrefix()).Count() | 
					
						
							| 
									
										
										
										
											2017-06-09 18:14:55 +08:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2018-01-26 18:07:55 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-24 17:24:02 +08:00
										 |  |  |  | 		return nil, 0, err | 
					
						
							| 
									
										
										
										
											2017-06-09 18:14:55 +08:00
										 |  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-01-26 18:07:55 +08:00
										 |  |  |  | 	totalCount = int(total) | 
					
						
							| 
									
										
										
										
											2017-06-09 18:14:55 +08:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  | 	var list []*Attachment | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-12-19 13:19:31 +08:00
										 |  |  |  | 	offset := (pageIndex - 1) * pageSize | 
					
						
							|  |  |  |  | 	if pageSize == 0 { | 
					
						
							|  |  |  |  | 		_, err = o.QueryTable(m.TableNameWithPrefix()).OrderBy("-attachment_id").Offset(offset).Limit(pageSize).All(&list) | 
					
						
							|  |  |  |  | 	} else { | 
					
						
							|  |  |  |  | 		_, err = o.QueryTable(m.TableNameWithPrefix()).OrderBy("-attachment_id").All(&list) | 
					
						
							|  |  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-06-09 18:14:55 +08:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2018-08-07 17:19:56 +08:00
										 |  |  |  | 		if err == orm.ErrNoRows { | 
					
						
							| 
									
										
										
										
											2021-03-23 21:55:50 +08:00
										 |  |  |  | 			logs.Info("没有查到附件 ->", err) | 
					
						
							| 
									
										
										
										
											2018-08-07 17:19:56 +08:00
										 |  |  |  | 			err = nil | 
					
						
							|  |  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2017-06-09 18:14:55 +08:00
										 |  |  |  | 		return | 
					
						
							|  |  |  |  | 	} | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 	for _, item := range list { | 
					
						
							|  |  |  |  | 		attach := &AttachmentResult{} | 
					
						
							|  |  |  |  | 		attach.Attachment = *item | 
					
						
							| 
									
										
										
										
											2018-03-24 17:24:02 +08:00
										 |  |  |  | 		attach.FileShortSize = filetil.FormatBytes(int64(attach.FileSize)) | 
					
						
							| 
									
										
										
										
											2018-07-17 19:13:11 +08:00
										 |  |  |  | 		//当项目ID为0标识是文章的附件 | 
					
						
							|  |  |  |  | 		if item.BookId == 0 && item.DocumentId > 0 { | 
					
						
							|  |  |  |  | 			blog := NewBlog() | 
					
						
							| 
									
										
										
										
											2021-03-23 15:09:17 +08:00
										 |  |  |  | 			if err := o.QueryTable(blog.TableNameWithPrefix()).Filter("blog_id", item.DocumentId).One(blog, "blog_title"); err == nil { | 
					
						
							| 
									
										
										
										
											2018-07-17 19:13:11 +08:00
										 |  |  |  | 				attach.BookName = blog.BlogTitle | 
					
						
							| 
									
										
										
										
											2021-03-23 15:09:17 +08:00
										 |  |  |  | 			} else { | 
					
						
							| 
									
										
										
										
											2018-07-17 19:13:11 +08:00
										 |  |  |  | 				attach.BookName = "[文章不存在]" | 
					
						
							|  |  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2021-03-23 15:09:17 +08:00
										 |  |  |  | 		} else { | 
					
						
							| 
									
										
										
										
											2018-07-17 19:13:11 +08:00
										 |  |  |  | 			book := NewBook() | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 			if e := o.QueryTable(book.TableNameWithPrefix()).Filter("book_id", item.BookId).One(book, "book_name"); e == nil { | 
					
						
							|  |  |  |  | 				attach.BookName = book.BookName | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 				doc := NewDocument() | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 				if e := o.QueryTable(doc.TableNameWithPrefix()).Filter("document_id", item.DocumentId).One(doc, "document_name"); e == nil { | 
					
						
							|  |  |  |  | 					attach.DocumentName = doc.DocumentName | 
					
						
							|  |  |  |  | 				} else { | 
					
						
							|  |  |  |  | 					attach.DocumentName = "[文档不存在]" | 
					
						
							|  |  |  |  | 				} | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 			} else { | 
					
						
							|  |  |  |  | 				attach.BookName = "[项目不存在]" | 
					
						
							|  |  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2017-06-09 18:14:55 +08:00
										 |  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2018-01-26 17:17:38 +08:00
										 |  |  |  | 		attach.LocalHttpPath = strings.Replace(item.FilePath, "\\", "/", -1) | 
					
						
							| 
									
										
										
										
											2017-06-09 18:14:55 +08:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  | 		attachList = append(attachList, attach) | 
					
						
							|  |  |  |  | 	} | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 	return | 
					
						
							|  |  |  |  | } |