UserEventController.java 3.31 KB
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);
    }
}