Optimize SysLogController
This commit is contained in:
@@ -30,12 +30,10 @@ class SysLogController(
|
||||
) {
|
||||
@Operation(summary = "获取")
|
||||
@GetMapping
|
||||
fun get(@Valid sysLogGetParam: SysLogGetParam): ResponseResult<PageVo<SysLogGetVo>> {
|
||||
fun get(@Valid sysLogGetParam: SysLogGetParam?): ResponseResult<PageVo<SysLogGetVo>> {
|
||||
return ResponseResult.success(
|
||||
ResponseCode.DATABASE_SELECT_SUCCESS, data = SysLogConverter.sysLogPageToSysLogPageVo(
|
||||
sysLogService.getPage(
|
||||
sysLogGetParam.currentPage, sysLogGetParam.pageSize
|
||||
)
|
||||
sysLogService.getPage(sysLogGetParam)
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
@@ -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)
|
||||
|
||||
@@ -15,5 +15,5 @@ import org.apache.ibatis.annotations.Mapper
|
||||
*/
|
||||
@Mapper
|
||||
interface SysLogMapper : BaseMapper<SysLog> {
|
||||
fun selectPage(page: IPage<SysLog>): IPage<SysLog>
|
||||
fun selectPage(page: IPage<SysLog>, logType: List<String>?, requestMethod: List<String>?): IPage<SysLog>
|
||||
}
|
||||
|
||||
@@ -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()
|
||||
@@ -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
|
||||
|
||||
/**
|
||||
* <p>
|
||||
@@ -13,5 +14,5 @@ import top.fatweb.api.entity.system.SysLog
|
||||
* @since 2023-10-18
|
||||
*/
|
||||
interface ISysLogService : IService<SysLog> {
|
||||
fun getPage(page: Long, pageSize: Long): IPage<SysLog>
|
||||
fun getPage(sysLogGetParam: SysLogGetParam?): IPage<SysLog>
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
/**
|
||||
* <p>
|
||||
@@ -19,10 +21,24 @@ import top.fatweb.api.service.system.ISysLogService
|
||||
*/
|
||||
@Service
|
||||
class SysLogServiceImpl : ServiceImpl<SysLogMapper, SysLog>(), ISysLogService {
|
||||
override fun getPage(page: Long, pageSize: Long): IPage<SysLog> {
|
||||
val sysLogPage = Page<SysLog>(page, pageSize)
|
||||
sysLogPage.addOrder(OrderItem.desc("start_time"))
|
||||
override fun getPage(sysLogGetParam: SysLogGetParam?): IPage<SysLog> {
|
||||
val sysLogPage = Page<SysLog>(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(","))
|
||||
}
|
||||
}
|
||||
|
||||
35
src/main/kotlin/top/fatweb/api/util/StrUtil.kt
Normal file
35
src/main/kotlin/top/fatweb/api/util/StrUtil.kt
Normal file
@@ -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()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user