package web_group;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import redis.clients.jedis.Jedis;

/**
 * clear redis data 
 * @author yjl 2020-06-05
 * @version 1.0.0
 * */
public class DataClearUtils {
	
	private  void clear_data() {
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Map<String,String> map = new HashMap<>();
		map.put("force_ver", "1");
		map.put("ver", "1");
		Jedis jedis = new Jedis("127.0.0.1", 6380);
		jedis.auth("123456");
		//选择库
		jedis.select(5);
		Set<String> keys = jedis.keys("*");
		int i = 0;
		for(String str : keys) {
			long stime  = Long.valueOf(str.substring(15,str.length()));
			if(stime < 1591286400) {
				jedis.del(str);
				i++;
				System.out.println(i);
			}
			
		}
		System.out.println(i);
		
		jedis.close();
	}
	
	private  void clear_data_thread() {
//		Thread th = new Thread(new clear_data_t(1592323200, 1592668800,1));
//		th.start();
//		
//		Thread th1 = new Thread(new clear_data_t(1591977600, 1592323200,2));
//		th1.start();
//		
//		Thread th2 = new Thread(new clear_data_t(1591718400, 1591977600,3));
//		th2.start();
//		
		Thread th3 = new Thread(new clear_data_test(1590940800, 1593964800,4));
		th3.start();
		
//		Thread th4 = new Thread(new clear_data_t(1588262400, 1590336000,5));
//		th4.start();
////		
//		Thread th5 = new Thread(new clear_data_t(1588262400, 1589904000,6));
//		th5.start();
	}
	class clear_data_test implements Runnable{
		long start = 0;
		long end = 0;
		int num = 0;
		
		public clear_data_test(long start,long end,int num) {
			this.start =  start;
			this.end = end;
			this.num = num;
		}
		@Override
		public void run() {
			// TODO Auto-generated method stub
			SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			Map<String,String> map = new HashMap<>();
			map.put("force_ver", "1");
			map.put("ver", "1");
			Jedis jedis = new Jedis("127.0.0.1", 6379);
			jedis.auth("123456");
			//选择库
			jedis.select(0);
			Set<String> keys = jedis.keys("*");
			int i = 0;
			for(String str : keys) {
				long stime  = Long.valueOf(str.substring(15,str.length()));
				if(stime < end && stime > start) {
					jedis.del(str);
					i++;
					System.out.println("#"+num +"Thread"+i);
				}
				
			}
			System.out.println(i);
			
			jedis.close();
		}
		
	}
	
	//
	
	public static void main(String[] args) {
		DataClearUtils initDataUtils = new DataClearUtils();
		initDataUtils.clear_data_thread();
	}
	
	class clear_data_t implements Runnable{
		long start = 0;
		long end = 0;
		int num = 0;
		
		public clear_data_t(long start,long end,int num) {
			this.start =  start;
			this.end = end;
			this.num = num;
		}
		@Override
		public void run() {
			// TODO Auto-generated method stub
			SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			Map<String,String> map = new HashMap<>();
			map.put("force_ver", "1");
			map.put("ver", "1");
			Jedis jedis = new Jedis("127.0.0.1", 6380);
			jedis.auth("123456");
			//选择库
			jedis.select(5);
			Set<String> keys = jedis.keys("*");
			int i = 0;
			for(String str : keys) {
				long stime  = Long.valueOf(str.substring(15,str.length()));
				if(stime < end && stime > start) {
					jedis.del(str);
					i++;
					System.out.println("#"+num +"Thread"+i);
				}
				
			}
			System.out.println(i);
			
			jedis.close();
		}
		
	}
}
