SELECT
	r.wxMpOpenid,
	d.CODE,
	e.NAME AS goodsName,
	s.goodsNumber AS goodsNumber,
CASE 
		WHEN r.wxMpOpenid IS NOT NULL 
		then '家庭菜窖'
	END type 
FROM
	lpk_giftcard d
	LEFT JOIN lpk_customer r ON r.sid = d.customerSid
	LEFT JOIN lpk_giftbag_goods s ON s.giftbagSid = d.giftbagSid
	LEFT JOIN lpk_goods e ON e.sid = s.goodsSid 
WHERE
	customerSid IS NOT NULL 
	AND customerSid != '' 
 UNION
SELECT
	r.wxMpOpenid,
	d.CODE,
	e.NAME AS goodsName,
	s.goodsNumber AS goodsNumber,
CASE
		
		WHEN r.wxMpOpenid IS NOT NULL 
		then '企业菜窖' 
	END type 
FROM
	emp_card d
	LEFT JOIN lpk_customer r ON r.sid = d.customerSid
	LEFT JOIN lpk_giftbag_goods s ON s.giftbagSid = d.giftbagSid
	LEFT JOIN lpk_goods e ON e.sid = s.goodsSid 
WHERE
	customerSid IS NOT NULL 
	AND customerSid != '' 
 UNION
SELECT
	r.wxMpOpenid,
	d.CODE,
	e.NAME AS goodsName,
	s.goodsNumber AS goodsNumber,
CASE
		
		WHEN r.wxMpOpenid IS NOT NULL 
		then '购买的家庭菜窖' 
	END type 
FROM
	emp_card_gift d
	LEFT JOIN lpk_customer r ON r.sid = d.customerSid
	LEFT JOIN emp_card_gift_goods s ON s.empCardGiftSid = d.sid
	LEFT JOIN lpk_goods e ON e.sid = s.goodsSid 
WHERE
	customerSid IS NOT NULL 
	AND customerSid != '' 
	AND isSenior = '1' 
GROUP BY
	CODE,
	NAME 
ORDER BY
	wxMpOpenid,
	CODE