RankLogService.java 4.59 KB
Newer Older
刘基明's avatar
刘基明 committed
1 2
package com.tanpu.community.service;

张辰's avatar
张辰 committed
3
import com.alibaba.fastjson.JSON;
刘基明's avatar
刘基明 committed
4 5 6 7 8 9 10
import com.tanpu.common.util.JsonUtil;
import com.tanpu.community.api.beans.qo.ThemeAnalysDO;
import com.tanpu.community.api.beans.qo.TopicRankQo;
import com.tanpu.community.api.enums.RankLogTypeEnum;
import com.tanpu.community.dao.entity.community.RankLogEntity;
import com.tanpu.community.dao.mapper.community.RankLogMapper;
import com.tanpu.community.util.BizUtils;
张辰's avatar
张辰 committed
11
import lombok.extern.slf4j.Slf4j;
刘基明's avatar
刘基明 committed
12
import org.apache.commons.collections4.CollectionUtils;
张辰's avatar
张辰 committed
13 14
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
刘基明's avatar
刘基明 committed
15
import org.springframework.stereotype.Service;
刘基明's avatar
刘基明 committed
16
import org.springframework.transaction.annotation.Transactional;
刘基明's avatar
刘基明 committed
17 18

import javax.annotation.Resource;
张辰's avatar
张辰 committed
19
import java.io.ByteArrayOutputStream;
刘基明's avatar
刘基明 committed
20
import java.time.LocalDateTime;
张辰's avatar
张辰 committed
21
import java.time.format.DateTimeFormatter;
刘基明's avatar
刘基明 committed
22
import java.util.List;
张辰's avatar
张辰 committed
23
import java.util.stream.Collectors;
刘基明's avatar
刘基明 committed
24

张辰's avatar
张辰 committed
25
@Slf4j
刘基明's avatar
刘基明 committed
26 27 28
@Service
public class RankLogService {

张辰's avatar
张辰 committed
29 30
    @Autowired
    private OSSFileService ossFileService;
刘基明's avatar
刘基明 committed
31 32 33 34 35 36 37 38

    @Resource
    private RankLogMapper rankLogMapper;

    private Integer pageSize = 50;


    //话题排序日志
刘基明's avatar
刘基明 committed
39
    @Transactional
刘基明's avatar
刘基明 committed
40 41 42 43
    public void logTopicRank(List<TopicRankQo> rankList, LocalDateTime logTime, Long cost) {
        if (CollectionUtils.isEmpty(rankList)) {
            return;
        }
张辰's avatar
张辰 committed
44

刘基明's avatar
刘基明 committed
45
        Long round = rankLogMapper.selectMaxRound(RankLogTypeEnum.TOPIC.getCode());
张辰's avatar
张辰 committed
46 47
        round = round == null ? 0L : round + 1;

刘基明's avatar
刘基明 committed
48 49 50 51 52 53 54 55 56 57 58
        //分页插入
        for (int i = 0; i * pageSize < rankList.size(); i++) {
            int pageStart = i * pageSize;
            List<TopicRankQo> sublist = BizUtils.subList(rankList, pageStart, pageSize);
            RankLogEntity entity = RankLogEntity.builder().rankTime(logTime)
                    .type(RankLogTypeEnum.TOPIC.getCode())
                    .totalCount(rankList.size())
                    .rankCost(cost)
                    .content(JsonUtil.toJson(sublist))
                    .pageNumber(i + 1)
                    .pageSize(sublist.size())
张辰's avatar
张辰 committed
59
                    .round(round)
刘基明's avatar
刘基明 committed
60 61 62 63 64 65
                    .build();
            rankLogMapper.insert(entity);
        }
    }

    //主题排序日志
刘基明's avatar
刘基明 committed
66
    @Transactional
刘基明's avatar
刘基明 committed
67 68 69 70
    public void logThemeRank(List<ThemeAnalysDO> themeList, LocalDateTime logTime, Long cost) {
        if (CollectionUtils.isEmpty(themeList)) {
            return;
        }
刘基明's avatar
刘基明 committed
71 72 73 74

        Long round = rankLogMapper.selectMaxRound(RankLogTypeEnum.THEME.getCode());
        round = round == null ? 0L : round + 1;

刘基明's avatar
刘基明 committed
75 76 77 78 79 80 81 82 83 84 85
        //分页插入
        for (int i = 0; i * pageSize < themeList.size(); i++) {
            int pageStart = i * pageSize;
            List<ThemeAnalysDO> sublist = BizUtils.subList(themeList, pageStart, pageSize);
            RankLogEntity entity = RankLogEntity.builder().rankTime(logTime)
                    .type(RankLogTypeEnum.THEME.getCode())
                    .totalCount(themeList.size())
                    .rankCost(cost)
                    .content(JsonUtil.toJson(sublist))
                    .pageNumber(i + 1)
                    .pageSize(sublist.size())
刘基明's avatar
刘基明 committed
86
                    .round(round)
刘基明's avatar
刘基明 committed
87 88 89 90
                    .build();
            rankLogMapper.insert(entity);
        }
    }
刘基明's avatar
刘基明 committed
91

张辰's avatar
张辰 committed
92 93 94 95

    // 定时清除ranklog,并上传到oss
    public void clearRankLog() {
        LocalDateTime t = LocalDateTime.now().minusDays(7L);
张辰's avatar
张辰 committed
96 97
        String d = t.format(DateTimeFormatter.BASIC_ISO_DATE);
        log.info("start clearRankLog job before {}", d);
张辰's avatar
张辰 committed
98 99 100 101

        for (RankLogTypeEnum type : RankLogTypeEnum.values()) {
            int idx = 0;
            while (true) {
张辰's avatar
张辰 committed
102
                List<RankLogEntity> logs = rankLogMapper.selectByTypeLimit(type.getCode(), 100);
张辰's avatar
张辰 committed
103
                if (logs.isEmpty() || logs.get(0).getRankTime().isAfter(t)) {
张辰's avatar
张辰 committed
104 105 106 107
                    break;
                }

                try {
张辰's avatar
张辰 committed
108
                    String fileName = "ranklog_" + type.getCode() + "_" + idx;
张辰's avatar
张辰 committed
109 110
                    ByteArrayOutputStream os = new ByteArrayOutputStream();
                    IOUtils.writeLines(logs.stream().map(JSON::toJSONString).collect(Collectors.toList()), null, os);
张辰's avatar
张辰 committed
111
                    ossFileService.uploadFileNoRecord(os.toByteArray(), fileName, ".txt", "rankLog/");
张辰's avatar
张辰 committed
112 113 114 115 116 117 118 119

                    Thread.sleep(1000);
                } catch (Exception e) {
                    log.error("error in clearRankLog", e);
                    throw new RuntimeException(e);
                }

                // delete
张辰's avatar
张辰 committed
120 121
                List<Long> ids = logs.stream().map(RankLogEntity::getId).collect(Collectors.toList());
                rankLogMapper.deleteBatchIds(ids);
张辰's avatar
张辰 committed
122 123 124 125 126 127

                idx++;
            }
        }
    }

刘基明's avatar
刘基明 committed
128
}