| | |
| | | import com.vci.dto.LogInfoDTO; |
| | | import com.vci.dto.LogPeriodInfoDTO; |
| | | import com.vci.dto.LogQueryCriteriaDTO; |
| | | import com.vci.frameworkcore.compatibility.SmUserQueryServiceI; |
| | | import com.vci.starter.poi.bo.WriteExcelData; |
| | | import com.vci.starter.poi.bo.WriteExcelOption; |
| | | import com.vci.starter.poi.util.ExcelUtil; |
| | | import com.vci.starter.web.exception.VciBaseException; |
| | | import com.vci.starter.web.pagemodel.BaseResult; |
| | | import com.vci.starter.web.util.LangBaseUtil; |
| | | import com.vci.starter.web.util.LocalFileUtil; |
| | | import com.vci.starter.web.util.VciBaseUtil; |
| | | import com.vci.web.service.*; |
| | | import com.vci.web.util.*; |
| | | import org.apache.commons.lang3.StringUtils; |
| | |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.stereotype.Service; |
| | | |
| | | import java.io.File; |
| | | import java.text.ParseException; |
| | | import java.text.SimpleDateFormat; |
| | | import java.util.*; |
| | | import java.util.concurrent.atomic.AtomicInteger; |
| | | import java.util.stream.Collectors; |
| | | |
| | | /** |
| | |
| | | */ |
| | | @Autowired |
| | | private PlatformClientUtil platformClientUtil; |
| | | |
| | | @Autowired |
| | | private SmUserQueryServiceI userQueryServiceI; |
| | | |
| | | public static final String UTF8_BOM="\uFEFF"; |
| | | private final String LOG_SAVE_PERIOD = "logSavePeriod";//日志保存期限 |
| | | |
| | |
| | | */ |
| | | @Override |
| | | public BaseResult getLogListByContion(LogQueryCriteriaDTO queryDto) throws PLException { |
| | | VciBaseUtil.alertNotNull(queryDto,"日志查询参数对象"); |
| | | List<String> userNameList = userQueryServiceI.queryUserNameByRoleType(queryDto.getRoleType()); |
| | | queryDto.setUserNameList(userNameList); |
| | | String querySql = getSQL(queryDto); |
| | | LogInfo[] logInfos = platformClientUtil.getLogService().getLogListByContion(queryDto.getPageNo(),queryDto.getPageSize(),querySql); |
| | | |
| | | List<LogInfoDTO> dtos = new ArrayList<>(); |
| | | for (LogInfo logInfo : logInfos) { |
| | | LogInfoDTO dto = new LogInfoDTO(); |
| | |
| | | result.setTotal(sumLogRows); |
| | | return result; |
| | | } |
| | | |
| | | /** |
| | | * 操作用户获取 |
| | | */ |
| | |
| | | } |
| | | |
| | | /** |
| | | * 导出日志 |
| | | * @param dto 导出的文件名 |
| | | * @return |
| | | */ |
| | | @Override |
| | | public String exportLogs(LogQueryCriteriaDTO dto) throws PLException{ |
| | | VciBaseUtil.alertNotNull(dto,"日志查询参数对象"); |
| | | List<String> userNameList = userQueryServiceI.queryUserNameByRoleType(dto.getRoleType()); |
| | | dto.setUserNameList(userNameList); |
| | | String querySql = getSQL(dto); |
| | | //全查询 |
| | | if(dto.getPageSize() == -1){ |
| | | long sumLogRows = platformClientUtil.getLogService().getSumLogRows(querySql); |
| | | dto.setPageSize((int) sumLogRows); |
| | | } |
| | | LogInfo[] logList = platformClientUtil.getLogService().getLogListByContion(dto.getPageNo(),dto.getPageSize(),querySql); |
| | | |
| | | //界面没传名称,使用默认导出名称 |
| | | String exportFileName = "日志导出_" + Func.format(new Date(),"yyyy-MM-dd HHmmss.sss"); |
| | | //设置列名 |
| | | List<String> columns = new ArrayList<>( |
| | | Arrays.asList("用户名", "姓名", "用户ip","模块", "操作", "时间", "操作结果","描述") |
| | | ); |
| | | |
| | | //写excel |
| | | String excelPath = LocalFileUtil.getDefaultTempFolder() + File.separator + exportFileName + ".xls"; |
| | | try { |
| | | new File(excelPath).createNewFile(); |
| | | } catch (Throwable e) { |
| | | throw new VciBaseException(LangBaseUtil.getErrorMsg(e), new String[]{excelPath}, e); |
| | | } |
| | | //设置列 |
| | | List<WriteExcelData> excelDataList = new ArrayList<>(); |
| | | //设置列头 |
| | | for (int index = 0; index < columns.size(); index++) { |
| | | excelDataList.add(new WriteExcelData(0,index, columns.get(index))); |
| | | } |
| | | |
| | | if(Func.isEmpty(logList)){ |
| | | excelDataList.add(new WriteExcelData(1,1, "导出的日志列表为空!")); |
| | | }else{ |
| | | //先按照属性类型排序,不同属性类型导出的数据乱的效果 |
| | | AtomicInteger i = new AtomicInteger(); |
| | | Arrays.stream(logList).forEach(log->{ |
| | | excelDataList.add(new WriteExcelData(i.get() +1,0, log.username)); |
| | | excelDataList.add(new WriteExcelData(i.get() +1,1, log.truename)); |
| | | excelDataList.add(new WriteExcelData(i.get() +1,2, log.userIp)); |
| | | excelDataList.add(new WriteExcelData(i.get() +1,3, log.moduleName)); |
| | | excelDataList.add(new WriteExcelData(i.get() +1,4, log.type)); |
| | | excelDataList.add(new WriteExcelData(i.get() +1,5, log.date)); |
| | | excelDataList.add(new WriteExcelData(i.get() +1,6, log.logType)); |
| | | excelDataList.add(new WriteExcelData(i.get() +1,7, log.result)); |
| | | i.getAndIncrement(); |
| | | }); |
| | | } |
| | | WriteExcelOption excelOption = new WriteExcelOption(excelDataList); |
| | | ExcelUtil.writeDataToFile(excelPath, excelOption); |
| | | return excelPath; |
| | | } |
| | | |
| | | /** |
| | | * 获取用户信息 |
| | | * @param userNames 用户名称 |
| | | * @return 用户信息 |
| | | * @throws PLException |
| | | */ |
| | | private List<UserObject> getUsersByUserNames(List<String> userNames) throws PLException { |
| | | List<UserObject> userList = new ArrayList<UserObject>(); |
| | | List<UserObject> userList = new ArrayList<>(); |
| | | for(String userName : userNames){ |
| | | UserInfo userInfo = platformClientUtil.getFrameworkService().fetchUserInfoByName(userName); |
| | | UserObject user = new UserObject(); |
| | |
| | | } |
| | | return userNames; |
| | | } |
| | | |
| | | /** |
| | | * 获取查询条件并拼成SQL,只拼where子句后面的SQL |
| | | * @return |
| | | */ |
| | | public String getSQL(LogQueryCriteriaDTO dto) throws PLException { |
| | | StringBuffer sql = new StringBuffer(""); |
| | | // int period = getPeriod(LOG_SAVE_PERIOD);//获取保存期限,以月为单位 |
| | | int period = platformClientUtil.getLogService().getCurPeriod();;//获取保存期限,以月为单位 |
| | | // int period = getPeriod(LOG_SAVE_PERIOD);//获取保存期限,以月为单位 |
| | | int period = platformClientUtil.getLogService().getCurPeriod();//获取保存期限,以月为单位 |
| | | //下面是拼出SQL |
| | | if(period != 0){ |
| | | sql.append(" to_date(PLDATE) >= add_months(to_date(sysdate),"+(-period)+")"); |
| | |
| | | } |
| | | sql.append("PLLOGTYPE in('" + getLogTypeString(LogType.General) + "')"); |
| | | } |
| | | if(Func.isNotEmpty(dto.getUserNameList())){ |
| | | if(!sql.toString().equals("")){ |
| | | sql.append(" and "); |
| | | } |
| | | if(dto.getUserNameList().size()<=1){ |
| | | sql.append(" PLUSER = '").append(dto.getUserNameList().get(0).trim()).append("'"); |
| | | }else{ |
| | | sql.append(" PLUSER in ("); |
| | | String inWhere = dto.getUserNameList().stream() |
| | | .map(item -> "'" + item + "'") |
| | | .collect(Collectors.joining(",")); |
| | | sql.append(inWhere).append(")"); |
| | | } |
| | | } |
| | | |
| | | if(StringUtils.isNotBlank(dto.getUserName())){//操作用户 |
| | | if(!dto.getUserName().trim().equals("")){//如果用户名为空格则不加用户条件 |
| | | if(!sql.toString().equals("")){ |
| | |
| | | } |
| | | return sql.toString(); |
| | | } |
| | | |
| | | protected String getLogTypeString(LogType logType){ |
| | | String res = ""; |
| | | if(logType == LogType.Login) { |
| | |
| | | res = true; |
| | | return res; |
| | | } |
| | | |
| | | |
| | | } |