-
Notifications
You must be signed in to change notification settings - Fork 162
/
Copy pathannounce.go
132 lines (109 loc) · 3.08 KB
/
announce.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
// +----------------------------------------------------------------------
// | GoCMS 0.1
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2014 http://www.6574.com.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: zzdboy <[email protected]>
// +----------------------------------------------------------------------
package models
//公告管理
import "time"
import "admin/utils"
import "html/template"
import "github.com/revel/revel"
type Announce struct {
Id int64 `xorm:"pk"`
Title string `xorm:"varchar(255)"`
Content string `xorm:"text"`
Starttime string `xorm:"DateTime"`
Endtime string `xorm:"DateTime"`
Hits int64 `xorm:"int(11)"`
Status int64 `xorm:"default 1"`
Createtime string `xorm:"DateTime"`
}
//根据Id获取菜单信息
func (a *Announce) GetById(Id int64) *Announce {
announce := new(Announce)
has, err := DB_Read.Id(Id).Get(announce)
if err != nil {
revel.WARN.Println(has)
revel.WARN.Printf("错误: %v", err)
}
return announce
}
//获取公告列表
func (a *Announce) GetByAll(Page int64, Perpage int64) ([]*Announce, template.HTML) {
announce_list := []*Announce{}
//查询总数
announce := new(Announce)
Total, err := DB_Read.Count(announce)
if err != nil {
revel.WARN.Printf("错误: %v", err)
}
//分页
Pager := new(utils.Page)
Pager.SubPage_link = "/Announce/"
Pager.Nums = Total
Pager.Perpage = Perpage
Pager.Current_page = Page
Pager.SubPage_type = 2
pages := Pager.Show()
//查询数据
DB_Read.Limit(int(Perpage), int((Page-1)*Pager.Perpage)).Desc("id").Find(&announce_list)
return announce_list, pages
}
//添加公告
func (a *Announce) Save() bool {
announce := new(Announce)
announce.Title = a.Title
announce.Content = a.Content
announce.Starttime = a.Starttime
announce.Endtime = a.Endtime
announce.Hits = 0
announce.Status = a.Status
announce.Createtime = time.Now().Format("2006-01-02 15:04:05")
has, err := DB_Write.Insert(announce)
if err != nil {
revel.WARN.Println(has)
revel.WARN.Printf("错误: %v", err)
return false
}
return true
}
//编辑公告
func (a *Announce) Edit(Id int64) bool {
announce := new(Announce)
if len(a.Title) > 0 {
announce.Title = a.Title
}
if len(a.Content) > 0 {
announce.Content = a.Content
}
if len(a.Starttime) > 0 {
announce.Starttime = a.Starttime
}
if len(a.Endtime) > 0 {
announce.Endtime = a.Endtime
}
announce.Status = a.Status
has, err := DB_Write.Id(Id).Cols("title", "content", "starttime", "endtime", "status").Update(announce)
if err != nil {
revel.WARN.Println(has)
revel.WARN.Printf("错误: %v", err)
return false
}
return true
}
//删除公告
func (a *Announce) DelByID(Id int64) bool {
announce := new(Announce)
has, err := DB_Write.Id(Id).Delete(announce)
if err != nil {
revel.WARN.Println(has)
revel.WARN.Printf("错误: %v", err)
return false
}
return true
}