MallServiceImpl.java 2.82 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.MallDTO;
import vion.mapper.monitor.MallMapper;
import vion.model.monitor.EventRecord;
import vion.model.monitor.MAccount;
import vion.model.monitor.Mall;
import vion.service.monitor.IEventRecordService;
import vion.service.monitor.IMallService;
import vion.vo.monitor.MallVO;

import java.util.stream.Collectors;

/**
 * @author vion
 * @date 2024/10/31
 */
@Service
@RequiredArgsConstructor
public class MallServiceImpl extends MPJBaseServiceImpl<MallMapper, Mall> implements IMallService {

    private final IEventRecordService eventRecordService;
    private final Converter converter;

    @Override
    public Page<MallVO> list(MallDTO dto) {
        Assert.notNull(dto.getAgentType(), "agentType 不能为空");
        var wrapper = new MPJLambdaWrapper<>(converter.convert(dto, Mall.class))
                .selectAll(Mall.class)
                .selectAs(MAccount::getName, MallVO::getAccountName)
                .leftJoin(MAccount.class, MAccount::getUid, Mall::getAccountUid)
                .orderByDesc(Mall::getUpdateTime);
        var mallVOPage = this.selectJoinListPage(Page.of(dto.getPageNum(), dto.getPageSize()), MallVO.class, wrapper);
        Opt.ofEmptyAble(mallVOPage.getRecords())
                .ifPresent(r -> {
                    var mallUidList = r.stream().map(MallVO::getUid).toList();
                    // fixme {@link EventServiceImpl #51}
                    var eventRecWrapper = Wrappers.<EventRecord>query()
                            .select("DISTINCT on (mall_uid) *")
                            .in("mall_uid", mallUidList)
                            .orderByDesc("mall_uid", "create_time");
                    var eventRecordList = eventRecordService.list(eventRecWrapper);
                    var mallUid2SelfMap = eventRecordList.stream().collect(Collectors.groupingBy(EventRecord::getMallUid));

                    r.forEach(tmp -> tmp.setEventRecordList(mallUid2SelfMap.get(tmp.getUid())));
                });
        return mallVOPage;
    }

    @Override
    public MallVO get(Long id) {
        var wrapper = new MPJLambdaWrapper<Mall>()
                .selectAll(Mall.class)
                .selectAs(MAccount::getName, MallVO::getAccountName)
                .leftJoin(MAccount.class, MAccount::getUid, Mall::getAccountUid)
                .eq(Mall::getId, id);
        return this.selectJoinOne(MallVO.class, wrapper);
    }
}