Quellcode durchsuchen

导出操作日志

zhulu vor 10 Monaten
Ursprung
Commit
b10fde9d25

+ 26 - 0
soc-api/soc-api-system/src/main/java/com/xunmei/system/api/domain/SysOperLog.java

@@ -77,6 +77,12 @@ public class SysOperLog extends BaseEntity
     /** 消耗时间 */
     private Long costTime;
 
+    @TableField(exist = false)
+    private Date beginDate;
+
+    @TableField(exist = false)
+    private Date endDate;
+
     public Long getOperId()
     {
         return operId;
@@ -246,4 +252,24 @@ public class SysOperLog extends BaseEntity
     {
         this.costTime = costTime;
     }
+
+    public Date getBeginDate()
+    {
+        return beginDate;
+    }
+
+    public void setBeginDate(Date beginDate)
+    {
+        this.beginDate = beginDate;
+    }
+
+    public Date getEndDate()
+    {
+        return endDate;
+    }
+
+    public void setEndDate(Date endDate)
+    {
+        this.endDate = endDate;
+    }
 }

+ 31 - 1
soc-modules/soc-modules-system/src/main/java/com/xunmei/system/controller/SysOperlogController.java

@@ -1,6 +1,9 @@
 package com.xunmei.system.controller;
 
 import cn.hutool.core.date.DateUtil;
+import cn.hutool.core.util.ObjectUtil;
+import com.alibaba.excel.EasyExcel;
+import com.alibaba.excel.write.style.column.LongestMatchColumnWidthStyleStrategy;
 import com.xunmei.common.core.web.controller.BaseController;
 import com.xunmei.common.core.web.domain.AjaxResult;
 import com.xunmei.common.core.web.page.TableDataInfo;
@@ -10,12 +13,17 @@ import com.xunmei.common.security.annotation.InnerAuth;
 import com.xunmei.common.security.annotation.RequiresPermissions;
 import com.xunmei.system.api.domain.SysOperLog;
 import com.xunmei.system.service.ISysOperLogService;
+import com.xunmei.system.util.OperLogExport;
+import com.xunmei.system.util.SecurityUserExport;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.web.bind.annotation.*;
 
 import javax.servlet.http.HttpServletResponse;
+import java.net.URLEncoder;
+import java.util.ArrayList;
 import java.util.Date;
 import java.util.List;
