提交 | 用户 | 时间
|
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.ApiAccessLogCreateReqDTO; |
H |
9 |
import com.iailab.module.infra.controller.admin.logger.vo.apiaccesslog.ApiAccessLogPageReqVO; |
|
10 |
import com.iailab.module.infra.dal.dataobject.logger.ApiAccessLogDO; |
|
11 |
import com.iailab.module.infra.dal.mysql.logger.ApiAccessLogMapper; |
|
12 |
import lombok.extern.slf4j.Slf4j; |
|
13 |
import org.springframework.stereotype.Service; |
|
14 |
import org.springframework.validation.annotation.Validated; |
|
15 |
|
|
16 |
import javax.annotation.Resource; |
|
17 |
import java.time.LocalDateTime; |
|
18 |
|
|
19 |
import static com.iailab.module.infra.dal.dataobject.logger.ApiAccessLogDO.REQUEST_PARAMS_MAX_LENGTH; |
|
20 |
import static com.iailab.module.infra.dal.dataobject.logger.ApiAccessLogDO.RESULT_MSG_MAX_LENGTH; |
|
21 |
|
|
22 |
/** |
|
23 |
* API 访问日志 Service 实现类 |
|
24 |
* |
|
25 |
* @author iailab |
|
26 |
*/ |
|
27 |
@Slf4j |
|
28 |
@Service |
|
29 |
@Validated |
|
30 |
public class ApiAccessLogServiceImpl implements ApiAccessLogService { |
|
31 |
|
|
32 |
@Resource |
|
33 |
private ApiAccessLogMapper apiAccessLogMapper; |
|
34 |
|
|
35 |
@Override |
|
36 |
public void createApiAccessLog(ApiAccessLogCreateReqDTO createDTO) { |
|
37 |
ApiAccessLogDO apiAccessLog = BeanUtils.toBean(createDTO, ApiAccessLogDO.class); |
|
38 |
apiAccessLog.setRequestParams(StrUtil.maxLength(apiAccessLog.getRequestParams(), REQUEST_PARAMS_MAX_LENGTH)); |
|
39 |
apiAccessLog.setResultMsg(StrUtil.maxLength(apiAccessLog.getResultMsg(), RESULT_MSG_MAX_LENGTH)); |
325d2f
|
40 |
if (TenantContextHolder.getTenantId() != null) { |
H |
41 |
apiAccessLogMapper.insert(apiAccessLog); |
|
42 |
} else { |
|
43 |
// 极端情况下,上下文中没有租户时,此时忽略租户上下文,避免插入失败! |
|
44 |
TenantUtils.executeIgnore(() -> apiAccessLogMapper.insert(apiAccessLog)); |
|
45 |
} |
e7c126
|
46 |
} |
H |
47 |
|
|
48 |
@Override |
|
49 |
public PageResult<ApiAccessLogDO> getApiAccessLogPage(ApiAccessLogPageReqVO pageReqVO) { |
|
50 |
return apiAccessLogMapper.selectPage(pageReqVO); |
|
51 |
} |
|
52 |
|
|
53 |
@Override |
|
54 |
@SuppressWarnings("DuplicatedCode") |
|
55 |
public Integer cleanAccessLog(Integer exceedDay, Integer deleteLimit) { |
|
56 |
int count = 0; |
|
57 |
LocalDateTime expireDate = LocalDateTime.now().minusDays(exceedDay); |
|
58 |
// 循环删除,直到没有满足条件的数据 |
|
59 |
for (int i = 0; i < Short.MAX_VALUE; i++) { |
|
60 |
int deleteCount = apiAccessLogMapper.deleteByCreateTimeLt(expireDate, deleteLimit); |
|
61 |
count += deleteCount; |
|
62 |
// 达到删除预期条数,说明到底了 |
|
63 |
if (deleteCount < deleteLimit) { |
|
64 |
break; |
|
65 |
} |
|
66 |
} |
|
67 |
return count; |
|
68 |
} |
|
69 |
|
|
70 |
} |