前言
本次我要实现的功能是,用户日志的生成以及查看,用户相关学习信息的展示,用户相关关卡信息的展示,用户相关项目的展示,这次任务的日志功能上需要使用AOP增强功能。
一、实现添加日志和展示日志功能
AOP切面代码:
@Component
@Aspect
@CrossOrigin
public class LogAspect {
@Autowired
private OperationLogService operationLogService;
@Autowired
private UserMapper userMapper;
@Pointcut("@annotation(com.itheima.aop.LogAnnotation)") //使用LogAnnotation注解的方法
public void pt(){}
@AfterReturning("pt()")
public void log(JoinPoint proceedingJoinPoint){
//获取request对象
ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest request = requestAttributes.getRequest();
//获得方法签名
MethodSignature signature = (MethodSignature)proceedingJoinPoint.getSignature();
//获得方法
Method method = signature.getMethod();
String methodName = method.getName();
HttpSession session = null;
if ("loginByEmail".equals(methodName)){
session = request.getSession();
}else {
String sessionId = request.getHeader("x-cookie");
session = MySessionContext.getSession(sessionId);
}
if (!"N".equals(session.getAttribute("status"))){
//获得用户ID
Integer userId = (Integer) session.getAttribute("userId");
String username = (String) session.getAttribute("username");
//获得自定义注解
LogAnnotation annotation = method.getAnnotation(LogAnnotation.class);
//获得注解的值
String model = annotation.model();
String operator = annotation.operator();
SimpleDateFormat sdf = new SimpleDateFormat("yy-MM-dd HH:mm:ss");
String date = sdf.format(new Date());
//添加到数据库
operationLogService.log(userId, username, model, operator, date);
}
}
}
二、学习信息的展示
部分代码:
@Override
public ResultInfo count(Integer userId) {
ResultInfo info = new ResultInfo();
info.setFlag(true);
try {
User user = new User();
user.setId(userId);
//通过UserId获取对应的用户名
String username = userMapper.selectByCondition(user).getUsername();
//通过Username获取完成关卡数量
Integer completeCount = studyOverviewMapper.selectCompleteCount(username);
//通过Username获取未完成关卡数
Integer incompleteCount = studyOverviewMapper.selectIncompleteCount(username);
//通过UserId获取相应的项目数
Integer projectCount = studyOverviewMapper.selectProjectCount(userId);
//通过UserId获取延期过的关卡数
Integer postponeCount = studyOverviewMapper.selectPostponeCount(userId);
///通过UserId获取逾期过的关卡数
Integer overdue = studyOverviewMapper.selectOverdue(username);
//通过UserId获取通知的数量
Integer messageCount = studyOverviewMapper.selectMessageCount(userId);
StudyOverview studyOverview = new StudyOverview(completeCount,incompleteCount,
projectCount,postponeCount,overdue,messageCount);
info.setData(studyOverview);
} catch (Exception e) {
info.setFlag(false);
info.setErrorMsg("查看失败");
e.printStackTrace();
}
return info;
}
三、关卡信息的展示
部分代码实现:
/**
* 查看方向名称
*
* @param userId
* @return
*/
@Override
public ResultInfo selectSubjectName(Integer userId) {
ResultInfo info = new ResultInfo();
info.setFlag(true);
try {
//创建map集合
Map<Integer, String> map = new HashMap<>();
//获取方向Id
Integer subjectId = customspassOverviewMapper.selectSubjectByUserId(userId);
//获取方向名称
String subjectname = customspassOverviewMapper.selectSubjectNameBySubjectId(subjectId);
//添加到map集合
map.put(subjectId, subjectname);
info.setData(map);
} catch (Exception e) {
info.setFlag(false);
info.setErrorMsg("查看失败");
e.printStackTrace();
}
return info;
}
/**
* 查看阶段名称
*
* @param userId
* @return
*/
@Override
public ResultInfo selectStageNames(Integer userId) {
ResultInfo info = new ResultInfo();
info.setFlag(true);
try {
//创建map集合
Map<Integer, String> map = new HashMap<>();
//获取阶段Id
List<Integer> stageIds = customspassOverviewMapper.selectStageByUserId(userId);
//获取阶段名称并将名称与Id添加到集合中去
for (Integer stageId : stageIds) {
String stagename = customspassOverviewMapper.selectStageNameByStageId(stageId);
map.put(stageId, stagename);
}
info.setData(map);
} catch (Exception e) {
info.setFlag(false);
info.setErrorMsg("查看失败");
e.printStackTrace();
}
return info;
}
/**
* 查看关卡名称
*
* @param userId
* @return
*/
@Override
public ResultInfo selectCustomspassName(Integer userId) {
ResultInfo info = new ResultInfo();
info.setFlag(true);
try {
//创建map集合
Map<Integer, String> map = new HashMap<>();
//获取关卡Id
List<Integer> customspassIds = customspassOverviewMapper.selectCustomspassByUserId(userId);
//获取关卡名称并将Id与名称添加到map集合中去
for (Integer customspassId : customspassIds) {
String customspassname = customspassOverviewMapper.selectCustomspassNameByCustomspassId(customspassId);
map.put(customspassId, customspassname);
}
info.setData(map);
} catch (Exception e) {
e.printStackTrace();
}
return info;
}
/**
* 查看关卡概览相关数据
*
* @param userId
* @param stageName
* @return
*/
@Override
public ResultInfo select(Integer userId, String stageName) {
ResultInfo info = new ResultInfo();
info.setFlag(true);
try {
//如果用户没有进行的关卡,就默认展示用户所属方向的第一个阶段
if ("null".equals(stageName)) {
Integer subjectId = customspassOverviewMapper.selectSubjectByUserId(userId);
stageName = customspassOverviewMapper.selectStageNameBySubjectId(subjectId);
}
Integer customspassTotalTime = 0;
Integer customspassCompleteTime = 0;
//获取所有关卡数目
Integer customspassCount = customspassOverviewMapper.selectTotalCustomspassCount(userId);
//获取完成关卡数目
Integer customspassCompleteCount = customspassOverviewMapper.selectCompleteCustomspassCount(userId);
//获取所有关卡时长
List<String> customspassTimes = customspassOverviewMapper.selectCustomspassTime(userId);
for (String time : customspassTimes) {
Integer customspassTime = Integer.parseInt(time.substring(time.length() - 1));
customspassTotalTime = customspassTotalTime + customspassTime;
}
//获取完成关卡时长
List<String> customspassCompleteTimes = customspassOverviewMapper.selectCustomspassCompleteTime(userId);
for (String time : customspassCompleteTimes) {
Integer customspassTime = Integer.parseInt(time.substring(time.length() - 1));
customspassCompleteTime = customspassCompleteTime + customspassTime;
}
Integer stageTotalTime = 0;
Integer stageCompleteTime = 0;
//获取一个阶段下所有关卡数目
Integer stageCount = customspassOverviewMapper.selectTotalStageCount(userId, stageName);
//获取一个阶段下完成关卡数目
Integer stageCompleteCount = customspassOverviewMapper.selectCompleteStageCount(userId, stageName);
//获取当前阶段所有关卡时长
List<String> stageTimes = customspassOverviewMapper.selectStageTime(userId, stageName);
for (String time : stageTimes) {
Integer stageTime = Integer.parseInt(time.substring(time.length() - 1));
stageTotalTime = stageTotalTime + stageTime;
}
//获取当前阶段完成关卡时长
List<String> stageCompleteTimes = customspassOverviewMapper.selectStageCompleteTime(userId, stageName);
for (String time : stageCompleteTimes) {
Integer stageTime = Integer.parseInt(time.substring(time.length() - 1));
stageCompleteTime = stageCompleteTime + stageTime;
}
//获取完成关卡数量占所有关卡的百分比
Integer customspassCompleteCountPercent = CalculatePercentUtils.result(customspassCompleteCount, customspassCount);
//获取完成关卡时长占所有关卡的百分比
Integer customspassCompleteTimePercent = CalculatePercentUtils.result(customspassCompleteTime, customspassTotalTime);
//获取当前阶段下完成关卡数量占所有关卡的百分比
Integer stageCompleteCountPercent = CalculatePercentUtils.result(stageCompleteCount, stageCount);
//获取当前阶段下完成关卡时长占所有关卡的百分比
Integer stageCompleteTimePercent = CalculatePercentUtils.result(stageCompleteTime, stageTotalTime);
CustomspassOverview customspassOverview = new CustomspassOverview(customspassTotalTime,
customspassCompleteTime, customspassCount, customspassCompleteCount, stageTotalTime,
stageCompleteTime, stageCount, stageCompleteCount, customspassCompleteCountPercent,
customspassCompleteTimePercent, stageCompleteCountPercent, stageCompleteTimePercent);
info.setData(customspassOverview);
} catch (Exception e) {
info.setFlag(false);
info.setErrorMsg("查看失败");
e.printStackTrace();
}
return info;
}
三、项目信息的展示
部分代码:
/**
* 查看关卡概览
* @param userId
* @return
*/
@Override
public ResultInfo selectProjectOverview(Integer userId) {
ResultInfo info = new ResultInfo();
info.setFlag(true);
try {
//获取未开启项目
Integer notStartProjectCount = projectMapper.selectNotStartProjectCountByUserId(userId);
//获取正在进行项目
Integer goingProjectCount = projectMapper.selectGoingProjectCountByUserId(userId);
//获取完成项目
Integer completeProjectCount = projectMapper.selectCompleteProjectCountByUserId(userId);
//获取项目总数
Integer projectCount = notStartProjectCount + goingProjectCount + completeProjectCount;
ProjectOverview projectOverview = new ProjectOverview(notStartProjectCount,
goingProjectCount,completeProjectCount,projectCount);
info.setData(projectOverview);
} catch (Exception e) {
info.setFlag(false);
info.setErrorMsg("查看失败!");
e.printStackTrace();
}
return info;
}
代码不足之处
这次代码不足之处主要在于项目用户信息的储存,在日志功能,我为了实现每个用户展示自己的日志,将每个用户的信息分别存在服务器中各自的session中,这样编写虽然能实现功能,但是会出现跨域,服务器内存不足等问题。之后可以对代码进行优化,通过token来验证用户信息,不存储在session中,完美解决这次代码的不足