From b4f1b655dc57cc90caaf93922649170b14545d31 Mon Sep 17 00:00:00 2001 From: FatttSnake Date: Fri, 3 Nov 2023 18:24:58 +0800 Subject: [PATCH] Optimize get sys log api --- .../api/controller/system/SysLogController.kt | 5 +-- .../api/converter/system/SysLogConverter.kt | 30 +++++++------- .../top/fatweb/api/entity/system/SysLog.kt | 9 +++-- .../api/interceptor/SysLogInterceptor.kt | 9 ++--- .../fatweb/api/mapper/system/SysLogMapper.kt | 5 ++- .../kotlin/top/fatweb/api/param/PageParam.kt | 14 +++++++ .../api/param/authentication/LoginParam.kt | 12 +++--- .../fatweb/api/param/system/SysLogGetParam.kt | 16 ++------ .../service/system/impl/SysLogServiceImpl.kt | 7 +--- .../top/fatweb/api/vo/system/SysLogGetVo.kt | 6 ++- .../V1_0_0_231020__Add_table_'t_sys_log'.sql | 4 +- .../resources/mapper/system/SysLogMapper.xml | 40 +++++++++++++++++++ 12 files changed, 102 insertions(+), 55 deletions(-) create mode 100644 src/main/kotlin/top/fatweb/api/param/PageParam.kt diff --git a/src/main/kotlin/top/fatweb/api/controller/system/SysLogController.kt b/src/main/kotlin/top/fatweb/api/controller/system/SysLogController.kt index 1d2be0b..2cfcaba 100644 --- a/src/main/kotlin/top/fatweb/api/controller/system/SysLogController.kt +++ b/src/main/kotlin/top/fatweb/api/controller/system/SysLogController.kt @@ -4,7 +4,6 @@ import io.swagger.v3.oas.annotations.Operation import io.swagger.v3.oas.annotations.tags.Tag import jakarta.validation.Valid import org.springframework.web.bind.annotation.GetMapping -import org.springframework.web.bind.annotation.RequestBody import org.springframework.web.bind.annotation.RequestMapping import org.springframework.web.bind.annotation.RestController import top.fatweb.api.converter.system.SysLogConverter @@ -31,11 +30,11 @@ class SysLogController( ) { @Operation(summary = "获取") @GetMapping - fun get(@Valid @RequestBody sysLogGetParam: SysLogGetParam): ResponseResult> { + fun get(@Valid sysLogGetParam: SysLogGetParam): ResponseResult> { return ResponseResult.success( ResponseCode.DATABASE_SELECT_SUCCESS, data = SysLogConverter.sysLogPageToSysLogPageVo( sysLogService.getPage( - sysLogGetParam.page, sysLogGetParam.pageSize + sysLogGetParam.currentPage, sysLogGetParam.pageSize ) ) ) diff --git a/src/main/kotlin/top/fatweb/api/converter/system/SysLogConverter.kt b/src/main/kotlin/top/fatweb/api/converter/system/SysLogConverter.kt index d7ddad5..a12f1b9 100644 --- a/src/main/kotlin/top/fatweb/api/converter/system/SysLogConverter.kt +++ b/src/main/kotlin/top/fatweb/api/converter/system/SysLogConverter.kt @@ -13,21 +13,21 @@ object SysLogConverter { syslogPage.current, syslogPage.records.map { SysLogGetVo( - it.id, - it.logType, - it.operateUserId, - it.operateTime, - it.requestUri, - it.requestMethod, - it.requestParams, - it.requestIp, - it.requestServerAddress, - it.isException, - it.exceptionInfo, - it.startTime, - it.endTime, - it.executeTime, - it.userAgent + id = it.id, + logType = it.logType, + operateUserId = it.operateUserId, + operateTime = it.operateTime, + requestUri = it.requestUri, + requestMethod = it.requestMethod, + requestParams = it.requestParams, + requestIp = it.requestIp, + requestServerAddress = it.requestServerAddress, + exception = it.exception == 1, + exceptionInfo = it.exceptionInfo, + startTime = it.startTime, + endTime = it.endTime, + executeTime = it.executeTime, + userAgent = it.userAgent ) }) diff --git a/src/main/kotlin/top/fatweb/api/entity/system/SysLog.kt b/src/main/kotlin/top/fatweb/api/entity/system/SysLog.kt index 29b12e7..304228c 100644 --- a/src/main/kotlin/top/fatweb/api/entity/system/SysLog.kt +++ b/src/main/kotlin/top/fatweb/api/entity/system/SysLog.kt @@ -71,8 +71,8 @@ class SysLog : Serializable { /** * 是否异常 */ - @TableField("is_exception") - var isException: Int? = null + @TableField("exception") + var exception: Int? = null /** * 异常信息 @@ -104,7 +104,10 @@ class SysLog : Serializable { @TableField("user_agent") var userAgent: String? = null + @TableField(exist = false) + var operateUsername: String? = null + override fun toString(): String { - return "SysLog(id=$id, logType=$logType, operateUserId=$operateUserId, operateTime=$operateTime, requestUri=$requestUri, requestMethod=$requestMethod, requestParams=$requestParams, requestIp=$requestIp, requestServerAddress=$requestServerAddress, isException=$isException, exceptionInfo=$exceptionInfo, startTime=$startTime, endTime=$endTime, executeTime=$executeTime, userAgent=$userAgent)" + return "SysLog(id=$id, logType=$logType, operateUserId=$operateUserId, operateTime=$operateTime, requestUri=$requestUri, requestMethod=$requestMethod, requestParams=$requestParams, requestIp=$requestIp, requestServerAddress=$requestServerAddress, exception=$exception, exceptionInfo=$exceptionInfo, startTime=$startTime, endTime=$endTime, executeTime=$executeTime, userAgent=$userAgent, operateUsername=$operateUsername)" } } diff --git a/src/main/kotlin/top/fatweb/api/interceptor/SysLogInterceptor.kt b/src/main/kotlin/top/fatweb/api/interceptor/SysLogInterceptor.kt index ce26b0e..e128bd6 100644 --- a/src/main/kotlin/top/fatweb/api/interceptor/SysLogInterceptor.kt +++ b/src/main/kotlin/top/fatweb/api/interceptor/SysLogInterceptor.kt @@ -2,8 +2,6 @@ package top.fatweb.api.interceptor import jakarta.servlet.http.HttpServletRequest import jakarta.servlet.http.HttpServletResponse -import org.slf4j.Logger -import org.slf4j.LoggerFactory import org.springframework.core.MethodParameter import org.springframework.http.MediaType import org.springframework.http.converter.HttpMessageConverter @@ -12,8 +10,8 @@ import org.springframework.http.server.ServerHttpResponse import org.springframework.web.bind.annotation.ControllerAdvice import org.springframework.web.servlet.HandlerInterceptor import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice -import top.fatweb.api.entity.system.SysLog import top.fatweb.api.entity.common.ResponseResult +import top.fatweb.api.entity.system.SysLog import top.fatweb.api.service.system.ISysLogService import top.fatweb.api.util.WebUtil import java.net.URI @@ -27,7 +25,6 @@ import java.util.concurrent.Executor class SysLogInterceptor( private val customThreadPoolTaskExecutor: Executor, private val sysLogService: ISysLogService ) : HandlerInterceptor, ResponseBodyAdvice { - private val logger: Logger = LoggerFactory.getLogger(this::class.java) private val sysLogThreadLocal = ThreadLocal() private val resultThreadLocal = ThreadLocal() @@ -59,12 +56,12 @@ class SysLogInterceptor( if (result.success) { sysLog.apply { logType = "INFO" - isException = 0 + exception = 0 } } else { sysLog.apply { logType = "ERROR" - isException = 1 + exception = 1 exceptionInfo = result.msg } } diff --git a/src/main/kotlin/top/fatweb/api/mapper/system/SysLogMapper.kt b/src/main/kotlin/top/fatweb/api/mapper/system/SysLogMapper.kt index 857a59a..6efc235 100644 --- a/src/main/kotlin/top/fatweb/api/mapper/system/SysLogMapper.kt +++ b/src/main/kotlin/top/fatweb/api/mapper/system/SysLogMapper.kt @@ -2,6 +2,7 @@ package top.fatweb.api.mapper.system import top.fatweb.api.entity.system.SysLog import com.baomidou.mybatisplus.core.mapper.BaseMapper +import com.baomidou.mybatisplus.core.metadata.IPage import org.apache.ibatis.annotations.Mapper /** @@ -13,4 +14,6 @@ import org.apache.ibatis.annotations.Mapper * @since 2023-10-18 */ @Mapper -interface SysLogMapper : BaseMapper +interface SysLogMapper : BaseMapper { + fun selectPage(page: IPage): IPage +} diff --git a/src/main/kotlin/top/fatweb/api/param/PageParam.kt b/src/main/kotlin/top/fatweb/api/param/PageParam.kt new file mode 100644 index 0000000..1478722 --- /dev/null +++ b/src/main/kotlin/top/fatweb/api/param/PageParam.kt @@ -0,0 +1,14 @@ +package top.fatweb.api.param + +import io.swagger.v3.oas.annotations.media.Schema +import jakarta.validation.constraints.Min + +open class PageParam { + @Schema(description = "分页页码", example = "1", defaultValue = "1") + @field:Min(1, message = "Pagination page number must be a positive integer") + var currentPage: Long = 1 + + @Schema(description = "分页大小", example = "20", defaultValue = "20") + @field:Min(1, message = "The number of data per page must be a positive integer") + var pageSize: Long = 20 +} \ No newline at end of file diff --git a/src/main/kotlin/top/fatweb/api/param/authentication/LoginParam.kt b/src/main/kotlin/top/fatweb/api/param/authentication/LoginParam.kt index 84d15e1..3ed78f2 100644 --- a/src/main/kotlin/top/fatweb/api/param/authentication/LoginParam.kt +++ b/src/main/kotlin/top/fatweb/api/param/authentication/LoginParam.kt @@ -2,16 +2,14 @@ package top.fatweb.api.param.authentication import io.swagger.v3.oas.annotations.media.Schema import jakarta.validation.constraints.NotBlank -import java.io.Serializable @Schema(description = "登录请求参数") -class LoginParam : Serializable { - +data class LoginParam( @Schema(description = "用户名", example = "test", required = true) - @NotBlank(message = "Username can not be blank") - val username: String? = null + @field:NotBlank(message = "Username can not be blank") + val username: String? = null, @Schema(description = "密码", example = "test123456", required = true) - @NotBlank(message = "Password can not be blank") + @field:NotBlank(message = "Password can not be blank") val password: String? = null -} \ No newline at end of file +) \ No newline at end of file diff --git a/src/main/kotlin/top/fatweb/api/param/system/SysLogGetParam.kt b/src/main/kotlin/top/fatweb/api/param/system/SysLogGetParam.kt index 9fcb3a3..4f9ffe5 100644 --- a/src/main/kotlin/top/fatweb/api/param/system/SysLogGetParam.kt +++ b/src/main/kotlin/top/fatweb/api/param/system/SysLogGetParam.kt @@ -1,17 +1,9 @@ package top.fatweb.api.param.system import io.swagger.v3.oas.annotations.media.Schema -import jakarta.validation.constraints.Min -import java.io.Serializable +import top.fatweb.api.param.PageParam @Schema(description = "获取系统日志请求参数") -class SysLogGetParam : Serializable { - - @Schema(description = "分页页码", example = "1", defaultValue = "1") - @Min(1, message = "Pagination page number must be a positive integer") - val page: Long = 1 - - @Schema(description = "分页大小", example = "20", defaultValue = "20") - @Min(1, message = "The number of data per page must be a positive integer") - val pageSize: Long = 20 -} \ No newline at end of file +data class SysLogGetParam( + val logType: String? = null +) : PageParam() \ No newline at end of file diff --git a/src/main/kotlin/top/fatweb/api/service/system/impl/SysLogServiceImpl.kt b/src/main/kotlin/top/fatweb/api/service/system/impl/SysLogServiceImpl.kt index 10cfa10..ba72dd9 100644 --- a/src/main/kotlin/top/fatweb/api/service/system/impl/SysLogServiceImpl.kt +++ b/src/main/kotlin/top/fatweb/api/service/system/impl/SysLogServiceImpl.kt @@ -1,7 +1,6 @@ package top.fatweb.api.service.system.impl import com.baomidou.mybatisplus.core.metadata.IPage -import com.baomidou.mybatisplus.extension.kotlin.KtQueryWrapper import com.baomidou.mybatisplus.extension.plugins.pagination.Page import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl import org.springframework.stereotype.Service @@ -20,10 +19,8 @@ import top.fatweb.api.service.system.ISysLogService @Service class SysLogServiceImpl : ServiceImpl(), ISysLogService { override fun getPage(page: Long, pageSize: Long): IPage { - var sysLogPage = Page(page, pageSize) + val sysLogPage = Page(page, pageSize) - sysLogPage = baseMapper.selectPage(sysLogPage, KtQueryWrapper(SysLog()).orderByDesc(SysLog::id)) - - return sysLogPage + return baseMapper.selectPage(sysLogPage) } } diff --git a/src/main/kotlin/top/fatweb/api/vo/system/SysLogGetVo.kt b/src/main/kotlin/top/fatweb/api/vo/system/SysLogGetVo.kt index 42fe7f7..b3ed3b0 100644 --- a/src/main/kotlin/top/fatweb/api/vo/system/SysLogGetVo.kt +++ b/src/main/kotlin/top/fatweb/api/vo/system/SysLogGetVo.kt @@ -1,16 +1,20 @@ package top.fatweb.api.vo.system +import com.fasterxml.jackson.databind.annotation.JsonSerialize +import com.fasterxml.jackson.databind.ser.std.ToStringSerializer import io.swagger.v3.oas.annotations.media.Schema import java.time.LocalDateTime @Schema(description = "获取系统日志返回参数") class SysLogGetVo( + @JsonSerialize(using = ToStringSerializer::class) val id: Long?, @Schema(description = "日志类型") val logType: String?, @Schema(description = "操作用户 ID") + @JsonSerialize(using = ToStringSerializer::class) val operateUserId: Long?, @Schema(description = "操作时间") @@ -32,7 +36,7 @@ class SysLogGetVo( val requestServerAddress: String?, @Schema(description = "是否异常") - val isException: Int?, + val exception: Boolean?, @Schema(description = "异常信息") val exceptionInfo: String?, diff --git a/src/main/resources/db/migration/V1_0_0_231020__Add_table_'t_sys_log'.sql b/src/main/resources/db/migration/V1_0_0_231020__Add_table_'t_sys_log'.sql index 5816ccb..626377c 100644 --- a/src/main/resources/db/migration/V1_0_0_231020__Add_table_'t_sys_log'.sql +++ b/src/main/resources/db/migration/V1_0_0_231020__Add_table_'t_sys_log'.sql @@ -10,7 +10,7 @@ create table t_sys_log request_params text comment '请求参数', request_ip varchar(20) not null comment '请求 IP', request_server_address varchar(50) not null comment '请求服务器地址', - is_exception int not null default 0 comment '是否异常', + exception int not null default 0 comment '是否异常', exception_info text comment '异常信息', start_time datetime(3) not null comment '开始时间', end_time datetime(3) not null comment '结束时间', @@ -19,6 +19,6 @@ create table t_sys_log primary key (id) using btree, key idx_sys_log_log_type (log_type) using btree, key idx_sys_log_operate_user_id (operate_user_id) using btree, - key idx_sys_log_is_exception (is_exception) using btree, + key idx_sys_log_exception (exception) using btree, key idx_sys_log_operate_time (operate_time) using btree ) comment '系统日志表'; \ No newline at end of file diff --git a/src/main/resources/mapper/system/SysLogMapper.xml b/src/main/resources/mapper/system/SysLogMapper.xml index 208129b..85a1725 100644 --- a/src/main/resources/mapper/system/SysLogMapper.xml +++ b/src/main/resources/mapper/system/SysLogMapper.xml @@ -1,5 +1,45 @@ + + + + + + + + + + + + + + + + + + + +