UserSignServiceImpl.java
5.15 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
package com.bsth.service.logger.impl;
import com.bsth.entity.logger.UserSign;
import com.bsth.entity.sys.SysUser;
import com.bsth.repository.logger.UserSignRepository;
import com.bsth.service.logger.UserSignService;
import com.bsth.service.schedule.exception.ScheduleException;
import com.bsth.service.schedule.impl.BServiceImpl;
import com.bsth.service.schedule.utils.DataToolsFile;
import com.bsth.service.schedule.utils.DataToolsService;
import com.bsth.service.sys.SysUserService;
import org.joda.time.DateTime;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import java.io.File;
import java.util.Date;
import java.util.List;
import java.util.Map;
@Service
public class UserSignServiceImpl extends BServiceImpl<UserSign, Long> implements UserSignService {
/** 日志记录器 */
private final static Logger LOG = LoggerFactory.getLogger(UserSignServiceImpl.class);
@Autowired
@Qualifier(value = "usersign_dataTool")
private DataToolsService dataToolsService;
@Autowired
private UserSignRepository userLogInoutRepository;
@Autowired
private SysUserService sysUserService;
@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.READ_COMMITTED)
@Override
public void userLogIn(String userName, String sessionId, Date logInDate) {
try {
SysUser user = sysUserService.findByUserName(userName);
if (user == null) {
LOG.warn("用户{}不存在!", userName);
return;
}
if (!user.isEnabled()) {
LOG.warn("用户{}被锁定!", userName);
}
// joda日期转换
DateTime _jodaDT = new DateTime(logInDate.getTime());
UserSign userLogInout = new UserSign();
userLogInout.setLogName(userName);
userLogInout.setRealName(user.getName());
userLogInout.setSessionId(sessionId);
userLogInout.setLogInDate(_jodaDT.dayOfWeek().roundFloorCopy().toDate()); // 登录时间(不带时分秒)
userLogInout.setLogInDateTime(_jodaDT.toDate()); // 登录时间(带时分秒)
userLogInoutRepository.save(userLogInout);
} catch (Exception exp) {
LOG.error("用户登录日志异常!", exp);
}
}
@Override
public void userLogOut(String userName, String sessionId, Date logOutDate) {
try {
SysUser user = sysUserService.findByUserName(userName);
if (user == null) {
LOG.warn("用户{}不存在!", userName);
return;
}
if (!user.isEnabled()) {
LOG.warn("用户{}被锁定!", userName);
}
// 根据sessionId查找对应的登录日志
UserSign userLogInout = userLogInoutRepository.findBySessionId(sessionId);
// joda日期转换
DateTime _jodaDT = new DateTime(logOutDate.getTime());
if (userLogInout == null) { // 说明之前登录的操作没有记录日志
userLogInout = new UserSign();
userLogInout.setLogName(userName);
userLogInout.setRealName(user.getName());
userLogInout.setSessionId(sessionId);
userLogInout.setLogOutDate(_jodaDT.dayOfWeek().roundFloorCopy().toDate()); // 登出时间(不带时分秒)
userLogInout.setLogOutDateTime(_jodaDT.toDate()); // 登出时间(带时分秒)
} else {
userLogInout.setLogOutDate(_jodaDT.dayOfWeek().roundFloorCopy().toDate()); // 登出时间(不带时分秒)
userLogInout.setLogOutDateTime(_jodaDT.toDate()); // 登出时间(带时分秒)
}
userLogInoutRepository.save(userLogInout);
} catch (Exception exp) {
LOG.error("用户登出日志异常!", exp);
}
}
@Override
public UserSign save(UserSign userLogInout) {
throw new RuntimeException("不支持save方法!");
}
@Override
public <S extends UserSign> List<S> bulkSave(List<S> entities) {
throw new RuntimeException("不支持bulkSave方法!");
}
@Override
public void delete(Long aLong) throws ScheduleException {
throw new RuntimeException("不支持delete方法!");
}
@Override
public DataToolsFile uploadFile(String filename, byte[] filedata) throws ScheduleException {
throw new RuntimeException("不支持uploadFile方法!");
}
@Override
public void importData(File file, Map<String, Object> params) throws ScheduleException {
throw new RuntimeException("不支持importData方法!");
}
@Override
public DataToolsFile exportData(Map<String, Object> params) throws ScheduleException {
return dataToolsService.exportData(params);
}
}