/
AttachModel.go
156 lines (145 loc) · 3.98 KB
/
AttachModel.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package models
import (
// "github.com/astaxie/beego"
"github.com/astaxie/beego/orm"
// _ "github.com/mattn/go-sqlite3"
// "strconv"
// "strings"
"time"
)
//附件,attachment 和 topic 是 ManyToOne 关系,也就是 ForeignKey 为 topic
type Attachment struct {
Id int64
FileName string
FileSize int64
Downloads int64
ProductId int64 //*Topic `orm:"rel(fk)"`
Created time.Time `orm:"auto_now_add;type(datetime)"`
Updated time.Time `orm:"auto_now;type(datetime)"`
}
func init() {
orm.RegisterModel(new(Attachment)) //, new(Article)
// orm.RegisterDriver("sqlite", orm.DRSqlite)
// orm.RegisterDataBase("default", "sqlite3", "database/engineer.db", 10)
}
//添加附件到成果id下
//如果附件名称已经存在,则不再追加写入数据库
//应该用ReadOrCreate尝试从数据库读取,不存在的话就创建一个
func AddAttachment(filename string, filesize, downloads, productid int64) (id int64, err error) {
o := orm.NewOrm()
var attach Attachment
err = o.QueryTable("Attachment").Filter("productid", productid).Filter("filename", filename).One(&attach)
if err == orm.ErrNoRows { // 没有找到记录
attachment := &Attachment{
FileName: filename,
FileSize: filesize,
Downloads: downloads,
ProductId: productid,
Created: time.Now(),
Updated: time.Now(),
}
id, err = o.Insert(attachment)
if err != nil {
return 0, err
}
} else if err == orm.ErrMultiRows {
return 0, err
} else if err == nil {
return attach.Id, err
}
return id, err
}
//修改_这个意义不大,对于附件的修改,一般是追加或删除
func UpdateAttachment(cid int64, filename string, filesize, downloads int64) (err error) {
o := orm.NewOrm()
attachment := &Attachment{Id: cid}
if o.Read(attachment) == nil {
attachment.FileName = filename
attachment.FileSize = filesize
attachment.Downloads = downloads
attachment.Updated = time.Now()
_, err := o.Update(attachment)
if err != nil {
return err
}
}
return err
}
//修改附件的日期和changesurl修改记录地址
func UpdateAttachmentTime(cid int64) (err error) {
o := orm.NewOrm()
attachment := &Attachment{Id: cid}
if o.Read(attachment) == nil {
attachment.Updated = time.Now()
_, err = o.Update(attachment, "Updated")
if err != nil {
return err
}
}
return err
}
//删除
func DeleteAttachment(cid int64) error {
o := orm.NewOrm()
attachment := &Attachment{Id: cid}
if o.Read(attachment) == nil {
_, err := o.Delete(attachment)
if err != nil {
return err
}
}
return nil
}
//根据成果id取得所有附件
func GetAttachments(id int64) (attachments []*Attachment, err error) {
o := orm.NewOrm()
qs := o.QueryTable("Attachment")
_, err = qs.Filter("Productid", id).All(&attachments)
if err != nil {
return nil, err
}
return attachments, err
}
//根据成果id取得所有附件数量
func GetAttachmentsCount(id int64) (cnt int64, err error) {
o := orm.NewOrm()
qs := o.QueryTable("Attachment")
cnt, err = qs.Filter("Productid", id).Count()
// cnt, err := o.QueryTable("user").Count()
// if err != nil {
// beego.Error(err)
// }
if err != nil {
return 0, err
}
return cnt, err
}
//根据名字title查询到附件
func GetAttachmentTitle(title string) (cate Attachment, err error) {
o := orm.NewOrm()
qs := o.QueryTable("Attachment")
// var cate Attachment
err = qs.Filter("title", title).One(&cate)
// if pid != "" {
// cate := Attachment{Title: title}这句无效
// categories = make([]*Attachment, 0)
// _, err = qs.Filter("parentid", cate.Id).All(&categories)
// if err != nil {
// return nil, err
// }
return cate, err
// } else { //如果不给定父id(PID=0),则取所有一级
// _, err = qs.Filter("parentid", 0).All(&categories)
// if err != nil {
// return nil, err
// }
// return categories, err
// }
}
//根据附件id查询附件
func GetAttachbyId(Id int64) (attach Attachment, err error) {
o := orm.NewOrm()
qs := o.QueryTable("Attachment")
err = qs.Filter("id", Id).One(&attach)
return attach, err
}