import datetime
import pymysql


#一周之前的date(7)
my_date1 = (datetime.date.today() - datetime.timedelta(days=7)).strftime("%Y-%m-%d")
#二周之前的date(14)
my_date2 = (datetime.date.today() - datetime.timedelta(days=14)).strftime("%Y-%m-%d")
#一个月之前的date(30)
my_date3 = (datetime.date.today() - datetime.timedelta(days=30)).strftime("%Y-%m-%d")
#两个月之前的date(60)
my_date4 = (datetime.date.today() - datetime.timedelta(days=60)).strftime("%Y-%m-%d")
#三个月之前的date(90)
my_date5 = (datetime.date.today() - datetime.timedelta(days=90)).strftime("%Y-%m-%d")


def get_register_uid_count():
	"""
	rtype : dict
	"""
	db = pymysql.connect(host='10.66.157.22', port=4000, user='root', passwd='3SYz54LS9#^9sBvC', db='jerry_test')
	cursor = db.cursor()
	sql = "select '0-7' as label,count(distinct(device_id)) \
	from data_feed_exposure \
	where device_id not in \
	(select distinct(device_id) from data_feed_exposure \
	where stat_date < {0}) \
	union all \
	select '7-14' as label,count(distinct(device_id)) \
	from data_feed_exposure \
	where device_id not in \
	(select distinct(device_id) from data_feed_exposure \
	where stat_date < {1}) \
	and device_id in \
	(select distinct(device_id) from data_feed_exposure \
	where stat_date < {0}) \
	union all \
	select '14-30' as label,count(distinct(device_id)) \
	from data_feed_exposure \
	where device_id not in \
	(select distinct(device_id) from data_feed_exposure \
	where stat_date < {2}) \
	and device_id in \
	(select distinct(device_id) from data_feed_exposure \
	where stat_date < {1}) \
	union all \
	select '30-60' as label,count(distinct(device_id)) \
	from data_feed_exposure \
	where device_id not in \
	(select distinct(device_id) from data_feed_exposure \
	where stat_date < {3}) \
	and device_id in \
	(select distinct(device_id) from data_feed_exposure \
	where stat_date < {2}) \
	union all \
	select '60-90' as label,count(distinct(device_id)) \
	from data_feed_exposure \
	where device_id not in \
	(select distinct(device_id) from data_feed_exposure \
	where stat_date < {4}) \
	and device_id in \
	(select distinct(device_id) from data_feed_exposure \
	where stat_date < {3}) \
	union all \
	select '90+' as label,count(distinct(device_id)) \
	from data_feed_exposure \
	where device_id in \
	(select distinct(device_id) from data_feed_exposure \
	where stat_date < {4})".format(my_date1,my_date2,my_date3,my_date4,my_date5)
	cursor.execute(sql)
	result = cursor.fetchall()
	db.close()
	dct = {}
	for i in result:
		dct[i[0]] = i[1]
	return dct

	
if __name__ == '__main__':
	register_uid_detail = get_register_uid_count()