+import java.util.concurrent.atomic.AtomicInteger;
 
 /**
  * 操作日志记录
@@ -41,7 +49,29 @@ public class SysOperlogController extends BaseController
     @PostMapping("/export")
     public void export(HttpServletResponse response, SysOperLog operLog)
     {
-        List<SysOperLog> list = operLogService.selectOperLogList(operLog);
+        List<OperLogExport> list = operLogService.exportOperLogList(operLog);
+        if (ObjectUtil.isEmpty(list)) {
+            throw new RuntimeException("导出数据为空!");
+        }
+        AtomicInteger xh = new AtomicInteger();
+        xh.getAndIncrement();
+        list.forEach(e -> {
+            e.setXh(String.valueOf(xh.getAndIncrement()));
+        });
+        try {
+            // 设置响应头
+            response.addHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode("操作日志数据", "utf-8"));
+            response.setContentType("application/octet-stream;charset=UTF-8");
+            response.setCharacterEncoding("utf-8");
+            // 数据导出
+            EasyExcel.write(response.getOutputStream(), OperLogExport.class)
+                    .registerWriteHandler(new LongestMatchColumnWidthStyleStrategy()).sheet("操作日志").doWrite(list);
+        } catch (Exception e) {
+            // 重置response
+            response.reset();
+            response.setContentType("application/json");
+            response.setCharacterEncoding("utf-8");
+        }
     }
 
     @Log(title = "操作日志", businessType = BusinessType.DELETE)

+ 9 - 0
soc-modules/soc-modules-system/src/main/java/com/xunmei/system/mapper/SysOperLogMapper.java

@@ -3,6 +3,7 @@ package com.xunmei.system.mapper;
 import com.baomidou.mybatisplus.core.mapper.BaseMapper;
 import com.xunmei.system.api.domain.SysOperLog;
 import com.xunmei.system.domain.SysNotice;
+import com.xunmei.system.util.OperLogExport;
 
 import java.util.Date;
 import java.util.List;
@@ -30,6 +31,14 @@ public interface SysOperLogMapper extends BaseMapper<SysOperLog>
      List<SysOperLog> selectOperLogList(SysOperLog operLog);
 
     /**
+     * 查询系统操作日志集合
+     *
+     * @param operLog 操作日志对象
+     * @return 操作日志集合
+     */
+    List<OperLogExport> exportOperLogList(SysOperLog operLog);
+
+    /**
      * 批量删除系统操作日志
      * 
      * @param operIds 需要删除的操作日志ID

+ 9 - 0
soc-modules/soc-modules-system/src/main/java/com/xunmei/system/service/ISysOperLogService.java

@@ -5,6 +5,7 @@ import com.xunmei.common.core.web.page.TableDataInfo;
 import com.xunmei.system.api.domain.SysLogininfor;
 import com.xunmei.system.api.domain.SysOperLog;
 import com.xunmei.system.domain.SysNotice;
+import com.xunmei.system.util.OperLogExport;
 
 import java.util.Date;
 import java.util.List;
@@ -40,6 +41,14 @@ public interface ISysOperLogService extends IService<SysOperLog>
     List<SysOperLog> selectOperLogList(SysOperLog operLog);
 
     /**
+     * 导出系统操作日志集合
+     *
+     * @param operLog 操作日志对象
+     * @return 操作日志集合
+     */
+    List<OperLogExport> exportOperLogList(SysOperLog operLog);
+
+    /**
      * 批量删除系统操作日志
      *
      * @param operIds 需要删除的操作日志ID

+ 16 - 0
soc-modules/soc-modules-system/src/main/java/com/xunmei/system/service/impl/SysOperLogServiceImpl.java

@@ -11,6 +11,7 @@ import com.xunmei.system.domain.SysNotice;
 import com.xunmei.system.mapper.SysNoticeMapper;
 import com.xunmei.system.mapper.SysOperLogMapper;
 import com.xunmei.system.service.ISysOperLogService;
+import com.xunmei.system.util.OperLogExport;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 
@@ -72,6 +73,21 @@ public class SysOperLogServiceImpl extends ServiceImpl<SysOperLogMapper, SysOper
         return operLogMapper.selectOperLogList(operLog);
     }
 
+    @Override
+    public List<OperLogExport> exportOperLogList(SysOperLog operLog) {
+
+        Object beginTime = operLog.getParams().get("beginTime");
+        Object endTime = operLog.getParams().get("endTime");
+        if(beginTime != null){
+            operLog.setBeginDate(DateUtils.parseDate(beginTime));
+        }
+        if(endTime != null) {
+            operLog.setEndDate(DateUtils.parseDate(endTime));
+        }
+        return operLogMapper.exportOperLogList(operLog);
+
+    }
+
     /**
      * 批量删除系统操作日志
      *

+ 72 - 0
soc-modules/soc-modules-system/src/main/java/com/xunmei/system/util/OperLogExport.java

@@ -0,0 +1,72 @@
+package com.xunmei.system.util;
+
+import com.alibaba.excel.annotation.ExcelIgnore;
+import com.alibaba.excel.annotation.ExcelProperty;
+import com.alibaba.excel.annotation.format.DateTimeFormat;
+import com.alibaba.excel.annotation.write.style.ColumnWidth;
+import com.alibaba.excel.annotation.write.style.ContentRowHeight;
+import com.alibaba.excel.annotation.write.style.HeadRowHeight;
+import com.baomidou.mybatisplus.annotation.TableField;
+import com.fasterxml.jackson.annotation.JsonFormat;
+import lombok.Data;
+
+import java.util.Date;
+
+/**
+ * 安保人员导出
+ * @author :LuoWei
+ * @date : 2023/11/23
+ */
+@Data
+@ColumnWidth(15) //列宽,最大值为255
+@HeadRowHeight(16) //表头行高
+@ContentRowHeight(16) //数据行高
+public class OperLogExport {
+
+
+    @ExcelProperty(value = "序号", index = 0)
+    private String xh;
+
+    @ExcelProperty(value = "日志编号", index = 1)
+    private String operId;
+
+    @ExcelProperty(value = "系统模块", index = 2)
+    private String title;
+
+    @ExcelProperty(value = "操作类型", index = 3)
+    private String businessType;
+
+    @ExcelProperty(value = "请求方式", index = 4)
+    private String requestMethod;
+
+    @ExcelProperty(value = "操作人员", index = 5)
+    private String operName;
+
+    @ExcelProperty(value = "主机", index = 6)
+    private String operIp;
+
+    @ExcelProperty(value = "操作状态", index = 7,converter = OperLogStatusConverter.class)
+    private String status;
+
+    @ExcelProperty(value = "操作日期", index = 8)
+    private Date operTime;
+
+    @ExcelProperty(value = "消耗时间", index = 9)
+    private String costTime;
+
+    @ExcelProperty(value = "请求地址", index = 10)
+    private String operUrl;
+
+    @ExcelProperty(value = "method", index = 11)
+    private String method;
+
+
+    @ExcelProperty(value = "请求参数", index = 12)
+    private String operParam;
+
+    @ExcelProperty(value = "返回参数", index = 13)
+    private String jsonResult;
+
+    @ExcelProperty(value= {"异常信息"}, index = 14)
+    private String errorMsg;
+}

