package com.data.util;



import com.taurus.core.util.DateUtils;
import com.taurus.core.util.StringUtil;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.Pipeline;

public class CountUtil {
	
	public static final int LOG_FOREVER = 0;
	public static final int LOG_DAY = 1;
	public static final int LOG_LASTDAY = 2;
	public static final int LOG_WEEK = 3;
	public static final int LOG_LASTWEEK = 4;
	public static final int LOG_MONTH = 5;
	public static final int LOG_LASTMONTH = 6;
	
	private static final int DAY2 = 172800;
	private static final int DAY3 = 259200;
	private static final int DAY10 = 864000;
//	private static final int MONTH = 2592000;
//	private static final int WEEK2 = 1209600;
//	private static final int MONTH2 = 2678400;
	private static final int MONTH = 1209600;
	private static final int MONTH2 = 1209600;

	public static int getCountLog(String key,int type,Jedis jedis) {
		String value = jedis.get(key);
		if(StringUtil.isNotEmpty(value)) {
			return Integer.parseInt(value);
		}
		return 0;
	}
	

	
	
	/**
	 * 获取天数据
	 * @param key
	 * @param jedis
	 * @return
	 */
	public static int getCountLogByDay(String key,Jedis jedis) {
		String day_key = key + ":d"+DateUtils.getBeginDay();
		String value = jedis.get(day_key);
		if(StringUtil.isEmpty(value))return 0;
		return Integer.parseInt(value);
	}
	
	
	
	
	/**
	 * 统计
	 * @param key
	 * @param num
	 * @param jedis
	 */
	public static void countLog(String key,int num,Jedis jedis) {
		countLog(key,num,jedis,false);
	}
	
	public static void countLog(String key,int num,Pipeline pipeline) {
		countLog(key,num,pipeline,false);
	}
	
	/**
	 * 统计
	 * @param key
	 * @param num
	 * @param jedis
	 * @param day_expire_month 天数据保存一个月
	 */
	public static void countLog(String key,int num,Jedis jedis,boolean day_expire_month) {
		
		String day_key = key + ":d"+DateUtils.getBeginDay();
//		String week_key = key + ":w"+DateUtils.getBeginWeek();
//		String month_key = key + ":m"+DateUtils.getBeginMonth();
		jedis.incrBy(day_key,num);
		jedis.expire(day_key, day_expire_month?MONTH2:DAY10);
//		jedis.incrBy(week_key,num);
//		jedis.expire(week_key, WEEK2);
//		jedis.incrBy(month_key,num);
//		jedis.expire(month_key, MONTH2);
	}
	
	/**
	 * 统计
	 * @param key
	 * @param num
	 * @param jedis
	 * @param day_expire_month 天数据保存一个月
	 */
	public static void countLog(String key,int num,Pipeline pipeline,boolean day_expire_month) {
		
		String day_key = key + ":d"+DateUtils.getBeginDay();
//		String week_key = key + ":w"+DateUtils.getBeginWeek();
//		String month_key = key + ":m"+DateUtils.getBeginMonth();
		pipeline.incrBy(day_key,num);
		pipeline.expire(day_key, day_expire_month?MONTH2:DAY10);
//		pipeline.incrBy(week_key,num);
//		pipeline.expire(week_key, WEEK2);
//		pipeline.incrBy(month_key,num);
//		pipeline.expire(month_key, MONTH2);
	}
	
	public static void countLog(String key,int num,Jedis jedis,boolean day_expire_month,boolean total) {
		
		if(total) {
			jedis.incrBy(key,num);
		}
		countLog(key,num,jedis,day_expire_month);
	}
	
	public static void countLog(String key,int num,Pipeline pipeline,boolean day_expire_month,boolean total) {
		
		if(total) {
			pipeline.incrBy(key,num);
		}
		countLog(key,num,pipeline,day_expire_month);
	}
	
	/**
	 * 统计天数据(存10天)
	 * @param key
	 * @param num
	 * @param jedis
	 */
	public static long countLogByDay(String key,int num,Jedis jedis) {
		return countLogByDay(key,num,jedis,DAY10);
	}
	
	
	/**
	 * 统计天数据(存10天)
	 * @param key
	 * @param num
	 * @param jedis
	 */
	public static void countLogByDay(String key,int num,Pipeline pipeline) {
		countLogByDay(key,num,pipeline,DAY10);
	}
	
	/**
	 * 统计天数据(存10天)
	 * @param key
	 * @param num
	 * @param jedis
	 */
	public static void countLogByDay30(String key,int num,Pipeline pipeline) {
		
		countLogByDay(key,num,pipeline,MONTH);
		
		String month_key = key + ":m"+DateUtils.getBeginMonth();
		pipeline.incrBy(month_key,num);
		pipeline.expire(month_key, MONTH2);
	}
	
	/**
	 * 统计天数据(存10天)
	 * @param key
	 * @param num
	 * @param jedis
	 */
	public static void countLogByDay(String key,int num,Pipeline pipeline,boolean total) {
		
		if(total) {
			pipeline.incrBy(key,num);
		}
		
		countLogByDay(key,num,pipeline,DAY10);
	}
	
	/**
	 * 统计天数据(存30天)
	 * @param key
	 * @param num
	 * @param jedis
	 */
	public static void countLogByDay30(String key,int num,Pipeline pipeline,boolean total) {
		
		if(total) {
			pipeline.incrBy(key,num);
		}
		
		countLogByDay(key,num,pipeline,MONTH);
		
		String month_key = key + ":m"+DateUtils.getBeginMonth();
		pipeline.incrBy(month_key,num);
		pipeline.expire(month_key, MONTH2);
	}
	
	/**
	 * 统计天数据(存10天)
	 * @param key
	 * @param num
	 * @param jedis
	 */
	public static long countLogByDay(String key,int num,Jedis jedis,boolean total) {
		
		if(total) {
			jedis.incrBy(key,num);
		}
		
		return countLogByDay(key,num,jedis,DAY10);
	}
	
	/**
	 * 统计天数据
	 * @param key
	 * @param num
	 * @param jedis
	 */
	public static long countLogByDay(String key,int num,Jedis jedis,int time) {
		String day_key = key + ":d"+DateUtils.getBeginDay();
		
		long value = jedis.incrBy(day_key,num);
		jedis.expire(day_key, time);
		return value;
	}
	
	/**
	 * 统计天数据
	 * @param key
	 * @param num
	 * @param jedis
	 */
	public static void countLogByDay(String key,int num,Pipeline pipeline,int time) {
		String day_key = key + ":d"+DateUtils.getBeginDay();
		
		pipeline.incrBy(day_key,num);
		pipeline.expire(day_key, time);
	}
	
	/**
	 * 统计天数据(存2天)
	 * @param key
	 * @param num
	 * @param jedis
	 */
	public static long countLogByDay2(String key,int num,Jedis jedis) {
		String day_key = key + ":d"+DateUtils.getBeginDay();
		long value = jedis.incrBy(day_key,num);
		jedis.expire(day_key, DAY2);
		return value;
	}

	/**
	 * 统计天数据(存2天)
	 * @param key
	 * @param num
	 * @param jedis
	 */
	public static long countLogByDay3(String key,int num,Jedis jedis) {
		String day_key = key + ":d"+DateUtils.getBeginDay();
		long value = jedis.incrBy(day_key,num);
		jedis.expire(day_key, DAY3);
		return value;
	}
}
