<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper 
PUBLIC 
	"-//mybatis.org//DTD Mapper 3.0//EN" 
	"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!-- 命名，每一个映射对象不一样
	namespace:必须与对应的接口全类名一致
 -->
<mapper namespace="com.iamberry.wechat.service.mapper.AdminStatisticsMapper">
	
	<!-- 查询门店关注人数等报表数据 -->
	<select id="selectStoreAttentionList" resultType="AttentionInfo" parameterType="AttentionInfo">
		select  
			p.place_id placeId,
			 sum(case WHEN u.user_status <![CDATA[<>]]> 1 THEN 1 else 0 end) attentionSum,
			sum(case WHEN u.user_status = 2 THEN 1 else 0 end) nowAttentionNum,
			sum(case WHEN u.user_status = 0 THEN 1 else 0 end) cancelAttentionNum,
			case when Q.QRCODE_STATUS = 1 THEN Q.QRCODE_URL end qrcodeUrl,
			MIN(u.USER_SUB_DATE) firstEmployDate 
		from
		 tb_iamberry_place_info p LEFT JOIN tb_iamberry_user_userinfo u on p.place_pids = u.user_dealers 
			LEFT JOIN TB_IAMBERRY_USER_QRCODE q on p.place_id = Q.QRCODE_REPLACE_OPENID 
		where
			p.Place_role_id in  (
			<foreach collection="roleList" item="item" separator=",">
		 		#{item} 
		 	</foreach>)
		 	<if test="storeName != null and storeName !=''">
				AND PLACE_NAME LIKE CONCAT('%',#{storeName},'%') 
			</if>
		GROUP BY
		 	p.place_id 
		 LIMIT ${page.recordBegin},${page.pageSize}
	</select>
	
	<!-- 查询省代收益报表数据数量 -->
	<select id="selectStoreAttentionListCount" resultType="Integer" parameterType="AttentionInfo">
		select count(t.placeId) from (select  
			p.place_id placeId,
			sum(case WHEN u.user_status is not null THEN 1 else 0 end),
			sum(case WHEN u.user_status = 2 THEN 1 else 0 end),
			sum(case WHEN u.user_status = 0 THEN 1 else 0 end),
			case when Q.QRCODE_STATUS = 1 THEN Q.QRCODE_URL end qrcodeUrl,
			MIN(u.USER_SUB_DATE) 
		from
		 tb_iamberry_place_info p LEFT JOIN tb_iamberry_user_userinfo u on p.place_pids = u.user_dealers 
			LEFT JOIN TB_IAMBERRY_USER_QRCODE q on p.place_id = Q.QRCODE_REPLACE_OPENID 
		where
		 	p.Place_role_id in  (
			<foreach collection="roleList" item="item" separator=",">
		 		#{item}
		 	</foreach>)
		 	<if test="storeName != null and storeName !=''">
				AND PLACE_NAME LIKE CONCAT('%',#{storeName},'%') 
			</if>
		GROUP BY
		 	p.place_id ) t
	</select>
	
	<!-- 分页查询省代收益报表数据 -->
	<select id="selectProvinceEarningsList" resultType="EarningsInfo" parameterType="EarningsInfo">
		SELECT 
			PLACE_ID placeId,
			PLACE_NAME provinceName,
			PLACE_PID placePid,
			PLACE_AMOUNT usableMoney,
			PLACE_OPENID  palceOpenid,
			PLACE_AREA district,
			(SELECT COUNT(PLACE_ID) from TB_IAMBERRY_PLACE_INFO WHERE PLACE_PID = placeId AND PLACE_ROLE_ID = (SELECT ROLE_ID FROM TB_IAMBERRY_PLACE_ROLE WHERE ROLE_NAME = '代理')) cityNum,
			(SELECT COUNT(PLACE_ID) from TB_IAMBERRY_PLACE_INFO WHERE PLACE_PIDS IN (SELECT DEALER_ID FROM TB_IAMBERRY_DEALERS WHERE DEALER_FIRST = placeId) AND PLACE_ROLE_ID IN (SELECT ROLE_ID FROM TB_IAMBERRY_PLACE_ROLE WHERE (ROLE_NAME = '门店' OR ROLE_NAME = '母婴店'))) storeNum,
			(SELECT SUM(reback_money) FROM TB_IAMBERRY_REBACK WHERE REBACK_TO_OPENID = palceOpenid) awaitMoney,
			(SELECT SUM(SALES_YET_AMOUNT) FROM TB_IAMBERRY_SHOP_SALES_ORDER WHERE (SALES_STATUS = 5 OR SALES_STATUS = 2 OR SALES_STATUS = 12) AND SALES_OPENID IN (SELECT USER_OPENID FROM TB_IAMBERRY_USER_USERINFO WHERE USER_DEALERS IN (SELECT DEALER_ID FROM TB_IAMBERRY_DEALERS WHERE DEALER_FIRST = placeId))) marketMoney 
		FROM 
			TB_IAMBERRY_PLACE_INFO 
		WHERE 
			PLACE_ROLE_ID = (SELECT ROLE_ID FROM TB_IAMBERRY_PLACE_ROLE WHERE ROLE_NAME = '省代') 
			<if test="provinceName != null and provinceName !=''">
				AND PLACE_NAME LIKE CONCAT('%',#{provinceName},'%') 
			</if>
			LIMIT ${page.recordBegin},${page.pageSize}
	</select>
	
	<select id="selectProvinceEarningsListCount" resultType="Integer" parameterType="EarningsInfo">
		SELECT 
			COUNT(PLACE_ID) 
		FROM 
			TB_IAMBERRY_PLACE_INFO 
		WHERE 
			PLACE_ROLE_ID = (SELECT ROLE_ID FROM TB_IAMBERRY_PLACE_ROLE WHERE ROLE_NAME = '省代') 
			<if test="provinceName != null and provinceName !=''">
				AND PLACE_NAME LIKE CONCAT('%',#{provinceName},'%') 
			</if>
	</select>
	
	<!-- 查询商户层的激活信息 -->
	<select id="selectActivateList" parameterType="ActivateInfo" resultType="ActivateInfo">
		select
			t.PLACE_ROLE_ID roleId,
			r.ROLE_NAME roleName,
			sum(case when t.PLACE_OPENID is null then 1 else 0 end) nonactivatedNum, 
			sum(case when t.PLACE_OPENID is not null then 1 else 0 end) activateNum, 
			round(sum(case when t.PLACE_OPENID is not null then 1 else 0 end)/(sum(case when t.PLACE_OPENID is null then 1 else 0 end) + sum(case when t.PLACE_OPENID is not null then 1 else 0 end))*100,2) roundNum 
		from 
			TB_IAMBERRY_PLACE_INFO t LEFT JOIN TB_IAMBERRY_PLACE_ROLE r ON t.PLACE_ROLE_ID = r.ROLE_ID 
		GROUP BY t.PLACE_ROLE_ID,r.ROLE_NAME 
		ORDER BY t.PLACE_ROLE_ID
		LIMIT ${page.recordBegin},${page.pageSize} 
	</select>
	
	<!-- 查询商户层的激活信息总条目数 -->
	<select id="selectActivateListCount" parameterType="ActivateInfo" resultType="Integer">
		SELECT COUNT(a.placeNum) FROM (SELECT 
			COUNT(t.PLACE_ROLE_ID) placeNum 
		FROM 
			TB_IAMBERRY_PLACE_INFO t LEFT JOIN TB_IAMBERRY_PLACE_ROLE r ON t.PLACE_ROLE_ID = r.ROLE_ID 
		GROUP BY t.PLACE_ROLE_ID,r.ROLE_NAME 
		ORDER BY t.PLACE_ROLE_ID) a
	</select>
	
	<!-- 根据条件查询商户层信息 -->
	<select id="selectUserActivateList" parameterType="UerActivateInfo" resultType="UerActivateInfo">
		select 
			p.PLACE_ROLE_ID placeRoleId,
			t.USER_HEAD userHead,
			t.USER_NICKNAME userNickname,
			p.PLACE_NAME placeName,
			p.PLACE_AREA placeArea 
		FROM 
			TB_IAMBERRY_PLACE_INFO p LEFT JOIN TB_IAMBERRY_USER_USERINFO t 
 		ON 
 			p.PLACE_OPENID = t.USER_OPENID 
		<where>
			<if test="placeRoleId != null and placeRoleId != ''">
				p.PLACE_ROLE_ID = #{placeRoleId} 
			</if>
			<if test="isActivate == 1">
				AND p.PLACE_OPENID is not null 
			</if>
			<if test="isActivate == 2">
				AND p.PLACE_OPENID is null 
			</if>
			<if test="placeName != null and placeName != ''">
				AND p.PLACE_NAME LIKE CONCAT('%',#{placeName},'%') 
			</if>
		</where> 
		LIMIT ${page.recordBegin},${page.pageSize} 
	</select>
	
	<!-- 查询是否激活的商户层列表数量 -->
	<select id="selectUserActivateListCount" parameterType="UerActivateInfo" resultType="Integer">
		select 
			COUNT(p.PLACE_ID) 
		FROM 
			TB_IAMBERRY_PLACE_INFO p LEFT JOIN TB_IAMBERRY_USER_USERINFO t 
 		ON 
 			p.PLACE_OPENID = t.USER_OPENID 
		<where>
			<if test="placeRoleId != null and placeRoleId != ''">
				p.PLACE_ROLE_ID = #{placeRoleId} 
			</if>
			<if test="isActivate == 1">
				AND p.PLACE_OPENID is not null 
			</if>
			<if test="isActivate == 2">
				AND p.PLACE_OPENID is null 
			</if>
		</where> 
	</select>
	
	<!-- 按月份查询商户层收益金额 -->
	<select id="selectMarketList" parameterType="MarketInfo" resultType="MarketInfo">
		SELECT
			 p.PLACE_ID placeId,
			 DATE_FORMAT(r.REBACK_CREATE_DATE,'%Y-%m') rebackMonth,
			 SUM(CASE WHEN (o.SALES_STATUS = 2 OR o.SALES_STATUS = 5 OR o.SALES_STATUS = 12) THEN o.SALES_YET_AMOUNT ELSE 0 END) monthMoney,
			 SUM(CASE WHEN r.REBACK_STATUS = 2 THEN r.REBACK_MONEY ELSE 0 END) alreadyMoney,
			 SUM(CASE WHEN r.REBACK_STATUS = 1 THEN r.REBACK_MONEY ELSE 0 END) waitMoney 
		from 
			TB_IAMBERRY_REBACK r LEFT JOIN TB_IAMBERRY_SHOP_SALES_ORDER o ON r.REBACK_ORDERID = o.SALES_ORDERID 
			LEFT JOIN TB_IAMBERRY_PLACE_INFO p ON r.REBACK_TO_OPENID = p.PLACE_OPENID 
		<where>
			<if test="rebackMonth != null and rebackMonth != ''">
				DATE_FORMAT(r.REBACK_CREATE_DATE,'%Y-%m') = DATE_FORMAT(#{rebackMonth},'%Y-%m') 
			</if>
			<if test="placeName != null and placeName != ''">
				AND p.PLACE_NAME LIKE CONCAT('%',#{placeName},'%') 
			</if>
			<if test="placeType != null and placeType != '' and placeType != '门店'.toString()">
				AND PLACE_ROLE_ID = (SELECT ROLE_ID FROM TB_IAMBERRY_PLACE_ROLE WHERE ROLE_NAME = #{placeType}) 
			</if>
			<if test="placeType != null and placeType != '' and placeType == '门店'.toString()">
				AND (PLACE_ROLE_ID = (SELECT ROLE_ID FROM TB_IAMBERRY_PLACE_ROLE WHERE ROLE_NAME = #{placeType}) OR PLACE_ROLE_ID = (SELECT ROLE_ID FROM TB_IAMBERRY_PLACE_ROLE WHERE ROLE_NAME = '母婴店'))
			</if>
		</where> 
		GROUP BY 
			DATE_FORMAT(r.REBACK_CREATE_DATE,'%Y-%m'),r.REBACK_TO_OPENID 
		LIMIT ${page.recordBegin},${page.pageSize} 
	</select>
	
	<!-- 按月份查询商户层收益总条目数 -->
	<select id="selectMarketListCount" parameterType="MarketInfo" resultType="Integer">
		SELECT COUNT(a.rebackMonth) FROM (SELECT 
			DATE_FORMAT(r.REBACK_CREATE_DATE,'%Y-%m') rebackMonth 
		FROM 
			TB_IAMBERRY_REBACK r LEFT JOIN TB_IAMBERRY_SHOP_SALES_ORDER o ON r.REBACK_FROM_OPENID = o.SALES_ORDERID 
			LEFT JOIN TB_IAMBERRY_PLACE_INFO p ON r.REBACK_TO_OPENID = p.PLACE_OPENID 
		<where>
			<if test="rebackMonth != null and rebackMonth != ''">
				DATE_FORMAT(r.REBACK_CREATE_DATE,'%Y-%m') = DATE_FORMAT(#{rebackMonth},'%Y-%m') 
			</if>
			<if test="placeName != null and placeName != ''">
				AND p.PLACE_NAME LIKE CONCAT('%',#{placeName},'%') 
			</if>
			<if test="placeType != null and placeType != ''">
				AND PLACE_ROLE_ID = (SELECT ROLE_ID FROM TB_IAMBERRY_PLACE_ROLE WHERE ROLE_NAME = #{placeType}) 
			</if>
		</where> 
		GROUP BY 
			DATE_FORMAT(r.REBACK_CREATE_DATE,'%Y-%m'),r.REBACK_TO_OPENID) a
	</select>
	
	<select id="selectPlaceList" resultType="MarketInfo">
		SELECT
			 PLACE_ID placeId,
			 PLACE_NAME storeName,
			 PLACE_PID placePid,
			 PLACE_OPENID palceOpenid,
			 (SELECT PLACE_NAME FROM TB_IAMBERRY_PLACE_INFO WHERE PLACE_ID = placePid) cityName,
			 (SELECT PLACE_NAME FROM TB_IAMBERRY_PLACE_INFO WHERE PLACE_ID = (SELECT PLACE_PID FROM TB_IAMBERRY_PLACE_INFO WHERE PLACE_ID = placePid)) provinceName 
		FROM
			 TB_IAMBERRY_PLACE_INFO 
		WHERE PLACE_ROLE_ID = (SELECT ROLE_ID FROM TB_IAMBERRY_PLACE_ROLE WHERE ROLE_NAME = '门店') 
		UNION  
		SELECT
			 PLACE_ID placeId,
			 NULL storeName,
			 PLACE_PID placePid,
			 PLACE_OPENID palceOpenid,
			 PLACE_NAME cityName, 
			 (SELECT PLACE_NAME FROM TB_IAMBERRY_PLACE_INFO WHERE PLACE_ID = placePid) provinceName 
		FROM TB_IAMBERRY_PLACE_INFO 
		WHERE PLACE_ROLE_ID = (SELECT ROLE_ID FROM TB_IAMBERRY_PLACE_ROLE WHERE ROLE_NAME = '代理')
		UNION 
		SELECT
			 PLACE_ID placeId,
			 PLACE_NAME storeName,
			 PLACE_PID placePid,
			 PLACE_OPENID palceOpenid,
			 NULL cityName,
			 (SELECT PLACE_NAME FROM TB_IAMBERRY_PLACE_INFO WHERE PLACE_ID = placePid) provinceName 
		FROM
			 TB_IAMBERRY_PLACE_INFO 
		WHERE PLACE_ROLE_ID = (SELECT ROLE_ID FROM TB_IAMBERRY_PLACE_ROLE WHERE ROLE_NAME = '母婴店') 
		UNION 
		SELECT
			 PLACE_ID placeId,
			 NULL storeName,
			 PLACE_PID placePid,
			 PLACE_OPENID palceOpenid,
			 NULL cityName,
			 PLACE_NAME provinceName 
		FROM
			 TB_IAMBERRY_PLACE_INFO 
		WHERE PLACE_ROLE_ID = (SELECT ROLE_ID FROM TB_IAMBERRY_PLACE_ROLE WHERE ROLE_NAME = '省代') 
	</select>
	
	<!-- 查询总关注人数 -->
	<select id="selectAttentionSum" resultType="Integer">
		SELECT 
			COUNT(DISTINCT USER_ID) 
		FROM 
			TB_IAMBERRY_USER_USERINFO 
		WHERE 
			USER_STATUS <![CDATA[<>]]> 1
	</select>
	
	<!-- 查询总销售台数和总金额 -->
	<select id="selectSalesAmount" resultType="StatisticsInfo">
		SELECT 
			SUM(t.amount) salesMoney, 
			SUM(t.num) salesSum 
		FROM 
			(SELECT 
				SUM(ITEM_NUM) num,
				SALES_YET_AMOUNT amount 
			FROM 
				TB_IAMBERRY_SHOP_SALES_ORDER LEFT JOIN TB_IAMBERRY_SHOP_ORDER_ITEM 
				ON SALES_ORDERID = iTEM_SALES_ORDERID 
			WHERE 
				SALES_STATUS = 12 
			GROUP BY SALES_ORDERID
			) t
	</select>
	
	<!-- 查询今日关注人数，今日取消关注人数 -->
	<select id="selectCurrentAttentionNum" resultType="StatisticsInfo">
		SELECT 
			SUM(CASE WHEN USER_STATUS = 2 then 1 else 0 end) newAttentionNum,
			SUM(CASE WHEN USER_STATUS = 0 then 1 else 0 end) cancelAttentionNum 
		FROM 
			TB_IAMBERRY_USER_USERINFO 
		WHERE 
			DATE_FORMAT(USER_SUB_DATE,'%Y-%m-%d') = DATE_FORMAT(NOW(),'%Y-%m-%d') 
	</select>
	
	<!-- 查询当前已激活省代数量，市代数量、门店数量 -->
	<select id="selectPlaceActivate" resultType="ActivateInfo">
		SELECT 
			ROLE_NAME roleName,
			COUNT(PLACE_ID) activateNum 
		FROM 
			TB_IAMBERRY_PLACE_INFO LEFT JOIN TB_IAMBERRY_PLACE_ROLE 
			ON PLACE_ROLE_ID = ROLE_ID 
		WHERE 
			PLACE_OPENID IS NOT NULL 
		GROUP BY PLACE_ROLE_ID
	</select>
	
	<!-- 查询当前门店二维码启用数量 -->
	<select id="selectQrCodeNum" resultType="Integer">
		SELECT 
			COUNT(DISTINCT PLACE_ID) qrCodeNum 
		FROM 
			TB_IAMBERRY_PLACE_INFO 
		WHERE 
			PLACE_ROLE_ID IN (SELECT ROLE_ID FROM TB_IAMBERRY_PLACE_ROLE WHERE ROLE_NAME = '门店' OR ROLE_NAME = '母婴店') 
		AND 
			PLACE_PIDS IN (SELECT USER_DEALERS FROM TB_IAMBERRY_USER_USERINFO)
	</select>
	
	<!-- 查询当前通过门店二维码销售台数,总金额 -->
	<select id="selectStoreSalesInfo" resultType="StatisticsInfo">
		SELECT 
			SUM(t.num) storeSalesNum,
			SUM(t.amount) storeSalesMoney 
		FROM 
			(SELECT 
				SUM(ITEM_NUM) num,
				SALES_YET_AMOUNT amount 
			FROM 
				TB_IAMBERRY_SHOP_SALES_ORDER LEFT JOIN TB_IAMBERRY_SHOP_ORDER_ITEM 
				ON SALES_ORDERID = ITEM_SALES_ORDERID 
			WHERE 
				SALES_STATUS = 12 
			AND 
				SALES_OPENID IN 
					(SELECT 
						DISTINCT USER_OPENID 
					FROM 
						TB_IAMBERRY_USER_USERINFO LEFT JOIN TB_IAMBERRY_PLACE_INFO 
						ON USER_DEALERS = PLACE_PIDS 
					WHERE 
						PLACE_ROLE_ID IN (SELECT ROLE_ID FROM TB_IAMBERRY_PLACE_ROLE WHERE ROLE_NAME = '门店' OR ROLE_NAME = '母婴店')
				) 
				GROUP BY SALES_ORDERID
			)t;
	</select>
</mapper>