package com.ycl.controller.user; import com.alibaba.excel.EasyExcel; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.toolkit.StringUtils; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.ycl.annotation.LogSave; import com.ycl.api.CommonResult; import com.ycl.dto.log.LogQueryParams; import com.ycl.entity.user.UmsAdmin; import com.ycl.entity.user.UmsAdminLoginLog; import com.ycl.entity.user.vo.UmsAdminLogVO; import com.ycl.service.user.IAdminLoginLogService; import com.ycl.service.user.UmsAdminService; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import lombok.SneakyThrows; import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; import javax.servlet.http.HttpServletResponse; import java.net.URLEncoder; import java.time.LocalDateTime; import java.util.List; import java.util.stream.Collectors; @RestController @RequestMapping("/log_manage") @Api(tags = "日志管理") public class UmsAdminLogController { @Autowired IAdminLoginLogService iAdminLoginLogService; @Autowired UmsAdminService umsAdminService; @GetMapping("/query") @ApiOperation(value = "查询") @LogSave(operationType = "日志管理", contain = "查询日志信息") public CommonResult searchLog(LogQueryParams logQueryParams) { Page umsAdminLoginLogPage = new Page<>(); umsAdminLoginLogPage.setCurrent(logQueryParams.getCurrent()); umsAdminLoginLogPage.setSize(logQueryParams.getSize()); Page page = iAdminLoginLogService .page(umsAdminLoginLogPage, new LambdaQueryWrapper() .like(StringUtils.isNotBlank(logQueryParams.getContent()), UmsAdminLoginLog::getContain, logQueryParams.getContent()) .eq(StringUtils.isNotBlank(logQueryParams.getOperationType()), UmsAdminLoginLog::getOperationType, logQueryParams.getOperationType()) .eq(StringUtils.isNotBlank(logQueryParams.getPortEquipment()), UmsAdminLoginLog::getUserAgent, logQueryParams.getPortEquipment()) .eq(logQueryParams.getId() != null, UmsAdminLoginLog::getAdminId, logQueryParams.getId()) .between(logQueryParams.getStartTime() != null && logQueryParams.getEndTime() != null, UmsAdminLoginLog::getCreateTime, logQueryParams.getStartTime(), logQueryParams.getEndTime()) .orderBy(logQueryParams.getSort() != null && logQueryParams.getSort() == 0, true, UmsAdminLoginLog::getCreateTime) .orderBy(logQueryParams.getSort() != null && logQueryParams.getSort() == 1, false, UmsAdminLoginLog::getCreateTime)); List umsAdminLogVOList = page.getRecords().stream() .map(item -> { UmsAdminLogVO umsAdminlogVO = new UmsAdminLogVO(); BeanUtils.copyProperties(item, umsAdminlogVO); umsAdminlogVO.setAdminId(umsAdminService .getOne(new LambdaQueryWrapper() .eq(UmsAdmin::getId, item.getAdminId())) .getUsername()); return umsAdminlogVO; }).collect(Collectors.toList()); Page umsAdminLogVOPage = new Page<>(); BeanUtils.copyProperties(page, umsAdminLogVOPage); umsAdminLogVOPage.setRecords(umsAdminLogVOList); return CommonResult.success(umsAdminLogVOPage); } @DeleteMapping("/deletion_batch") @ApiOperation(value = "批量删除日志") @LogSave(operationType = "日志管理", contain = "批量删除日志信息") public CommonResult deleteBatch(@RequestParam List ids) { return CommonResult.success(iAdminLoginLogService.removeBatchByIds(ids)); } @DeleteMapping("/clear") @ApiOperation(value = "清空日志") @LogSave(operationType = "日志管理", contain = "清空日志信息") public CommonResult clear() { return CommonResult.success(iAdminLoginLogService.remove(new LambdaQueryWrapper())); } @GetMapping("/query_operationType") @ApiOperation(value = "查询操作类型") public CommonResult queryOperationType() { return CommonResult.success(iAdminLoginLogService.list(new LambdaQueryWrapper().groupBy(UmsAdminLoginLog::getOperationType))); } @PostMapping("/export") @ApiOperation(value = "导出") @SneakyThrows @LogSave(operationType = "日志管理", contain = "导出日志") public void export(HttpServletResponse response, LogQueryParams logQueryParams) { Page umsAdminLoginLogPage = new Page<>(); umsAdminLoginLogPage.setCurrent(logQueryParams.getCurrent()); umsAdminLoginLogPage.setSize(logQueryParams.getSize()); StringBuilder fileName = new StringBuilder(); fileName.append(LocalDateTime.now().getYear()) .append("-") .append(LocalDateTime.now().getMonthValue()) .append("-") .append(LocalDateTime.now().getDayOfMonth()) .append("日志数据"); response.setContentType("application/vnd.ms-excel"); response.setCharacterEncoding("utf-8"); String file = URLEncoder.encode(fileName.toString(), "UTF-8").replaceAll("\\+", "%20"); response.setHeader("Content-disposition", "attachment;filename=" + file + ".xlsx"); EasyExcel .write(response.getOutputStream(), UmsAdminLogVO.class) .sheet("数据") .doWrite(iAdminLoginLogService .page(umsAdminLoginLogPage, new LambdaQueryWrapper() .like(StringUtils.isNotBlank(logQueryParams.getContent()), UmsAdminLoginLog::getContain, logQueryParams.getContent()) .eq(StringUtils.isNotBlank(logQueryParams.getOperationType()), UmsAdminLoginLog::getOperationType, logQueryParams.getOperationType()) .between(logQueryParams.getStartTime() != null && logQueryParams.getEndTime() != null, UmsAdminLoginLog::getCreateTime, logQueryParams.getStartTime(), logQueryParams.getEndTime()) .orderBy(logQueryParams.getSort() != null && logQueryParams.getSort() == 0, true, UmsAdminLoginLog::getCreateTime) .orderBy(logQueryParams.getSort() != null && logQueryParams.getSort() == 1, false, UmsAdminLoginLog::getCreateTime)) .getRecords() .stream() .map(item -> { UmsAdminLogVO umsAdminlogVO = new UmsAdminLogVO(); BeanUtils.copyProperties(item, umsAdminlogVO); umsAdminlogVO.setAdminId(umsAdminService .getOne(new LambdaQueryWrapper() .eq(UmsAdmin::getId, item.getAdminId())) .getUsername()); return umsAdminlogVO; }).collect(Collectors.toList())); } }