forked from vckai/novel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
novel.go
300 lines (247 loc) · 7.28 KB
/
novel.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
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
// Copyright 2017 Vckai Author. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package models
import (
"fmt"
"strings"
"time"
"github.com/astaxie/beego/orm"
)
const (
// 小说状态
BOOKOPEN uint8 = iota + 1
BOOKFINISH
BOOKEUNUCHS
)
type ArgsNovelList struct {
ArgsBase
StartTextNum int
EndTextNum int
MaxChapterUpdatedAt int64
FilterMaps map[string]int
}
// 小说内容
type Novel struct {
Id uint32 `orm:"auto"`
Name string `orm:"size(100)"`
Desc string `orm:"size(2555)"`
Cover string `orm:"size(100);"`
CateId uint32 `orm:"size(11);default(0);"`
CateName string `orm:"size(30);"`
Author string `orm:"size(30);"`
IsOriginal uint8 `orm:"size(1);default(0);"`
IsHot uint8 `orm:"size(1);default(0);"`
IsRec uint8 `orm:"size(1);default(0);"`
IsTodayRec uint8 `orm:"size(1);default(0);"`
IsVipRec uint8 `orm:"size(1);default(0);"`
IsVipReward uint8 `orm:"size(1);default(0);"`
IsVipUp uint8 `orm:"size(1);default(0);"`
IsSignNewBook uint8 `orm:"size(1);default(0);"`
IsCollect uint8 `orm:"size(1);default(0);"`
Status uint8 `orm:"size(11);default(0);"`
Views uint32 `orm:"size(11);default(0);"`
CollectNum uint32 `orm:"size(11);default(0)"`
RecNum uint32 `orm:"size(11);default(0)"`
TextNum uint32 `orm:"size(11);default(0)"`
ChapterNum uint32 `orm:"size(6);default(0)"`
ChapterUpdatedAt uint32 `orm:"size(11);default(0);"`
ChapterId uint64 `orm:"size(20);default(0);"`
ChapterTitle string `orm:"size(100);"`
CreatedAt uint32 `orm:"size(11);default(0);"`
UpdatedAt uint32 `orm:"size(11);default(0);"`
DeletedAt uint32 `orm:"size(11);default(0)"`
}
func NewNovel() *Novel {
return &Novel{}
}
// 初始化
// 注册模型
func init() {
orm.RegisterModelWithPrefix(TABLE_PREFIX, new(Novel))
}
func (m *Novel) query() orm.QuerySeter {
return orm.NewOrm().QueryTable(m)
}
// 添加
func (m *Novel) Insert() error {
m.CreatedAt = uint32(time.Now().Unix())
m.UpdatedAt = uint32(time.Now().Unix())
if _, err := orm.NewOrm().Insert(m); err != nil {
return err
}
return nil
}
// 读取
func (m *Novel) Read(fields ...string) error {
if err := orm.NewOrm().Read(m, fields...); err != nil {
return err
}
return nil
}
// 修改
func (m *Novel) Update(fields ...string) error {
m.UpdatedAt = uint32(time.Now().Unix())
if len(fields) > 0 {
fields = append(fields, "updated_at")
}
if _, err := orm.NewOrm().Update(m, fields...); err != nil {
return err
}
return nil
}
// 批量更新推荐
func (m *Novel) UpRecBatch(field string, books []string) error {
marks := make([]string, len(books))
for i := range marks {
marks[i] = "?"
}
// 取消之前推荐
sqlStr := fmt.Sprintf("UPDATE %snovel SET %s=0, updated_at=? WHERE `%s`=1", TABLE_PREFIX, field, field)
orm.NewOrm().Raw(sqlStr, time.Now().Unix()).Exec()
// 更新新书推荐
sqlStr = fmt.Sprintf("UPDATE %snovel SET %s=1, updated_at=? WHERE `name` IN(%s)", TABLE_PREFIX, field, strings.Join(marks, ", "))
_, err := orm.NewOrm().Raw(sqlStr, time.Now().Unix(), books).Exec()
return err
}
// 批量删除
func (m *Novel) DeleteBatch(ids []string) error {
marks := make([]string, len(ids))
for i := range marks {
marks[i] = "?"
}
sqlStr := fmt.Sprintf("DELETE FROM %snovel WHERE `id` IN(%s)", TABLE_PREFIX, strings.Join(marks, ", "))
_, err := orm.NewOrm().Raw(sqlStr, ids).Exec()
return err
}
// 删除
func (m *Novel) Delete(forceDelete ...bool) error {
// 软删除
if len(forceDelete) == 0 {
m.DeletedAt = uint32(time.Now().Unix())
return m.Update("deleted_at")
}
// 强制删除
if _, err := orm.NewOrm().Delete(m); err != nil {
return err
}
return nil
}
// 判断小说是否存在
func (m *Novel) GetByName(name string) *Novel {
var n Novel
if len(name) == 0 {
return &Novel{}
}
m.query().Filter("name", name).One(&n, "id", "author", "cover", "desc", "created_at")
return &n
}
// 获取小说列表
func (m *Novel) GetAll(args ArgsNovelList) ([]*Novel, int64) {
list := make([]*Novel, 0)
qs := m.optionHandle(args)
// 统计
var count int64 = 0
if args.Count {
count, _ = qs.Count()
}
// 排序
orderBy := "-id"
if args.OrderBy != "" {
orderBy = args.OrderBy
}
// 获取字段
fields := []string{"id", "name", "author", "cate_id", "cate_name", "status", "is_original", "is_hot", "is_rec", "is_vip_rec", "chapter_updated_at", "chapter_num"}
if len(args.Fields) > 0 {
fields = args.Fields
}
// 分页
if args.Limit > 0 {
qs = qs.Limit(args.Limit, args.Offset)
}
if count > 0 || args.Count == false {
qs.OrderBy(orderBy).All(&list, fields...)
}
return list, count
}
func (m Novel) StatusList() map[uint8]string {
return map[uint8]string{
BOOKOPEN: "连载中",
BOOKFINISH: "完结",
BOOKEUNUCHS: "太监",
}
}
// 获取状态
func (m Novel) StatusName() string {
status := m.StatusList()
if text, ok := status[m.Status]; ok {
return text
}
return "未开始"
}
// 获取是否原创小说
func (m Novel) IsOriginalName() string {
if m.IsOriginal == 0 {
return `<span class="layui-btn layui-btn-primary layui-btn-xs">否</span>`
}
return `<span class="layui-btn layui-btn-xs">是</span>`
}
// 获取是否热门
func (m Novel) IsHotName() string {
if m.IsHot == 0 {
return `<span class="layui-btn layui-btn-primary layui-btn-xs">否</span>`
}
return `<span class="layui-btn layui-btn-xs">是</span>`
}
// 获取是否推荐
func (m Novel) IsRecName() string {
if m.IsRec == 0 {
return `<span class="layui-btn layui-btn-primary layui-btn-xs">否</span>`
}
return `<span class="layui-btn layui-btn-xs">是</span>`
}
// 获取是否精品
func (m Novel) IsVipRecName() string {
if m.IsVipRec == 0 {
return `<span class="layui-btn layui-btn-primary layui-btn-xs">否</span>`
}
return `<span class="layui-btn layui-btn-xs">是</span>`
}
// 条件生成
func (m *Novel) optionHandle(args ArgsNovelList) orm.QuerySeter {
qs := m.query()
qs = qs.Filter("deleted_at", 0)
// 名称搜索
if args.Keyword != "" {
qs = qs.Filter("name__contains", args.Keyword)
}
argsFilters := []string{"status", "is_original", "is_hot", "is_rec", "is_vip_rec", "is_today_rec", "cate_id", "is_sign_new_book", "is_collect", "is_vip_up", "is_vip_reward"}
for _, v := range argsFilters {
if c, ok := args.FilterMaps[v]; ok && c > 0 {
qs = qs.Filter(v, c)
}
}
// 字数过滤
if args.StartTextNum > 0 {
qs = qs.Filter("text_num__gt", args.StartTextNum)
}
// 字数过滤
if args.EndTextNum > 0 {
qs = qs.Filter("text_num__lte", args.EndTextNum)
}
// 章节更新时间过滤
if args.MaxChapterUpdatedAt > 0 {
qs = qs.Filter("chapter_updated_at__gt", args.MaxChapterUpdatedAt)
}
return qs
}