RankLogService.java 4.66 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 15
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
刘基明's avatar
刘基明 committed
16
import org.springframework.stereotype.Service;
刘基明's avatar
刘基明 committed
17
import org.springframework.transaction.annotation.Transactional;
刘基明's avatar
刘基明 committed
18 19

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

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

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

    @Resource
    private RankLogMapper rankLogMapper;

    private Integer pageSize = 50;


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

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

刘基明's avatar
刘基明 committed
50 51 52 53 54 55 56 57 58 59 60
        //分页插入
        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
61
                    .round(round)
刘基明's avatar
刘基明 committed
62 63 64 65 66 67
                    .build();
            rankLogMapper.insert(entity);
        }
    }

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

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

刘基明's avatar
刘基明 committed
77 78 79 80 81 82 83 84 85 86 87
        //分页插入
        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
88
                    .round(round)
刘基明's avatar
刘基明 committed
89 90 91 92
                    .build();
            rankLogMapper.insert(entity);
        }
    }
刘基明's avatar
刘基明 committed
93

张辰's avatar
张辰 committed
94 95 96 97

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

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

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

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

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

                idx++;
            }
        }
    }

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