package com.ssj.dao.weixin.problem.dao;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import com.ssj.bean.weixin.problem.domain.ValidVip;


@Repository
public interface ValidVipDao extends JpaRepository<ValidVip, String>{
	
	@Query(nativeQuery = true, value = "INSERT INTO tb_valid_vip(id,lib_id,vip_total,family,year_card,half_card,month_card,weekend_card,task_card,taskyear_card,Holiday,features,months,type)\n" +
			"SELECT uuid(), a.lib_id,a.vip_total,b.family,a.year_card,a.half_card,a.month_card,a.weekend_card,a.task_card,a.taskyear_card,a.Holiday,a.features,a.months,1 FROM \n" +
			"(SELECT\n" +
			"	t.lib_id,\n" +
			"COUNT(distinct t.vip_id)AS vip_total ,\n" +
			"	sum(\n" +
			"		CASE\n" +
			"		WHEN t.species = 'S001'\n" +
			"    AND t.type ='C001'\n" +
			"		AND t.stat = '1'\n" +
			"		 THEN\n" +
			"			1\n" +
			"		ELSE\n" +
			"			0\n" +
			"		END\n" +
			"	) as year_card,\n" +
			"	sum(\n" +
			"		CASE\n" +
			"		WHEN t.species = 'S001'\n" +
			"    AND t.type ='C002'\n" +
			"		AND t.stat = '1'\n" +
			"	 THEN \n" +
			"			1\n" +
			"		ELSE\n" +
			"			0\n" +
			"		END\n" +
			"	)as half_card,\n" +
			"sum(\n" +
			"		CASE\n" +
			"		WHEN t.species = 'S001'\n" +
			"    AND t.type ='C003'\n" +
			"		AND t.stat = '1'\n" +
			"	 THEN\n" +
			"			1\n" +
			"		ELSE\n" +
			"			0\n" +
			"		END\n" +
			"	)as month_card,\n" +
			"sum(\n" +
			"		CASE\n" +
			"		WHEN t.species = 'S003'\n" +
			"    AND t.type ='C004'\n" +
			"		AND t.stat = '1'\n" +
			"		THEN\n" +
			"			1\n" +
			"		ELSE\n" +
			"			0\n" +
			"		END\n" +
			"	)as task_card,\n" +
			"	sum(\n" +
			"		CASE\n" +
			"		WHEN t.species = 'S003'\n" +
			"    AND t.type ='C006'\n" +
			"		AND t.stat = '1'\n" +
			"		 THEN\n" +
			"			1\n" +
			"		ELSE\n" +
			"			0\n" +
			"		END\n" +
			"	)as taskyear_card,\n" +
			"sum(\n" +
			"		CASE\n" +
			"		WHEN t.species = 'S001'\n" +
			"    AND t.type ='C007'\n" +
			"		AND t.stat = '1'\n" +
			"	 THEN\n" +
			"			1\n" +
			"		ELSE\n" +
			"			0\n" +
			"		END\n" +
			"	)as weekend_card,\n" +
			"sum(\n" +
			"		CASE\n" +
			"		WHEN t.species = 'S004'\n" +
			"		AND t.stat = '1'\n" +
			"	 THEN\n" +
			"			1\n" +
			"		ELSE\n" +
			"			0\n" +
			"		END\n" +
			"	)as features,\n" +
			"sum(\n" +
			"		CASE\n" +
			"		WHEN t.species = 'S001'\n" +
			"    AND t.type ='C008'\n" +
			"		AND t.stat = '1'\n" +
			"	 THEN\n" +
			"			1\n" +
			"		ELSE\n" +
			"			0\n" +
			"		END\n" +
			"	)as Holiday,\n" +
			"date_format(now(), '%Y-%m') AS months\n" +
			"FROM\n" +
			"	tb_lib_vip_service t \n" +
			"WHERE t.stat='1'\n" +
			"GROUP BY t.lib_id) AS a\n" +
			"LEFT JOIN \n" +
			"(SELECT count(distinct v.user_id) AS family ,s.lib_id FROM tb_lib_vip_service s LEFT JOIN\n" +
			"tb_lib_vip v ON s.vip_id=v.id\n" +
			"WHERE s.stat='1'\n" +
			"GROUP BY s.lib_id) b\n" +
			"on a.lib_id=b.lib_id")
	@Modifying
	void saveValidVip();
	
