| | |
| | | package com.xiaopeng.operation.controller; |
| | | |
| | | import cn.afterturn.easypoi.excel.ExcelExportUtil; |
| | | import cn.afterturn.easypoi.excel.entity.ExportParams; |
| | | import com.xiaopeng.api.model.operation.XpAdminOperationLog; |
| | | import com.xiaopeng.common.core.domain.R; |
| | | import com.xiaopeng.common.core.exception.ServiceException; |
| | | import com.xiaopeng.common.core.utils.WebUtils; |
| | | import com.xiaopeng.common.core.web.domain.AjaxResult; |
| | | import com.xiaopeng.common.core.web.page.PageInfo; |
| | | import com.xiaopeng.common.log.annotation.AdminOperationLog; |
| | | import com.xiaopeng.common.log.enums.AdminBusinessType; |
| | | import com.xiaopeng.common.log.enums.AdminOperationType; |
| | | import com.xiaopeng.common.security.annotation.InnerAuth; |
| | | import com.xiaopeng.common.security.annotation.Logical; |
| | | import com.xiaopeng.common.security.annotation.RequiresPermissions; |
| | | import com.xiaopeng.operation.exportExcle.XpAdminOperationLogExportExcel; |
| | | import com.xiaopeng.operation.query.AdminOperationLogPageQuery; |
| | | import com.xiaopeng.operation.service.IXpAdminOperationLogService; |
| | | import io.swagger.annotations.Api; |
| | | import io.swagger.annotations.ApiOperation; |
| | | import org.apache.poi.ss.usermodel.Workbook; |
| | | import org.springframework.web.bind.annotation.GetMapping; |
| | | import org.springframework.web.bind.annotation.PostMapping; |
| | | import org.springframework.web.bind.annotation.RequestBody; |
| | |
| | | import org.springframework.web.bind.annotation.RestController; |
| | | |
| | | import javax.annotation.Resource; |
| | | import javax.servlet.ServletOutputStream; |
| | | import javax.servlet.http.HttpServletResponse; |
| | | import java.io.IOException; |
| | | import java.net.URLEncoder; |
| | | import java.time.format.DateTimeFormatter; |
| | | import java.time.LocalDateTime; |
| | | import java.util.ArrayList; |
| | | import java.util.Arrays; |
| | | import java.util.Comparator; |
| | | import java.util.LinkedHashMap; |
| | |
| | | * 管理后台操作记录 |
| | | */ |
| | | @RestController |
| | | @RequestMapping({"/api/v1/adminOperationLog"}) |
| | | @RequestMapping({""}) |
| | | @Api(tags = "管理后台-操作记录") |
| | | public class XpAdminOperationLogController { |
| | | |
| | | private static final DateTimeFormatter EXPORT_TIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); |
| | | |
| | | @Resource |
| | | private IXpAdminOperationLogService adminOperationLogService; |
| | | |
| | | @RequiresPermissions(value = {"/adminOperationLog/list", "/operationLog/list"}, logical = Logical.OR) |
| | | @GetMapping("/list") |
| | | // @RequiresPermissions(value = {"/adminOperationLog/list", "/operationLog/list"}, logical = Logical.OR) |
| | | @GetMapping("/api/v1/adminOperationLog/list") |
| | | @ApiOperation(value = "管理后台操作记录分页列表", tags = {"管理后台-操作记录"}) |
| | | public AjaxResult<PageInfo<XpAdminOperationLog>> list(AdminOperationLogPageQuery query) { |
| | | return AjaxResult.success(adminOperationLogService.pageList(query)); |
| | | } |
| | | |
| | | @PostMapping("/api/v1/adminOperationLog/exportExcel") |
| | | @ApiOperation(value = "管理后台操作记录导出", tags = {"管理后台-操作记录"}) |
| | | @AdminOperationLog( |
| | | businessType = AdminBusinessType.SYSTEM, |
| | | operationType = AdminOperationType.EXPORT, |
| | | content = "导出管理后台操作记录", |
| | | appendRequestData = true |
| | | ) |
| | | public void exportExcel(@RequestBody AdminOperationLogPageQuery query) { |
| | | List<XpAdminOperationLog> list = adminOperationLogService.exportList(query); |
| | | List<XpAdminOperationLogExportExcel> exportList = list == null ? new ArrayList<>() : list.stream() |
| | | .map(this::toExportRow) |
| | | .collect(Collectors.toList()); |
| | | HttpServletResponse response = WebUtils.response(); |
| | | if (response == null) { |
| | | throw new ServiceException("导出失败:未获取到响应对象"); |
| | | } |
| | | response.setContentType("application/vnd.ms-excel"); |
| | | response.setCharacterEncoding("utf-8"); |
| | | try { |
| | | String fileName = URLEncoder.encode("管理后台操作记录.xls", "utf-8"); |
| | | response.setHeader("Content-dispodition", "attachment;filename=" + fileName); |
| | | try (Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams(), XpAdminOperationLogExportExcel.class, exportList); |
| | | ServletOutputStream outputStream = response.getOutputStream()) { |
| | | workbook.write(outputStream); |
| | | } |
| | | } catch (IOException e) { |
| | | throw new ServiceException("管理后台操作记录导出失败"); |
| | | } |
| | | } |
| | | |
| | | @RequiresPermissions(value = {"/adminOperationLog/list", "/operationLog/list"}, logical = Logical.OR) |
| | |
| | | @ApiOperation(value = "管理后台操作记录业务类型列表", tags = {"管理后台-操作记录"}) |
| | | public AjaxResult<List<Map<String, Object>>> businessTypeList() { |
| | | List<Map<String, Object>> list = Arrays.stream(AdminBusinessType.values()) |
| | | .filter(type -> type != AdminBusinessType.OTHER) |
| | | .sorted(Comparator.comparingInt(AdminBusinessType::getCode)) |
| | | .map(type -> { |
| | | Map<String, Object> item = new LinkedHashMap<>(); |
| | |
| | | return AjaxResult.success(list); |
| | | } |
| | | |
| | | @InnerAuth |
| | | @PostMapping("/saveLog") |
| | | // @InnerAuth |
| | | @PostMapping("/logan/adminOperationLog/saveLog") |
| | | public R<Boolean> saveLog(@RequestBody XpAdminOperationLog adminOperationLog) { |
| | | if (adminOperationLog == null |
| | | || Integer.valueOf(AdminBusinessType.OTHER.getCode()).equals(adminOperationLog.getBusinessType()) |
| | | || Integer.valueOf(AdminOperationType.OTHER.getCode()).equals(adminOperationLog.getOperationType())) { |
| | | return R.ok(false); |
| | | } |
| | | if (adminOperationLog.getOperationTime() == null) { |
| | | adminOperationLog.setOperationTime(LocalDateTime.now()); |
| | | } |
| | | adminOperationLog.setCreateTime(LocalDateTime.now()); |
| | | return R.ok(adminOperationLogService.save(adminOperationLog)); |
| | | } |
| | | |
| | | private XpAdminOperationLogExportExcel toExportRow(XpAdminOperationLog log) { |
| | | XpAdminOperationLogExportExcel row = new XpAdminOperationLogExportExcel(); |
| | | row.setBusinessTypeName(log.getBusinessTypeName()); |
| | | row.setOperationTypeName(log.getOperationTypeName()); |
| | | row.setOperatorName(log.getOperatorName()); |
| | | row.setOperatorAccount(log.getOperatorAccount()); |
| | | row.setOperationContent(log.getOperationContent()); |
| | | row.setOperationIp(log.getOperationIp()); |
| | | row.setRequestUri(log.getRequestUri()); |
| | | row.setRequestMethod(log.getRequestMethod()); |
| | | row.setStatusName(Integer.valueOf(0).equals(log.getStatus()) ? "成功" : "失败"); |
| | | row.setErrorMsg(log.getErrorMsg()); |
| | | row.setOperationTime(log.getOperationTime() == null ? null : EXPORT_TIME_FORMATTER.format(log.getOperationTime())); |
| | | return row; |
| | | } |
| | | } |