提交 | 用户 | 时间
|
e7c126
|
1 |
package com.iailab.module.infra.service.logger; |
H |
2 |
|
|
3 |
import cn.hutool.core.util.StrUtil; |
|
4 |
import com.iailab.framework.common.pojo.PageResult; |
|
5 |
import com.iailab.framework.common.util.object.BeanUtils; |
325d2f
|
6 |
import com.iailab.framework.tenant.core.context.TenantContextHolder; |
H |
7 |
import com.iailab.framework.tenant.core.util.TenantUtils; |
e7c126
|
8 |
import com.iailab.module.infra.api.logger.dto.ApiErrorLogCreateReqDTO; |
H |
9 |
import com.iailab.module.infra.controller.admin.logger.vo.apierrorlog.ApiErrorLogPageReqVO; |
|
10 |
import com.iailab.module.infra.dal.dataobject.logger.ApiErrorLogDO; |
|
11 |
import com.iailab.module.infra.dal.mysql.logger.ApiErrorLogMapper; |
|
12 |
import com.iailab.module.infra.enums.logger.ApiErrorLogProcessStatusEnum; |
|
13 |
import lombok.extern.slf4j.Slf4j; |
|
14 |
import org.springframework.stereotype.Service; |
|
15 |
import org.springframework.validation.annotation.Validated; |
|
16 |
|
|
17 |
import javax.annotation.Resource; |
|
18 |
import java.time.LocalDateTime; |
|
19 |
|
|
20 |
import static com.iailab.framework.common.exception.util.ServiceExceptionUtil.exception; |
|
21 |
import static com.iailab.module.infra.dal.dataobject.logger.ApiErrorLogDO.REQUEST_PARAMS_MAX_LENGTH; |
|
22 |
import static com.iailab.module.infra.enums.ErrorCodeConstants.API_ERROR_LOG_NOT_FOUND; |
|
23 |
import static com.iailab.module.infra.enums.ErrorCodeConstants.API_ERROR_LOG_PROCESSED; |
|
24 |
|
|
25 |
/** |
|
26 |
* API 错误日志 Service 实现类 |
|
27 |
* |
|
28 |
* @author iailab |
|
29 |
*/ |
|
30 |
@Service |
|
31 |
@Validated |
325d2f
|
32 |
@Slf4j |
e7c126
|
33 |
public class ApiErrorLogServiceImpl implements ApiErrorLogService { |
H |
34 |
|
|
35 |
@Resource |
|
36 |
private ApiErrorLogMapper apiErrorLogMapper; |
|
37 |
|
|
38 |
@Override |
|
39 |
public void createApiErrorLog(ApiErrorLogCreateReqDTO createDTO) { |
|
40 |
ApiErrorLogDO apiErrorLog = BeanUtils.toBean(createDTO, ApiErrorLogDO.class) |
|
41 |
.setProcessStatus(ApiErrorLogProcessStatusEnum.INIT.getStatus()); |
|
42 |
apiErrorLog.setRequestParams(StrUtil.maxLength(apiErrorLog.getRequestParams(), REQUEST_PARAMS_MAX_LENGTH)); |
325d2f
|
43 |
if (TenantContextHolder.getTenantId() != null) { |
H |
44 |
apiErrorLogMapper.insert(apiErrorLog); |
|
45 |
} else { |
|
46 |
// 极端情况下,上下文中没有租户时,此时忽略租户上下文,避免插入失败! |
|
47 |
TenantUtils.executeIgnore(() -> apiErrorLogMapper.insert(apiErrorLog)); |
|
48 |
} |
e7c126
|
49 |
} |
H |
50 |
|
|
51 |
@Override |
|
52 |
public PageResult<ApiErrorLogDO> getApiErrorLogPage(ApiErrorLogPageReqVO pageReqVO) { |
|
53 |
return apiErrorLogMapper.selectPage(pageReqVO); |
|
54 |
} |
|
55 |
|
|
56 |
@Override |
|
57 |
public void updateApiErrorLogProcess(Long id, Integer processStatus, Long processUserId) { |
|
58 |
ApiErrorLogDO errorLog = apiErrorLogMapper.selectById(id); |
|
59 |
if (errorLog == null) { |
|
60 |
throw exception(API_ERROR_LOG_NOT_FOUND); |
|
61 |
} |
|
62 |
if (!ApiErrorLogProcessStatusEnum.INIT.getStatus().equals(errorLog.getProcessStatus())) { |
|
63 |
throw exception(API_ERROR_LOG_PROCESSED); |
|
64 |
} |
|
65 |
// 标记处理 |
|
66 |
apiErrorLogMapper.updateById(ApiErrorLogDO.builder().id(id).processStatus(processStatus) |
|
67 |
.processUserId(processUserId).processTime(LocalDateTime.now()).build()); |
|
68 |
} |
|
69 |
|
|
70 |
@Override |
|
71 |
@SuppressWarnings("DuplicatedCode") |
|
72 |
public Integer cleanErrorLog(Integer exceedDay, Integer deleteLimit) { |
|
73 |
int count = 0; |
|
74 |
LocalDateTime expireDate = LocalDateTime.now().minusDays(exceedDay); |
|
75 |
// 循环删除,直到没有满足条件的数据 |
|
76 |
for (int i = 0; i < Short.MAX_VALUE; i++) { |
|
77 |
int deleteCount = apiErrorLogMapper.deleteByCreateTimeLt(expireDate, deleteLimit); |
|
78 |
count += deleteCount; |
|
79 |
// 达到删除预期条数,说明到底了 |
|
80 |
if (deleteCount < deleteLimit) { |
|
81 |
break; |
|
82 |
} |
|
83 |
} |
|
84 |
return count; |
|
85 |
} |
|
86 |
|
|
87 |
} |