TKzyCourseServiceImpl.java
11.5 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
328
329
330
331
332
333
334
335
package com.server.web.common.service.Impl;
import com.server.utils.weixin.WeixinBaseUtil;
import com.server.utils.weixin.bean.AccessTokenBean;
import com.server.web.common.mapper.TKzyCommentMapper;
import com.server.web.common.mapper.TKzyCourseMapper;
import com.server.web.common.mapper.TKzyUserMapper;
import com.server.web.common.mapping.CommentMapper;
import com.server.web.common.mapping.CourseMapper;
import com.server.web.common.model.TKzyComment;
import com.server.web.common.model.TKzyCourse;
import com.server.web.common.service.TKzyCourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import redis.clients.jedis.JedisPool;
import java.awt.*;
import java.awt.font.FontRenderContext;
import java.awt.geom.AffineTransform;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.List;
import java.util.concurrent.TimeUnit;
/**
* 课程相关
* Created by weiwenfu@163.com on 2018/12/5 下午 1:45.
*/
@Service
public class TKzyCourseServiceImpl implements TKzyCourseService {
@Autowired
private TKzyCourseMapper tKzyCourseMapper;
@Autowired
private TKzyCommentMapper tKzyCommentMapper;
@Autowired
private TKzyUserMapper tKzyUserMapper;
@Autowired
private CommentMapper commentMapper;
@Autowired
private CourseMapper courseMapper;
private static final int ERCORD_WIDTH = 600;
private static final int ERCORD_HEIGHT = 600;
@Value("${remoteFileUrl}")
private String remoteFileUrl;
@Value("${upyun.baseDir}")
private String upyunBaseDir;
@Value("${upyun.bucket}")
private String upyunBucket;
@Value("${upyun.username}")
private String upyunUsername;
@Value("${upyun.password}")
private String upyunPassword;
@Value("${wx.http.url}")
private String WX_HTTP_URL;
@Value("${wx.app.secret}")
private String appSecret;
@Value("${wx.app.id}")
private String appId;
@Autowired
RedisTemplate redisTemplate;
/**
* 热门课程
*
* @return
*/
@Override
public Map<String, Object> hotCourse() {
Map<String, Object> resultMap = new HashMap<String, Object>();
resultMap.put("status", "1");
resultMap.put("message", "");
Map<String, Object> param = new HashMap<>();
param.put("limit", 4);
List<Map<String, Object>> list = courseMapper.hotCourse(param);
resultMap.put("data", list);
return resultMap;
}
/**
* 直播课
*
* @return
*/
@Override
public Map<String, Object> livingCourse() {
Map<String, Object> resultMap = new HashMap<String, Object>();
resultMap.put("status", "1");
resultMap.put("message", "");
Map<String, Object> param = new HashMap<>();
List<Map<String, Object>> list = courseMapper.livingCourse(param);
resultMap.put("data", list);
return resultMap;
}
/**
* 录播课
*
* @param pageNo
* @param pageSize
* @return
*/
@Override
public Map<String, Object> recordCourse(Long typeId, Integer pageNo, Integer pageSize) {
Map<String, Object> resultMap = new HashMap<String, Object>();
resultMap.put("status", "1");
resultMap.put("message", "");
if (pageNo == null || pageNo <= 0) {
pageNo = 1;
}
if (pageSize == null || pageSize <= 0) {
pageSize = 10;
}
Map<String, Object> param = new HashMap<>();
param.put("start", (pageNo - 1) * pageSize);
param.put("pageSize", pageSize);
// param.put("typeId", typeId);
List<Map<String, Object>> list = courseMapper.recordCourse(param);
resultMap.put("data", list);
return resultMap;
}
@Override
public Map<String, Object> livingCourseDetail(Long courseId, Long userId) {
Map<String, Object> resultMap = new HashMap<String, Object>();
resultMap.put("status", "0");
resultMap.put("message", "");
if (courseId == null || courseId <= 0l) {
resultMap.put("message", "课程ID不能为空或有误~");
return resultMap;
}
TKzyCourse tKzyCourse = tKzyCourseMapper.selectByPrimaryKey(courseId);
if (tKzyCourse == null) {
resultMap.put("message", "未查到该课程信息,请确认~");
return resultMap;
}
resultMap.put("status", "1");
Map<String, Object> courseMap = new HashMap<>();
courseMap.put("courseName", tKzyCourse.getCourseName());
courseMap.put("courseIntroduce", tKzyCourse.getCourseIntroduce());
courseMap.put("startDt", tKzyCourse.getStartDt());
courseMap.put("endDt", tKzyCourse.getEndDt());
courseMap.put("picUrl", tKzyCourse.getPicUrl());
courseMap.put("liveUrl", tKzyCourse.getLiveUrl());
courseMap.put("courseType", tKzyCourse.getCourseType());
resultMap.put("data", courseMap);
return resultMap;
}
@Override
public Map<String, Object> recordCourseDetail(Long courseId, Long userId) {
Map<String, Object> resultMap = new HashMap<String, Object>();
resultMap.put("status", "0");
resultMap.put("message", "");
if (courseId == null || courseId <= 0l) {
resultMap.put("message", "课程ID不能为空或有误~");
return resultMap;
}
TKzyCourse tKzyCourse = tKzyCourseMapper.selectByPrimaryKey(courseId);
if (tKzyCourse == null) {
resultMap.put("message", "未查到该课程信息,请确认~");
return resultMap;
}
resultMap.put("status", "1");
Map<String, Object> courseMap = new HashMap<>();
courseMap.put("courseName", tKzyCourse.getCourseName());
courseMap.put("courseIntroduce", tKzyCourse.getCourseIntroduce());
courseMap.put("startDt", tKzyCourse.getStartDt());
courseMap.put("endDt", tKzyCourse.getEndDt());
courseMap.put("picUrl", tKzyCourse.getPicUrl());
courseMap.put("liveUrl", tKzyCourse.getLiveUrl());
resultMap.put("data", courseMap);
return resultMap;
}
/**
* 评论列表
*
* @param courseId
* @return
*/
@Override
public Map<String, Object> commentList(Long courseId, Integer pageNo, Integer pageSize) {
Map<String, Object> resultMap = new HashMap<String, Object>();
resultMap.put("status", "1");
resultMap.put("message", "");
Map<String, Object> dataMap = new HashMap<>();
Map<String, Object> param = new HashMap<>();
param.put("courseId", courseId);
param.put("start", (pageNo - 1) * pageSize);
param.put("pageSize", pageSize);
List<Map<String, Object>> list = commentMapper.commentList(param);
Integer count = commentMapper.commentListTotal(param);
dataMap.put("list", list);
dataMap.put("count", count);
resultMap.put("data", dataMap);
return resultMap;
}
public String getAccessToken(String appId, String appSecret) {
if (org.springframework.util.StringUtils.hasText(appSecret)) {
String json = "";
boolean redisAccessToken = true;
if (redisTemplate.hasKey("wx_accessToken_" + appId)) {
json = (String) redisTemplate.opsForValue().get("wx_accessToken_" + appId);
if (org.springframework.util.StringUtils.hasText(json)) {
redisAccessToken = false;
}
}
if (redisAccessToken) {
String url = WeixinBaseUtil.ACCESS_TOKEN_URL + "&appid=" + appId + "&secret=" + appSecret;
RestTemplate restTemplate = new RestTemplate();
json = restTemplate.getForObject(url, String.class);
redisTemplate.opsForValue().set("wx_accessToken_" + appId, json, 60 * 60, TimeUnit.SECONDS);
}
AccessTokenBean accessTokenBean = new AccessTokenBean(json);
return accessTokenBean.getAccessToken();
} else {
return null;
}
}
/**
* 评论
*
* @param courseId
* @param userId
* @param comment
* @return
*/
@Override
public Map<String, Object> comment(Long courseId, Long userId, String comment) {
Map<String, Object> resultMap = new HashMap<String, Object>();
resultMap.put("status", "0");
resultMap.put("message", "");
if (courseId == null || courseId <= 0l) {
resultMap.put("message", "课程ID不能为空或有误~");
return resultMap;
}
TKzyCourse tKzyCourse = tKzyCourseMapper.selectByPrimaryKey(courseId);
if (tKzyCourse == null) {
resultMap.put("message", "未查到该课程信息,请确认~");
return resultMap;
}
if (userId == null || userId <= 0l) {
resultMap.put("message", "请先登录~");
resultMap.put("status", "-1");
return resultMap;
}
if (comment == null) {
resultMap.put("message", "评论内容不能为空~");
return resultMap;
}
TKzyComment tKzyCourseComment = new TKzyComment();
tKzyCourseComment.setCreateDt(new Date());
tKzyCourseComment.setUserId(userId);
tKzyCourseComment.setRelationId(courseId);
tKzyCourseComment.setIsDelete(0);
tKzyCourseComment.setReplyContent(comment);
tKzyCourseComment.setType(2);
tKzyCourseComment.setAuthStatus(1);
tKzyCommentMapper.insertSelective(tKzyCourseComment);
resultMap.put("message", "评论成功~");
resultMap.put("status", "1");
return resultMap;
}
/**
* 课程分类
*
* @return
*/
@Override
public Map<String, Object> courseType() {
return null;
}
@Override
public TKzyCourse selectByPrimaryKey(Long courseId) {
TKzyCourse tKzyCourse = null;
if (courseId != null && courseId > 0l) {
tKzyCourse = tKzyCourseMapper.selectByPrimaryKey(courseId);
}
return tKzyCourse;
}
@Autowired
private JedisPool jedisPool;
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
public static String formateCourseDate(Date date) {
DateFormat yearFormat = new SimpleDateFormat("YYYY年MM月dd日");
DateFormat timeFormat = new SimpleDateFormat("HH:mm");
String courseDate = yearFormat.format(date);
String courseTime = timeFormat.format(date);
return courseDate + " " + getWeekOfDate(date) + " " + courseTime;
}
/**
* 获取当前日期是星期几<br>
*
* @param dt
* @return 当前日期是星期几
*/
public static String getWeekOfDate(Date dt) {
String[] weekDays = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
Calendar cal = Calendar.getInstance();
cal.setTime(dt);
int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
if (w < 0)
w = 0;
return weekDays[w];
}
public static double getWith(Font font, String str) {
FontRenderContext frc = new FontRenderContext(new AffineTransform(), true, true);
Rectangle rec = font.getStringBounds(str, frc).getBounds();
return rec.getWidth();
}
}