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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package service
import (
"gin-vue-admin/global"
"gin-vue-admin/model"
"gin-vue-admin/model/request"
"gin-vue-admin/model/response"
"strconv"
)
// 获取书列表
func GetReadBooks() (error, *[]model.ReadBook) {
bookList := make([]model.ReadBook, 0)
field := " id, name, author, description, image_url, create_time "
table := " read_books "
conditions := " AND delflag = 0 "
orderby := " create_time desc "
sqlStr := "SELECT " + field +
" FROM " + table +
" WHERE 1>0 " + conditions +
" ORDER BY " + orderby
global.GVA_DB.Raw(sqlStr).Scan(&bookList)
if global.GVA_DB.Error != nil {
return global.GVA_DB.Error, nil
}
return nil, &bookList
}
// 获取目录
func GetReadCatalog(bookId int) (error, []*model.ReadCatalog) {
readCatalogs := make([]*model.ReadCatalog, 0)
field := " dc.id, dc.read_books_id, dc.name, dc.parent_id, dc.sort_order, dc.create_time, dc.update_time" +
", IF(rc.content!='',1,0) as content "
table := " read_catalog as dc LEFT JOIN read_contents AS rc ON dc.id = rc.read_catalog_id AND rc.delflag = 0 "
conditions := " AND dc.read_books_id = ? AND dc.delflag = 0 "
orderby := " dc.sort_order asc"
sqlStr := "SELECT " + field +
" FROM " + table +
" WHERE 1>0 " + conditions +
" ORDER BY " + orderby
global.GVA_DB.Raw(sqlStr, bookId).Scan(&readCatalogs)
if global.GVA_DB.Error != nil {
return global.GVA_DB.Error, nil
}
return nil, readCatalogs
}
// 获取文章
func GetReadContent(cataId int) (error, *model.ReadContents) {
var content model.ReadContents
global.GVA_DB.Where("read_catalog_id = ? and delflag = 0", cataId).Find(&content)
if global.GVA_DB.Error != nil {
return global.GVA_DB.Error, nil
}
return nil, &content
}
// 获取文章 (admin端)
func GetAdminReadContent(req request.GetReadContentReq) (err error, list []response.ReadContentsRes, total int64) {
content := make([]response.ReadContentsRes, 0)
pagesize := 10
page := 1
if req.PageSize != 0 {
pagesize = req.PageSize
}
if req.Page != 0 {
page = req.Page
}
currentpage := pagesize * (page - 1)
table := " read_contents as dc " +
" left join read_catalog as dl on dc.read_catalog_id = dl.id " +
" LEFT JOIN read_books as db on db.id = dl.read_books_id "
field := " db.name as book_name, dl.name as catalog_name, dl.id as read_catalog_id, dc.id, db.id as book_id, dc.keywords"
conditions := " AND dc.delflag=0 "
orderby := " dl.id asc"
if req.Name != "" {
conditions += " AND dl.name like '%" + req.Name + "%'"
}
if req.ReadBooksId != 0 {
conditions += " AND db.id = " + strconv.Itoa(req.ReadBooksId)
}
//@@总条数,总页数
var totalItem int64 = 0
sqlStr := "SELECT count(dc.id) as totalItem FROM " + table + " where 1=1 " + conditions
global.GVA_DB.Raw(sqlStr).Count(&totalItem) //获取总条数
sqlStr2 := "SELECT " + field +
" FROM " + table +
" where 1>0 " + conditions +
" ORDER BY " + orderby +
" LIMIT " + strconv.Itoa(currentpage) + "," + strconv.Itoa(pagesize)
global.GVA_DB.Raw(sqlStr2).Scan(&content)
if global.GVA_DB.Error != nil {
return global.GVA_DB.Error, nil, totalItem
}
return nil, content, totalItem
}
// 创建目录
func CreateReadCatalog(req request.CreateReadCatalog) error {
sqlStr := "INSERT INTO read_catalog(read_books_id, name, parent_id, sort_order) VALUE(?,?,?,?)"
global.GVA_DB.Exec(sqlStr, req.ReadBooksId, req.Name, req.ParentId, req.SortOrder)
return global.GVA_DB.Error
}
// 创建文章
func CreateReadContent(req request.CreateReadContent) error {
sqlStr := "INSERT INTO read_contents(read_catalog_id, content, keywords) VALUE(?,?,?)"
global.GVA_DB.Exec(sqlStr, req.ReadCatalogId, req.Content, req.Keywords)
return global.GVA_DB.Error
}
// 更新目录
func UpdateReadCatalog(req request.CreateReadCatalog) error {
err := global.GVA_DB.Table("read_catalog").Where("id=?", req.Id).Updates(&req).Error
return err
}
// 更新文章
func UpdateReadContent(req request.CreateReadContent) error {
err := global.GVA_DB.Table("read_contents").Where("id=?", req.Id).Updates(&req).Error
return err
}
// 删除目录
func DeleteReadCatalog(req request.CreateReadCatalog) error {
sqlStr := "UPDATE read_catalog SET delflag=? WHERE id=?"
global.GVA_DB.Exec(sqlStr, 1, req.Id)
return global.GVA_DB.Error
}
// 删除文章
func DeleteReadContent(req request.CreateReadContent) error {
sqlStr := "UPDATE read_contents SET delflag=? WHERE id=?"
global.GVA_DB.Exec(sqlStr, 1, req.Id)
return global.GVA_DB.Error
}
// 获取浏览记录
func GetReadHistory(req request.GetReadHistoryReq) (error, *[]model.ReadHistory) {
list := make([]model.ReadHistory, 0)
field := " id, user_id, read_book_id, read_history_id, current, create_time "
table := " read_history "
conditions := " AND user_id = ? AND read_book_id = ? "
orderby := " create_time desc "
sqlStr := "SELECT " + field +
" FROM " + table +
" WHERE 1>0 " + conditions +
" ORDER BY " + orderby
global.GVA_DB.Raw(sqlStr, req.UserId, req.ReadBookId).Scan(&list)
if global.GVA_DB.Error != nil {
return global.GVA_DB.Error, nil
}
return nil, &list
}
// 新增浏览记录
func CreateReadHistory(req request.CreateReadHistory) error {
sqlStr := "INSERT INTO read_history(read_book_id, user_id, read_history_id, current) VALUE(?,?,?,?)"
global.GVA_DB.Exec(sqlStr, req.ReadBookId, req.UserId, req.ReadHistoryId, req.Current)
return global.GVA_DB.Error
}
// 更新浏览记录
func UpdateReadHistory(req request.CreateReadHistory) error {
err := global.GVA_DB.Table("read_history").Where("id=?", req.Id).Updates(&req).Error
return err
}