| | |
| | | import com.github.pagehelper.PageHelper; |
| | | import com.github.pagehelper.PageInfo; |
| | | import com.whyc.constant.UserOperation; |
| | | import com.whyc.mapper.CommonMapper; |
| | | import com.whyc.mapper.UserLogMapper; |
| | | import com.whyc.pojo.UserLog; |
| | | import com.whyc.util.ActionUtil; |
| | | import com.whyc.util.DateUtil; |
| | | import com.whyc.util.ExcelUtil; |
| | | import com.whyc.util.*; |
| | | import org.apache.poi.hssf.usermodel.HSSFWorkbook; |
| | | import org.springframework.stereotype.Service; |
| | | |
| | | import javax.annotation.Resource; |
| | | import javax.servlet.http.HttpServletRequest; |
| | | import javax.servlet.http.HttpServletResponse; |
| | | import java.util.Date; |
| | | import java.util.List; |
| | | import java.text.ParseException; |
| | | import java.util.*; |
| | | |
| | | @Service |
| | | public class UserLogService { |
| | | |
| | | @Resource |
| | | private UserLogMapper mapper; |
| | | |
| | | @Resource |
| | | private CommonMapper commonMapper; |
| | | |
| | | @Resource |
| | | private SubTablePageInfoUtils subTablePageInfoUtils; |
| | | |
| | | public PageInfo<UserLog>getPage(int pageNum, int pageSize, UserLog userLog) { |
| | | PageHelper.startPage(pageNum, pageSize, true); |
| | |
| | | |
| | | } |
| | | |
| | | /**根据查询优化分页查询方式*/ |
| | | public PageInfo<Object>getPage2(int pageNum, int pageSize, UserLog userLog) throws ParseException { |
| | | //PageHelper.startPage(pageNum,pageSize,true); |
| | | //根据传入的查询时间,获取分表及默认表的拆分查询 |
| | | Date startTime = userLog.getOperationTime(); |
| | | Date endTime = userLog.getOperationTime2(); |
| | | /*Map<String, List<Date>> queryTimeForSubTables = DateUtil.getQueryTimeForSubTablesDesc(startTime, endTime); |
| | | //查询分表是否存在,存在则查询结果 |
| | | Map<String,Integer> queryCountMap = new LinkedHashMap<>(); |
| | | Set<String> tableYearKeySet = queryTimeForSubTables.keySet(); |
| | | for (String tableYear : tableYearKeySet) { |
| | | List<Date> queryTime = queryTimeForSubTables.get(tableYear); |
| | | userLog.setOperationTime(queryTime.get(0)); |
| | | userLog.setOperationTime2(queryTime.get(1)); |
| | | String tableName; |
| | | if(!tableYear.equals("default")){ |
| | | //数值 |
| | | tableName = "tb_user_log_"+tableYear; |
| | | String existTableName = commonMapper.existTable("db_user", tableName); |
| | | if(existTableName == null){ |
| | | continue; |
| | | } |
| | | }else{ |
| | | tableName = "tb_user_log"; |
| | | } |
| | | userLog.setRecordYear(tableName); |
| | | int currentCount = mapper.getCount(userLog); |
| | | queryCountMap.put(tableYear,currentCount); |
| | | } |
| | | |
| | | //分页信息 |
| | | //确认总页数,总记录数 |
| | | PageInfo<Object> pageInfo = new PageInfo<>(); |
| | | |
| | | int total = 0; |
| | | Set<String> queryKeySet = queryCountMap.keySet(); |
| | | for (String queryKey : queryKeySet) { |
| | | int size = queryCountMap.get(queryKey); |
| | | total+=size; |
| | | } |
| | | int pages = (int) Math.ceil(Float.parseFloat(String.valueOf(total))/pageSize); |
| | | pageInfo.setTotal(total); |
| | | pageInfo.setPages(pages); |
| | | pageInfo.setPageNum(pageNum); |
| | | pageInfo.setPageSize(pageSize); |
| | | //根据当前页所需记录,查询当前页记录 |
| | | int startNum = (pageNum-1)*pageSize+1; |
| | | int endNum = pageNum*pageSize; |
| | | |
| | | //最后一个算法:上面不应该先查询所有记录,应该取count. 这后面定位到哪个表或哪几张表后,采取limit获取当前页记录数; |
| | | //格式:{表名,[limit 2,20]} |
| | | Map<String,List<Integer>> tableAndLimitMap = MathUtil.getQueryTableAndLimit(startNum,endNum,pageSize,queryCountMap); |
| | | Set<String> keySet = tableAndLimitMap.keySet(); |
| | | List<Object> userLogList = new LinkedList<>(); |
| | | for (String key : keySet) { |
| | | List<Date> queryTime = queryTimeForSubTables.get(key); |
| | | userLog.setOperationTime(queryTime.get(0)); |
| | | userLog.setOperationTime2(queryTime.get(1)); |
| | | |
| | | List<Integer> limitList = tableAndLimitMap.get(key); |
| | | userLog.setLimitStart(limitList.get(0)); |
| | | userLog.setLimitEnd(limitList.get(1)); |
| | | userLog.setRecordYear(key.equals("default")?"tb_user_log":"tb_user_log_"+key); |
| | | List<UserLog> list = mapper.getList2(userLog); |
| | | userLogList.addAll(list); |
| | | }*/ |
| | | PageInfo<Object> pageInfo = subTablePageInfoUtils.getPageInfo(pageNum, pageSize, startTime, endTime, "db_user", "tb_user_log", userLog); |
| | | List<Object> userLogList = pageInfo.getList(); |
| | | |
| | | //这个接口要兼容新老审计日志记录的查询 |
| | | //List<UserLog> userLogList = mapper.getList(userLog); |
| | | userLogList.stream().forEach(userLogTemp -> { |
| | | UserLog userLog1 = (UserLog)userLogTemp; |
| | | switch (userLog1.getOperationType()){ |
| | | case 1: userLog1.setOperationTypeStr(UserOperation.TYPE_LOGIN.getTypeName());break; |
| | | case 2: userLog1.setOperationTypeStr(UserOperation.TYPE_LOGOUT.getTypeName());break; |
| | | case 3: userLog1.setOperationTypeStr(UserOperation.TYPE_ADD.getTypeName());break; |
| | | case 4: userLog1.setOperationTypeStr(UserOperation.TYPE_UPDATE.getTypeName());break; |
| | | case 5: userLog1.setOperationTypeStr(UserOperation.TYPE_DELETE.getTypeName());break; |
| | | case 20:userLog1.setOperationTypeStr(UserOperation.TYPE_UNAUTHORIZED_ACCESS.getTypeName());break; |
| | | case 21:userLog1.setOperationTypeStr(UserOperation.TYPE_EXCEPTION.getTypeName());break; |
| | | case 31:userLog1.setOperationTypeStr(UserOperation.TYPE_LOGIN_FAIL.getTypeName());break; |
| | | case 32:userLog1.setOperationTypeStr(UserOperation.TYPE_PARAM_CHANGE.getTypeName());break; |
| | | case 33:userLog1.setOperationTypeStr(UserOperation.TYPE_PASSWORD_CHANGE.getTypeName());break; |
| | | case 34:userLog1.setOperationTypeStr(UserOperation.TYPE_LOGIN_TIMEOUT.getTypeName());break; |
| | | default:userLog1.setOperationTypeStr(UserOperation.TYPE_UNRECOGNIZED.getTypeName()); |
| | | } |
| | | }); |
| | | //PageInfo<UserLog> pageInfo = new PageInfo<>(userLogList); |
| | | return pageInfo; |
| | | |
| | | } |
| | | |
| | | public void exportExcel(HttpServletResponse response, String[][] value) { |
| | | //根据当前的语言环境设置不同的日志记录 |
| | | String lang = ActionUtil.getLang(); |
| | |
| | | ExcelUtil.exportExcel("UserLog-" + now, "sheet1", columnTitleArr, value, new HSSFWorkbook(), response); |
| | | } |
| | | |
| | | public void record(long uId, int operationType, String msg) { |
| | | public void record(long uId, int operationType, String msg, String msgEnUS) { |
| | | UserLog userLog = new UserLog(); |
| | | userLog.setTerminalIp(ActionUtil.getRequest().getRemoteAddr()); |
| | | //userLog.setTerminalIp(request.getRemoteAddr()); |
| | | userLog.setOperationTime(new Date()); |
| | | userLog.setUId((int)(uId)); |
| | | userLog.setUId((int) (uId)); |
| | | userLog.setOperationType(operationType); |
| | | userLog.setOperationMsg(msg); |
| | | userLog.setOperationMsgEnUs(msgEnUS); |
| | | mapper.insert(userLog); |
| | | } |
| | | |
| | | public void record(long uId, int operationType, String msg,String msgDetail) { |
| | | public void record(long uId, int operationType, String msg, String msgDetail, String msgEnUs, String msgDetailEnUs) { |
| | | UserLog userLog = new UserLog(); |
| | | userLog.setTerminalIp(ActionUtil.getRequest().getRemoteAddr()); |
| | | //userLog.setTerminalIp(request.getRemoteAddr()); |
| | | userLog.setOperationTime(new Date()); |
| | | userLog.setUId((int)(uId)); |
| | | userLog.setUId((int) (uId)); |
| | | userLog.setOperationType(operationType); |
| | | userLog.setOperationMsg(msg); |
| | | userLog.setOperationDetail(msgDetail); |
| | | userLog.setOperationMsg(msgEnUs); |
| | | userLog.setOperationDetail(msgDetailEnUs); |
| | | mapper.insert(userLog); |
| | | } |
| | | |
| | | public void record2(HttpServletRequest request,long uId, int operationType, String msg,String msgDetail) { |
| | | public void record2(HttpServletRequest request, long uId, int operationType, String msg, String msgDetail, String msgEnUs, String msgDetailEnUs) { |
| | | UserLog userLog = new UserLog(); |
| | | userLog.setTerminalIp(request.getRemoteAddr()); |
| | | //userLog.setTerminalIp(request.getRemoteAddr()); |
| | | userLog.setOperationTime(new Date()); |
| | | userLog.setUId((int)(uId)); |
| | | userLog.setUId((int) (uId)); |
| | | userLog.setOperationType(operationType); |
| | | userLog.setOperationMsg(msg); |
| | | userLog.setOperationDetail(msgDetail); |
| | | userLog.setOperationMsg(msgEnUs); |
| | | userLog.setOperationDetail(msgDetailEnUs); |
| | | mapper.insert(userLog); |
| | | } |
| | | |