package com.evt.mgr.job;

import java.util.List;
import java.util.Set;

import com.taurus.core.plugin.database.DataBase;
import com.taurus.core.util.DateUtils;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobKey;

import com.data.util.EventType;
import com.data.util.Utility;
import com.evt.mgr.GroupPublisherService;
import com.taurus.core.entity.ITArray;
import com.taurus.core.entity.TArray;
import com.taurus.core.plugin.redis.Redis;
import com.taurus.core.plugin.redis.RedisLock;
import com.taurus.core.util.Logger;
import com.taurus.core.util.StringUtil;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.ScanParams;
import redis.clients.jedis.ScanResult;

public class CleanGroupLogJob implements Job {
    private Logger logger = Logger.getLogger(CleanGroupLogJob.class);

    public CleanGroupLogJob() {

    }

    /**
     * 清除亲友圈中无效的房间
     */
    private void cleanGroupMemberLog() {
        int time = DateUtils.getBeginDay() - 31 * 24 * 3600;
        String deleteSql = String.format("delete from group_member_log where time < %s", time);
        try {
            long startTime = System.currentTimeMillis();
            DataBase.use().executeUpdate(deleteSql);
            logger.info("cleanGroupMemberLog use time:"+(System.currentTimeMillis()-startTime)+ " " + deleteSql);
        }
        catch (Exception e)
        {
            logger.error(e);
        }
    }

    private void CleanGroupHpLog() {
        int time = DateUtils.getBeginDay() - 31 * 24 * 3600;
        String deleteSql3 = String.format("delete from group_hp_log where time < %s limit 1000000", time);
        try {
            long startTime = System.currentTimeMillis();
            DataBase.use().executeUpdate(deleteSql3);
            logger.info("CleanGroupHpLog use time:"+(System.currentTimeMillis()-startTime)+ " " + deleteSql3);
        }
        catch (Exception e)
        {
            logger.error(e);
        }
    }

    private void CleanRecRoomLog() {
        int time = DateUtils.getBeginDay() - 31 * 24 * 3600;
        String deleteSql2 = String.format("delete from room_rec_log where time < %s limit 1000000", time);
        try {
            long startTime = System.currentTimeMillis();
            DataBase.use().executeUpdate(deleteSql2);
            logger.info("CleanRecRoomLog use time:"+(System.currentTimeMillis()-startTime) + " " + deleteSql2);
        }
        catch (Exception e)
        {
            logger.error(e);
        }
    }

    @Override
    public void execute(JobExecutionContext context) {
        JobKey jobKey = context.getJobDetail().getKey();
        if(jobKey.getName().equals("clean_group_member_log")) {
            logger.info("执行成功" + jobKey.getName());
            cleanGroupMemberLog();
        }
        if(jobKey.getName().equals("clean_group_hp_log")) {
            logger.info("执行成功" + jobKey.getName());
            CleanGroupHpLog();
        }
        if(jobKey.getName().equals("clean_rec_room_log")) {
            logger.info("执行成功" + jobKey.getName());
            CleanRecRoomLog();
        }
    }
}
