ab
2024-11-05 0c6faf64f2a02ae94b0d719729754f7ca29da416
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
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<ChannelViewLogMapper, ChannelViewLog> 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<ChannelViewLog> 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<ChannelVisitVo> getChannelVisitList(PageRequest pageRequest, ChannelVisitDto visitDto) {
        visitDto = visitDto.initDate();
        List<ChannelVisitVo> records = channelViewLogMapper.getChannelVisitList(visitDto);
        List<ChannelVisitVo> result = new ArrayList<>();
        if (records.size() > 0) {
            List<Long> recordIds = records.stream().map(ChannelVisitVo::getId).collect(Collectors.toList());
            List<ChannelDownloadCountVo> downloadList = channelDownloadLogMapper.getDownloadCount(recordIds, visitDto.getStartDate(), visitDto.getEndDate());
            for (ChannelVisitVo record : records) {
                Integer count = 0;
                Optional<ChannelDownloadCountVo> 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;
    }
}