package cn.iocoder.yudao.framework.operatelog.core.util;
|
|
import com.mzt.logapi.context.LogRecordContext;
|
|
import java.util.Collection;
|
import java.util.Map;
|
|
/**
|
* 操作日志的工具类
|
*
|
* 主要解决「导入」这类批量操作的日志文案:一次导入只写一条汇总日志,
|
* 而不是逐行写(一次导入几百行就是几百条日志,日志页会没法看)。
|
*
|
* 用法:在导入方法返回前调用 {@link #putImportSummary},注解文案里引用 {{#importSummary}}:
|
* <pre>
|
* @LogRecord(type = XXX_TYPE, subType = XXX_IMPORT_SUB_TYPE, bizNo = "0", success = XXX_IMPORT_SUCCESS)
|
* public XxxImportRespVO importXxxList(...) {
|
* ...
|
* OperateLogUtils.putImportSummary(respVO.getCreateList(), respVO.getUpdateList(), respVO.getFailureList());
|
* return respVO;
|
* }
|
* </pre>
|
*
|
* 注意:注解的 bizNo 必须给数字(导入没有单一业务编号,统一填 "0")。
|
*
|
* @author 超级管理员
|
*/
|
public class OperateLogUtils {
|
|
/**
|
* 导入汇总文案在 {@link LogRecordContext} 里的变量名,对应注解文案的 {{#importSummary}}
|
*/
|
public static final String IMPORT_SUMMARY = "importSummary";
|
|
/**
|
* 记录导入结果的汇总变量(新增/更新/失败条数),直接传各模块导入 RespVO 的字段
|
*
|
* 各模块的导入结果字段形态不统一:成功的一般是 Collection(createUsernames / createList /
|
* createCustomerNames …),失败的则多是 Map(failureList / failureDeptNames,key 为名称或行号),
|
* 且有的模块没有「更新」这一列(builder 未赋值时为 null)。所以这里统一按「集合 / Map / 数字 / null」
|
* 取条数,取不到按 0 处理——统计失败不能让整条导入日志丢掉。
|
*
|
* @param createResult 新增成功的集合(或条数)
|
* @param updateResult 更新成功的集合(或条数),没有则传 null
|
* @param failureResult 失败的集合 / Map(或条数)
|
*/
|
public static void putImportSummary(Object createResult, Object updateResult, Object failureResult) {
|
LogRecordContext.putVariable(IMPORT_SUMMARY, "新增 " + size(createResult) + " 条、更新 "
|
+ size(updateResult) + " 条、失败 " + size(failureResult) + " 条");
|
}
|
|
private static int size(Object result) {
|
if (result == null) {
|
return 0;
|
}
|
if (result instanceof Collection) {
|
return ((Collection<?>) result).size();
|
}
|
if (result instanceof Map) {
|
return ((Map<?, ?>) result).size();
|
}
|
if (result instanceof Number) {
|
return ((Number) result).intValue();
|
}
|
return 0;
|
}
|
|
}
|