CleanDataJob.java 5.89 KB
package com.yoho.search.consumer.job;

import java.util.Calendar;
import java.util.List;

import com.yoho.search.dal.*;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.yoho.search.base.utils.DateStyle;
import com.yoho.search.base.utils.DateUtil;

/**
 * Created by wangnan on 2017/8/24.
 */
@Component
public class CleanDataJob {

    private final Logger logger = LoggerFactory.getLogger(getClass());
    private static final int CLEAN_BATCH_COUNT = 1000;

    @Autowired
    private ProductHeatValuesMapper productHeatValuesMapper;
    @Autowired
    private ProductModelValueMapper productModelValueMapper;
    @Autowired
    private SknFlowMapper sknFlowMapper;
    @Autowired
    private SknCtrValueMapper sknCtrValueMapper;
    @Autowired
    private ProductSalesInfoMapper productSalesInfoMapper;
    @Autowired
    private BrandVectorFeatureMapper brandVectorFeatureMapper;
    @Autowired
    private SimilarSknMapper similarSknMapper;

    private String getLastThreeDayDate() {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DAY_OF_MONTH, -3);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        return DateUtil.DateToString(calendar.getTime(), DateStyle.YYYYMMDD);
    }

    @Scheduled(cron = "0 0 1 * * ?")
    public void cleanProductModelValue() {
        String generateDateStr = getLastThreeDayDate();
        long begin = System.currentTimeMillis();
        logger.info("productModelValue clean start----[begin={}]", begin);
        List<Long> sknList = productModelValueMapper.selectSknByDate(generateDateStr, CLEAN_BATCH_COUNT);
        while (CollectionUtils.isNotEmpty(sknList)) {
            productModelValueMapper.deleteBatch(sknList, generateDateStr);
            sknList = productModelValueMapper.selectSknByDate(generateDateStr, CLEAN_BATCH_COUNT);
        }
        logger.info("productModelValue clean end----[clearDate={}][cost={}]", generateDateStr, System.currentTimeMillis() - begin);
    }

    @Scheduled(cron = "0 5 1 * * ?")
    public void cleanSknFlow() {
        String generateDateStr = getLastThreeDayDate();
        long begin = System.currentTimeMillis();
        logger.info("sknFlow clean start----[begin={}]", begin);
        List<Long> sknList = sknFlowMapper.selectSknByDate(generateDateStr, CLEAN_BATCH_COUNT);
        while (CollectionUtils.isNotEmpty(sknList)) {
            sknFlowMapper.deleteBatch(sknList, generateDateStr);
            sknList = sknFlowMapper.selectSknByDate(generateDateStr, CLEAN_BATCH_COUNT);
        }
        logger.info("sknFlow clean end----[clearDate={}][cost={}]", generateDateStr, System.currentTimeMillis() - begin);
    }

    @Scheduled(cron = "0 10 1 * * ?")
    public void cleanSknCtrValue() {
        String generateDateStr = getLastThreeDayDate();
        long begin = System.currentTimeMillis();
        logger.info("sknCtrValue clean start----[begin={}]", begin);
        List<Long> sknList = sknCtrValueMapper.selectSknByDate(generateDateStr, CLEAN_BATCH_COUNT);
        while (CollectionUtils.isNotEmpty(sknList)) {
            sknCtrValueMapper.deleteBatch(sknList, generateDateStr);
            sknList = sknCtrValueMapper.selectSknByDate(generateDateStr, CLEAN_BATCH_COUNT);
        }
        logger.info("sknCtrValue clean end----[clearDate={}][cost={}]", generateDateStr, System.currentTimeMillis() - begin);
    }

    @Scheduled(cron = "0 15 1 * * ?")
    public void cleanProductSalesInfo() {
        String generateDateStr = getLastThreeDayDate();
        long begin = System.currentTimeMillis();
        logger.info("productSalesInfo clean start----[begin={}]", begin);
        List<Long> sknList = productSalesInfoMapper.selectSknByDate(generateDateStr, CLEAN_BATCH_COUNT);
        while (CollectionUtils.isNotEmpty(sknList)) {
            productSalesInfoMapper.deleteBatch(sknList, generateDateStr);
            sknList = productSalesInfoMapper.selectSknByDate(generateDateStr, CLEAN_BATCH_COUNT);
        }
        logger.info("productSalesInfo clean end----[clearDate={}][cost={}]", generateDateStr, System.currentTimeMillis() - begin);
    }

    @Scheduled(cron = "0 20 1 * * ?")
    public void cleanProductHeatValues() {
        String generateDateStr = getLastThreeDayDate();
        long begin = System.currentTimeMillis();
        logger.info("productHeatValues clean start----[begin={}]", begin);
        List<Long> sknList = productHeatValuesMapper.selectSknByDate(generateDateStr, CLEAN_BATCH_COUNT);
        while (CollectionUtils.isNotEmpty(sknList)) {
            productHeatValuesMapper.deleteBatch(sknList, generateDateStr);
            sknList = productHeatValuesMapper.selectSknByDate(generateDateStr, CLEAN_BATCH_COUNT);
        }
        logger.info("productHeatValues clean end----[clearDate={}][cost={}]", generateDateStr, System.currentTimeMillis() - begin);
    }
    
    @Scheduled(cron = "0 25 1 * * ?")
    public void cleanBrandVectorFeature() {
        String generateDateStr = getLastThreeDayDate();
        long begin = System.currentTimeMillis();
        logger.info("BrandVectorFeature clean start----[begin={}]", begin);
        brandVectorFeatureMapper.deleteBatchByDateId(generateDateStr);
        logger.info("BrandVectorFeature clean end----[clearDateStr={}][cost={}]", generateDateStr, System.currentTimeMillis() - begin);
    }

    @Scheduled(cron = "0 30 1 * * ?")
    public void cleanSimilarSkn() {
        String generateDateStr = getLastThreeDayDate();
        long begin = System.currentTimeMillis();
        logger.info("SimilarSkn clean start----[begin={}]", begin);
        similarSknMapper.deleteBatchByDateId(Integer.valueOf(generateDateStr));
        logger.info("SimilarSkn clean end----[clearDateStr={}][cost={}]", generateDateStr, System.currentTimeMillis() - begin);
    }
}