UserEventController.java
3.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package com.diligrp.assistant.logging.controller;
import com.diligrp.assistant.logging.domain.EventPageQuery;
import com.diligrp.assistant.logging.domain.UserEventDTO;
import com.diligrp.assistant.logging.service.UserEventService;
import com.diligrp.assistant.shared.domain.Message;
import com.diligrp.assistant.shared.domain.PageMessage;
import com.diligrp.assistant.shared.service.ThreadPollService;
import com.diligrp.assistant.shared.util.AssertUtils;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.time.LocalDateTime;
import java.util.List;
@RestController
@RequestMapping("/logging/event")
public class UserEventController {
@Resource
private UserEventService userEventService;
@RequestMapping(value = "/fire.do")
public Message<?> fire(@RequestBody UserEventDTO request, @RequestParam(value = "async", defaultValue = "false") boolean async) {
AssertUtils.notEmpty(request.getModuleId(), "moduleId missed");
AssertUtils.notEmpty(request.getAction(), "action missed");
AssertUtils.notEmpty(request.getObjectId(), "objectId missed");
AssertUtils.notNull(request.getWhen(), "when missed");
AssertUtils.notEmpty(request.getUserId(), "userId missed");
if (async) {
ThreadPollService.getInstance().submit(() -> userEventService.insertUserEvent(request));
} else {
userEventService.insertUserEvent(request);
}
return Message.success();
}
@RequestMapping(value = "/fires.do")
public Message<?> fires(@RequestBody List<UserEventDTO> request, @RequestParam(value = "async", defaultValue = "false") boolean async) {
request.stream().forEach(event -> {
AssertUtils.notEmpty(event.getModuleId(), "moduleId missed");
AssertUtils.notEmpty(event.getAction(), "action missed");
AssertUtils.notEmpty(event.getObjectId(), "objectId missed");
AssertUtils.notNull(event.getWhen(), "when missed");
AssertUtils.notEmpty(event.getUserId(), "userId missed");
});
if (async) {
ThreadPollService.getInstance().submit(() -> userEventService.insertUserEvents(request));
} else {
userEventService.insertUserEvents(request);
}
return Message.success();
}
@RequestMapping(value = "/list.do")
public PageMessage<UserEventDTO> list(@RequestBody EventPageQuery request) {
AssertUtils.notNull(request.getStartTime(), "startTime missed");
AssertUtils.notNull(request.getPageNo(), "pageNo missed");
AssertUtils.notNull(request.getPageSize(), "pageSize missed");
AssertUtils.isTrue(request.getPageNo() > 0, "invalid pageNo");
AssertUtils.isTrue(request.getPageSize() > 0, "invalid pageSize");
// 如果未指定结束时间,则查询一天的日志数据
LocalDateTime endTime = request.getEndTime() == null ? request.getStartTime().plusDays(1) : request.getEndTime();
request.setEndTime(endTime);
request.from(request.getPageNo(), request.getPageSize());
return userEventService.listUserEvents(request);
}
}