+ 39 - 0
soc-modules/soc-modules-system/src/main/java/com/xunmei/system/util/OperLogStatusConverter.java

@@ -0,0 +1,39 @@
+package com.xunmei.system.util;
+
+import com.alibaba.excel.converters.Converter;
+import com.alibaba.excel.enums.CellDataTypeEnum;
+import com.alibaba.excel.metadata.CellData;
+import com.alibaba.excel.metadata.GlobalConfiguration;
+import com.alibaba.excel.metadata.property.ExcelContentProperty;
+public class OperLogStatusConverter implements Converter<String> {
+
+
+
+    @Override
+    public Class supportJavaTypeKey() {
+        return null;
+    }
+
+    @Override
+    public CellDataTypeEnum supportExcelTypeKey() {
+        return null;
+    }
+
+    @Override
+    public String convertToJavaData(CellData cellData, ExcelContentProperty excelContentProperty, GlobalConfiguration globalConfiguration) throws Exception {
+        return null;
+    }
+
+    @Override
+    public CellData convertToExcelData(String status, ExcelContentProperty excelContentProperty, GlobalConfiguration globalConfiguration) throws Exception {
+        if ("0".equals(status)) {
+            return new CellData("成功");
+        } else if ("1".equals(status)) {
+            return new CellData("失败");
+        }
+        else {
+            return new CellData(status+"");
+        }
+
+    }
+}

+ 48 - 0
soc-modules/soc-modules-system/src/main/resources/mapper/system/SysOperLogMapper.xml

@@ -23,6 +23,23 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
 		<result property="costTime"       column="cost_time"      />
 	</resultMap>
 
+	<resultMap type="com.xunmei.system.util.OperLogExport" id="SysOperLogExportResult">
+		<id     property="operId"         column="oper_id"        />
+		<result property="title"          column="title"          />
+		<result property="businessType"   column="business_type"  />
+		<result property="method"         column="method"         />
+		<result property="requestMethod"  column="request_method" />
+		<result property="operName"       column="oper_name"      />
+		<result property="operUrl"        column="oper_url"       />
+		<result property="operIp"         column="oper_ip"        />
+		<result property="operParam"      column="oper_param"     />
+		<result property="jsonResult"     column="json_result"    />
+		<result property="status"         column="status"         />
+		<result property="errorMsg"       column="error_msg"      />
+		<result property="operTime"       column="oper_time"      />
+		<result property="costTime"       column="cost_time"      />
+	</resultMap>
+
 	<sql id="selectOperLogVo">
         select oper_id, title, business_type, method, request_method, operator_type, oper_name, dept_name, oper_url, oper_ip, oper_param, json_result, status, error_msg, oper_time, cost_time
         from sys_oper_log
@@ -63,6 +80,37 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
 		</where>
 		order by oper_id desc
 	</select>
+
+	<select id="exportOperLogList" parameterType="com.xunmei.system.api.domain.SysOperLog" resultMap="SysOperLogExportResult">
+		<include refid="selectOperLogVo"/>
+		<where>
+			<if test="title != null and title != ''">
+				AND title like concat('%', #{title}, '%')
+			</if>
+			<if test="businessType != null">
+				AND business_type = #{businessType}
+			</if>
+			<if test="businessTypes != null and businessTypes.length > 0">
+				AND business_type in
+				<foreach collection="businessTypes" item="businessType" open="(" separator="," close=")">
+					#{businessType}
+				</foreach>
+			</if>
+			<if test="status != null">
+				AND status = #{status}
+			</if>
+			<if test="operName != null and operName != ''">
+				AND oper_name like concat('%', #{operName}, '%')
+			</if>
+			<if test="beginDate != null"><!-- 开始时间检索 -->
+				AND oper_time &gt;= #{beginDate}
+			</if>
+			<if test="endDate != null"><!-- 结束时间检索 -->
+				AND oper_time &lt;= #{endDate}
+			</if>
+		</where>
+		order by oper_id desc
+	</select>
 	
 	<delete id="deleteOperLogByIds" parameterType="Long">
  		delete from sys_oper_log where oper_id in