EventServiceImpl.java 3.03 KB
package vion.service.impl.monitor;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.base.MPJBaseServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import io.github.linpeilie.Converter;
import lombok.RequiredArgsConstructor;
import org.dromara.hutool.core.lang.Assert;
import org.dromara.hutool.core.lang.Opt;
import org.springframework.stereotype.Service;
import vion.dto.monitor.EventDTO;
import vion.mapper.monitor.EventMapper;
import vion.model.monitor.Event;
import vion.model.monitor.EventRecord;
import vion.model.monitor.RAgentEvent;
import vion.service.monitor.IEventRecordService;
import vion.service.monitor.IEventService;
import vion.vo.monitor.EventVO;

import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author vion
 * @date 2024/10/31
 */
@Service
@RequiredArgsConstructor
public class EventServiceImpl extends MPJBaseServiceImpl<EventMapper, Event> implements IEventService {

    private final IEventRecordService eventRecordService;
    private final Converter converter;

    @Override
    public Page<EventVO> list(EventDTO dto) {
        Assert.notNull(dto.getMallUid(), "mallUid 不能为空");
        var wrapper = new MPJLambdaWrapper<>(converter.convert(dto, Event.class))
                .selectAs(Event::getName, EventVO::getName)
                .selectAs(Event::getUid, EventVO::getUid)
                .selectAsClass(RAgentEvent.class, EventVO.class)
                .leftJoin(RAgentEvent.class, RAgentEvent::getEventUid, Event::getUid)
                .eq(RAgentEvent::getMallUid, dto.getMallUid())
                .orderByDesc(RAgentEvent::getUpdateTime);
        var eventVOPage = this.selectJoinListPage(Page.of(dto.getPageNum(), dto.getPageSize()), EventVO.class, wrapper);
        Opt.ofEmptyAble(eventVOPage.getRecords())
                .ifPresent(r -> {
                    var eventUidList = r.stream().map(EventVO::getUid).toList();
                    // fixme data 是字符串
                   /* var eventRecWrapper1 = new MPJLambdaWrapper<EventRecord>()
                            .select("DISTINCT on (event_uid) *")
                            .in("event_uid", eventUidList)
                            .orderByDesc("event_uid", "create_time");
                    var eventRecordList1 = eventRecordService.selectJoinList(EventRecord.class, eventRecWrapper1);*/
                    var eventRecWrapper = Wrappers.<EventRecord>query()
                            .select("DISTINCT on (event_uid) *")
                            .in("event_uid", eventUidList)
                            .orderByDesc("event_uid", "create_time");
                    var eventRecordList = eventRecordService.list(eventRecWrapper);
                    var eventUid2SelfMap = eventRecordList.stream().collect(Collectors.toMap(EventRecord::getEventUid, Function.identity()));

                    r.forEach(tmp -> tmp.setEventRecord(eventUid2SelfMap.get(tmp.getUid())));
                });
        return eventVOPage;
    }
}