HomeService.java
10.0 KB
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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
package com.sinocontact.app.service;
import com.sinocontact.app.common.CommonConstant;
import com.sinocontact.app.dao.HomeMapper;
import com.sinocontact.app.entity.Chapter;
import com.sinocontact.app.entity.Comment;
import com.sinocontact.app.entity.Novel;
import com.sinocontact.app.entity.User;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
/**
* 公共业务处理
* @author caoMingYang
* @since 2019/4/25 16:12
**/
@Service
public class HomeService {
private static Logger logger = Logger.getLogger(HomeService.class);
@Autowired
private HomeMapper mapper;
/**
* 通过id获取小说信息
* @param novelId 小说id
* @return novel
* @author caoMingYang
* @since 2019/4/25 16:27
*/
public Novel getNovelById(Integer novelId){
try{
Novel novel = mapper.getNovelById(novelId);
this.formatNovelDate(novel);
return novel;
}catch (Exception e){
logger.error("通过id获取小说信息异常",e);
}
return null;
}
/**
* 增加点击
* @param novelId 小说id
* @return true 成功,false 失败
* @author caoMingYang
* @since 2019/4/26 0:26
*/
@Transactional
public boolean addClick(Integer novelId){
try{
Integer novelClick = mapper.getNovelClick(novelId);
Integer num = mapper.addClick(novelId,novelClick+1);
return num > 0 ? true : true;
}catch (Exception e){
logger.error("添加点击失败",e);
}
return false;
}
/**
* 通过小说id获取小说最新的五个章节
* @param novelId 小说id
* @return chapterList
* @author caoMingYang
* @since 2019/4/27 12:33
*/
public List<Chapter> queryFiveChapter(Integer novelId){
try{
return mapper.queryFiveChapter(novelId);
}catch (Exception e){
logger.error("通过小说id获取小说最新的五个章节异常",e);
}
return null;
}
/**
* 通过小说id获取小说评论
* @param novelId 小说id
* @return commentList
* @author caoMingYang
* @since 2019/4/27 17:42
*/
public List<Comment> getNovelComment(Integer novelId){
List<Comment> commentList = null;
try{
commentList = mapper.getNovelComment(novelId);
this.setCommentUserName(commentList);
}catch (Exception e){
logger.error("通过小说id获取用户评论异常",e);
}
return commentList;
}
/**
* 获取小说章节信息
* @param novelId 小说id
* @return chapterList
* @author caoMingYang
* @since 2019/4/27 22:36
*/
public List<Chapter> queryChapterList(Integer novelId){
List<Chapter> chapterList = null;
try{
chapterList = mapper.queryChapterList(novelId);
}catch (Exception e){
logger.error("获取小说章节异常",e);
}
return chapterList;
}
/**
* 通过id获取章节信息
* @param novelId 小说id
* @param chapterId 章节id
* @return chapter
* @author caoMingYang
* @since 2019/5/11 13:35
*/
public Chapter getChapter(Integer novelId,Integer chapterId){
try{
Chapter chapter = mapper.getChapterInfo(novelId,chapterId);
this.setChapterContent(chapter);
return chapter;
}catch (Exception e){
logger.error("获取章节信息异常",e);
}
return null;
}
/**
* 用户投推荐票
* @param novelId 小说id
* @param user 用户
* @return -2 推荐次数不足 ,-1 推荐失败,系统异常,0 推荐成功
* @author caoMingYang
* @since 2019/4/28 20:48
*/
@Transactional
public Integer updateRecommend(Integer novelId, User user) {
Integer recommendNum = 0;
try{
//获取用户当前剩余推荐次数
recommendNum = mapper.queryRecommendNumById(user.getUserId());
}catch (Exception e){
logger.error("",e);
}
//推荐次数大于0
if (recommendNum > 0) {
//推荐
Integer num = mapper.updateRecommendById(novelId);
Integer userNum = mapper.updateUserRecommendNum(user.getUserId());
if (num > 0 && userNum > 0) {
return 0;
}
}else{
logger.error("今日推荐次数不足");
return -2;
}
return -1;
}
/**
* 保存小说到用户书架中
* @param novelId 小说id
* @param user 用户信息
* @return -1 保存失败,0:小说已存在书架,1 保存成功
*/
public Integer addBookCase(Integer novelId,User user,Integer chapterId){
try {
Integer count = mapper.checkBookCase(novelId,user.getUserId(),chapterId);
if (count == 0) {
return mapper.addBookCase(novelId,user.getUserId(),chapterId);
}else{
return mapper.updateBookCase(novelId,user.getUserId(),chapterId);
}
}catch (Exception e){
logger.error("保存小说到书架异常",e);
}
return -1;
}
/**
* 获取上一章信息
* @param novelId 小说id
* @param chapterId 当前章id
* @return chapter
* @author caoMingYang
* @since 2019/5/11 18:42
*/
public Chapter previousChapter(Integer novelId,Integer chapterId){
try{
Integer num = mapper.previousChapterCount(novelId, chapterId);
if (num <= 0){
Chapter chapter = new Chapter();
chapter.setChapterId(-1);
return chapter;
}
Chapter chapter = mapper.previousChapter(novelId, chapterId);
this.setChapterContent(chapter);
return chapter;
}catch (Exception e){
logger.error("获取上一章章节信息异常",e);
}
return null;
}
/**
* 获取下一章信息
* @param novelId
* @param chapterId
* @return
*/
public Chapter nextChapter(Integer novelId,Integer chapterId){
try {
Integer num = mapper.nextChapterCount(novelId, chapterId);
if (num <= 0){
Chapter chapter = new Chapter();
chapter.setChapterId(-1);
return chapter;
}
Chapter chapter = mapper.nextChapter(novelId, chapterId);
this.setChapterContent(chapter);
return chapter;
}catch (Exception e){
logger.error("获取下一章章节信息异常",e);
}
return null;
}
/**
* 保存评论
* @param userId 用户id
* @param novelId 小说id
* @param comment 评论
* @return false 失败,true 成功
* @author caoMingYang
* @since 2019/5/25 13:55
*/
public boolean saveComment(Integer userId,Integer novelId,String comment){
try {
Integer num = mapper.saveComment(novelId,userId,comment);
return num > 0 ? true : false;
}catch (Exception e){
logger.error("",e);
}
return false;
}
/**
* 设置章节内容格式
* @param chapter 章节信息
* @author caoMingYang
* @since 2019/5/11 18:40
*/
private void setChapterContent(Chapter chapter){
if (null != chapter){
String chapterContent = chapter.getChapterContent();
if (!StringUtils.isEmpty(chapterContent)){
chapter.setChapterContent(chapterContent.replace(" ","<br><br> "));
}
}
}
/**
* 当评论人昵称为空时,设置默认名称
* @param commentList 评论
* @author caoMingYang
* @since 2019/4/27 17:42
*/
private void setCommentUserName(List<Comment> commentList)throws Exception{
if (null != commentList){
for (Comment comment : commentList){
if (StringUtils.isEmpty(comment.getNickname())){
comment.setNickname(CommonConstant.NICKNAME_IS_NULL);
}
String createTime = comment.getUserCommentTime().replace(".0","");
Date date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(createTime);
String result = new SimpleDateFormat("MM月dd日 HH:mm").format(date);
comment.setUserCommentTime(result);
}
}
}
/**
* 格式化小说罪行章节更新时间
* @param novel 小说
* @author caoMingYang
* @since 2019/4/25 16:26
*/
private void formatNovelDate(Novel novel)throws Exception{
if (null != novel){
if (null != novel.getLastChapterUpdate()){
String updateTime = novel.getLastChapterUpdate().replace(".0","");
Date date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(updateTime);
String result = new SimpleDateFormat("MM月dd日 HH:mm").format(date);
novel.setLastChapterUpdate(result);
}
}
}
/**
* 更新用户每日推荐次数
* @author caoMingYang
* @since 2019/5/18 0:22
*/
public Integer updateRecommendTimes(){
try {
return mapper.updateRecommendTimes();
}catch (Exception e){
logger.error("更新用户每日推荐次数异常",e);
}
return -1;
}
}