From 0ff2698f339bf29a2bd2f97a161aad3faa6a5c02 Mon Sep 17 00:00:00 2001 From: FatttSnake Date: Wed, 8 Nov 2023 18:18:48 +0800 Subject: [PATCH] Optimize SysLogController --- .../api/controller/system/SysLogController.kt | 6 +-- .../fatweb/api/entity/common/ResponseCode.kt | 3 +- .../fatweb/api/handler/ExceptionHandler.kt | 6 +++ .../fatweb/api/mapper/system/SysLogMapper.kt | 2 +- .../fatweb/api/param/system/SysLogGetParam.kt | 16 ++++++- .../api/service/system/ISysLogService.kt | 3 +- .../service/system/impl/SysLogServiceImpl.kt | 24 ++++++++-- .../kotlin/top/fatweb/api/util/StrUtil.kt | 35 +++++++++++++++ .../resources/mapper/system/SysLogMapper.xml | 45 +++++++++++-------- .../fatweb/api/FatWebApiApplicationTests.kt | 11 +++++ 10 files changed, 121 insertions(+), 30 deletions(-) create mode 100644 src/main/kotlin/top/fatweb/api/util/StrUtil.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 2cfcaba..6e1cf42 100644 --- a/src/main/kotlin/top/fatweb/api/controller/system/SysLogController.kt +++ b/src/main/kotlin/top/fatweb/api/controller/system/SysLogController.kt @@ -30,12 +30,10 @@ class SysLogController( ) { @Operation(summary = "获取") @GetMapping - fun get(@Valid sysLogGetParam: SysLogGetParam): ResponseResult> { + fun get(@Valid sysLogGetParam: SysLogGetParam?): ResponseResult> { return ResponseResult.success( ResponseCode.DATABASE_SELECT_SUCCESS, data = SysLogConverter.sysLogPageToSysLogPageVo( - sysLogService.getPage( - sysLogGetParam.currentPage, sysLogGetParam.pageSize - ) + sysLogService.getPage(sysLogGetParam) ) ) } diff --git a/src/main/kotlin/top/fatweb/api/entity/common/ResponseCode.kt b/src/main/kotlin/top/fatweb/api/entity/common/ResponseCode.kt index e536c40..222e39c 100644 --- a/src/main/kotlin/top/fatweb/api/entity/common/ResponseCode.kt +++ b/src/main/kotlin/top/fatweb/api/entity/common/ResponseCode.kt @@ -27,7 +27,8 @@ enum class ResponseCode(val code: Int) { DATABASE_UPDATE_SUCCESS(BusinessCode.DATABASE, 20), DATABASE_UPDATE_FILED(BusinessCode.DATABASE, 25), DATABASE_DELETE_SUCCESS(BusinessCode.DATABASE, 30), - DATABASE_DELETE_FILED(BusinessCode.DATABASE, 35); + DATABASE_DELETE_FILED(BusinessCode.DATABASE, 35), + DATABASE_EXECUTE_ERROR(BusinessCode.DATABASE, 40); constructor(businessCode: BusinessCode, code: Int) : this(businessCode.code * 100 + code) } \ No newline at end of file diff --git a/src/main/kotlin/top/fatweb/api/handler/ExceptionHandler.kt b/src/main/kotlin/top/fatweb/api/handler/ExceptionHandler.kt index 98c0bbf..398a19a 100644 --- a/src/main/kotlin/top/fatweb/api/handler/ExceptionHandler.kt +++ b/src/main/kotlin/top/fatweb/api/handler/ExceptionHandler.kt @@ -6,6 +6,7 @@ import com.auth0.jwt.exceptions.TokenExpiredException import org.slf4j.Logger import org.slf4j.LoggerFactory import org.springframework.http.converter.HttpMessageNotReadableException +import org.springframework.jdbc.BadSqlGrammarException import org.springframework.security.authentication.BadCredentialsException import org.springframework.security.authentication.InsufficientAuthenticationException import org.springframework.security.authentication.InternalAuthenticationServiceException @@ -70,6 +71,11 @@ class ExceptionHandler { ResponseResult.fail(ResponseCode.SYSTEM_TOKEN_HAS_EXPIRED, e.localizedMessage, null) } + is BadSqlGrammarException -> { + logger.debug(e.localizedMessage, e) + ResponseResult.fail(ResponseCode.DATABASE_EXECUTE_ERROR, "Incorrect SQL syntax", null) + } + else -> { logger.error(e.localizedMessage, e) ResponseResult.fail(ResponseCode.SYSTEM_ERROR, data = null) 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 6efc235..5b2f979 100644 --- a/src/main/kotlin/top/fatweb/api/mapper/system/SysLogMapper.kt +++ b/src/main/kotlin/top/fatweb/api/mapper/system/SysLogMapper.kt @@ -15,5 +15,5 @@ import org.apache.ibatis.annotations.Mapper */ @Mapper interface SysLogMapper : BaseMapper { - fun selectPage(page: IPage): IPage + fun selectPage(page: IPage, logType: List?, requestMethod: List?): IPage } 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 4f9ffe5..c0fb4c2 100644 --- a/src/main/kotlin/top/fatweb/api/param/system/SysLogGetParam.kt +++ b/src/main/kotlin/top/fatweb/api/param/system/SysLogGetParam.kt @@ -5,5 +5,19 @@ import top.fatweb.api.param.PageParam @Schema(description = "获取系统日志请求参数") data class SysLogGetParam( - val logType: String? = null + @Schema(description = "排序字段", example = "id") + val sortField: String? = null, + + @Schema(description = "排序方式", example = "desc", allowableValues = ["desc", "asc"]) + val sortOrder: String? = null, + + @Schema(description = "类型过滤(多个使用逗号分隔)", example = "INFO", allowableValues = ["INFO", "ERROR"]) + val logType: String? = null, + + @Schema( + description = "请求方式过滤(多个使用逗号分隔)", + example = "GET,POST", + allowableValues = ["GET", "POST", "PUT", "PATCH", "DELETE", "DELETE", "OPTIONS"] + ) + val requestMethod: String? = null ) : PageParam() \ No newline at end of file diff --git a/src/main/kotlin/top/fatweb/api/service/system/ISysLogService.kt b/src/main/kotlin/top/fatweb/api/service/system/ISysLogService.kt index 4938d41..1aa0fb6 100644 --- a/src/main/kotlin/top/fatweb/api/service/system/ISysLogService.kt +++ b/src/main/kotlin/top/fatweb/api/service/system/ISysLogService.kt @@ -3,6 +3,7 @@ package top.fatweb.api.service.system import com.baomidou.mybatisplus.core.metadata.IPage import com.baomidou.mybatisplus.extension.service.IService import top.fatweb.api.entity.system.SysLog +import top.fatweb.api.param.system.SysLogGetParam /** *

@@ -13,5 +14,5 @@ import top.fatweb.api.entity.system.SysLog * @since 2023-10-18 */ interface ISysLogService : IService { - fun getPage(page: Long, pageSize: Long): IPage + fun getPage(sysLogGetParam: SysLogGetParam?): IPage } 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 129b348..cc1c891 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 @@ -7,7 +7,9 @@ import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl import org.springframework.stereotype.Service import top.fatweb.api.entity.system.SysLog import top.fatweb.api.mapper.system.SysLogMapper +import top.fatweb.api.param.system.SysLogGetParam import top.fatweb.api.service.system.ISysLogService +import top.fatweb.api.util.StrUtil /** *

@@ -19,10 +21,24 @@ import top.fatweb.api.service.system.ISysLogService */ @Service class SysLogServiceImpl : ServiceImpl(), ISysLogService { - override fun getPage(page: Long, pageSize: Long): IPage { - val sysLogPage = Page(page, pageSize) - sysLogPage.addOrder(OrderItem.desc("start_time")) + override fun getPage(sysLogGetParam: SysLogGetParam?): IPage { + val sysLogPage = Page(sysLogGetParam?.currentPage ?: 1, sysLogGetParam?.pageSize ?: 20) - return baseMapper.selectPage(sysLogPage) + if (sysLogGetParam?.sortField == null && sysLogGetParam?.sortOrder == null) { + sysLogPage.addOrder(OrderItem.desc("start_time")) + } else { + sysLogPage.addOrder( + if (sysLogGetParam.sortOrder?.startsWith( + "desc", true + ) == true + ) OrderItem.desc(StrUtil.upperToUnderLetter(sysLogGetParam.sortField)) else OrderItem.asc( + StrUtil.upperToUnderLetter( + sysLogGetParam.sortField + ) + ) + ) + } + + return baseMapper.selectPage(sysLogPage, sysLogGetParam?.logType?.split(","), sysLogGetParam?.requestMethod?.split(",")) } } diff --git a/src/main/kotlin/top/fatweb/api/util/StrUtil.kt b/src/main/kotlin/top/fatweb/api/util/StrUtil.kt new file mode 100644 index 0000000..e2296a1 --- /dev/null +++ b/src/main/kotlin/top/fatweb/api/util/StrUtil.kt @@ -0,0 +1,35 @@ +package top.fatweb.api.util + +import java.util.regex.Pattern + +object StrUtil { + fun upperToUnderLetter(str: String?): String { + str ?: let { return "" } + + val matcher = Pattern.compile("([A-Z])").matcher(str) + val stringBuilder = StringBuilder() + + while (matcher.find()) { + matcher.appendReplacement(stringBuilder, "_" + matcher.group().lowercase()) + } + + matcher.appendTail(stringBuilder) + + return stringBuilder.toString() + } + + fun underToUpperLetter(str: String?): String { + str ?: let { return "" } + + val matcher = Pattern.compile("(_)([a-z])").matcher(str) + val stringBuilder = StringBuilder() + + while (matcher.find()) { + matcher.appendReplacement(stringBuilder, matcher.group().replace("_", "").uppercase()) + } + + matcher.appendTail(stringBuilder) + + return stringBuilder.toString() + } +} \ 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 85a1725..70c9c22 100644 --- a/src/main/resources/mapper/system/SysLogMapper.xml +++ b/src/main/resources/mapper/system/SysLogMapper.xml @@ -2,25 +2,34 @@ diff --git a/src/test/kotlin/top/fatweb/api/FatWebApiApplicationTests.kt b/src/test/kotlin/top/fatweb/api/FatWebApiApplicationTests.kt index 9b97a3b..ff42471 100644 --- a/src/test/kotlin/top/fatweb/api/FatWebApiApplicationTests.kt +++ b/src/test/kotlin/top/fatweb/api/FatWebApiApplicationTests.kt @@ -10,6 +10,7 @@ import org.springframework.test.context.junit.jupiter.SpringExtension import top.fatweb.api.properties.SecurityProperties import top.fatweb.api.util.ByteUtil import top.fatweb.api.util.JwtUtil +import top.fatweb.api.util.StrUtil import kotlin.io.encoding.Base64 import kotlin.io.encoding.ExperimentalEncodingApi @@ -51,4 +52,14 @@ class FatWebApiApplicationTests { val bytes = avatar.createAsPngBytes(1232132134543L) logger.info(Base64.encode(bytes)) } + + @Test + fun upperToUnderLetterTest() { + assertEquals("create_time", StrUtil.upperToUnderLetter("createTime")) + } + + @Test + fun underToUpperLetterTest() { + assertEquals("createTime", StrUtil.underToUpperLetter("create_time")) + } }