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