diff --git a/kernel-a-rule/src/main/java/cn/stylefeng/roses/kernel/rule/annotation/BizLog.java b/kernel-a-rule/src/main/java/cn/stylefeng/roses/kernel/rule/annotation/BizLog.java new file mode 100644 index 000000000..def240b38 --- /dev/null +++ b/kernel-a-rule/src/main/java/cn/stylefeng/roses/kernel/rule/annotation/BizLog.java @@ -0,0 +1,48 @@ + +/* + * Copyright [2020-2030] [https://www.stylefeng.cn] + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Guns采用APACHE LICENSE 2.0开源协议,您在使用过程中,需要注意以下几点: + * + * 1.请不要删除和修改根目录下的LICENSE文件。 + * 2.请不要删除和修改Guns源码头部的版权声明。 + * 3.请保留源码和相关描述文件的项目出处,作者声明等。 + * 4.分发源码时候,请注明软件出处 https://gitee.com/stylefeng/guns + * 5.在修改包名,模块名称,项目代码等时,请注明软件出处 https://gitee.com/stylefeng/guns + * 6.若您的项目无法满足以上几点,可申请商业授权 + */ +package cn.stylefeng.roses.kernel.rule.annotation; + +import cn.hutool.core.util.StrUtil; + +import java.lang.annotation.*; + +/** + * 用来标记在方法上,进行开启业务日志记录 + * + * @author fengshuonan + * @since 2023/7/21 15:37 + */ +@Target({ElementType.METHOD, ElementType.TYPE}) +@Retention(RetentionPolicy.RUNTIME) +@Documented +public @interface BizLog { + + /** + * 日志记录类型的编码 + */ + String logTypeCode() default StrUtil.EMPTY; + +} diff --git a/kernel-d-log/log-api/src/main/java/cn/stylefeng/roses/kernel/log/api/BizLogServiceApi.java b/kernel-d-log/log-api/src/main/java/cn/stylefeng/roses/kernel/log/api/BizLogServiceApi.java new file mode 100644 index 000000000..aba8d31d7 --- /dev/null +++ b/kernel-d-log/log-api/src/main/java/cn/stylefeng/roses/kernel/log/api/BizLogServiceApi.java @@ -0,0 +1,37 @@ +/* + * Copyright [2020-2030] [https://www.stylefeng.cn] + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Guns采用APACHE LICENSE 2.0开源协议,您在使用过程中,需要注意以下几点: + * + * 1.请不要删除和修改根目录下的LICENSE文件。 + * 2.请不要删除和修改Guns源码头部的版权声明。 + * 3.请保留源码和相关描述文件的项目出处,作者声明等。 + * 4.分发源码时候,请注明软件出处 https://gitee.com/stylefeng/guns + * 5.在修改包名,模块名称,项目代码等时,请注明软件出处 https://gitee.com/stylefeng/guns + * 6.若您的项目无法满足以上几点,可申请商业授权 + */ +package cn.stylefeng.roses.kernel.log.api; + +/** + * 业务日志service + * + * @author fengshuonan + * @since 2023/7/21 15:42 + */ +public interface BizLogServiceApi { + + + +} diff --git a/kernel-d-log/log-api/src/main/java/cn/stylefeng/roses/kernel/log/api/constants/LogFileConstants.java b/kernel-d-log/log-api/src/main/java/cn/stylefeng/roses/kernel/log/api/constants/LogFileConstants.java index a545a3bd2..e4dbbb763 100644 --- a/kernel-d-log/log-api/src/main/java/cn/stylefeng/roses/kernel/log/api/constants/LogFileConstants.java +++ b/kernel-d-log/log-api/src/main/java/cn/stylefeng/roses/kernel/log/api/constants/LogFileConstants.java @@ -37,6 +37,11 @@ public interface LogFileConstants { */ Integer DEFAULT_API_LOG_AOP_SORT = 500; + /** + * 默认业务日志记录的aop的顺序 + */ + Integer DEFAULT_BUSINESS_LOG_AOP_SORT = 400; + /** * 默认全局记录日志的开关 */ diff --git a/kernel-d-log/log-business/pom.xml b/kernel-d-log/log-business/pom.xml index d455a2943..a84dfd72a 100644 --- a/kernel-d-log/log-business/pom.xml +++ b/kernel-d-log/log-business/pom.xml @@ -40,6 +40,13 @@ ${roses.version} + + + cn.stylefeng.roses + db-sdk-mp + ${roses.version} + + diff --git a/kernel-d-log/log-business/src/main/java/cn/stylefeng/roses/kernel/log/business/aop/BusinessLogRecordAop.java b/kernel-d-log/log-business/src/main/java/cn/stylefeng/roses/kernel/log/business/aop/BusinessLogRecordAop.java new file mode 100644 index 000000000..539b9ae7e --- /dev/null +++ b/kernel-d-log/log-business/src/main/java/cn/stylefeng/roses/kernel/log/business/aop/BusinessLogRecordAop.java @@ -0,0 +1,148 @@ +/* + * Copyright [2020-2030] [https://www.stylefeng.cn] + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Guns采用APACHE LICENSE 2.0开源协议,您在使用过程中,需要注意以下几点: + * + * 1.请不要删除和修改根目录下的LICENSE文件。 + * 2.请不要删除和修改Guns源码头部的版权声明。 + * 3.请保留源码和相关描述文件的项目出处,作者声明等。 + * 4.分发源码时候,请注明软件出处 https://gitee.com/stylefeng/guns + * 5.在修改包名,模块名称,项目代码等时,请注明软件出处 https://gitee.com/stylefeng/guns + * 6.若您的项目无法满足以上几点,可申请商业授权 + */ +package cn.stylefeng.roses.kernel.log.business.aop; + +import cn.stylefeng.roses.kernel.auth.api.context.LoginContext; +import cn.stylefeng.roses.kernel.auth.api.pojo.login.LoginUser; +import cn.stylefeng.roses.kernel.log.api.constants.LogFileConstants; +import cn.stylefeng.roses.kernel.log.business.context.BusinessLogHolder; +import cn.stylefeng.roses.kernel.log.business.entity.SysLogBusiness; +import cn.stylefeng.roses.kernel.log.business.service.SysLogBusinessService; +import cn.stylefeng.roses.kernel.rule.annotation.BizLog; +import cn.stylefeng.roses.kernel.rule.util.HttpServletUtil; +import com.baomidou.mybatisplus.core.toolkit.IdWorker; +import lombok.extern.slf4j.Slf4j; +import org.aspectj.lang.ProceedingJoinPoint; +import org.aspectj.lang.annotation.Around; +import org.aspectj.lang.annotation.Aspect; +import org.aspectj.lang.annotation.Pointcut; +import org.aspectj.lang.reflect.MethodSignature; +import org.springframework.core.Ordered; + +import javax.annotation.Resource; +import java.lang.reflect.Method; +import java.util.List; + +/** + * 业务日志的AOP + * + * @author fengshuonan + * @since 2023/7/21 15:06 + */ +@Aspect +@Slf4j +public class BusinessLogRecordAop implements Ordered { + + @Resource + private SysLogBusinessService sysLogBusinessService; + + /** + * 切面为切BizLog注解 + * + * @author fengshuonan + * @since 2023/7/21 15:49 + */ + @Pointcut(value = "@annotation(cn.stylefeng.roses.kernel.rule.annotation.BizLog)") + public void cutService() { + } + + @Around("cutService()") + public Object aroundPost(ProceedingJoinPoint joinPoint) throws Throwable { + + try { + + // 初始化日志的环境信息 + initBusinessContext(joinPoint); + + // 执行真正业务逻辑,这期间会调用BusinessLogHolder类进行业务日志存储 + Object result = joinPoint.proceed(); + + // 业务日志进行保存 + SysLogBusiness logContext = BusinessLogHolder.getContext(); + List contentList = BusinessLogHolder.getContent(); + sysLogBusinessService.saveBatchLogs(logContext, contentList); + + return result; + + } catch (Exception e) { + + log.error("业务日志过程中出现异常!", e); + + // 这里不拦截异常,给上级处理异常 + throw e; + + } finally { + // 清除所有日志缓存记录 + BusinessLogHolder.clearContext(); + } + } + + /** + * 初始化业务日志的环境 + * + * @author fengshuonan + * @since 2023/7/21 15:55 + */ + private void initBusinessContext(ProceedingJoinPoint joinPoint) { + + MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature(); + Method method = methodSignature.getMethod(); + BizLog bizLog = method.getAnnotation(BizLog.class); + + SysLogBusiness sysLogBusiness = new SysLogBusiness(); + + // 设置业务日志的类型编码 + sysLogBusiness.setLogTypeCode(bizLog.logTypeCode()); + + // 设置调用链id + sysLogBusiness.setTraceId(IdWorker.getId()); + + // 设置请求的URL + String servletPath = HttpServletUtil.getRequest().getServletPath(); + sysLogBusiness.setRequestUrl(servletPath); + + // 设置HTTP请求方式 + sysLogBusiness.setHttpMethod(HttpServletUtil.getRequest().getMethod()); + + // 设置客户端IP + String requestClientIp = HttpServletUtil.getRequestClientIp(HttpServletUtil.getRequest()); + sysLogBusiness.setClientIp(requestClientIp); + + // 设置当前登录用户id + LoginUser loginUserNullable = LoginContext.me().getLoginUserNullable(); + if (loginUserNullable != null) { + sysLogBusiness.setUserId(loginUserNullable.getUserId()); + } + + // 添加到线程环境变量 + BusinessLogHolder.setContext(sysLogBusiness); + } + + @Override + public int getOrder() { + return LogFileConstants.DEFAULT_BUSINESS_LOG_AOP_SORT; + } + +} diff --git a/kernel-d-log/log-business/src/main/java/cn/stylefeng/roses/kernel/log/business/context/BusinessLogHolder.java b/kernel-d-log/log-business/src/main/java/cn/stylefeng/roses/kernel/log/business/context/BusinessLogHolder.java new file mode 100644 index 000000000..70c57830b --- /dev/null +++ b/kernel-d-log/log-business/src/main/java/cn/stylefeng/roses/kernel/log/business/context/BusinessLogHolder.java @@ -0,0 +1,98 @@ +package cn.stylefeng.roses.kernel.log.business.context; + +import cn.hutool.core.util.ObjectUtil; +import cn.stylefeng.roses.kernel.log.business.entity.SysLogBusiness; + +import java.util.LinkedList; +import java.util.List; + +/** + * 业务日志暂存 + * + * @author fengshuonan + * @since 2023/7/21 16:06 + */ +public class BusinessLogHolder { + + /** + * 暂存当前请求的日志信息 + */ + private static final ThreadLocal BUSINESS_LOG_CONTEXT = new ThreadLocal<>(); + + /** + * 暂存本次请求的所有日志详情 + */ + private static final ThreadLocal> CONTENT_LIST = new ThreadLocal<>(); + + /** + * 本次业务日志记录上下文信息存储到日志中 + * + * @author fengshuonan + * @since 2023/7/21 16:23 + */ + public static void setContext(SysLogBusiness sysLogBusiness) { + BUSINESS_LOG_CONTEXT.set(sysLogBusiness); + } + + /** + * 获取本次业务日志记录上下文信息 + * + * @author fengshuonan + * @since 2021/3/23 17:41 + */ + public static SysLogBusiness getContext() { + return BUSINESS_LOG_CONTEXT.get(); + } + + /** + * 清除所有的日志记录上下文 + * + * @author fengshuonan + * @since 2021/3/23 17:42 + */ + public static void clearContext() { + BUSINESS_LOG_CONTEXT.remove(); + CONTENT_LIST.remove(); + } + + /** + * 添加日志记录 + * + * @author fengshuonan + * @since 2023/7/21 16:53 + */ + public static void addContent(String contentStr) { + List contentList = CONTENT_LIST.get(); + if (ObjectUtil.isEmpty(contentList)) { + contentList = new LinkedList<>(); + } + contentList.add(contentStr); + CONTENT_LIST.set(contentList); + } + + /** + * 获取所有的日志详情记录 + * + * @author fengshuonan + * @since 2023/7/21 16:53 + */ + public static List getContent() { + return CONTENT_LIST.get(); + } + + /** + * 设置日志的摘要信息,便于后台搜索 + * + * @author fengshuonan + * @since 2023/7/21 17:30 + */ + public static void setLogTitle(String logTitle) { + SysLogBusiness sysLogBusiness = BUSINESS_LOG_CONTEXT.get(); + if (sysLogBusiness == null) { + return; + } + sysLogBusiness.setLogTitle(logTitle); + BUSINESS_LOG_CONTEXT.set(sysLogBusiness); + } + +} diff --git a/kernel-d-log/log-business/src/main/java/cn/stylefeng/roses/kernel/log/business/controller/SysLogBusinessController.java b/kernel-d-log/log-business/src/main/java/cn/stylefeng/roses/kernel/log/business/controller/SysLogBusinessController.java new file mode 100644 index 000000000..df8f098ab --- /dev/null +++ b/kernel-d-log/log-business/src/main/java/cn/stylefeng/roses/kernel/log/business/controller/SysLogBusinessController.java @@ -0,0 +1,39 @@ +package cn.stylefeng.roses.kernel.log.business.controller; + +import cn.stylefeng.roses.kernel.db.api.pojo.page.PageResult; +import cn.stylefeng.roses.kernel.log.business.entity.SysLogBusiness; +import cn.stylefeng.roses.kernel.log.business.pojo.request.SysLogBusinessRequest; +import cn.stylefeng.roses.kernel.log.business.service.SysLogBusinessService; +import cn.stylefeng.roses.kernel.rule.pojo.response.ResponseData; +import cn.stylefeng.roses.kernel.rule.pojo.response.SuccessResponseData; +import cn.stylefeng.roses.kernel.scanner.api.annotation.ApiResource; +import cn.stylefeng.roses.kernel.scanner.api.annotation.GetResource; +import org.springframework.web.bind.annotation.RestController; + +import javax.annotation.Resource; + +/** + * 业务日志记录控制器 + * + * @author fengshuonan + * @date 2023/07/21 15:00 + */ +@RestController +@ApiResource(name = "业务日志记录") +public class SysLogBusinessController { + + @Resource + private SysLogBusinessService sysLogBusinessService; + + /** + * 查询业务日志列表(带分页) + * + * @author fengshuonan + * @date 2023/07/21 15:00 + */ + @GetResource(name = "查询业务日志列表(带分页)", path = "/sysLogBusiness/page") + public ResponseData> page(SysLogBusinessRequest sysLogBusinessRequest) { + return new SuccessResponseData<>(sysLogBusinessService.findPage(sysLogBusinessRequest)); + } + +} diff --git a/kernel-d-log/log-business/src/main/java/cn/stylefeng/roses/kernel/log/business/entity/SysLogBusiness.java b/kernel-d-log/log-business/src/main/java/cn/stylefeng/roses/kernel/log/business/entity/SysLogBusiness.java new file mode 100644 index 000000000..e727a303c --- /dev/null +++ b/kernel-d-log/log-business/src/main/java/cn/stylefeng/roses/kernel/log/business/entity/SysLogBusiness.java @@ -0,0 +1,86 @@ +package cn.stylefeng.roses.kernel.log.business.entity; + +import cn.stylefeng.roses.kernel.db.api.pojo.entity.BaseEntity; +import cn.stylefeng.roses.kernel.rule.annotation.ChineseDescription; +import com.baomidou.mybatisplus.annotation.IdType; +import com.baomidou.mybatisplus.annotation.TableField; +import com.baomidou.mybatisplus.annotation.TableId; +import com.baomidou.mybatisplus.annotation.TableName; +import lombok.Data; +import lombok.EqualsAndHashCode; + +/** + * 业务日志记录实例类 + * + * @author fengshuonan + * @date 2023/07/21 15:00 + */ +@TableName("sys_log_business") +@Data +@EqualsAndHashCode(callSuper = true) +public class SysLogBusiness extends BaseEntity { + + /** + * 主键 + */ + @TableId(value = "business_log_id", type = IdType.ASSIGN_ID) + @ChineseDescription("主键") + private Long businessLogId; + + /** + * 日志的业务分类的编码 + */ + @TableField("log_type_code") + @ChineseDescription("日志的业务分类的编码") + private String logTypeCode; + + /** + * 日志的标题,摘要信息 + */ + @TableField("log_title") + @ChineseDescription("日志的标题,摘要信息") + private String logTitle; + + /** + * 日志记录的内容 + */ + @TableField("log_content") + @ChineseDescription("日志记录的内容") + private String logContent; + + /** + * 调用链唯一id,一个id代表一次接口调用 + */ + @TableField("trace_id") + @ChineseDescription("调用链唯一id,一个id代表一次接口调用") + private Long traceId; + + /** + * 当前用户请求的url + */ + @TableField("request_url") + @ChineseDescription("当前用户请求的url") + private String requestUrl; + + /** + * 请求http方法 + */ + @TableField("http_method") + @ChineseDescription("请求http方法") + private String httpMethod; + + /** + * 客户端的ip + */ + @TableField("client_ip") + @ChineseDescription("客户端的ip") + private String clientIp; + + /** + * 业务操作的用户id + */ + @TableField("user_id") + @ChineseDescription("业务操作的用户id") + private Long userId; + +} diff --git a/kernel-d-log/log-business/src/main/java/cn/stylefeng/roses/kernel/log/business/enums/SysLogBusinessExceptionEnum.java b/kernel-d-log/log-business/src/main/java/cn/stylefeng/roses/kernel/log/business/enums/SysLogBusinessExceptionEnum.java new file mode 100644 index 000000000..2e2b4897d --- /dev/null +++ b/kernel-d-log/log-business/src/main/java/cn/stylefeng/roses/kernel/log/business/enums/SysLogBusinessExceptionEnum.java @@ -0,0 +1,36 @@ +package cn.stylefeng.roses.kernel.log.business.enums; + +import cn.stylefeng.roses.kernel.rule.constants.RuleConstants; +import cn.stylefeng.roses.kernel.rule.exception.AbstractExceptionEnum; +import lombok.Getter; + +/** + * 业务日志记录异常相关枚举 + * + * @author fengshuonan + * @date 2023/07/21 15:00 + */ +@Getter +public enum SysLogBusinessExceptionEnum implements AbstractExceptionEnum { + + /** + * 查询结果不存在 + */ + SYS_LOG_BUSINESS_NOT_EXISTED(RuleConstants.USER_OPERATION_ERROR_TYPE_CODE + "10001", "查询结果不存在"); + + /** + * 错误编码 + */ + private final String errorCode; + + /** + * 提示用户信息 + */ + private final String userTip; + + SysLogBusinessExceptionEnum(String errorCode, String userTip) { + this.errorCode = errorCode; + this.userTip = userTip; + } + +} \ No newline at end of file diff --git a/kernel-d-log/log-business/src/main/java/cn/stylefeng/roses/kernel/log/business/mapper/SysLogBusinessMapper.java b/kernel-d-log/log-business/src/main/java/cn/stylefeng/roses/kernel/log/business/mapper/SysLogBusinessMapper.java new file mode 100644 index 000000000..5e6300e5d --- /dev/null +++ b/kernel-d-log/log-business/src/main/java/cn/stylefeng/roses/kernel/log/business/mapper/SysLogBusinessMapper.java @@ -0,0 +1,14 @@ +package cn.stylefeng.roses.kernel.log.business.mapper; + +import cn.stylefeng.roses.kernel.db.mp.injector.CustomBaseMapper; +import cn.stylefeng.roses.kernel.log.business.entity.SysLogBusiness; + +/** + * 业务日志记录 Mapper 接口 + * + * @author fengshuonan + * @date 2023/07/21 15:00 + */ +public interface SysLogBusinessMapper extends CustomBaseMapper { + +} \ No newline at end of file diff --git a/kernel-d-log/log-business/src/main/java/cn/stylefeng/roses/kernel/log/business/mapper/mapping/SysLogBusinessMapper.xml b/kernel-d-log/log-business/src/main/java/cn/stylefeng/roses/kernel/log/business/mapper/mapping/SysLogBusinessMapper.xml new file mode 100644 index 000000000..dfbe39990 --- /dev/null +++ b/kernel-d-log/log-business/src/main/java/cn/stylefeng/roses/kernel/log/business/mapper/mapping/SysLogBusinessMapper.xml @@ -0,0 +1,5 @@ + + + + + \ No newline at end of file diff --git a/kernel-d-log/log-business/src/main/java/cn/stylefeng/roses/kernel/log/business/pojo/request/SysLogBusinessRequest.java b/kernel-d-log/log-business/src/main/java/cn/stylefeng/roses/kernel/log/business/pojo/request/SysLogBusinessRequest.java new file mode 100644 index 000000000..7554945bc --- /dev/null +++ b/kernel-d-log/log-business/src/main/java/cn/stylefeng/roses/kernel/log/business/pojo/request/SysLogBusinessRequest.java @@ -0,0 +1,75 @@ +package cn.stylefeng.roses.kernel.log.business.pojo.request; + +import cn.stylefeng.roses.kernel.rule.annotation.ChineseDescription; +import cn.stylefeng.roses.kernel.rule.pojo.request.BaseRequest; +import lombok.Data; +import lombok.EqualsAndHashCode; + +import javax.validation.constraints.NotNull; + +/** + * 业务日志记录封装类 + * + * @author fengshuonan + * @date 2023/07/21 15:00 + */ +@EqualsAndHashCode(callSuper = true) +@Data +public class SysLogBusinessRequest extends BaseRequest { + + /** + * 主键 + */ + @NotNull(message = "主键不能为空", groups = {edit.class, delete.class}) + @ChineseDescription("主键") + private Long businessLogId; + + /** + * 日志的业务分类的编码 + */ + @ChineseDescription("日志的业务分类的编码") + private String logTypeCode; + + /** + * 日志的标题,摘要信息 + */ + @ChineseDescription("日志的标题,摘要信息") + private String logTitle; + + /** + * 日志记录的内容 + */ + @ChineseDescription("日志记录的内容") + private String logContent; + + /** + * 调用链唯一id,一个id代表一次接口调用 + */ + @ChineseDescription("调用链唯一id,一个id代表一次接口调用") + private Long traceId; + + /** + * 当前用户请求的url + */ + @ChineseDescription("当前用户请求的url") + private String requestUrl; + + /** + * 请求http方法 + */ + @ChineseDescription("请求http方法") + private String httpMethod; + + /** + * 客户端的ip + */ + @ChineseDescription("客户端的ip") + private String clientIp; + + /** + * 业务操作的用户id + */ + @ChineseDescription("业务操作的用户id") + private Long userId; + +} diff --git a/kernel-d-log/log-business/src/main/java/cn/stylefeng/roses/kernel/log/business/service/SysLogBusinessService.java b/kernel-d-log/log-business/src/main/java/cn/stylefeng/roses/kernel/log/business/service/SysLogBusinessService.java new file mode 100644 index 000000000..cfb1b46d9 --- /dev/null +++ b/kernel-d-log/log-business/src/main/java/cn/stylefeng/roses/kernel/log/business/service/SysLogBusinessService.java @@ -0,0 +1,82 @@ +package cn.stylefeng.roses.kernel.log.business.service; + +import cn.stylefeng.roses.kernel.db.api.pojo.page.PageResult; +import cn.stylefeng.roses.kernel.log.business.entity.SysLogBusiness; +import cn.stylefeng.roses.kernel.log.business.pojo.request.SysLogBusinessRequest; +import com.baomidou.mybatisplus.extension.service.IService; + +import java.util.List; + +/** + * 业务日志记录 服务类 + * + * @author fengshuonan + * @date 2023/07/21 15:00 + */ +public interface SysLogBusinessService extends IService { + + /** + * 新增 + * + * @param sysLogBusinessRequest 请求参数 + * @author fengshuonan + * @date 2023/07/21 15:00 + */ + void add(SysLogBusinessRequest sysLogBusinessRequest); + + /** + * 删除 + * + * @param sysLogBusinessRequest 请求参数 + * @author fengshuonan + * @date 2023/07/21 15:00 + */ + void del(SysLogBusinessRequest sysLogBusinessRequest); + + /** + * 编辑 + * + * @param sysLogBusinessRequest 请求参数 + * @author fengshuonan + * @date 2023/07/21 15:00 + */ + void edit(SysLogBusinessRequest sysLogBusinessRequest); + + /** + * 查询详情 + * + * @param sysLogBusinessRequest 请求参数 + * @author fengshuonan + * @date 2023/07/21 15:00 + */ + SysLogBusiness detail(SysLogBusinessRequest sysLogBusinessRequest); + + /** + * 获取列表 + * + * @param sysLogBusinessRequest 请求参数 + * @return List 返回结果 + * @author fengshuonan + * @date 2023/07/21 15:00 + */ + List findList(SysLogBusinessRequest sysLogBusinessRequest); + + /** + * 获取列表(带分页) + * + * @param sysLogBusinessRequest 请求参数 + * @return PageResult 返回结果 + * @author fengshuonan + * @date 2023/07/21 15:00 + */ + PageResult findPage(SysLogBusinessRequest sysLogBusinessRequest); + + /** + * 批量保存业务日志 + * + * @author fengshuonan + * @since 2023/7/21 17:01 + */ + void saveBatchLogs(SysLogBusiness context, List batchContentList); + +} \ No newline at end of file diff --git a/kernel-d-log/log-business/src/main/java/cn/stylefeng/roses/kernel/log/business/service/impl/SysLogBusinessServiceImpl.java b/kernel-d-log/log-business/src/main/java/cn/stylefeng/roses/kernel/log/business/service/impl/SysLogBusinessServiceImpl.java new file mode 100644 index 000000000..f43f1689f --- /dev/null +++ b/kernel-d-log/log-business/src/main/java/cn/stylefeng/roses/kernel/log/business/service/impl/SysLogBusinessServiceImpl.java @@ -0,0 +1,129 @@ +package cn.stylefeng.roses.kernel.log.business.service.impl; + +import cn.hutool.core.bean.BeanUtil; +import cn.hutool.core.util.ObjectUtil; +import cn.stylefeng.roses.kernel.db.api.factory.PageFactory; +import cn.stylefeng.roses.kernel.db.api.factory.PageResultFactory; +import cn.stylefeng.roses.kernel.db.api.pojo.page.PageResult; +import cn.stylefeng.roses.kernel.log.business.entity.SysLogBusiness; +import cn.stylefeng.roses.kernel.log.business.enums.SysLogBusinessExceptionEnum; +import cn.stylefeng.roses.kernel.log.business.mapper.SysLogBusinessMapper; +import cn.stylefeng.roses.kernel.log.business.pojo.request.SysLogBusinessRequest; +import cn.stylefeng.roses.kernel.log.business.service.SysLogBusinessService; +import cn.stylefeng.roses.kernel.rule.exception.base.ServiceException; +import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; +import com.baomidou.mybatisplus.extension.plugins.pagination.Page; +import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; +import org.springframework.stereotype.Service; + +import java.util.ArrayList; +import java.util.List; + +/** + * 业务日志记录业务实现层 + * + * @author fengshuonan + * @date 2023/07/21 15:00 + */ +@Service +public class SysLogBusinessServiceImpl extends ServiceImpl implements SysLogBusinessService { + + @Override + public void add(SysLogBusinessRequest sysLogBusinessRequest) { + SysLogBusiness sysLogBusiness = new SysLogBusiness(); + BeanUtil.copyProperties(sysLogBusinessRequest, sysLogBusiness); + this.save(sysLogBusiness); + } + + @Override + public void del(SysLogBusinessRequest sysLogBusinessRequest) { + SysLogBusiness sysLogBusiness = this.querySysLogBusiness(sysLogBusinessRequest); + this.removeById(sysLogBusiness.getBusinessLogId()); + } + + @Override + public void edit(SysLogBusinessRequest sysLogBusinessRequest) { + SysLogBusiness sysLogBusiness = this.querySysLogBusiness(sysLogBusinessRequest); + BeanUtil.copyProperties(sysLogBusinessRequest, sysLogBusiness); + this.updateById(sysLogBusiness); + } + + @Override + public SysLogBusiness detail(SysLogBusinessRequest sysLogBusinessRequest) { + return this.querySysLogBusiness(sysLogBusinessRequest); + } + + @Override + public PageResult findPage(SysLogBusinessRequest sysLogBusinessRequest) { + LambdaQueryWrapper wrapper = createWrapper(sysLogBusinessRequest); + Page sysRolePage = this.page(PageFactory.defaultPage(), wrapper); + return PageResultFactory.createPageResult(sysRolePage); + } + + @Override + public void saveBatchLogs(SysLogBusiness context, List batchContentList) { + + if (ObjectUtil.isEmpty(batchContentList)) { + return; + } + + List sysLogBusinesses = new ArrayList<>(); + for (String content : batchContentList) { + SysLogBusiness sysLogBusiness = new SysLogBusiness(); + BeanUtil.copyProperties(context, sysLogBusiness); + sysLogBusiness.setLogContent(content); + sysLogBusinesses.add(sysLogBusiness); + } + + this.getBaseMapper().insertBatchSomeColumn(sysLogBusinesses); + } + + @Override + public List findList(SysLogBusinessRequest sysLogBusinessRequest) { + LambdaQueryWrapper wrapper = this.createWrapper(sysLogBusinessRequest); + return this.list(wrapper); + } + + /** + * 获取信息 + * + * @author fengshuonan + * @date 2023/07/21 15:00 + */ + private SysLogBusiness querySysLogBusiness(SysLogBusinessRequest sysLogBusinessRequest) { + SysLogBusiness sysLogBusiness = this.getById(sysLogBusinessRequest.getBusinessLogId()); + if (ObjectUtil.isEmpty(sysLogBusiness)) { + throw new ServiceException(SysLogBusinessExceptionEnum.SYS_LOG_BUSINESS_NOT_EXISTED); + } + return sysLogBusiness; + } + + /** + * 创建查询wrapper + * + * @author fengshuonan + * @date 2023/07/21 15:00 + */ + private LambdaQueryWrapper createWrapper(SysLogBusinessRequest sysLogBusinessRequest) { + LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<>(); + + // 根据日志类型编码查询 + String logTypeCode = sysLogBusinessRequest.getLogTypeCode(); + queryWrapper.eq(ObjectUtil.isNotEmpty(logTypeCode), SysLogBusiness::getLogTypeCode, logTypeCode); + + // 根据调用链日志信息查询 + Long traceId = sysLogBusinessRequest.getTraceId(); + queryWrapper.eq(ObjectUtil.isNotNull(traceId), SysLogBusiness::getTraceId, traceId); + + // 根据文本检索内容查询 + String searchText = sysLogBusinessRequest.getSearchText(); + if (ObjectUtil.isNotEmpty(searchText)) { + queryWrapper.nested(wrap -> { + wrap.like(SysLogBusiness::getLogTitle, searchText).or().like(SysLogBusiness::getLogContent, searchText); + }); + } + + return queryWrapper; + } + +} \ No newline at end of file