| | |
| | | package com.whyc.service; |
| | | |
| | | import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; |
| | | import com.github.pagehelper.PageHelper; |
| | | import com.github.pagehelper.PageInfo; |
| | | import com.whyc.constant.UserOperation; |
| | | import com.whyc.dto.Response; |
| | | import com.whyc.mapper.DocLogMapper; |
| | | import com.whyc.pojo.DocLog; |
| | | import com.whyc.util.ActionUtil; |
| | | import com.whyc.util.DateUtil; |
| | | import com.whyc.util.ExcelUtil; |
| | | import org.apache.poi.hssf.usermodel.HSSFWorkbook; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.stereotype.Service; |
| | | |
| | | import javax.servlet.http.HttpServletRequest; |
| | | import javax.servlet.http.HttpServletResponse; |
| | | import java.util.Date; |
| | | import java.util.List; |
| | | |
| | | @Service |
| | | public class DocLogService { |
| | | @Autowired |
| | | @Autowired(required = false) |
| | | private DocLogMapper mapper; |
| | | |
| | | public void recordOperationLog(Long uId,String name, Integer operationType, Date operationTime, String terminalIp, String operationMsg, String operationDetail) { |
| | | DocLog docLog=new DocLog(); |
| | | docLog.setId(uId.intValue()); |
| | | docLog.setName(name); |
| | | docLog.setOprateType(operationType); |
| | | docLog.setOprateDay(operationTime); |
| | | docLog.setTerminalIp(terminalIp); |
| | | docLog.setOprateMsg(operationMsg); |
| | | docLog.setOperationDetail(operationDetail); |
| | | mapper.insert(docLog); |
| | | } |
| | | public PageInfo<DocLog> getPage(int pageNum, int pageSize, DocLog docLog) { |
| | | PageHelper.startPage(pageNum,pageSize,true); |
| | | //这个接口要兼容新老审计日志记录的查询 |
| | | List<DocLog> userLogList = mapper.getList(docLog); |
| | | userLogList.stream().forEach(docLog1-> { |
| | | switch (docLog1.getOprateType()){ |
| | | case 1: docLog1.setOperationTypeStr(UserOperation.TYPE_LOGIN.getTypeName());break; |
| | | case 2: docLog1.setOperationTypeStr(UserOperation.TYPE_LOGOUT.getTypeName());break; |
| | | case 3: docLog1.setOperationTypeStr(UserOperation.TYPE_ADD.getTypeName());break; |
| | | case 4: docLog1.setOperationTypeStr(UserOperation.TYPE_UPDATE.getTypeName());break; |
| | | case 5: docLog1.setOperationTypeStr(UserOperation.TYPE_DELETE.getTypeName());break; |
| | | case 20:docLog1.setOperationTypeStr(UserOperation.TYPE_UNAUTHORIZED_ACCESS.getTypeName());break; |
| | | case 21:docLog1.setOperationTypeStr(UserOperation.TYPE_EXCEPTION.getTypeName());break; |
| | | case 31:docLog1.setOperationTypeStr(UserOperation.TYPE_LOGIN_FAIL.getTypeName());break; |
| | | case 32:docLog1.setOperationTypeStr(UserOperation.TYPE_PARAM_CHANGE.getTypeName());break; |
| | | case 33:docLog1.setOperationTypeStr(UserOperation.TYPE_PASSWORD_CHANGE.getTypeName());break; |
| | | case 34:docLog1.setOperationTypeStr(UserOperation.TYPE_LOGIN_TIMEOUT.getTypeName());break; |
| | | default:docLog1.setOperationTypeStr(UserOperation.TYPE_UNRECOGNIZED.getTypeName()); |
| | | } |
| | | }); |
| | | PageInfo<DocLog> pageInfo = new PageInfo<>(userLogList); |
| | | return pageInfo; |
| | | |
| | | } |
| | | |
| | | public void exportExcel(HttpServletResponse response, String[][] value) { |
| | | |
| | | String[] columnTitleArr = new String[]{"操作人姓名","操作类型","操作时间","终端IP","操作事件","具体参数"}; |
| | | String now = DateUtil.YYYY_MM_DD_HH_MM_SS_UNION.format(new Date()); |
| | | ExcelUtil.exportExcel("UserLog-"+now,"测试sheet1",columnTitleArr,value,new HSSFWorkbook(),response); |
| | | } |
| | | |
| | | public void record(long uId, int operationType, String msg) { |
| | | DocLog docLog = new DocLog(); |
| | | docLog.setTerminalIp(ActionUtil.getRequest().getRemoteAddr()); |
| | | docLog.setOprateDay(new Date()); |
| | | docLog.setId((int)(uId)); |
| | | docLog.setOprateType(operationType); |
| | | docLog.setOprateMsg(msg); |
| | | mapper.insert(docLog); |
| | | } |
| | | |
| | | public void record(long uId, int operationType, String msg,String msgDetail) { |
| | | DocLog docLog = new DocLog(); |
| | | docLog.setTerminalIp(ActionUtil.getRequest().getRemoteAddr()); |
| | | docLog.setOprateDay(new Date()); |
| | | docLog.setId((int)(uId)); |
| | | docLog.setOprateType(operationType); |
| | | docLog.setOprateMsg(msg); |
| | | docLog.setOperationDetail(msgDetail); |
| | | mapper.insert(docLog); |
| | | } |
| | | |
| | | public void record2(HttpServletRequest request, long uId, int operationType, String msg, String msgDetail) { |
| | | DocLog docLog = new DocLog(); |
| | | docLog.setTerminalIp(request.getRemoteAddr()); |
| | | //userLog.setTerminalIp(request.getRemoteAddr()); |
| | | docLog.setOprateDay(new Date()); |
| | | docLog.setId((int)(uId)); |
| | | docLog.setOprateType(operationType); |
| | | docLog.setOprateMsg(msg); |
| | | docLog.setOperationDetail(msgDetail); |
| | | mapper.insert(docLog); |
| | | } |
| | | //日志查询 |
| | | public Response searchLog(DocLog docLog,int pageCurr, int pageSize) { |
| | | PageHelper.startPage(pageCurr,pageSize); |
| | | QueryWrapper wrapper=new QueryWrapper(); |
| | | if(docLog!=null){ |
| | | if(docLog.getName()!=null&&docLog.getName()!=""){ |
| | | wrapper.like("name",docLog.getName()); |
| | | } |
| | | if(docLog.getOprateType()!=null){ |
| | | wrapper.eq("oprate_type",docLog.getOprateType()); |
| | | } |
| | | if(docLog.getOprateDay()!=null){ |
| | | wrapper.ge("oprate_day",docLog.getOprateDay()); |
| | | } |
| | | if(docLog.getOprateDay2()!=null){ |
| | | wrapper.le("oprate_day",docLog.getOprateDay2()); |
| | | } |
| | | } |
| | | wrapper.ne("id", 0); |
| | | wrapper.orderByDesc("oprate_day"); |
| | | List<DocLog> list=mapper.selectList(wrapper); |
| | | list.stream().forEach(dlog->{ |
| | | dlog.setOperationTypeStr(UserOperation.getNameByType(dlog.getOprateType())); |
| | | }); |
| | | PageInfo pageInfo=new PageInfo(list); |
| | | return new Response().setII(1,list.size()>0?true:false,pageInfo,"数据返回"); |
| | | } |
| | | //操作对应关系 |
| | | public Response getOperate() { |
| | | return new Response().setII(1,true,UserOperation.values(),"数据返回"); |
| | | } |
| | | } |