import datetime
import pymysql
from utils import get_yesterday_date



#一周之前的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_click_zero_uid_count(platform):
	"""
	platform : "ios","android","all"
	rtype : dict
	"""
	if platform == "ios":
		platform = "='App Store'"
	elif platform == "android":
		platform = "!='App Store'"
	else:
		platform = " is not null"
	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_type{0} \
	and stat_date = '{7}' \
	and device_id not in \
	(select distinct(device_id) from data_feed_click \
	where stat_date = '{7}' \
		and device_type{1}) \
	and device_id in \
	(select distinct(device_id) \
	from data_feed_exposure \
	where device_id not in \
	(select distinct(device_id) from data_feed_exposure \
		where stat_date < {2})) \
	union all \
	select '7-14' as label,count(distinct(device_id)) from data_feed_exposure \
	where device_type{0} \
	and stat_date = '{7}' \
	and device_id not in \
	(select distinct(device_id) from data_feed_click \
	where stat_date = '{7}' \
		and device_type{1}) \
	and device_id in \
	(select 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 '14-30' as label,count(distinct(device_id)) from data_feed_exposure \
	where device_type{0} \
	and stat_date = '{7}' \
	and device_id not in \
	(select distinct(device_id) from data_feed_click \
	where stat_date = '{7}' \
		and device_type{1}) \
	and device_id in \
	(select 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 '30-60' as label,count(distinct(device_id)) from data_feed_exposure \
	where device_type{0} \
	and stat_date = '{7}' \
	and device_id not in \
	(select distinct(device_id) from data_feed_click \
	where stat_date = '{7}' \
		and device_type{1}) \
	and device_id in \
	(select distinct(device_id) \
	from data_feed_exposure \
	where device_id not in \
	(select distinct(device_id) from data_feed_exposure \
		where stat_date < {5}) \
	and device_id in \
	(select distinct(device_id) from data_feed_exposure \
		where stat_date < {4})) \
	union all \
	select '60-90' as label,count(distinct(device_id)) from data_feed_exposure \
	where device_type{0} \
	and stat_date = '{7}' \
	and device_id not in \
	(select distinct(device_id) from data_feed_click \
	where stat_date = '{7}' \
		and device_type{1}) \
	and device_id in \
	(select distinct(device_id) \
	from data_feed_exposure \
	where device_id not in \
	(select distinct(device_id) from data_feed_exposure \
		where stat_date < {6}) \
	and device_id in \
	(select distinct(device_id) from data_feed_exposure \
		where stat_date < {5})) \
	union all \
	select '90+' as label,count(distinct(device_id)) from data_feed_exposure \
	where device_type{0} \
	and stat_date = '{7}' \
	and device_id not in \
	(select distinct(device_id) from data_feed_click \
	where stat_date = '{7}' \
		and device_type{1}) \
	and device_id in \
	(select distinct(device_id) \
	from data_feed_exposure \
	where device_id in \
	(select distinct(device_id) from data_feed_exposure \
		where stat_date < {6}))".format(platform,platform.replace(' ','') if platform[-2]=='e' else platform,my_date1,my_date2,my_date3,my_date4,my_date5,get_yesterday_date())
	cursor.execute(sql)
	result = cursor.fetchall()
	db.close()
	dct = {}
	for i in result:
		dct[i[0]] = i[1]
	return dct

	
if __name__ == '__main__':
	no_click_uid_detail_all = get_click_zero_uid_count("all")
	no_click_uid_detail_ios = get_click_zero_uid_count("ios")
	no_click_uid_detail_android = get_click_zero_uid_count("android")



