-- 用户基本信息视图
CREATE VIEW consumer_base_info_view AS
SELECT
	t1.*, cl.channel_name
FROM
	(
		SELECT
			c.user_id,
			c.nick_name,
			c.created_at register_at,
			c.mobile register_mobile,
			c.level,
			c.channel_no
		FROM
			consumer_info c
		LEFT JOIN `user` u ON c.user_id = u.user_id
	) t1
LEFT JOIN channel cl ON t1.channel_no = cl.channel_no;

-- 创建用户服务卡视图
CREATE VIEW consumer_service_card_view AS 
SELECT
	t1.user_id,
	t1.card_detail,
	t2.max_card_type,
	t2.sum_total_service_cnt,
	t2.sum_serviced_cnt,
	t2.sum_booked_cnt
FROM
	(
		SELECT
			user_id,
			GROUP_CONCAT(card_name, ':', card_cnt) card_detail
		FROM
			(
				SELECT
					user_id,
					CASE card_type
				WHEN 10 THEN
					'单次卡'
				WHEN 20 THEN
					'月卡'
				WHEN 30 THEN
					'季卡'
				WHEN 40 THEN
					'半年卡'
				WHEN 50 THEN
					'年卡'
				ELSE
					''
				END AS card_name,
				COUNT(card_type) AS card_cnt
			FROM
				service_card
			GROUP BY
				user_id,
				card_type
			) ot
		GROUP BY
			user_id
	) t1,
	(
		SELECT
			user_id,
			MAX(card_type) max_card_type,
			SUM(total_service_cnt) sum_total_service_cnt,
			SUM(booked_cnt) sum_booked_cnt,
			SUM(serviced_cnt) sum_serviced_cnt
		FROM
			service_card -- WHERE sc.user_id = '180515171015928100'
		GROUP BY
			user_id
	) t2
WHERE
	t1.user_id = t2.user_id;

-- 创建用户服务评价视图
CREATE VIEW service_evaluation_view AS
SELECT
	*,
	IF (
		(
			time_score + service_score + profession_score = '15'
			AND content != ''
		),
		1,
		0
	) evaluation_status
FROM
	service_evaluation;

-- 创建人员回访列表视图
CREATE VIEW feedback_order_view AS
SELECT
	f.*,
	IF (
		(
			attitude_score + quality_score + efficiency_score = '15'
			AND feedback_content != ''
		),
		1,
		0
	) feedback_evaluation_status,
  a.nick_name feedback_name
FROM
	feedback_order f
LEFT JOIN admin_info a ON f.feedback_by = a.user_id;

-- 创建客户商家关系视图
CREATE VIEW rel_consumer_merchant_view AS
SELECT t3.user_id,
	IF (
			t3.merchant_no = t4.coupon_merchant_no,
			t3.merchant_no,
			GROUP_CONCAT(
				t3.merchant_no,
				'&',
				t4.coupon_merchant_no
			)
		) merchant_no 
FROM 
(
	SELECT
		t1.user_id,
		IF (
			t1.order_merchant = t2.card_merchant_no,
			t1.order_merchant,
			GROUP_CONCAT(
				t1.order_merchant,
				'&',
				t2.card_merchant_no
			)
		) merchant_no
	FROM
	(
		select t.user_id, GROUP_CONCAT(t.merchant_no ORDER BY t.merchant_no asc separator '&') order_merchant from 
		(
		select DISTINCT c.user_id, v.merchant_no FROM consumer_info c, vorder v
		WHERE c.user_id = v.user_id
		GROUP BY c.user_id, v.merchant_no
		) t 

		GROUP BY t.user_id
	) t1,
	(
	select t.user_id, GROUP_CONCAT(t.merchant_no ORDER BY t.merchant_no asc separator '&') card_merchant_no FROM
	(
	select DISTINCT c.user_id, s.merchant_no FROM consumer_info c, service_card s
	WHERE c.user_id = s.user_id

	GROUP BY c.user_id,s.merchant_no
	) t

	GROUP BY  t.user_id
	) t2
	where t1.user_id = t2.user_id
	GROUP BY t1.user_id
) t3,
(
select t.user_id, GROUP_CONCAT(t.merchant_no ORDER BY t.merchant_no asc separator '&') coupon_merchant_no FROM
(
select DISTINCT c.user_id, cp.merchant_no FROM consumer_info c, coupon cp
WHERE c.user_id = cp.user_id

GROUP BY c.user_id,cp.merchant_no
) t

GROUP BY  t.user_id
) t4
WHERE t3.user_id = t4.user_id
GROUP BY t3.user_id;

-- 创建服务卡与预约明细关系视图
CREATE VIEW rel_service_card_book_detail AS SELECT
	s.card_no,
	s.card_name,
	b.book_no
FROM
	service_card s,
	book_detail b
WHERE
	s.card_no = b.card_no;


-- SELECT * FROM CONSUMER_BASE_INFO_VIEW;
-- SELECT * FROM CONSUMER_SERVICE_CARD_VIEW;
-- SELECT * FROM FEEDBACK_ORDER_VIEW;
-- SELECT * FROM REL_CONSUMER_MERCHANT_VIEW;
-- SELECT * FROM SERVICE_EVALUATION_VIEW;
-- SELECT * FROM REL_SERVICE_CARD_BOOK_DETAIL;

