package com.nova.sankuai.service.impl; import cn.hutool.core.util.IdUtil; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.nova.sankuai.domain.dto.ChannelVisitDto; import com.nova.sankuai.domain.entity.ChannelInfo; import com.nova.sankuai.domain.entity.ChannelViewLog; import com.nova.sankuai.domain.vo.channel.ChannelDownloadCountVo; import com.nova.sankuai.domain.vo.channel.ChannelVisitVo; import com.nova.sankuai.infra.config.CommonException; import com.nova.sankuai.infra.mapper.ChannelDownloadLogMapper; import com.nova.sankuai.infra.mapper.ChannelInfoMapper; import com.nova.sankuai.infra.mapper.ChannelViewLogMapper; import com.nova.sankuai.infra.utils.PageRequest; import com.nova.sankuai.infra.utils.PageUtil; import com.nova.sankuai.service.IChannelViewLogService; import lombok.AllArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import java.util.ArrayList; import java.util.Date; import java.util.List; import java.util.Optional; import java.util.stream.Collectors; /** * @author weikangdi * @create 2021/12/30 */ @Service @AllArgsConstructor @Slf4j public class ChannelViewLogServiceImpl extends ServiceImpl implements IChannelViewLogService { private final ChannelViewLogMapper channelViewLogMapper; private final ChannelInfoMapper channelInfoMapper; private final PageUtil pageUtil; private final ChannelDownloadLogMapper channelDownloadLogMapper; @Override @Transactional(rollbackFor = Exception.class) public void addVisitLog(String appId, Integer type) { List toSaveLogs = new ArrayList(); String uuid = IdUtil.randomUUID(); ChannelInfo channelInfo = channelInfoMapper.getChannelByAppId(appId); if (channelInfo == null) { log.error("渠道Id非法, 渠道值" + appId); throw new CommonException("渠道Id非法"); } Date date = new Date(); ChannelViewLog channelViewLog = new ChannelViewLog(); channelViewLog.setChannelId(channelInfo.getId()); channelViewLog.setCreation_date(date); channelViewLog.setVisitType(type); channelViewLog.setUuidFlag(uuid); toSaveLogs.add(channelViewLog); if (ChannelViewLog.TYPE_UV == type) { ChannelViewLog channelViewLogPV = new ChannelViewLog(); channelViewLogPV.setChannelId(channelInfo.getId()); channelViewLogPV.setCreation_date(date); channelViewLogPV.setVisitType(ChannelViewLog.TYPE_PV); channelViewLogPV.setUuidFlag(uuid); toSaveLogs.add(channelViewLogPV); } if (toSaveLogs.size() > 0) { saveBatch(toSaveLogs); } } @Override public List getChannelVisitList(PageRequest pageRequest, ChannelVisitDto visitDto) { visitDto = visitDto.initDate(); List records = channelViewLogMapper.getChannelVisitList(visitDto); List result = new ArrayList<>(); if (records.size() > 0) { List recordIds = records.stream().map(ChannelVisitVo::getId).collect(Collectors.toList()); List downloadList = channelDownloadLogMapper.getDownloadCount(recordIds, visitDto.getStartDate(), visitDto.getEndDate()); for (ChannelVisitVo record : records) { Integer count = 0; Optional countVoOptional = downloadList.stream().filter(i -> record.getId().equals(i.getId())).findFirst(); if (countVoOptional.isPresent()) { count = countVoOptional.get().getCount(); } record.setDownloadCount(count); result.add(record); } } return result; } }