	@Query(nativeQuery = true, value ="INSERT INTO tb_valid_vip (\n" +
"	id,\n" +
"	lib_id,\n" +
"	vip_total,\n" +
"	family,\n" +
"	year_card,\n" +
"	half_card,\n" +
"	month_card,\n" +
"	weekend_card,\n" +
"	task_card,\n" +
"	taskyear_card,\n" +
"	Holiday,\n" +
"	features,\n" +
"	months,\n" +
"	type\n" +
") SELECT\n" +
"	uuid(),\n" +
"	a.lib_id,\n" +
"	a.vip_total,\n" +
"	b.family,\n" +
"	a.year_card,\n" +
"	a.half_card,\n" +
"	a.month_card,\n" +
"	a.weekend_card,\n" +
"	a.task_card,\n" +
"	a.taskyear_card,\n" +
"	a.Holiday,\n" +
"	a.features,\n" +
"	a.months,\n" +
"	2\n" +
"FROM\n" +
"	(\n" +
"		SELECT\n" +
"			t.lib_id,\n" +
"			COUNT(DISTINCT t.vip_id) AS vip_total,\n" +
"			sum(\n" +
"				CASE\n" +
"				WHEN t.species = 'S001'\n" +
"				AND t.type = 'C001'\n" +
"				AND t.stat = '1'\n" +
"				AND TO_DAYS(t.start_time) >= TO_DAYS(\n" +
"					date(\n" +
"						concat(\n" +
"							YEAR (curdate()),\n" +
"							'-',\n" +
"							MONTH (curdate()),\n" +
"							'-',\n" +
"							'1'\n" +
"						)\n" +
"					)\n" +
"				)\n" +
"				AND TO_DAYS(t.start_time) <= TO_DAYS(last_day(curdate())) THEN\n" +
"					1\n" +
"				ELSE\n" +
"					0\n" +
"				END\n" +
"			) AS year_card,\n" +
"			sum(\n" +
"				CASE\n" +
"				WHEN t.species = 'S001'\n" +
"				AND t.type = 'C002'\n" +
"				AND t.stat = '1'\n" +
"				AND TO_DAYS(t.start_time) >= TO_DAYS(\n" +
"					date(\n" +
"						concat(\n" +
"							YEAR (curdate()),\n" +
"							'-',\n" +
"							MONTH (curdate()),\n" +
"							'-',\n" +
"							'1'\n" +
"						)\n" +
"					)\n" +
"				)\n" +
"				 AND TO_DAYS(t.start_time) <= TO_DAYS(last_day(curdate())) THEN\n" +
"					1\n" +
"				ELSE\n" +
"					0\n" +
"				END\n" +
"			) AS half_card,\n" +
"			sum(\n" +
"				CASE\n" +
"				WHEN t.species = 'S001'\n" +
"				AND t.type = 'C003'\n" +
"				AND t.stat = '1'\n" +
"				AND TO_DAYS(t.start_time) >= TO_DAYS(\n" +
"					date(\n" +
"						concat(\n" +
"							YEAR (curdate()),\n" +
"							'-',\n" +
"							MONTH (curdate()),\n" +
"							'-',\n" +
"							'1'\n" +
"						)\n" +
"					)\n" +
"				)\n" +
"				 AND TO_DAYS(t.start_time) <= TO_DAYS(last_day(curdate())) THEN\n" +
"					1\n" +
"				ELSE\n" +
"					0\n" +
"				END\n" +
"			) AS month_card,\n" +
"			sum(\n" +
"				CASE\n" +
"				WHEN t.species = 'S003'\n" +
"				AND t.type = 'C004'\n" +
"				AND t.stat = '1'\n" +
"				AND TO_DAYS(t.start_time) >= TO_DAYS(\n" +
"					date(\n" +
"						concat(\n" +
"							YEAR (curdate()),\n" +
"							'-',\n" +
"							MONTH (curdate()),\n" +
"							'-',\n" +
"							'1'\n" +
"						)\n" +
"					)\n" +
"				)\n" +
"				 AND TO_DAYS(t.start_time) <= TO_DAYS(last_day(curdate())) THEN\n" +
"					1\n" +
"				ELSE\n" +
"					0\n" +
"				END\n" +
"			) AS task_card,\n" +
"			sum(\n" +
"				CASE\n" +
"				WHEN t.species = 'S003'\n" +
"				AND t.type = 'C006'\n" +
"				AND t.stat = '1'\n" +
"				AND TO_DAYS(t.start_time) >= TO_DAYS(\n" +
"					date(\n" +
"						concat(\n" +
"							YEAR (curdate()),\n" +
"							'-',\n" +
"							MONTH (curdate()),\n" +
"							'-',\n" +
"							'1'\n" +
"						)\n" +
"					)\n" +
"				)\n" +
"			 AND TO_DAYS(t.start_time) <= TO_DAYS(last_day(curdate())) THEN\n" +
"					1\n" +
"				ELSE\n" +
"					0\n" +
"				END\n" +
"			) AS taskyear_card,\n" +
"			sum(\n" +
"				CASE\n" +
"				WHEN t.species = 'S001'\n" +
"				AND t.type = 'C007'\n" +
"				AND t.stat = '1'\n" +
"				AND TO_DAYS(t.start_time) >= TO_DAYS(\n" +
"					date(\n" +
"						concat(\n" +
"							YEAR (curdate()),\n" +
"							'-',\n" +
"							MONTH (curdate()),\n" +
"							'-',\n" +
"							'1'\n" +
"						)\n" +
"					)\n" +
"				)\n" +
"				 AND TO_DAYS(t.start_time) <= TO_DAYS(last_day(curdate())) THEN\n" +
"					1\n" +
"				ELSE\n" +
"					0\n" +
"				END\n" +
"			) AS weekend_card,\n" +
"			sum(\n" +
"				CASE\n" +
"				WHEN t.species = 'S004'\n" +
"				AND t.stat = '1'\n" +
"				AND TO_DAYS(t.start_time) >= TO_DAYS(\n" +
"					date(\n" +
"						concat(\n" +
"							YEAR (curdate()),\n" +
"							'-',\n" +
"							MONTH (curdate()),\n" +
"							'-',\n" +
"							'1'\n" +
"						)\n" +
"					)\n" +
"				)\n" +
"				 AND TO_DAYS(t.start_time) <= TO_DAYS(last_day(curdate())) THEN\n" +
"					1\n" +
"				ELSE\n" +
"					0\n" +
"				END\n" +
"			) AS features,\n" +
"			sum(\n" +
"				CASE\n" +
"				WHEN t.species = 'S001'\n" +
"				AND t.type = 'C008'\n" +
"				AND t.stat = '1'\n" +
"				AND TO_DAYS(t.start_time) >= TO_DAYS(\n" +
"					date(\n" +
"						concat(\n" +
"							YEAR (curdate()),\n" +
"							'-',\n" +
"							MONTH (curdate()),\n" +
"							'-',\n" +
"							'1'\n" +
"						)\n" +
"					)\n" +
"				)\n" +
"				 AND TO_DAYS(t.start_time) <= TO_DAYS(last_day(curdate())) THEN\n" +
"					1\n" +
"				ELSE\n" +
"					0\n" +
"				END\n" +
"			) AS Holiday,\n" +
"			date_format(now(), '%Y-%m') AS months\n" +
"		FROM\n" +
"			tb_lib_vip_service t\n" +
"		WHERE\n" +
"			t.stat = '1'\n" +
"		AND TO_DAYS(t.start_time) >= TO_DAYS(\n" +
"			date(\n" +
"				concat(\n" +
"					YEAR (curdate()),\n" +
"					'-',\n" +
"					MONTH (curdate()),\n" +
"					'-',\n" +
"					'1'\n" +
"				)\n" +
"			)\n" +
"		)\n" +
"		 AND TO_DAYS(t.start_time) <= TO_DAYS(last_day(curdate()))\n" +
"		GROUP BY\n" +
"			t.lib_id\n" +
"	) AS a\n" +
"LEFT JOIN (\n" +
"	SELECT\n" +
"		count(DISTINCT v.user_id) AS family,\n" +
"		s.lib_id\n" +
"	FROM\n" +
"		tb_lib_vip_service s\n" +
"	LEFT JOIN tb_lib_vip v ON s.vip_id = v.id\n" +
"	WHERE\n" +
"		s.stat = '1'\n" +
"	AND TO_DAYS(s.start_time) >= TO_DAYS(\n" +
"		date(\n" +
"			concat(\n" +
"				YEAR (curdate()),\n" +
"				'-',\n" +
"				MONTH (curdate()),\n" +
"				'-',\n" +
"				'1'\n" +
"			)\n" +
"		)\n" +
"	)\n" +
"	 AND TO_DAYS(s.start_time) <= TO_DAYS(last_day(curdate()))\n" +
"	GROUP BY\n" +
"		s.lib_id\n" +
") b ON a.lib_id = b.lib_id")
	@Modifying
	void saveMonthValidVip();

}
