<?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.StatisticsRelationMapper">
	<sql id="allField">
		s_id sId,
		s_openid sOpenId,
		s_roleid sRoleId,
		s_roleName sRoleName,
		s_status sStatus,
		s_createtime sCreateTime
	</sql>
	<!-- 根据openid，获取对应的业余员ID，如果为，那么表示非业务员 -->
	<select id="getRelationByOpenID" parameterType="String" resultType="SalesmanInfo">
		SELECT
			<include refid="allField"></include>
		FROM
			TB_IAMBERRY_STATISTICS_SALESMAN
		WHERE
			S_OPENID = #{openid}
	</select>
	
	<!-- 更加业务员id，查询订单消息 -->
	<select id="getStatisticsOrderByOpenID" parameterType="java.util.Map" resultType="com.iamberry.wechat.core.entity.statictics.StatisticsOrderDTO">
		SELECT
			O.SALES_ORDERID orderId,
			O.SALES_OPENID openID,
			U.USER_DEALERS dealers,
			O.SALES_STATUS orderStatus,
			T.PLACE_NAME placeInfoOne,
			P.PLACE_NAME placeInfoTwo
		<!-- 查询所有的已支付订单 -->
		FROM
			TB_IAMBERRY_SHOP_SALES_ORDER O
		<!-- 查询用户的信息 -->
		LEFT JOIN 
			TB_IAMBERRY_USER_USERINFO U 
		ON 
			U.USER_OPENID = O.SALES_OPENID
		<!-- 查询用户的商户层关系 -->
		LEFT JOIN 
			TB_IAMBERRY_DEALERS D 
		ON 
			D.DEALER_ID = U.USER_DEALERS
		<!-- 查询用户的第二级信息 -->
		LEFT JOIN
			TB_IAMBERRY_PLACE_INFO p
		ON
			P.PLACE_ID = D.DEALER_THIRD
		<!-- 查询用户的门店信息 -->
		LEFT JOIN
			TB_IAMBERRY_PLACE_INFO T
		ON
			T.PLACE_ID = D.DEALER_SECOND
		WHERE
			<!-- 订单状态为已支付、已发货、已完成 -->
			(
				SALES_STATUS = 2
				OR SALES_STATUS = 5
				OR SALES_STATUS = 12
			)
			AND
			<!-- 根据时间区间查询 -->
			<![CDATA[
			O.SALES_CREATE_DATE >= #{startDate}
			AND 
			O.SALES_CREATE_DATE <= #{endDate}
			]]>
			<!-- 查询销售订单的记录 -->
			AND
			SALES_ORDER_TYPE = 1
		AND USER_DEALERS IN (
			<!-- 根据门店的id，查询对应的关系 -->
			SELECT
				PLACE_PIDS
			FROM
				TB_IAMBERRY_PLACE_INFO
			WHERE
				PLACE_PID IN (
					<!-- 根据业务员id，查询业务员所管理的门店 -->
					SELECT
						T.SR_PLACEID
					FROM
						TB_IAMBERRY_STATISTICS_RELATION T
					WHERE
						T.SR_S_ID = #{otherInt}
				)
				AND NOT PLACE_PIDS IS NULL
		)
		<!-- 分页 -->
		LIMIT
			#{minNum}, #{maxNum}
	</select>
	
	<!-- 根据月份获取本月的销量  -->
	<select id="getTotalByMonth"  parameterType="java.util.Map" resultType="int">
		SELECT 
			<!-- 统计本月的销售订单总量 -->
			COUNT(O.SALES_ORDERID) MONTHSALES 
		FROM
			<!-- 查询订单 -->
			TB_IAMBERRY_SHOP_SALES_ORDER O
			
			<!-- 查询订单对应的用户信息 -->
		LEFT JOIN 
			TB_IAMBERRY_USER_USERINFO U 
		ON 
			O.SALES_OPENID = U.USER_OPENID
			<!-- 根据用户信息中的dealers查询对应的关系 -->
		LEFT JOIN 
			TB_IAMBERRY_DEALERS D 
		ON 
			U.USER_DEALERS = D.DEALER_ID
			<!-- 查询用户关系对应的商户 -->
		LEFT JOIN 
			TB_IAMBERRY_PLACE_INFO P 
		ON 
			D.DEALER_ID = P.PLACE_PIDS
			<!-- 查询商户的上级，也就是门店 -->
		LEFT JOIN 
			TB_IAMBERRY_PLACE_INFO PP 
		ON 
			P.PLACE_PID = PP.PLACE_ID
			<!-- 查询业务员管理的门店ID -->
		LEFT JOIN 
			TB_IAMBERRY_STATISTICS_RELATION SR 
		ON 
			PP.PLACE_ID = SR.SR_PLACEID
			<!-- 时间区间 -->
		<![CDATA[
		WHERE 
			O.SALES_CREATE_DATE >= #{startDate}
				AND 
			O.SALES_CREATE_DATE <= #{endDate}
		]]>
				AND 
			(
				SALES_STATUS = 2
				OR SALES_STATUS = 5
				OR SALES_STATUS = 12
			)
				AND 
			SR.SR_S_ID=#{salesmanId}
				AND	
			SALES_ORDER_TYPE = 1
	</select>
	
	<!-- 根据订单号、关系，查询订单详情 -->
	<select id="getOrderDetails" parameterType="java.util.Map" resultMap="orderDetailsMap">
		SELECT
			S.SALES_ORDERID orderId,
			S.SALES_CREATE_DATE createDate,
			S.SALES_STATUS orderStatus,
			S.SALES_TRANSACTION_DATE payDate,
			T.PLACE_NAME placeInfoOne,
			P.PLACE_NAME placeInfoTwo,
			I.ITEM_NUM item_num,
			I.ITEM_PRODUCT_NAME item_name,
			I.ITEM_PRODUCT_PIC item_pic,
			I.ITEM_TOTAL item_price,
			I.ITEM_ID item_id
		FROM
			TB_IAMBERRY_SHOP_SALES_ORDER S
		LEFT JOIN TB_IAMBERRY_DEALERS D ON D.DEALER_ID = #{dealer}
		LEFT JOIN TB_IAMBERRY_PLACE_INFO P ON P.PLACE_ID = D.DEALER_THIRD
		LEFT JOIN TB_IAMBERRY_PLACE_INFO T ON T.PLACE_ID = D.DEALER_SECOND
		LEFT JOIN TB_IAMBERRY_SHOP_ORDER_ITEM I ON I.ITEM_SALES_ORDERID = S.SALES_ORDERID
		WHERE
			S.SALES_ORDERID = #{orderId}
	</select>
	
	<!-- ReturnMap-->
	<resultMap type="com.iamberry.wechat.core.entity.statictics.StatisticsOrderDetailsDTO" id="orderDetailsMap">
		<id property="orderId" column="orderId"/>
		<result property="createDate" column="createDate"/>
		<result property="orderStatus" column="orderStatus"/>
		<result property="payDate" column="payDate"/>
		<result property="placeInfoTwo" column="placeInfoTwo"/>
		<result property="placeInfoOne" column="placeInfoOne"/>
		<collection property="items" ofType="com.iamberry.wechat.core.entity.statictics.StatisticsOrderItemDTO">
		    <id column="item_id" property="itemID"/>
		    <result column="item_name" property="itemName"/>
		    <result column="item_price" property="itemPrice"/>
		    <result column="item_num" property="itemNum"/>
		    <result column="item_pic" property="itemPic"/>
		</collection>
	</resultMap>
	
	<!-- 统计试用订单销量 -->
	<select id="selectProbationStatictics" parameterType="SalesmanDto" resultType="ProbationOrderDto">
		SELECT 
			DISTINCT o.PROBATION_ORDERID probationOrderid,
			o.PROBATION_STATUS probationStatus, 
			pd.PRODUCT_NAME productName,
			p.PLACE_NAME vipName,
			pr.ROLE_NAME vipRoleName,
			t.PLACE_NAME storeName,
			t.ROLE_NAME storeRoleName 
		FROM 
			TB_IAMBERRY_PROBATION_SHOP_ORDER o 
			LEFT JOIN TB_IAMBERRY_USER_USERINFO u ON o.PROBATION_OPENID = u.USER_OPENID 
			LEFT JOIN TB_IAMBERRY_PLACE_INFO p ON u.USER_DEALERS = p.PLACE_PIDS 
			LEFT JOIN TB_IAMBERRY_STATISTICS_RELATION r ON p.PLACE_PID = r.SR_PLACEID 
			LEFT JOIN TB_IAMBERRY_STATISTICS_SALESMAN m ON r.SR_S_ID = m.S_ID 
			LEFT JOIN TB_IAMBERRY_PLACE_ROLE pr ON P.PLACE_ROLE_ID = pr.ROLE_ID 
			LEFT JOIN (SELECT i.PLACE_ID,i.PLACE_NAME,ro.ROLE_NAME FROM 
			TB_IAMBERRY_PLACE_INFO 	i LEFT JOIN TB_IAMBERRY_PLACE_ROLE ro ON i.PLACE_ROLE_ID = ro.ROLE_ID) t 
			ON p.PLACE_PID = t.PLACE_ID 
			LEFT JOIN TB_IAMBERRY_PRODUCT_INFO pd ON o.PROBATION_PRODUCT_ID = pd.PRODUCT_ID 
		WHERE 
			m.S_OPENID = #{sOpenId} 
		AND 
			DATE_FORMAT(o.PROBATION_CREATE_TIME,'%Y-%m') = DATE_FORMAT(NOW(),'%Y-%m') 
		<if test="probationStatus != null and probationStatus == 1">
			<![CDATA[AND o.PROBATION_STATUS IN (1,2,3,4) ]]> 
		</if>
		<if test="probationStatus != null and probationStatus == 2">
			<![CDATA[AND o.PROBATION_STATUS >= 5 ]]> 
		</if>
		ORDER BY o.PROBATION_CREATE_TIME DESC 
	     LIMIT ${page.recordBegin},${page.pageSize}
	</select>
	
	<!-- 统计试用订单销量分页总数 -->
	<select id="selectProbationStaticticsCount" parameterType="SalesmanDto" resultType="Integer">
		SELECT 
			COUNT(DISTINCT o.PROBATION_ORDERID) 
		FROM 
			TB_IAMBERRY_PROBATION_SHOP_ORDER o 
			LEFT JOIN TB_IAMBERRY_USER_USERINFO u ON o.PROBATION_OPENID = u.USER_OPENID 
			LEFT JOIN TB_IAMBERRY_PLACE_INFO p ON u.USER_DEALERS = p.PLACE_PIDS 
			LEFT JOIN TB_IAMBERRY_STATISTICS_RELATION r ON p.PLACE_PID = r.SR_PLACEID 
			LEFT JOIN TB_IAMBERRY_STATISTICS_SALESMAN m ON r.SR_S_ID = m.S_ID 
			LEFT JOIN TB_IAMBERRY_PLACE_ROLE pr ON P.PLACE_ROLE_ID = pr.ROLE_ID 
			LEFT JOIN (SELECT i.PLACE_ID,i.PLACE_NAME,ro.ROLE_NAME FROM 
			TB_IAMBERRY_PLACE_INFO 	i LEFT JOIN TB_IAMBERRY_PLACE_ROLE ro ON i.PLACE_ROLE_ID = ro.ROLE_ID) t 
			ON p.PLACE_PID = t.PLACE_ID 
			LEFT JOIN TB_IAMBERRY_PRODUCT_INFO pd ON o.PROBATION_PRODUCT_ID = pd.PRODUCT_ID 
		WHERE 
			m.S_OPENID = #{sOpenId} 
		AND 
			DATE_FORMAT(o.PROBATION_CREATE_TIME,'%Y-%m') = DATE_FORMAT(NOW(),'%Y-%m') 
		<if test="probationStatus != null and probationStatus == 1">
			<![CDATA[AND o.PROBATION_STATUS < 5 ]]> 
		</if>
		<if test="probationStatus != null and probationStatus == 2">
			<![CDATA[AND o.PROBATION_STATUS >= 5 ]]> 
		</if>
		ORDER BY o.PROBATION_CREATE_TIME DESC 
	</select>
	
	<!-- 查询试用订单详情 -->
	<select id="selectProbationInfo" parameterType="ProbationOrderDto" resultType="ProbationOrderDto">
		SELECT 
			DISTINCT o.PROBATION_ORDERID probationOrderid,
			o.PROBATION_STATUS probationStatus, 
			o.PROBATION_YET_AMOUNT probationYetAmount, 
			o.PROBATION_TRANSACTION_TIME probationTransactionTime,
			o.PROBATION_SEND_NAME probationSendName,
			o.PROBATION_SEND_TEL probationSendTel,
			o.PROBATION_CREATE_TIME probationCreateTime, 
			pd.PRODUCT_NAME productName,
			pd.PRODUCT_INTRODUCE_IMG ProductIntroduceImg,
			p.PLACE_NAME vipName,
			pr.ROLE_NAME vipRoleName,
			t.PLACE_NAME storeName,
			t.ROLE_NAME storeRoleName 
		FROM 
			TB_IAMBERRY_PROBATION_SHOP_ORDER o 
			LEFT JOIN TB_IAMBERRY_USER_USERINFO u ON o.PROBATION_OPENID = u.USER_OPENID 
			LEFT JOIN TB_IAMBERRY_PLACE_INFO p ON u.USER_DEALERS = p.PLACE_PIDS  
			LEFT JOIN TB_IAMBERRY_PLACE_ROLE pr ON P.PLACE_ROLE_ID = pr.ROLE_ID 
			LEFT JOIN (SELECT i.PLACE_ID,i.PLACE_NAME,ro.ROLE_NAME FROM 
			TB_IAMBERRY_PLACE_INFO 	i LEFT JOIN TB_IAMBERRY_PLACE_ROLE ro ON i.PLACE_ROLE_ID = ro.ROLE_ID) t 
			ON p.PLACE_PID = t.PLACE_ID 
			LEFT JOIN TB_IAMBERRY_PRODUCT_INFO pd ON o.PROBATION_PRODUCT_ID = pd.PRODUCT_ID 
		WHERE 
			o.PROBATION_ORDERID = #{probationOrderid} 
	</select>
	
	<!-- 统计门店下线vip会员数量与销售总数 -->
	<select id="selectStoresStatistics" parameterType="StoresStatisticsDto" resultType="StoresStatisticsDto">
				SELECT mdid.name as name,mdid.dealerSecond as dealerSecond,CASE WHEN sum(viphy.num) IS NULL THEN 0 ELSE sum(viphy.num) END AS num,CASE WHEN sum(viphy.synum) IS NULL THEN 0 ELSE sum(viphy.synum) END AS synum,CASE WHEN viphy.place_pid IS NULL THEN 0 ELSE COUNT(viphy.place_pid) END as counts from (SELECT * from (SELECT sr_placeid from tb_iamberry_statistics_relation where sr_s_id = #{sid}) sr  
							LEFT JOIN (SELECT distinct d.Dealer_second as dealerSecond,p.place_name as name ,p.place_openid as openid from tb_iamberry_place_info p LEFT JOIN tb_iamberry_dealers d on p.place_id = d.Dealer_second
					where p.place_template_id in (21,22,23) 
					and p.place_id = d.Dealer_second) md on md.dealerSecond = sr.sr_placeid) mdid
					LEFT JOIN (SELECT syxx.placeName as name, 
					CASE WHEN sum(syxl.synum) IS NULL THEN 0 ELSE sum(syxl.synum) END AS synum,
					CASE WHEN sum(xl.num) IS NULL THEN 0 ELSE sum(xl.num) END AS num,
					CASE WHEN syxx.user_openid IS NULL THEN 0 ELSE COUNT(1) END as hyPidsNum,
					syxx.place_pid as place_pid,syxx.Place_role_id as Place_role_id,syxx.pids as pids
					from 
					(SELECT * from (
					SELECT
							p.place_name as placeName,
							p.place_pids as pids,
					p.place_pid as place_pid,
					p.Place_role_id as Place_role_id
						FROM
							tb_iamberry_place_info p where p.place_status = '1') pds
						LEFT JOIN tb_iamberry_user_userinfo u ON pds.pids = u.user_dealers) syxx
					LEFT JOIN	(
					SELECT
					count(1) AS synum,
					s.probation_openid AS syopenid
				FROM
					TB_IAMBERRY_PROBATION_SHOP_ORDER s
				LEFT JOIN tb_iamberry_user_userinfo u ON s.probation_openid = u.user_openid
				WHERE
					s.PROBATION_STATUS > 0
				GROUP BY
					syopenid
					) syxl on syxx.user_openid = syxl.syopenid
					LEFT JOIN	(SELECT
						sum(i.item_num) AS num,
						o.sales_openid AS openid
					FROM
						tb_iamberry_shop_sales_order o
					LEFT JOIN tb_iamberry_shop_order_item i ON o.sales_orderid = i.item_sales_orderid
					LEFT JOIN tb_iamberry_user_userinfo u ON o.sales_openid = u.user_openid
					WHERE
						o.sales_status in (2,5,12)
					AND i.item_product_type = 1
					AND o.sales_order_type = 1
					GROUP BY
						openid) xl on syxx.user_openid = xl.openid
				WHERE syxx.Place_role_id = '51'
				group by syxx.placeName) viphy on mdid.dealerSecond = viphy.place_pid
				<if test="name!=null and name!=''">
			   where mdid.name LIKE CONCAT('%',#{name},'%')
				</if>
				group by viphy.place_pid
				ORDER BY num DESC, synum DESC 
	     LIMIT ${page.recordBegin},${page.pageSize}
	</select>
	
	<!-- 统计门店下线vip会员数量与销售总数记录总条数 -->
	<select id="selectStoresStatisticsCount" parameterType="StoresStatisticsDto" resultType="Integer">
	SELECT count(*) from (
					SELECT mdid.name as name,mdid.dealerSecond as dealerSecond,sum(viphy.num) as num,sum(viphy.synum) as synum,CASE WHEN viphy.place_pid IS NULL THEN 0 ELSE COUNT(viphy.place_pid) END as counts from (SELECT * from (SELECT sr_placeid from tb_iamberry_statistics_relation where sr_s_id = #{sid}) sr 
						LEFT JOIN (SELECT distinct d.Dealer_second as dealerSecond,p.place_name as name ,p.place_openid as openid from tb_iamberry_place_info p LEFT JOIN tb_iamberry_dealers d on p.place_id = d.Dealer_second
				where p.place_template_id in (21,22,23) 
				and p.place_id = d.Dealer_second) md on md.dealerSecond = sr.sr_placeid) mdid
				LEFT JOIN (SELECT syxx.placeName as name, 
				CASE WHEN sum(syxl.synum) IS NULL THEN 0 ELSE sum(syxl.synum) END AS synum,
				CASE WHEN sum(xl.num) IS NULL THEN 0 ELSE sum(xl.num) END AS num,
				CASE WHEN syxx.user_openid IS NULL THEN 0 ELSE COUNT(1) END as hyPidsNum,
				syxx.place_pid as place_pid,syxx.Place_role_id as Place_role_id,syxx.pids as pids
				from 
				(SELECT * from (
				SELECT
						p.place_name as placeName,
						p.place_pids as pids,
				p.place_pid as place_pid,
				p.Place_role_id as Place_role_id
					FROM
						tb_iamberry_place_info p where p.place_status = '1') pds
					LEFT JOIN tb_iamberry_user_userinfo u ON pds.pids = u.user_dealers) syxx
				LEFT JOIN	(
					SELECT
					count(1) AS synum,
					s.probation_openid AS syopenid
				FROM
					TB_IAMBERRY_PROBATION_SHOP_ORDER s
				LEFT JOIN tb_iamberry_user_userinfo u ON s.probation_openid = u.user_openid
				WHERE
					s.PROBATION_STATUS > 0
				GROUP BY
					syopenid
					) syxl on syxx.user_openid = syxl.syopenid
			LEFT JOIN	(SELECT
						sum(i.item_num) AS num,
						o.sales_openid AS openid
					FROM
						tb_iamberry_shop_sales_order o
					LEFT JOIN tb_iamberry_shop_order_item i ON o.sales_orderid = i.item_sales_orderid
					LEFT JOIN tb_iamberry_user_userinfo u ON o.sales_openid = u.user_openid
					WHERE
						o.sales_status in (2,5,12)
					AND i.item_product_type = 1
					AND o.sales_order_type = 1
					GROUP BY
						openid) xl on syxx.user_openid = xl.openid
				WHERE syxx.Place_role_id = '51'
				group by syxx.placeName) viphy on mdid.dealerSecond = viphy.place_pid
				<if test="name!=null and name!=''">
			   where mdid.name LIKE CONCAT('%',#{name},'%')
				</if>
				group by viphy.place_pid
				) md
	</select>
	
	<!-- 统计vip下线vip会员数量与销售总数记录总条数 -->
	<select id="selectVipOfflineCount" parameterType="VipOfflineDto" resultType="Integer">
				SELECT count(*) from(
					SELECT syxx.placeName as name, 
					CASE WHEN sum(syxl.synum) IS NULL THEN 0 ELSE sum(syxl.synum) END AS synum,
					CASE WHEN sum(xl.num) IS NULL THEN 0 ELSE sum(xl.num) END AS num,
					CASE WHEN syxx.user_openid IS NULL THEN 0 ELSE COUNT(1) END as hyPidsNum
					from 
					(SELECT * from (
					SELECT
							p.place_name as placeName,
							p.place_pids as pids
						FROM
							tb_iamberry_place_info p where  p.place_pid=#{placeId} and p.Place_role_id = '51'  and p.place_status = '1') pds
						LEFT JOIN tb_iamberry_user_userinfo u ON pds.pids = u.user_dealers) syxx
					LEFT JOIN	(
						SELECT
						count(1) AS synum,
						s.probation_openid AS syopenid
					FROM
						TB_IAMBERRY_PROBATION_SHOP_ORDER s
					LEFT JOIN tb_iamberry_user_userinfo u ON s.probation_openid = u.user_openid
					WHERE
						s.PROBATION_STATUS > 0
					GROUP BY
						syopenid
					) syxl on syxx.user_openid = syxl.syopenid
				LEFT JOIN	(SELECT
						sum(i.item_num) AS num,
						o.sales_openid AS openid
					FROM
						tb_iamberry_shop_sales_order o
					LEFT JOIN tb_iamberry_shop_order_item i ON o.sales_orderid = i.item_sales_orderid
					LEFT JOIN tb_iamberry_user_userinfo u ON o.sales_openid = u.user_openid
					WHERE
						o.sales_status in (2,5,12)
					AND i.item_product_type = 1
					AND o.sales_order_type = 1
					GROUP BY
						openid) xl on syxx.user_openid = xl.openid
						<if test="name!=null and name!=''">
						   where syxx.placeName LIKE CONCAT('%',#{name},'%')
						</if>
				group by syxx.placeName) hy
				
	</select>
	<!-- 统计vip下线vip会员数量与销售总数 -->
	<select id="selectVipOffline" parameterType="VipOfflineDto" resultType="VipOfflineDto">
			SELECT syxx.placeName as name, 
					CASE WHEN sum(syxl.synum) IS NULL THEN 0 ELSE sum(syxl.synum) END AS synum,
					CASE WHEN sum(xl.num) IS NULL THEN 0 ELSE sum(xl.num) END AS num,
					CASE WHEN syxx.user_openid IS NULL THEN 0 ELSE COUNT(1) END as hyPidsNum
					from 
					(SELECT * from (
					SELECT
							p.place_name as placeName,
							p.place_pids as pids
						FROM
							tb_iamberry_place_info p where  p.place_pid=#{placeId} and p.Place_role_id = '51'  and p.place_status = '1') pds
						LEFT JOIN tb_iamberry_user_userinfo u ON pds.pids = u.user_dealers) syxx
					LEFT JOIN	(
					SELECT
					count(1) AS synum,
					s.probation_openid AS syopenid
				FROM
					TB_IAMBERRY_PROBATION_SHOP_ORDER s
				LEFT JOIN tb_iamberry_user_userinfo u ON s.probation_openid = u.user_openid
				WHERE
					s.PROBATION_STATUS > 0
				GROUP BY
					syopenid
					) syxl on syxx.user_openid = syxl.syopenid
					LEFT JOIN	(SELECT
						sum(i.item_num) AS num,
						o.sales_openid AS openid
					FROM
						tb_iamberry_shop_sales_order o
					LEFT JOIN tb_iamberry_shop_order_item i ON o.sales_orderid = i.item_sales_orderid
					LEFT JOIN tb_iamberry_user_userinfo u ON o.sales_openid = u.user_openid
					WHERE
						o.sales_status in (2,5,12)
					AND i.item_product_type = 1
					AND o.sales_order_type = 1
					GROUP BY
						openid) xl on syxx.user_openid = xl.openid
						<if test="name!=null and name!=''">
						   where syxx.placeName LIKE CONCAT('%',#{name},'%')
						</if>
				group by syxx.placeName
	     LIMIT ${page.recordBegin},${page.pageSize}
	</select>
	
	<!-- 统计所有门店下线vip会员数量与销售总数 （管理员）-->
	<select id="selectAdministratorStatistics" parameterType="StoresStatisticsDto" resultType="StoresStatisticsDto">
				SELECT
				mdid. NAME AS NAME,
				mdid.dealerSecond AS dealerSecond,
				CASE
			WHEN sum(viphy.num) IS NULL THEN
				0
			ELSE
				sum(viphy.num)
			END AS num,
			 CASE
			WHEN sum(viphy.synum) IS NULL THEN
				0
			ELSE
				sum(viphy.synum)
			END AS synum,
			 CASE
			WHEN viphy.pids IS NULL THEN
				0
			ELSE
				count(1)
			END AS counts
			FROM
				(
					SELECT DISTINCT
						d.Dealer_second AS dealerSecond,
						p.place_name AS NAME,
						p.place_openid AS openid
					FROM
						tb_iamberry_place_info p
					LEFT JOIN tb_iamberry_dealers d ON p.place_id = d.Dealer_second
					WHERE
						p.place_template_id IN (21, 22, 23)
					AND p.place_id = d.Dealer_second
				) mdid
			LEFT JOIN (
				SELECT
					syxx.placeName AS NAME,
					CASE
				WHEN sum(syxl.synum) IS NULL THEN
					0
				ELSE
					sum(syxl.synum)
				END AS synum,
				CASE
			WHEN sum(xl.num) IS NULL THEN
				0
			ELSE
				sum(xl.num)
			END AS num,
			 CASE
			WHEN syxx.user_openid IS NULL THEN
				0
			ELSE
				COUNT(1)
			END AS hyPidsNum,
			 syxx.place_pid AS place_pid,
			 syxx.Place_role_id AS Place_role_id,
			 syxx.pids AS pids
			FROM
				(
					SELECT
						*
					FROM
						(
							SELECT
								p.place_name AS placeName,
								p.place_pids AS pids,
								p.place_pid AS place_pid,
								p.Place_role_id AS Place_role_id
							FROM
								tb_iamberry_place_info p
								where p.place_status = '1'
						) pds
					LEFT JOIN tb_iamberry_user_userinfo u ON pds.pids = u.user_dealers
				) syxx
			LEFT JOIN (
				SELECT
					count(1) AS synum,
					s.probation_openid AS syopenid
				FROM
					TB_IAMBERRY_PROBATION_SHOP_ORDER s
				LEFT JOIN tb_iamberry_user_userinfo u ON s.probation_openid = u.user_openid
				WHERE
					s.PROBATION_STATUS > 0
				GROUP BY
					syopenid
			) syxl ON syxx.user_openid = syxl.syopenid
			LEFT JOIN (
				SELECT
					sum(i.item_num) AS num,
					o.sales_openid AS openid
				FROM
					tb_iamberry_shop_sales_order o
				LEFT JOIN tb_iamberry_shop_order_item i ON o.sales_orderid = i.item_sales_orderid
				LEFT JOIN tb_iamberry_user_userinfo u ON o.sales_openid = u.user_openid
				WHERE
					o.sales_status in (2,5,12)
				AND i.item_product_type = 1
				AND o.sales_order_type = 1
				GROUP BY
					openid
			) xl ON syxx.user_openid = xl.openid
			WHERE
				syxx.Place_role_id = '51'
			GROUP BY
				syxx.placeName
			) viphy ON mdid.dealerSecond = viphy.place_pid
							<if test="name!=null and name!=''">
						   where mdid.name LIKE CONCAT('%',#{name},'%')
							</if>
			GROUP BY
				viphy.place_pid
			ORDER BY num DESC, synum DESC 
	    		 LIMIT ${page.recordBegin},${page.pageSize}
	</select>
	
	<select id="selectAdministratorStatisticsCount" parameterType="StoresStatisticsDto" resultType="Integer">
	SELECT count(*) from (
	SELECT
				mdid. NAME AS NAME,
				mdid.dealerSecond AS dealerSecond,
				CASE
			WHEN sum(viphy.num) IS NULL THEN
				0
			ELSE
				sum(viphy.num)
			END AS num,
			 CASE
			WHEN sum(viphy.synum) IS NULL THEN
				0
			ELSE
				sum(viphy.synum)
			END AS synum,
			 CASE
			WHEN viphy.pids IS NULL THEN
				0
			ELSE
				count(1)
			END AS counts
			FROM
				(
					SELECT DISTINCT
						d.Dealer_second AS dealerSecond,
						p.place_name AS NAME,
						p.place_openid AS openid
					FROM
						tb_iamberry_place_info p
					LEFT JOIN tb_iamberry_dealers d ON p.place_id = d.Dealer_second
					WHERE
						p.place_template_id IN (21, 22, 23)
					AND p.place_id = d.Dealer_second
				) mdid
			LEFT JOIN (
				SELECT
					syxx.placeName AS NAME,
					CASE
				WHEN sum(syxl.synum) IS NULL THEN
					0
				ELSE
					sum(syxl.synum)
				END AS synum,
				CASE
			WHEN sum(xl.num) IS NULL THEN
				0
			ELSE
				sum(xl.num)
			END AS num,
			 CASE
			WHEN syxx.user_openid IS NULL THEN
				0
			ELSE
				COUNT(1)
			END AS hyPidsNum,
			 syxx.place_pid AS place_pid,
			 syxx.Place_role_id AS Place_role_id,
			 syxx.pids AS pids
			FROM
				(
					SELECT
						*
					FROM
						(
							SELECT
								p.place_name AS placeName,
								p.place_pids AS pids,
								p.place_pid AS place_pid,
								p.Place_role_id AS Place_role_id
							FROM
								tb_iamberry_place_info p
						) pds
					LEFT JOIN tb_iamberry_user_userinfo u ON pds.pids = u.user_dealers
				) syxx
			LEFT JOIN (
				SELECT
					count(1) AS synum,
					s.probation_openid AS syopenid
				FROM
					TB_IAMBERRY_PROBATION_SHOP_ORDER s
				LEFT JOIN tb_iamberry_user_userinfo u ON s.probation_openid = u.user_openid
				WHERE
					s.PROBATION_STATUS > 0
				GROUP BY
					syopenid
			) syxl ON syxx.user_openid = syxl.syopenid
			LEFT JOIN (
				SELECT
					sum(i.item_num) AS num,
					o.sales_openid AS openid
				FROM
					tb_iamberry_shop_sales_order o
				LEFT JOIN tb_iamberry_shop_order_item i ON o.sales_orderid = i.item_sales_orderid
				LEFT JOIN tb_iamberry_user_userinfo u ON o.sales_openid = u.user_openid
				WHERE
					o.sales_status in (2,5,12)
				AND i.item_product_type = 1
				AND o.sales_order_type = 1
				GROUP BY
					openid
			) xl ON syxx.user_openid = xl.openid
			WHERE
				syxx.Place_role_id = '51'
			GROUP BY
				syxx.placeName
			) viphy ON mdid.dealerSecond = viphy.place_pid
							<if test="name!=null and name!=''">
						   where mdid.name LIKE CONCAT('%',#{name},'%')
							</if>
			GROUP BY
				viphy.place_pid
				) md
	</select>
	
	<select id="selectSalesCount" parameterType="PaperBean" resultType="StatisticManagerDto">
		SELECT 
			<!-- 查询今日试用 -->
			(
				SELECT COUNT(O.PROBATION_ORDERID) todayProbation 
				FROM TB_IAMBERRY_PROBATION_SHOP_ORDER O 
				<!-- 根据订单所属用户找到用户的商户层关系 USER_DEALERS -->
				LEFT JOIN TB_IAMBERRY_USER_USERINFO U ON O.PROBATION_OPENID = U.USER_OPENID
				<!-- 根据商户层关系找到对应的VIP会员 -->
				LEFT JOIN TB_IAMBERRY_DEALERS D ON U.USER_DEALERS = D.DEALER_ID
				LEFT JOIN TB_IAMBERRY_PLACE_INFO P ON D.DEALER_ID = P.PLACE_PIDS
				<!-- 根据VIP会员找到门店 -->
				LEFT JOIN TB_IAMBERRY_PLACE_INFO PP ON P.PLACE_PID = PP.PLACE_ID
				<!-- 找到门店对应的业务员 -->
				LEFT JOIN TB_IAMBERRY_STATISTICS_RELATION SR ON PP.PLACE_ID = SR.SR_PLACEID
				LEFT JOIN TB_IAMBERRY_STATISTICS_SALESMAN SS ON SR.SR_S_ID = SS.S_ID
				WHERE DATE(O.PROBATION_CREATE_TIME) = CURDATE()
				<!-- 状态是：	3:派送完成，待支付	4:试用暂停	5:支付完成 -->
				AND O.PROBATION_STATUS IN (3,4,5)
				AND SS.S_OPENID = #{text}
			) todayProbation,
			<!-- 查询今日销售 -->
			(
				SELECT SUM(UI.ITEM_NUM) todaySales 
				FROM TB_IAMBERRY_SHOP_SALES_ORDER O
				JOIN TB_IAMBERRY_SHOP_ORDER_ITEM UI ON O.SALES_ORDERID = UI.ITEM_SALES_ORDERID
				<!-- 根据订单所属用户找到用户的商户层关系 USER_DEALERS -->
				LEFT JOIN TB_IAMBERRY_USER_USERINFO U ON O.SALES_OPENID = U.USER_OPENID
				<!-- 根据商户层关系找到对应的VIP会员 -->
				LEFT JOIN TB_IAMBERRY_DEALERS D ON U.USER_DEALERS = D.DEALER_ID
				LEFT JOIN TB_IAMBERRY_PLACE_INFO P ON D.DEALER_ID = P.PLACE_PIDS
				<!-- 根据VIP会员找到门店 -->
				LEFT JOIN TB_IAMBERRY_PLACE_INFO PP ON P.PLACE_PID = PP.PLACE_ID
				<!-- 找到门店对应的业务员 -->
				LEFT JOIN TB_IAMBERRY_STATISTICS_RELATION SR ON PP.PLACE_ID = SR.SR_PLACEID
				LEFT JOIN TB_IAMBERRY_STATISTICS_SALESMAN SS ON SR.SR_S_ID = SS.S_ID
				WHERE DATE(O.SALES_CREATE_DATE) = CURDATE()
				<!-- 查询销售订单的记录 -->
				AND O.SALES_ORDER_TYPE = 1
				AND UI.ITEM_PRODUCT_TYPE = 1
				AND O.SALES_STATUS IN (2,5,12)
				AND SS.S_OPENID = #{text}
			) todaySales,
			<!-- 查询本月试用台数 -->
			(
				SELECT COUNT(O.PROBATION_ORDERID) monthProbation 
				FROM TB_IAMBERRY_PROBATION_SHOP_ORDER O
				LEFT JOIN TB_IAMBERRY_USER_USERINFO U ON O.PROBATION_OPENID = U.USER_OPENID
				LEFT JOIN TB_IAMBERRY_DEALERS D ON U.USER_DEALERS = D.DEALER_ID
				LEFT JOIN TB_IAMBERRY_PLACE_INFO P ON D.DEALER_ID = P.PLACE_PIDS
				LEFT JOIN TB_IAMBERRY_PLACE_INFO PP ON P.PLACE_PID = PP.PLACE_ID
				LEFT JOIN TB_IAMBERRY_STATISTICS_RELATION SR ON PP.PLACE_ID = SR.SR_PLACEID
				LEFT JOIN TB_IAMBERRY_STATISTICS_SALESMAN SS ON SR.SR_S_ID = SS.S_ID
				WHERE 
				<![CDATA[O.PROBATION_CREATE_TIME > #{beginDate}]]> 
				<![CDATA[AND O.PROBATION_CREATE_TIME < #{endDate}]]> 
				<![CDATA[AND O.PROBATION_STATUS IN (3,4,5)]]> 
				AND SS.S_OPENID = #{text}
			) monthProbation,
			<!-- 查询本月销售台数 -->
			(
				SELECT SUM(UI.ITEM_NUM)  monthSales 
				FROM TB_IAMBERRY_SHOP_SALES_ORDER O
				JOIN TB_IAMBERRY_SHOP_ORDER_ITEM UI ON O.SALES_ORDERID = UI.ITEM_SALES_ORDERID
				LEFT JOIN TB_IAMBERRY_USER_USERINFO U ON O.SALES_OPENID = U.USER_OPENID
				LEFT JOIN TB_IAMBERRY_DEALERS D ON U.USER_DEALERS = D.DEALER_ID
				LEFT JOIN TB_IAMBERRY_PLACE_INFO P ON D.DEALER_ID = P.PLACE_PIDS
				LEFT JOIN TB_IAMBERRY_PLACE_INFO PP ON P.PLACE_PID = PP.PLACE_ID
				LEFT JOIN TB_IAMBERRY_STATISTICS_RELATION SR ON PP.PLACE_ID = SR.SR_PLACEID
				LEFT JOIN TB_IAMBERRY_STATISTICS_SALESMAN SS ON SR.SR_S_ID = SS.S_ID
				WHERE 
				<![CDATA[O.SALES_CREATE_DATE > #{beginDate}]]> 
				<![CDATA[AND O.SALES_CREATE_DATE < #{endDate}]]> 
				AND O.SALES_STATUS IN (2,5,12)
				<!-- 查询销售订单的记录 -->
				AND O.SALES_ORDER_TYPE = 1
				AND UI.ITEM_PRODUCT_TYPE = 1
				AND SS.S_OPENID = #{text}
			) monthSales, 
			<!-- 当前业务员角色名称 -->
			S_ROLENAME salesManRoleName,
			<!-- 当前业务员信息 -->
			UU.USER_OPENID salesManOpenid,
			UU.USER_NICKNAME salesManUserName
		FROM TB_IAMBERRY_STATISTICS_SALESMAN TS 
		JOIN TB_IAMBERRY_USER_USERINFO UU ON TS.S_OPENID = UU.USER_OPENID
		AND UU.user_openid= #{text}
	</select>
	
	<!-- 根据业务员 openid 查他对应的门店下的所有试用订单 -->
	<select id="selectProbationOrderBySalesManOpenid" parameterType="PaperBean" resultType="StatisticsOrderDTO">
		SELECT 
			O.PROBATION_ORDERID orderId,
			O.PROBATION_STATUS orderStatus,
			U.USER_DEALERS dealers,
			P.PLACE_NAME placeInfoOne,
			PP.PLACE_NAME placeInfoTwo
		FROM TB_IAMBERRY_PROBATION_SHOP_ORDER O 
		LEFT JOIN TB_IAMBERRY_USER_USERINFO U ON O.PROBATION_OPENID = U.USER_OPENID
		LEFT JOIN TB_IAMBERRY_DEALERS D ON U.USER_DEALERS = D.DEALER_ID
		LEFT JOIN TB_IAMBERRY_PLACE_INFO P ON D.DEALER_ID = P.PLACE_PIDS
		LEFT JOIN TB_IAMBERRY_PLACE_INFO PP ON P.PLACE_PID = PP.PLACE_ID
		LEFT JOIN TB_IAMBERRY_STATISTICS_RELATION SR ON PP.PLACE_ID = SR.SR_PLACEID
		LEFT JOIN TB_IAMBERRY_STATISTICS_SALESMAN SS ON SR.SR_S_ID = SS.S_ID
		WHERE O.PROBATION_STATUS IN (1,2,3,4)
		AND SS.S_OPENID = #{text}
		ORDER BY O.PROBATION_CREATE_TIME DESC
		LIMIT #{minNum}, #{maxNum}
	</select>
	
	<!-- 根据业务员 openid 查他对应的门店下的所有试用订单 总数-->
	<select id="selectProbationOrderCountNumBySalesManOpenid" parameterType="PaperBean" resultType="Integer">
		SELECT 
			COUNT(O.PROBATION_ORDERID)
		FROM TB_IAMBERRY_PROBATION_SHOP_ORDER O 
		LEFT JOIN TB_IAMBERRY_USER_USERINFO U ON O.PROBATION_OPENID = U.USER_OPENID
		LEFT JOIN TB_IAMBERRY_DEALERS D ON U.USER_DEALERS = D.DEALER_ID
		LEFT JOIN TB_IAMBERRY_PLACE_INFO P ON D.DEALER_ID = P.PLACE_PIDS
		LEFT JOIN TB_IAMBERRY_PLACE_INFO PP ON P.PLACE_PID = PP.PLACE_ID
		LEFT JOIN TB_IAMBERRY_STATISTICS_RELATION SR ON PP.PLACE_ID = SR.SR_PLACEID
		LEFT JOIN TB_IAMBERRY_STATISTICS_SALESMAN SS ON SR.SR_S_ID = SS.S_ID
		WHERE O.PROBATION_STATUS IN (1,2,3,4)
		AND SS.S_OPENID = #{text}
	</select>
	
	<!-- 根据业务员 openid 查他对应的门店下的所有销售订单 -->
	<select id="selectOrderBySalesManOpenid" parameterType="PaperBean" resultType="StatisticsOrderDTO">
		SELECT 
			O.SALES_ORDERID orderId,
			O.SALES_STATUS orderStatus,
			U.USER_DEALERS dealers,
			P.PLACE_NAME placeInfoOne,
			PP.PLACE_NAME placeInfoTwo
		FROM TB_IAMBERRY_SHOP_SALES_ORDER O
		LEFT JOIN TB_IAMBERRY_USER_USERINFO U ON O.SALES_OPENID = U.USER_OPENID
		LEFT JOIN TB_IAMBERRY_DEALERS D ON U.USER_DEALERS = D.DEALER_ID
		LEFT JOIN TB_IAMBERRY_PLACE_INFO P ON D.DEALER_ID = P.PLACE_PIDS
		LEFT JOIN TB_IAMBERRY_PLACE_INFO PP ON P.PLACE_PID = PP.PLACE_ID
		LEFT JOIN TB_IAMBERRY_STATISTICS_RELATION SR ON PP.PLACE_ID = SR.SR_PLACEID
		LEFT JOIN TB_IAMBERRY_STATISTICS_SALESMAN SS ON SR.SR_S_ID = SS.S_ID
		WHERE O.SALES_STATUS IN (2,5,12)
		<!-- 查询销售订单的记录 -->
		AND O.SALES_ORDER_TYPE = 1
		AND SS.S_OPENID = #{text}
		ORDER BY O.SALES_CREATE_DATE DESC
		LIMIT #{minNum}, #{maxNum}
	</select>
	
	<!-- 根据业务员 openid 查他对应的门店下的所有销售订单 总数-->
	<select id="selectOrderCountNumBySalesManOpenid" parameterType="PaperBean" resultType="Integer">
		SELECT 
			COUNT(O.SALES_ORDERID)
		FROM TB_IAMBERRY_SHOP_SALES_ORDER O
		LEFT JOIN TB_IAMBERRY_USER_USERINFO U ON O.SALES_OPENID = U.USER_OPENID
		LEFT JOIN TB_IAMBERRY_DEALERS D ON U.USER_DEALERS = D.DEALER_ID
		LEFT JOIN TB_IAMBERRY_PLACE_INFO P ON D.DEALER_ID = P.PLACE_PIDS
		LEFT JOIN TB_IAMBERRY_PLACE_INFO PP ON P.PLACE_PID = PP.PLACE_ID
		LEFT JOIN TB_IAMBERRY_STATISTICS_RELATION SR ON PP.PLACE_ID = SR.SR_PLACEID
		LEFT JOIN TB_IAMBERRY_STATISTICS_SALESMAN SS ON SR.SR_S_ID = SS.S_ID
		WHERE O.SALES_STATUS IN (2,5,12)
		<!-- 查询销售订单的记录 -->
		AND O.SALES_ORDER_TYPE = 1
		AND SS.S_OPENID = #{text}
	</select>
	
	<!-- 按月统计业务员试用订单台数 -->
	<select id="selectProbationCountNumByMonth" parameterType="PaperBean" resultType="StatisticOrderByMonthDto">
		SELECT PROBATIONCOUNTNUM,MONTH,THISYEAR FROM
		(
			SELECT 
				COUNT(O.PROBATION_ORDERID) PROBATIONCOUNTNUM,
				CONCAT(DATE_FORMAT(O.PROBATION_CREATE_TIME,'%c'),'月') MONTH,
				DATE_FORMAT(O.PROBATION_CREATE_TIME,'%Y') THISYEAR
			FROM TB_IAMBERRY_PROBATION_SHOP_ORDER O 
			LEFT JOIN TB_IAMBERRY_USER_USERINFO U ON O.PROBATION_OPENID = U.USER_OPENID
			LEFT JOIN TB_IAMBERRY_DEALERS D ON U.USER_DEALERS = D.DEALER_ID
			LEFT JOIN TB_IAMBERRY_PLACE_INFO P ON D.DEALER_ID = P.PLACE_PIDS
			LEFT JOIN TB_IAMBERRY_PLACE_INFO PP ON P.PLACE_PID = PP.PLACE_ID
			LEFT JOIN TB_IAMBERRY_STATISTICS_RELATION SR ON PP.PLACE_ID = SR.SR_PLACEID
			LEFT JOIN TB_IAMBERRY_STATISTICS_SALESMAN SS ON SR.SR_S_ID = SS.S_ID
			WHERE O.PROBATION_STATUS IN (1,2,3,4,5)
			AND SS.S_OPENID = #{text}
			GROUP BY MONTH
		) W
		WHERE W.THISYEAR = #{otherInt}
	</select>
	
	<!-- 按月统计业务员销售订单台数 -->
	<select id="selectOrderCountNumByMonth" parameterType="PaperBean" resultType="StatisticOrderByMonthDto">
		SELECT ORDERCOUNTNUM,MONTH,THISYEAR FROM
		(
			SELECT 
				COUNT(O.SALES_ORDERID) ORDERCOUNTNUM,
				CONCAT(DATE_FORMAT(O.SALES_CREATE_DATE,'%c'),'月') MONTH,
				DATE_FORMAT(O.SALES_CREATE_DATE,'%Y') THISYEAR
			FROM TB_IAMBERRY_SHOP_SALES_ORDER O
			LEFT JOIN TB_IAMBERRY_USER_USERINFO U ON O.SALES_OPENID = U.USER_OPENID
			LEFT JOIN TB_IAMBERRY_DEALERS D ON U.USER_DEALERS = D.DEALER_ID
			LEFT JOIN TB_IAMBERRY_PLACE_INFO P ON D.DEALER_ID = P.PLACE_PIDS
			LEFT JOIN TB_IAMBERRY_PLACE_INFO PP ON P.PLACE_PID = PP.PLACE_ID
			LEFT JOIN TB_IAMBERRY_STATISTICS_RELATION SR ON PP.PLACE_ID = SR.SR_PLACEID
			LEFT JOIN TB_IAMBERRY_STATISTICS_SALESMAN SS ON SR.SR_S_ID = SS.S_ID
			WHERE O.SALES_STATUS IN (2,5,12)
			<!-- 查询销售订单的记录 -->
			AND O.SALES_ORDER_TYPE = 1
			AND SS.S_OPENID = #{text}
			GROUP BY MONTH
		) W
		WHERE W.THISYEAR = #{otherInt}
	</select>
	
	<!-- 查询今日试用订单，今日销售订单，本月试用订单，本月销售订单 -->
	<select id="selectSalesInfo" resultType="com.iamberry.wechat.core.entity.statictics.StatisticsAdministratorDTO">
		SELECT 
			COUNT(DISTINCT p.probation_orderid) todayProbation,
			null todaySales, 
			null monthProbation,
			null monthSales, 
			null probationNum,
			null salesNum 
		FROM 
			TB_IAMBERRY_PROBATION_SHOP_ORDER p 
		WHERE 
			DATE(p.PROBATION_CREATE_TIME) = CURDATE() 
		AND 
			P.PROBATION_STATUS IN (1,2,3,4,5) 
		UNION
		SELECT 
			null todayProbation,
			IFNULL(SUM(i.ITEM_NUM),0) todaySales, 
			null monthProbation, 
			null monthSales,
			null probationNum,
			null salesNum 
		FROM 
			TB_IAMBERRY_SHOP_SALES_ORDER o 
		LEFT JOIN TB_IAMBERRY_SHOP_ORDER_ITEM i ON o.SALES_ORDERID = i.ITEM_SALES_ORDERID 
		WHERE 
			DATE(o.SALES_CREATE_DATE) = CURDATE() AND o.SALES_STATUS IN (2,5,12) 
		AND 
			i.ITEM_PRODUCT_TYPE = 1 
		AND 
			o.SALES_ORDER_TYPE = 1 
		UNION
		SELECT 
			null todayProbation,
			null todaySales, 
			COUNT(DISTINCT p.PROBATION_OPENID) monthProbation,
			null monthSales,
			null probationNum,
			null salesNum 
		FROM 
			TB_IAMBERRY_PROBATION_SHOP_ORDER p 
		WHERE 
			DATE_FORMAT(p.PROBATION_CREATE_TIME,'%Y-%m') = DATE_FORMAT(NOW(),'%Y-%m') 
		AND 
			P.PROBATION_STATUS IN (1,2,3,4,5) 
		UNION
		SELECT 
			null todayProbation,
			null todaySales, 
			null monthProbation, 
			IFNULL(SUM(i.ITEM_NUM),0) monthSales,
			null probationNum,
			null salesNum 
		FROM 
			TB_IAMBERRY_SHOP_SALES_ORDER o 
		LEFT JOIN TB_IAMBERRY_SHOP_ORDER_ITEM i ON o.SALES_ORDERID = i.ITEM_SALES_ORDERID 
		WHERE 
			DATE_FORMAT(o.SALES_CREATE_DATE,'%Y-%m') = DATE_FORMAT(NOW(),'%Y-%m') 
		AND 
			o.SALES_STATUS IN (2,5,12) 
		AND 
			i.ITEM_PRODUCT_TYPE = 1 
		AND 
			o.SALES_ORDER_TYPE = 1 
		UNION
		SELECT 
			null todayProbation,
			null todaySales, 
			null monthProbation,
			null monthSales,
			COUNT(DISTINCT p.PROBATION_OPENID) probationNum,
			null salesNum 
		FROM 
			TB_IAMBERRY_PROBATION_SHOP_ORDER p 
		WHERE 
			P.PROBATION_STATUS IN (1,2,3,4,5) 
		UNION 
		SELECT 
			null todayProbation,
			null todaySales, 
			null monthProbation, 
			null monthSales,
			null probationNum,
			IFNULL(SUM(i.ITEM_NUM),0) salesNum 
		FROM 
			TB_IAMBERRY_SHOP_SALES_ORDER o 
		LEFT JOIN TB_IAMBERRY_SHOP_ORDER_ITEM i ON o.SALES_ORDERID = i.ITEM_SALES_ORDERID 
		WHERE 
			o.SALES_STATUS IN (2,5,12) 
		AND 
			i.ITEM_PRODUCT_TYPE = 1 
		AND 
			o.SALES_ORDER_TYPE = 1 
	</select>
	
	<!-- 查询销售订单总额，试用订单已付，试用订单待付 -->
	<select id="selectOrderMoney" resultType="com.iamberry.wechat.core.entity.statictics.StatisticsAdministratorDTO">
		SELECT
			IFNULL(SUM(o.SALES_YET_AMOUNT),0) salesMoney,
			null probationUnpaidMoney,
			null probationPaidMoney 
		FROM
			TB_IAMBERRY_SHOP_SALES_ORDER o 
		WHERE
			o.SALES_STATUS IN (2,5,12) 
		AND
			o.SALES_ORDER_TYPE = 1 
		UNION
		SELECT
			null salesMoney,
			SUM(CASE WHEN p.PROBATION_STATUS IN (1,2,3,4) THEN p.PROBATION_YET_AMOUNT ELSE 0 END) probationUnpaidMoney,
			SUM(CASE WHEN p.PROBATION_STATUS IN (5) THEN p.PROBATION_YET_AMOUNT ELSE 0 END) probationPaidMoney 
		FROM
			TB_IAMBERRY_PROBATION_SHOP_ORDER p
	</select>
	
	<!-- 查询已激活合伙人，已激活门店，已激活VIP会员，金牌会员 -->
	<select id="selectRoleInfo" resultType="com.iamberry.wechat.core.entity.statictics.StatisticsAdministratorDTO">
		SELECT
			SUM(CASE WHEN r.ROLE_NAME = '合伙人' THEN 1 ELSE 0 END) partnerNum, 
			SUM(CASE WHEN r.ROLE_NAME = '门店' THEN 1 ELSE 0 END) storeNum,
			SUM(CASE WHEN r.ROLE_NAME = 'VIP会员' THEN 1 ELSE 0 END) vipNum,
			SUM(CASE WHEN r.ROLE_NAME = '金牌会员' THEN 1 ELSE 0 END) memberNum 
		FROM 
			TB_IAMBERRY_PLACE_INFO p 
		LEFT JOIN TB_IAMBERRY_PLACE_ROLE r ON p.PLACE_ROLE_ID = r.ROLE_ID 
		WHERE 
			p.PLACE_OPENID IS NOT NULL
	</select>
	
	<!-- 查询商城用户数，购买过商品的用户数 -->
	<select id="selectUserNum" resultType="com.iamberry.wechat.core.entity.statictics.StatisticsAdministratorDTO">
		SELECT 
			COUNT(DISTINCT u.USER_ID) userNum, 
			COUNT(DISTINCT o.SALES_OPENID) userSalesNum 
		FROM
			TB_IAMBERRY_USER_USERINFO u 
		LEFT JOIN ( 
				SELECT 
					SALES_OPENID 
				FROM 
					TB_IAMBERRY_SHOP_SALES_ORDER 
				WHERE 
					SALES_STATUS <![CDATA[>=]]> 2) o ON u.USER_OPENID = o.SALES_OPENID
	</select>
	
	<!-- 查询管理员信息 -->
	<select id="selectAdminInfo" parameterType="String" resultType="SalesmanDto">
		SELECT 
			s.S_ROLEID sRoleId,
			s.S_ROLENAME sRoleName,
			u.USER_NICKNAME sNickName 
		FROM 
			TB_IAMBERRY_STATISTICS_SALESMAN s 
			LEFT JOIN TB_IAMBERRY_USER_USERINFO u ON s.S_OPENID = u.USER_OPENID 
		WHERE s.S_OPENID = #{openId}
	</select>
	
	<!-- 查询金牌会员下线个数 -->
	<select id="selectUserSubordinateInfo" resultType="Integer">
		SELECT
			COUNT(u.USER_ID) 
		FROM
			TB_IAMBERRY_USER_USERINFO u 
		WHERE
			u.USER_DEALERS in (
				SELECT 
			 		DISTINCT p.PLACE_PIDS 
				FROM
		 	 		TB_IAMBERRY_PLACE_INFO p 
				LEFT JOIN TB_IAMBERRY_PLACE_ROLE r ON p.PLACE_ROLE_ID = r.ROLE_ID 
				WHERE
			 		r.ROLE_NAME = '金牌会员') 
		AND
			u.USER_RES_TYPE = 3 
	</select>
		
	<!-- 按月查询销售订单售出水机总数 -->
	<select id="selectOrderByMonth" parameterType="StatisticOrderByMonthDto" resultType="StatisticOrderByMonthDto">
		SELECT 
			CONCAT(DATE_FORMAT(o.SALES_CREATE_DATE,'%c'),'月') month,
			IFNULL(SUM(i.ITEM_NUM),0) orderCountNum 
		FROM 
			TB_IAMBERRY_SHOP_SALES_ORDER o 
		LEFT JOIN TB_IAMBERRY_SHOP_ORDER_ITEM i ON o.SALES_ORDERID = i.ITEM_SALES_ORDERID 
		WHERE 
			i.ITEM_PRODUCT_TYPE = 1 
		AND 
			o.SALES_STATUS IN (2,5,12) 
		AND 
			o.SALES_ORDER_TYPE = 1 
		<if test="month != null and month != ''">
			AND 
				DATE_FORMAT(o.SALES_CREATE_DATE,'%Y') = #{month} 
		</if>
		GROUP BY 
			DATE_FORMAT(o.SALES_CREATE_DATE,'%Y-%m')
	</select>
	
	<!-- 按月查询试用订单总数 -->
	<select id="selectProbationByMonth" parameterType="StatisticOrderByMonthDto" resultType="StatisticOrderByMonthDto">
		SELECT 
			CONCAT(DATE_FORMAT(p.PROBATION_CREATE_TIME,'%c'),'月') month, 
			COUNT(p.PROBATION_ORDERID) probationCountNum 
		FROM 
			TB_IAMBERRY_PROBATION_SHOP_ORDER p 
		WHERE 
			p.PROBATION_STATUS IN (1,2,3,4,5) 
		<if test="month != null and month != ''">
			AND 
				DATE_FORMAT(p.PROBATION_CREATE_TIME,'%Y') = #{month} 
		</if>
		GROUP BY 
			DATE_FORMAT(p.PROBATION_CREATE_TIME,'%Y-%m')
	</select>
	
	<!-- 金牌会员统计 所有金牌会员名称，对应的下线人数，及销量 -->
	<select id="selectAllGoldMemberStatistics" 
		parameterType="com.iamberry.wechat.core.entity.statictics.GoldMemberStatisticsDto" 
		resultType="com.iamberry.wechat.core.entity.statictics.GoldMemberStatisticsDto">
		SELECT 
			P.USER_NICKNAME placeName,
			P.PLACE_ID placeId,
			countSales,
			UU.COUNTUSER underUser 
		FROM 
		(
			SELECT 
				UU.USER_NICKNAME,
				PL.PLACE_ID,
				PL.PLACE_PIDS
			FROM TB_IAMBERRY_PLACE_INFO PL
			JOIN TB_IAMBERRY_USER_USERINFO UU ON PL.PLACE_OPENID = UU.USER_OPENID
			WHERE PL.PLACE_ROLE_ID = '52'
			<if test="placeName != null and placeName != ''">
				AND P.PLACE_NAME LIKE CONCAT('%',#{placeName},'%')
			</if>
		) P
		LEFT JOIN
			<!-- 查金牌会员下线销量 (包括试用、销售订单)-->
			(
				SELECT SUM(OI.ITEM_NUM) countSales,U.USER_OPENID,U.USER_DEALERS  
				FROM TB_IAMBERRY_SHOP_SALES_ORDER O
				LEFT JOIN TB_IAMBERRY_SHOP_ORDER_ITEM OI ON O.SALES_ORDERID = OI.ITEM_SALES_ORDERID
				LEFT JOIN TB_IAMBERRY_USER_USERINFO U ON O.SALES_OPENID = U.USER_OPENID
				WHERE U.USER_TEMPLATE_ID = 24
				AND O.SALES_STATUS IN (2,5,12)
				AND OI.ITEM_PRODUCT_TYPE = 1
				GROUP BY U.USER_DEALERS
			) SU
		ON P.PLACE_PIDS = SU.USER_DEALERS
		LEFT JOIN 
		(	
			<!-- 查金牌会员下线人数 -->
			SELECT COUNT(UU.USER_ID) COUNTUSER,USER_DEALERS 
			FROM TB_IAMBERRY_USER_USERINFO UU 
			WHERE USER_TEMPLATE_ID=24	
			AND USER_DEALERS IS NOT NULL
			GROUP BY USER_DEALERS
		) UU
		ON P.PLACE_PIDS = UU.USER_DEALERS
		ORDER BY countSales desc,underUser desc 
		<if test="page!=null and page.recordBegin>0 and page.pageSize>0 ">
	    	LIMIT ${page.recordBegin},${page.pageSize}
	    </if>
	    <if test="page!=null and page.recordBegin==0 and page.pageSize>0 ">
	    	LIMIT ${page.pageSize}
	    </if>
	</select>
	<!-- 金牌会员统计 所有金牌会员名称，对应的下线人数，及销量  记录条数-->
	<select id="selectAllGoldMemberStatisticsCountNum" parameterType="String" resultType="Integer">
		SELECT COUNT(P.PLACE_ID) 
		FROM TB_IAMBERRY_PLACE_INFO P
		JOIN
			<!-- 查金牌会员下线销量 -->
			(
				SELECT SUM(OI.ITEM_NUM) COUNTSALES,U.USER_OPENID,U.USER_DEALERS  
				FROM TB_IAMBERRY_SHOP_SALES_ORDER O
				LEFT JOIN TB_IAMBERRY_SHOP_ORDER_ITEM OI ON O.SALES_ORDERID = OI.ITEM_SALES_ORDERID
				LEFT JOIN TB_IAMBERRY_USER_USERINFO U ON O.SALES_OPENID = U.USER_OPENID
				WHERE U.USER_TEMPLATE_ID = 24
				AND O.SALES_STATUS IN (2,5,12)
				AND OI.ITEM_PRODUCT_TYPE = 1
				GROUP BY U.USER_DEALERS
			) SU
		ON P.PLACE_PIDS = SU.USER_DEALERS
		JOIN 
		(	
			<!-- 查金牌会员下线人数 (包括试用、销售订单)-->
			SELECT COUNT(UU.USER_ID) COUNTUSER,USER_DEALERS 
			FROM TB_IAMBERRY_USER_USERINFO UU 
			WHERE USER_TEMPLATE_ID=24	
			AND USER_DEALERS IS NOT NULL
			GROUP BY USER_DEALERS
		) UU
		ON P.PLACE_PIDS = UU.USER_DEALERS
		<if test="_parameter != null and _parameter != ''">
			AND P.PLACE_NAME LIKE CONCAT('%',#{_parameter},'%')
		</if>
	</select>
		
	<!-- 单个金牌会员下线会员购买信息 -->
	<select id="selectGoldUnderUserAndBoughtCount" 
		parameterType="com.iamberry.wechat.core.entity.statictics.GoldMemberStatisticsDto" 
		resultType="com.iamberry.wechat.core.entity.statictics.GoldMemberStatisticsDto">
		SELECT placeName,userHead,userNickName,userBuyCount FROM
			(
				SELECT 
					P.PLACE_NAME placeName,
					U.USER_HEAD userHead,
					U.USER_NICKNAME userNickName,
					SUM(OI.ITEM_NUM) userBuyCount 
				FROM TB_IAMBERRY_PLACE_INFO P
				JOIN TB_IAMBERRY_USER_USERINFO U ON P.PLACE_PIDS = U.USER_DEALERS
				JOIN TB_IAMBERRY_SHOP_SALES_ORDER O ON U.USER_OPENID = O.SALES_OPENID
				JOIN TB_IAMBERRY_SHOP_ORDER_ITEM OI ON O.SALES_ORDERID = OI.ITEM_SALES_ORDERID
				WHERE U.USER_TEMPLATE_ID = 24
				AND P.PLACE_ID = #{placeId}
				AND O.SALES_STATUS IN (2,5,12)
				<!-- 去掉赠品 -->
				AND OI.ITEM_PRODUCT_TYPE = 1
				GROUP BY U.USER_OPENID
				ORDER BY USERBUYCOUNT DESC
			) a
			<if test="userNickName != null and userNickName != ''">
				WHERE a.userNickName LIKE CONCAT('%',#{userNickName},'%')
			</if>
			<if test="page!=null and page.recordBegin>0 and page.pageSize>0 ">
		    	LIMIT ${page.recordBegin},${page.pageSize}
		    </if>
		    <if test="page!=null and page.recordBegin==0 and page.pageSize>0 ">
		    	LIMIT ${page.pageSize}
		    </if>
	</select>
	<!-- 单个金牌会员下线会员购买信息 记录条数-->
	<select id="selectGoldUnderUserAndBoughtCountNum" resultType="Integer"
		parameterType="com.iamberry.wechat.core.entity.statictics.GoldMemberStatisticsDto">
		SELECT 
			COUNT(USERBUYCOUNT)
		FROM
		(
			SELECT P.PLACE_NAME,U.USER_HEAD,U.USER_NICKNAME,O.SALES_ORDERID,SUM(OI.ITEM_NUM) USERBUYCOUNT 
			FROM TB_IAMBERRY_PLACE_INFO P
			JOIN TB_IAMBERRY_USER_USERINFO U ON P.PLACE_PIDS = U.USER_DEALERS
			JOIN TB_IAMBERRY_SHOP_SALES_ORDER O ON U.USER_OPENID = O.SALES_OPENID
			JOIN TB_IAMBERRY_SHOP_ORDER_ITEM OI ON O.SALES_ORDERID = OI.ITEM_SALES_ORDERID
			WHERE U.USER_TEMPLATE_ID = 24
			AND P.PLACE_ID = #{placeId}
			AND O.SALES_STATUS IN (2,5,12)
			AND OI.ITEM_PRODUCT_TYPE = 1
			<if test="userNickName != null and userNickName != ''">
			AND U.USER_NICKNAME LIKE CONCAT('%',#{userNickName},'%')
		</if>
			ORDER BY USERBUYCOUNT
		) a
	</select>
	
	<select id="selectSalesNumbers" resultType="StatisticOrderByMonthDto"
		parameterType="StatisticOrderByMonthDto">
		SELECT 
		CASE WHEN f.PLACE_ROLE_ID = 52 THEN fp.USER_NICKNAME ELSE f.PLACE_NAME END placeInfoTwo, 
			CASE WHEN p.PLACE_ROLE_ID = 52 THEN up.USER_NICKNAME ELSE p.PLACE_NAME END placeInfoOne, 
		SUM(i.item_num) orderCountNum
		FROM 
			TB_IAMBERRY_SHOP_SALES_ORDER o 
		LEFT JOIN TB_IAMBERRY_SHOP_ORDER_ITEM i ON o.SALES_ORDERID = i.ITEM_SALES_ORDERID 
		LEFT JOIN tb_iamberry_user_userinfo u on o.sales_openid = u.user_openid 
		LEFT JOIN tb_iamberry_place_info p on u.user_dealers = p.place_pids 
		LEFT JOIN tb_iamberry_place_info f on p.place_pid = f.place_id 
		LEFT JOIN TB_IAMBERRY_USER_USERINFO up ON p.PLACE_OPENID = up.USER_OPENID 
		LEFT JOIN TB_IAMBERRY_USER_USERINFO fp ON f.PLACE_OPENID = fp.USER_OPENID 
		WHERE 
			DATE_FORMAT(o.SALES_CREATE_DATE,'%Y-%m') = DATE_FORMAT(NOW(),'%Y-%m') 
		AND 
			o.SALES_STATUS IN (2,5,12) 
		AND 
			i.ITEM_PRODUCT_TYPE = 1 
		AND 
			o.SALES_ORDER_TYPE = 1 
		AND
		u.user_dealers is not null 
		
		GROUP BY p.place_id
	    LIMIT ${page.recordBegin},${page.pageSize}
	</select>
	
	<select id="selectSalesNumbersCount" resultType="Integer"
		parameterType="StatisticOrderByMonthDto">
		SELECT COUNT(1) FROM (
		SELECT 
			CASE WHEN f.PLACE_ROLE_ID = 52 THEN fp.USER_NICKNAME ELSE f.PLACE_NAME END placeInfoTwo, 
			CASE WHEN p.PLACE_ROLE_ID = 52 THEN up.USER_NICKNAME ELSE p.PLACE_NAME END placeInfoOne, 
		SUM(i.item_num) orderCountNum
		FROM 
			TB_IAMBERRY_SHOP_SALES_ORDER o 
		LEFT JOIN TB_IAMBERRY_SHOP_ORDER_ITEM i ON o.SALES_ORDERID = i.ITEM_SALES_ORDERID 
		LEFT JOIN tb_iamberry_user_userinfo u on o.sales_openid = u.user_openid 
		LEFT JOIN tb_iamberry_place_info p on u.user_dealers = p.place_pids 
		LEFT JOIN tb_iamberry_place_info f on p.place_pid = f.place_id 
		LEFT JOIN TB_IAMBERRY_USER_USERINFO up ON p.PLACE_OPENID = up.USER_OPENID 
		LEFT JOIN TB_IAMBERRY_USER_USERINFO fp ON f.PLACE_OPENID = fp.USER_OPENID 
		WHERE 
			DATE_FORMAT(o.SALES_CREATE_DATE,'%Y-%m') = DATE_FORMAT(NOW(),'%Y-%m') 
		AND 
			o.SALES_STATUS IN (2,5,12) 
		AND 
			i.ITEM_PRODUCT_TYPE = 1 
		AND 
			o.SALES_ORDER_TYPE = 1 
		AND
		u.user_dealers is not null 
		GROUP BY p.place_id) xl
	</select>
	
	
	<!-- 按月查询试用订单数 -->
	<select id="selectProbationSumByMonth" parameterType="StatisticOrderByMonthDto" resultType="StatisticOrderByMonthDto">
		SELECT 
			CASE WHEN f.PLACE_ROLE_ID = 52 THEN fp.USER_NICKNAME ELSE f.PLACE_NAME END placeInfoTwo, 
			CASE WHEN p.PLACE_ROLE_ID = 52 THEN up.USER_NICKNAME ELSE p.PLACE_NAME END placeInfoOne, 
			COUNT(DISTINCT o.PROBATION_ORDERID) probationCountNum 
		FROM 
			TB_IAMBERRY_PROBATION_SHOP_ORDER o 
			LEFT JOIN TB_IAMBERRY_USER_USERINFO u ON o.PROBATION_OPENID = u.USER_OPENID 
			LEFT JOIN TB_IAMBERRY_PLACE_INFO p ON u.USER_DEALERS = p.PLACE_PIDS 
			LEFT JOIN TB_IAMBERRY_PLACE_INFO f ON p.PLACE_PID = f.PLACE_ID 
			LEFT JOIN TB_IAMBERRY_USER_USERINFO up ON p.PLACE_OPENID = up.USER_OPENID 
			LEFT JOIN TB_IAMBERRY_USER_USERINFO fp ON f.PLACE_OPENID = fp.USER_OPENID 
		WHERE 
			o.PROBATION_STATUS > 0 
		AND 
			DATE_FORMAT(o.PROBATION_CREATE_TIME,'%Y-%m') = DATE_FORMAT(NOW(),'%Y-%m') 
		GROUP BY p.PLACE_ID 
		LIMIT ${page.recordBegin},${page.pageSize}
	</select>
	
</mapper>