<?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">
<mapper namespace="com.cku.oa.statistics.dao.ActivityRegisterDao">
	<!-- 赛事报名统计 -->
	<!-- 符合查询条件的主赛事、子赛事、报名人数、报名犬数的数量-->
	<select id="countTotal" resultType="map">
		SELECT
			IFNULL(COUNT(DISTINCT sm.id),0) as numOfMainShow,
			IFNULL(COUNT(DISTINCT sc.show_code),0)as numOfSubShow,
			IFNULL(COUNT(DISTINCT CASE WHEN LENGTH(sa.pedigree_certified)>4 THEN sa.pedigree_certified END),0) as numOfRegDog,
			IFNULL(COUNT(DISTINCT sa.dog_owner_member_code),0) as numOfReg
		FROM show_main sm
		LEFT JOIN show_child sc ON sm.id = sc.main_show_id
		LEFT JOIN show_apply sa ON sa.main_show_id = sm.id
		AND sa.show_code !=''
		AND sa.payment_state = '2'
		AND sa.absence_flag = '0'
		AND sa.refund_flag = '0'
		AND sa.del_flag = '0'
		WHERE sm.del_flag = '0'
		<if test="orgnizeType !=null and orgnizeType !=''">
			AND sm.orgnize_type = #{orgnizeType}
		</if>
		<if test="showCode != null and showCode != ''">
			AND LOCATE(#{showCode},sc.show_code) > 0
		</if>
		<if test="startDate != null and startDate != ''">
			AND sc.start_time >= #{startDate}
		</if>
		<if test="endDate != null and endDate != ''">
			AND sc.end_time &lt;= #{endDate}
		</if>
	</select>

	<!-- 查询符合条件的赛事收入及子赛事报名订单数总记 -->
	<select id="countTotalFee" resultType="map">
		SELECT
			ifnull(SUM(LENGTH(sa.show_code)- LENGTH(REPLACE(sa.show_code,',','')) + 1),0) AS 'numOfSubShow',
			ifnull(SUM(sa.order_price),0) AS 'feeOfShow'
		FROM show_apply sa
		WHERE
		sa.show_code !=''
		AND sa.payment_state = '2'
		AND sa.absence_flag = '0'
		AND sa.refund_flag = '0'
		AND sa.del_flag = '0'
		AND sa.main_show_id IN (
			SELECT t_sm.id
			FROM (
				SELECT
					sm.id,
					MIN(sc.start_time) AS 'start_time',
					MAX(sc.end_time) AS 'end_time'
				FROM show_main sm
				LEFT JOIN show_child sc ON sm.id = sc.main_show_id
				WHERE sm.del_flag = '0'
				<if test="orgnizeType !=null and orgnizeType !=''">
					AND sm.orgnize_type = #{orgnizeType}
				</if>
				<if test="showCode != null and showCode != ''">
					AND LOCATE(#{showCode},sc.show_code) > 0
				</if>
				GROUP BY sm.id
			) t_sm
		)
		<if test="startDate != null and startDate != ''">
			AND sa.payment_time >= #{startDate}
		</if>
		<if test="endDate != null and endDate != ''">
			AND sa.payment_time &lt;= #{endDate}
		</if>
	</select>

	<!-- 查询符合条件的主赛事ID -->
	<select id="showMainID" resultType="map">
		SELECT
			sm.id
		FROM show_main AS sm
		LEFT JOIN show_child AS sc ON sm.id = sc.main_show_id
		<where>
			sm.del_flag = 0 AND sc.del_flag = 0
			<if test="orgnizeType !=null and orgnizeType !=''">
				AND sm.orgnize_type = #{orgnizeType}
			</if>
			<if test="showCode != null and showCode != ''">
				AND LOCATE(#{showCode},sc.show_code) > 0
			</if>
			<if test="startDate != null and startDate != ''">
				AND sc.start_time >= #{startDate}
			</if>
			<if test="endDate != null and endDate != ''">
				AND sc.end_time &lt;= #{endDate}
			</if>
		</where>
		GROUP BY sm.id
		ORDER BY sm.start_time DESC
		limit #{pageNo},#{pageSize}
	</select>

	<!-- 根据主赛事ID查询符合条件的子赛事详情 -->
	<!-- 查询符合条件的子赛事明细 -->
	<select id="showChild" resultType="map">
		SELECT
			sc.show_code,
			sc.show_name,
			sc.start_time,
			sc.end_time,
			sm.show_location
			<!-- IFNULL(COUNT(sa.id),0) AS numOfSubShow,
			sc.id as child -->
		FROM show_child sc
		LEFT JOIN show_main sm ON sc.main_show_id = sm.id
		LEFT JOIN show_apply sa ON sc.main_show_id = sa.main_show_id
        AND sa.show_code !=''
        AND sa.payment_state = '2'
        AND sa.absence_flag = '0'
        AND sa.refund_flag = '0'
        AND sa.del_flag = '0'
		<where>
			sc.del_flag = '0'
			AND sc.main_show_id = #{showMainID}
		</where>
		GROUP BY sc.show_code
		ORDER BY sc.show_code ASC
	</select>

	<!-- 查询符合条件的子赛事明细 -->
	<select id="showChildTotal" resultType="map">
		SELECT
			COUNT(sa.id) AS numOfOrder,
			IFNULL(SUM(sa.order_price),0)  AS receivables,
			COUNT(IF(sa.absence_flag = 1,1,NULL)) AS numOfAbsence,
			COUNT(IF(sa.refund_flag = 1,1,NULL)) AS numOfRefund,
			IFNULL(SUM(IF(sa.absence_flag = 1 OR sa.refund_flag = 1,sa.order_price,0)),0) AS refund
		FROM
			show_apply as sa
		<where>
			sa.payment_state = 2
			AND sa.del_flag = 0
			<if test="showMainID !=null and showMainID !=''">
				AND sa.main_show_id = #{showMainID}
			</if>
		</where>
	</select>

	<!-- 赛场鉴定数量及金额 -->
	<select id="showAppraisal" resultType="map">
		SELECT
			COUNT(payment_amount) AS numOfAppraisal,
			IFNULL(SUM(payment_amount),0) AS feeOfAppraisal
		FROM payment_record
		<where>
			(charging_item_id = '79' OR charging_item_id = '154')
			AND LOCATE(LEFT(#{showCode},6),remarks)
			AND payment_record.del_flag = 0
		</where>
	</select>

	<!-- 查询赛事退费手续费数量及金额 -->
	<select id="showRefundHandlingFee" resultType="map">
		SELECT
			IFNULL(SUM(pr.payment_amount),0) AS 'refundHandlingFee',
			IFNULL(SUM(pr.charging_item_num),0) AS 'refundHandlingFeeCount'
		FROM payment_record pr
		LEFT JOIN show_apply sa ON SUBSTR(pr.business_id,1,LOCATE('#',pr.business_id)-1) = sa.id
		WHERE pr.payment_state = '2'
		AND pr.del_flag = '0'
		AND pr.charging_item_id = 'b196aba404ce4ff2b331d32ba1e3152f'
		<if test="showMainID !=null and showMainID !=''">
			AND sa.main_show_id = #{showMainID}
		</if>
	</select>


		<!-- 根据showcode查询比赛成绩 -->
		<select id="showResult" resultType="map">
			SELECT
				sh_r.order_code,
				sh_r.pedigree_certified,
				sh_r.result_cc,
				sh_r.result_cacib,
				sh_r.result_bob,
				sh_r.result_big,
				sh_r.result_bis,
				sh_r.show_result,
				sh_r.age_group,
				sh_r.referee,
				sh_r.race_lap
			FROM
				show_results as sh_r
			WHERE
				sh_r.show_code = #{showCode}
			AND
				sh_r.del_flag = 0
			ORDER BY
				sh_r.order_code
		</select>
		<!-- 根据pedigreeCertified查询犬只信息 -->
		<select id="showDog" resultType="Dog">
			SELECT
				*
			FROM
				dog
			WHERE
				dog.pedigree_certified_code = #{pedigreeCertified}
			AND
				dog.del_flag = 0
		</select>



	<!-- 美容报名统计 -->
		<select id="groomCountTotal" resultType="map">
			SELECT
				COUNT(DISTINCT g_s.show_code) as numOfGroom,
				COUNT(DISTINCT g_r.member_code) as numOfReg,
				sum(g_r.fee) as feeOfGroom
			FROM
				groomer_show as g_s
			LEFT JOIN
			(
				SELECT
					*
				FROM
					groomer_register
				WHERE
					groomer_register.leave_cancel = 0
				AND
					groomer_register.show_leave = 0
				AND
					groomer_register.payment_state = 2
				AND
					groomer_register.del_flag = 0
			) as g_r
			ON
				g_r.show_code=g_s.show_code
			WHERE
				g_s.del_flag = 0
			<if test="startDate != null and startDate != ''">
			AND
				g_s.start_time >= #{startDate}
			</if>
			<if test="showCode != null and showCode != ''">
			AND
				LOCATE(#{showCode},g_s.show_code)
			</if>
			<if test="endDate != null and endDate != ''">
			AND
				g_s.end_time &lt;= #{endDate}
			</if>
		</select>

		<!-- 查询符合条件同一场赛事编号 -->
		<select id = "countGroom" resultType="int">
			SELECT
				count(DISTINCT LEFT(g_s.show_code,9)) as numOfGroom
			FROM
				groomer_show as g_s
			LEFT JOIN
			(
				SELECT
					*
				FROM
					groomer_register
				WHERE
					groomer_register.leave_cancel = 0
				AND
					groomer_register.show_leave = 0
				AND
					groomer_register.payment_state = 2
				AND
					groomer_register.del_flag = 0
			) as g_r
			ON
				g_r.show_code=g_s.show_code
			WHERE
				g_s.del_flag = 0
			<if test="startDate != null and startDate != ''">
			AND
				g_s.start_time >= #{startDate}
			</if>
			<if test="showCode != null and showCode != ''">
			AND
				LOCATE(#{showCode},g_s.show_code)
			</if>
			<if test="endDate != null and endDate != ''">
			AND
				g_s.end_time &lt;= #{endDate}
			</if>

			ORDER BY g_s.start_time DESC
		</select>


		<!-- 查询符合条件在页面上显示的同一场赛事编号 -->
		<select id = "groomMainID" resultType="String">
		SELECT
			LEFT(g_s.show_code,9) as mainID
		FROM
			groomer_show as g_s
		LEFT JOIN
		(
			SELECT
				*
			FROM
				groomer_register
			WHERE
				groomer_register.leave_cancel = 0
			AND
				groomer_register.show_leave = 0
			AND
				groomer_register.payment_state = 2
			AND
				groomer_register.del_flag = 0
		) as g_r
		ON
			g_r.show_code=g_s.show_code
		WHERE
			g_s.del_flag = 0
			<if test="startDate != null and startDate != ''">
			AND
				g_s.start_time >= #{startDate}
			</if>
			<if test="showCode != null and showCode != ''">
			AND
				LOCATE(#{showCode},g_s.show_code)
			</if>
			<if test="endDate != null and endDate != ''">
			AND
				g_s.end_time &lt;= #{endDate}
			</if>
		GROUP BY LEFT(g_s.show_code,9)
		ORDER BY g_s.start_time DESC
		limit #{pageNo},#{pageSize}
		</select>
		<!-- 根据同一场赛事编号，查询相关赛事明细信息 -->
		<select id="groomChild" resultType="map">
		SELECT
			show_code,
			start_time,
			end_time,
			show_name,
			show_address,
			numOfReg
		FROM
		(
		SELECT
			g_s.id,
			g_s.show_code,
			DATE_FORMAT(g_s.start_time,'%Y-%m-%d') as start_time,
			DATE_FORMAT(g_s.end_time,'%Y-%m-%d') as end_time,
			g_s.show_name,
			g_s.show_address,
			count(DISTINCT g_r_d.member_code) as numOfReg
		FROM
			groomer_show as g_s
		LEFT JOIN
			(
				SELECT
			TRIM(SUBSTRING_INDEX(SUBSTRING_INDEX(REPLACE(g_r_d.show_code,'，',','), ',', base_num.id),',' ,-1))as show_code,
				g_r_d.member_code
		FROM
			(
				select
						 (@i:=@i+1) as id
						FROM
							show_apply,
							(select   @i:=0) as a
						LIMIT 1,20
			) AS base_num
		CROSS JOIN (
			SELECT
				*
			FROM
				groomer_register AS g_s
			WHERE
				g_s.show_code != ''
			AND
				g_s.payment_state = 2
			AND
				g_s.show_leave = 0
			AND
				g_s.leave_cancel = 0
			AND
				g_s.del_flag = 0
		) AS g_r_d
		WHERE
			base_num.id BETWEEN 1
		AND (
			SELECT
				1 + LENGTH(
					REPLACE(g_r_d.show_code,'，',',')
				) - LENGTH(
					REPLACE (
						REPLACE(g_r_d.show_code,'，',','),',',''
					)
				)
		)
		) as g_r_d
		ON
			g_s.show_code = g_r_d.show_code
		WHERE
			g_s.del_flag  = 0
		GROUP BY g_s.show_code
		) as countAll
		WHERE
			Locate(#{groomMainID},countAll.show_code)
		ORDER BY countAll.start_time DESC
		</select>

		<!-- 美容赛事根据主赛事ID，查询总计信息 -->
		<select id = "groomChildTotal" resultType="map">
		SELECT
			count(IF(g_s.payment_state = 2,1,NULL)) as numOfOrder,
			sum(g_s.fee) as receivables,
			count(IF(g_s.leave_cancel = 1,1,NULL)) as numOfAbsence,
			count(IF(g_s.show_leave = 1,1,NULL)) as numOfRefund,
			SUM(IF(g_s.show_leave = 1 or g_s.leave_cancel = 1,fee,0)) as refund
		FROM
			groomer_register as g_s
		WHERE
			LOCATE(#{groomMainID},g_s.show_code)
		AND
			g_s.del_flag = 0
		AND g_s.payment_state='2'

		</select>
		
		
		<!-- 根据同一场赛事编号，查询相关赛事明细信息 -->
		<select id="groomChildAll" resultType="com.cku.oa.statistics.entity.GroomChildAll">
		SELECT
			LEFT(show_code,9) as groomMainID,
			show_code,
			start_time,
			end_time,
			show_name,
			show_address,
			numOfReg
		FROM
		(
		SELECT
			g_s.id,
			g_s.show_code,
			DATE_FORMAT(g_s.start_time,'%Y-%m-%d') as start_time,
			DATE_FORMAT(g_s.end_time,'%Y-%m-%d') as end_time,
			g_s.show_name,
			g_s.show_address,
			count(DISTINCT g_r_d.member_code) as numOfReg
		FROM
			groomer_show as g_s
		LEFT JOIN
			(
				SELECT
			TRIM(SUBSTRING_INDEX(SUBSTRING_INDEX(REPLACE(g_r_d.show_code,'，',','), ',', base_num.id),',' ,-1))as show_code,
				g_r_d.member_code
		FROM
			(
				select
						 (@i:=@i+1) as id
						FROM
							show_apply,
							(select   @i:=0) as a
						LIMIT 1,20
			) AS base_num
		CROSS JOIN (
			SELECT
				*
			FROM
				groomer_register AS g_s
			WHERE
				g_s.show_code != ''
			AND
				g_s.payment_state = 2
			AND
				g_s.show_leave = 0
			AND
				g_s.leave_cancel = 0
			AND
				g_s.del_flag = 0
		) AS g_r_d
		WHERE
			base_num.id BETWEEN 1
		AND (
			SELECT
				1 + LENGTH(
					REPLACE(g_r_d.show_code,'，',',')
				) - LENGTH(
					REPLACE (
						REPLACE(g_r_d.show_code,'，',','),',',''
					)
				)
		)
		) as g_r_d
		ON
			g_s.show_code = g_r_d.show_code
		WHERE
			g_s.del_flag  = 0
		GROUP BY g_s.show_code
		) as countAll
		<where>
			<foreach collection="groomMainIDList" item="groomMainID" separator="  or " open="(" close=")">
			    Locate(#{groomMainID},countAll.show_code)
			</foreach>
		</where>
		ORDER BY countAll.start_time DESC
		</select>

		<!-- 美容赛事根据主赛事ID，查询总计信息 -->
		<select id = "groomChildTotalAll"  resultType ="com.cku.oa.statistics.entity.GroomChildTotalAll">
		SELECT
			LEFT(g_s.show_code,9) as groomMainID,
			count(IF(g_s.payment_state = 2,1,NULL)) as numOfOrder,
			sum(g_s.fee) as receivables,
			count(IF(g_s.leave_cancel = 1,1,NULL)) as numOfAbsence,
			count(IF(g_s.show_leave = 1,1,NULL)) as numOfRefund,
			SUM(IF(g_s.show_leave = 1 or g_s.leave_cancel = 1,fee,0)) as refund
		FROM
			groomer_register as g_s
		<where>
			<foreach collection="groomMainIDList" item="groomMainID" separator="  or " open="(" close=")">
			    LOCATE(#{groomMainID},g_s.show_code)
			</foreach>
		</where>
		AND
			g_s.del_flag = 0
		AND g_s.payment_state='2'
		GROUP BY LEFT(g_s.show_code,9)
		</select>

		<!-- 参赛美容师总数 -->
		<select id = "countGroomer" resultType = "int">
		SELECT
			count(*) as count
		FROM
			groomer_register as g_s
		WHERE
			LOCATE(#{showCode},g_s.show_code)
		AND
			g_s.del_flag = 0

		</select>

		<!-- 参赛美容师详情 -->
		<select id = "groomerView" resultType = "map">
		SELECT
			g_s.register_code,
			g_s.member_code,
			g_s.name,
			CASE WHEN s_m.gender = 'female' THEN '女' WHEN s_m.gender = 'male' THEN '男' END as gender,
			s_m.mobile,
			g_s.school_name_cn,
			g_s.show_dog_breed,
			g_s.show_code,
			g_s.show_level
		FROM
			groomer_register as g_s
		LEFT JOIN
			sys_member as s_m
		ON
			g_s.member_code = s_m.member_code
		WHERE
			LOCATE(#{showCode},g_s.show_code)
		AND
			g_s.del_flag = 0
		limit #{pageNo},#{pageSize}
		</select>

		<!-- 赛事订单总数统计 -->
		<select id = "showOrderListCount" resultType = "map">
		SELECT
			IFNULL(COUNT(*),0)as numOfShowOrder,
			SUM(IFNULL(orderCount.total,0)) as showFee
		FROM
		(
			SELECT
				orderList.total
			FROM
			(
			SELECT
				pd.order_code,
				sum(pd.total_price) as total,
				COUNT(*)
			FROM
				payment_order as po
			LEFT JOIN
				payment_order_detail as pd
			ON
				(
			po.order_code = pd.order_code
			AND pd.charging_item_id IN (
				SELECT
					id
				FROM
					payment_charging_item
				WHERE
					(`name` LIKE '%犬只参赛费%' or `name` ='单位往来')
					
				AND del_flag = 0)
			)

			WHERE
				po.del_flag = 0
			AND pd.del_flag = 0
			<if test="orderType != null and orderType == 'corp'">
				AND	LENGTH(po.order_code) =23
				AND  po.order_code LIKE '%HZ%'
			</if>
			<if test="orderType != null and orderType == 'order'">
				AND	LENGTH(po.order_code) =14
			</if>
			<if test="orderType != null and orderType == 'member'">
				AND	LENGTH(po.order_code) >14
				AND  po.order_code NOT LIKE '%HZ%'
			</if>
			<if test="paymentState != null and paymentState != ''">
				AND	po.payment_state = #{paymentState}
			</if>
			<if test="showChargeItemID != null and showChargeItemID != ''">
				AND	pd.charging_item_id = #{showChargeItemID}
			</if>
			<if test="startDate != null and startDate != ''">
				AND po.date_finance >= #{startDate}
			</if>
			<if test="endDate != null and endDate != ''">
				AND po.date_finance &lt;= #{endDate}
			</if>
			<if test="orderCode !=null and orderCode !=''">
				AND	po.order_code = #{orderCode}
			</if>
			GROUP BY
				pd.order_code
			) as orderList
			LEFT JOIN
			(
				SELECT
					DISTINCT running_number
				FROM
					show_apply
				WHERE
					del_flag = 0
				<if test="absence != null and absence != ''">
				AND
					absence_flag = #{absence}
				</if>
				<if test="refund != null and refund != ''">
				AND
					refund_flag = #{refund}
				</if>
				<if test="showCode != null and showCode != ''">
					AND
						LOCATE(#{showCode},show_apply.show_code)
				</if>


			) as s_a
			ON
				orderList.order_code = s_a.running_number
			LEFT JOIN
				payment_record as pr
			ON
				orderList.order_code = pr.running_number
			WHERE
				s_a.running_number IS not NULL
				AND
					pr.del_flag = 0
			<if test="memberCode != null and memberCode != ''">
					AND
						pr.member_code = #{memberCode}
			</if>
		) as orderCount
			<where>
				<if test="orderPrice != null and orderPrice != ''">
					AND
						orderCount.total = #{orderPrice}
				</if>
			</where>
		</select>

		<!-- 赛事订单详情统计 -->
		<select id = "showOrderList" resultType = "map">
			SELECT
				orderList.order_code as orderCode,
				pr.member_code as paymentMemberCode,
				IF(orderList.num>1,'多场比赛信息，请点击点击订单号查看详情',s_a.show_code) as showCode,
				orderList.total as orderPrice,
				orderList.payment_time as paymentTime,
				orderList.payment_state as paymentState
			FROM
			(
			SELECT
				pd.order_code,
				sum(pd.total_price) as total,
				sum(pd.num) as num,
				po.payment_time,
				po.payment_state
			FROM
				payment_order as po
			LEFT JOIN
				payment_order_detail as pd
			ON
				(
			po.order_code = pd.order_code
			AND pd.charging_item_id IN (
				SELECT
					id
				FROM
					payment_charging_item
				WHERE
					(`name` LIKE '%犬只参赛费%' or `name` ='单位往来')
				AND del_flag = 0)
			)


			WHERE
				po.del_flag = 0
			AND
				pd.del_flag = 0
			<if test="orderType != null and orderType == 'corp'">
				AND
					LENGTH(po.order_code) =23
				AND  po.order_code LIKE '%HZ%'
			</if>
			<if test="orderType != null and orderType == 'order'">
				AND
					LENGTH(po.order_code) =14
			</if>
			<if test="orderType != null and orderType == 'member'">
				AND
					LENGTH(po.order_code) >14
				AND  po.order_code NOT LIKE '%HZ%'
			</if>
			<if test="paymentState != null and paymentState != ''">
				AND
					po.payment_state = #{paymentState}
			</if>
			<if test="showChargeItemID != null and showChargeItemID != ''">
				AND
					pd.charging_item_id = #{showChargeItemID}
			</if>
			<if test="startDate != null and startDate != ''">
				AND
					po.payment_time >= #{startDate}
				</if>
			<if test="endDate != null and endDate != ''">
				AND
					po.payment_time &lt;= #{endDate}
			</if>
			<if test="orderCode !=null and orderCode !=''">
				AND po.order_code = #{orderCode}
			</if>
			GROUP BY
				pd.order_code
			) as orderList
			LEFT JOIN
			(
				SELECT
					running_number,
					show_code
				FROM
					show_apply
				WHERE
					del_flag = 0
				<if test="absence != null and absence != ''">
				AND
					absence_flag = #{absence}
				</if>
				<if test="refund != null and refund != ''">
				AND
					refund_flag = #{refund}
				</if>
				<if test="showCode != null and showCode != ''">
					AND
						LOCATE(#{showCode},show_apply.show_code)
				</if>

				GROUP BY
					running_number
			) as s_a
			ON
				orderList.order_code = s_a.running_number
			LEFT JOIN
				payment_record as pr
			ON
				orderList.order_code = pr.running_number
			WHERE
				s_a.running_number IS not NULL
				AND
					pr.del_flag =0
			<if test="memberCode != null and memberCode != ''">
					AND
						pr.member_code = #{memberCode}
			</if>
			<if test="orderPrice != null and orderPrice != ''">
					AND
						orderList.total = #{orderPrice}
				</if>
			order by orderList.payment_time
			limit #{pageNo},#{pageSize}
		</select>


	<select id = "showOrderCouponList" resultType = "map">
		SELECT
		orderList.order_code as orderCode,
		pr.member_code as paymentMemberCode,
		IF(orderList.num>1,'多场比赛信息，请点击点击订单号查看详情',s_a.show_code) as showCode,
		orderList.total as orderPrice,
		orderList.payment_time as paymentTime,
		orderList.payment_state as paymentState
		FROM
		(
		SELECT
		pd.order_code,
		sum(pd.total_price) as total,
		sum(pd.num) as num,
		po.payment_time,
		po.payment_state
		FROM
		payment_order as po
		LEFT JOIN
		payment_order_detail as pd
		ON
		(
		po.order_code = pd.order_code
		AND pd.charging_item_id IN (
		SELECT
		id
		FROM
		payment_charging_item
		WHERE
		(`name` LIKE '%CAC赛事免费参赛券%' or `name` LIKE '%CAC犬展免费参展券%')
		AND del_flag = 0)
		)


		WHERE
		po.del_flag = 0
		AND
		pd.del_flag = 0
		<if test="orderType != null and orderType == 'corp'">
			AND
			LENGTH(po.order_code) =23
			AND  po.order_code LIKE '%HZ%'
		</if>
		<if test="orderType != null and orderType == 'order'">
			AND
			LENGTH(po.order_code) =14
		</if>
		<if test="orderType != null and orderType == 'member'">
			AND
			LENGTH(po.order_code) >14
			AND  po.order_code NOT LIKE '%HZ%'
		</if>
		<if test="paymentState != null and paymentState != ''">
			AND
			po.payment_state = #{paymentState}
		</if>
		<if test="showChargeItemID != null and showChargeItemID != ''">
			AND
			pd.charging_item_id = #{showChargeItemID}
		</if>
		<if test="startDate != null and startDate != ''">
			AND
			po.payment_time >= #{startDate}
		</if>
		<if test="endDate != null and endDate != ''">
			AND
			po.payment_time &lt;= #{endDate}
		</if>
		<if test="orderCode !=null and orderCode !=''">
			AND po.order_code = #{orderCode}
		</if>
		GROUP BY
		pd.order_code
		) as orderList
		LEFT JOIN
		(
		SELECT
		running_number,
		show_code
		FROM
		show_apply
		WHERE
		del_flag = 0
		<if test="absence != null and absence != ''">
			AND
			absence_flag = #{absence}
		</if>
		<if test="refund != null and refund != ''">
			AND
			refund_flag = #{refund}
		</if>
		<if test="showCode != null and showCode != ''">
			AND
			LOCATE(#{showCode},show_apply.show_code)
		</if>

		GROUP BY
		running_number
		) as s_a
		ON
		orderList.order_code = s_a.running_number
		LEFT JOIN
		payment_record as pr
		ON
		orderList.order_code = pr.running_number
		WHERE
		s_a.running_number IS not NULL
		AND
		pr.del_flag =0
		<if test="memberCode != null and memberCode != ''">
			AND
			pr.member_code = #{memberCode}
		</if>
		<if test="orderPrice != null and orderPrice != ''">
			AND
			orderList.total = #{orderPrice}
		</if>
		order by orderList.payment_time
		limit #{pageNo},#{pageSize}
	</select>

		<!-- 活动赛事收费项目 -->
		<select id = "showChargeItem" resultType="map">
			SELECT
				id,
				name
			FROM
				payment_charging_item as p
			WHERE
-- 				( or `name` LIKE '%CAC赛事免费参赛券%' )
				(`name` LIKE '%犬只参赛费%' or `name` ='单位往来')
				
			AND
				del_flag = 0
			ORDER BY `name`
		</select>

	<select id = "countHandle" resultType="int">
			SELECT
				count(DISTINCT LEFT(h_s.show_code,9)) as numOfHandle
			FROM
				handler_show as h_s
			LEFT JOIN
			(
				SELECT
					*
				FROM
					handler_register
				WHERE
					handler_register.leave_cancel = 0
				AND
					handler_register.show_leave = 0
				AND
					handler_register.payment_state = 2
				AND
					handler_register.del_flag = 0
			) as h_r
			ON
				h_r.show_code=h_s.show_code
			WHERE
				h_s.del_flag = 0
			<if test="startDate != null and startDate != ''">
			AND
				h_s.start_time >= #{startDate}
			</if>
			<if test="showCode != null and showCode != ''">
			AND
				LOCATE(#{showCode},h_s.show_code)
			</if>
			<if test="endDate != null and endDate != ''">
			AND
				h_s.end_time &lt;= #{endDate}
			</if>

			ORDER BY h_s.start_time DESC
		</select>

	<select id="handleCountTotal" resultType="map">
			SELECT
				COUNT(DISTINCT h_s.show_code) as numOfHandle,
				COUNT(DISTINCT h_r.member_code) as numOfReg,
				sum(h_r.fee) as feeOfHandle
			FROM
				handler_show as h_s
			LEFT JOIN
			(
				SELECT
					*
				FROM
					handler_register
				WHERE
					handler_register.leave_cancel = 0
				AND
					(handler_register.show_leave = 0 OR handler_register.show_leave = 1)
				AND
					handler_register.payment_state = 2
				AND
					handler_register.del_flag = 0
			) as h_r
			ON
				h_r.show_code=h_s.show_code
			WHERE
				h_s.del_flag = 0
			<if test="startDate != null and startDate != ''">
			AND
				h_s.start_time >= #{startDate}
			</if>
			<if test="showCode != null and showCode != ''">
			AND
				LOCATE(#{showCode},h_s.show_code)
			</if>
			<if test="endDate != null and endDate != ''">
			AND
				h_s.end_time &lt;= #{endDate}
			</if>
		</select>

	<select id = "handleMainID" resultType="String">
		SELECT
			LEFT(h_s.show_code,9) as mainID
		FROM
			handler_show as h_s
		LEFT JOIN
		(
			SELECT
				*
			FROM
				handler_register
			WHERE
				handler_register.leave_cancel = 0
			AND
				handler_register.show_leave = 0
			AND
				handler_register.payment_state = 2
			AND
				handler_register.del_flag = 0
		) as h_r
		ON
			h_r.show_code=h_s.show_code
		WHERE
			h_s.del_flag = 0
			<if test="startDate != null and startDate != ''">
			AND
				h_s.start_time >= #{startDate}
			</if>
			<if test="showCode != null and showCode != ''">
			AND
				LOCATE(#{showCode},h_s.show_code)
			</if>
			<if test="endDate != null and endDate != ''">
			AND
				h_s.end_time &lt;= #{endDate}
			</if>
		GROUP BY LEFT(h_s.show_code,9)
		ORDER BY h_s.start_time DESC
		limit #{pageNo},#{pageSize}
		</select>

	<select id="handleChild" resultType="map">
			SELECT
				show_code,
				start_time,
				end_time,
				show_name,
				show_address,
				numOfReg
			FROM
			(
			SELECT
				g_s.id,
				g_s.show_code,
				DATE_FORMAT(g_s.start_time,'%Y-%m-%d') as start_time,
				DATE_FORMAT(g_s.end_time,'%Y-%m-%d') as end_time,
				g_s.show_name,
				g_s.show_address,
				count(DISTINCT g_r_d.member_code) as numOfReg
			FROM
				handler_show as g_s
			LEFT JOIN
				(
					SELECT
				TRIM(SUBSTRING_INDEX(SUBSTRING_INDEX(REPLACE(g_r_d.show_code,'，',','), ',', base_num.id),',' ,-1))as show_code,
					g_r_d.member_code
			FROM
				(
					select
							 (@i:=@i+1) as id
							FROM
								show_apply,
								(select   @i:=0) as a
							LIMIT 1,20
				) AS base_num
			CROSS JOIN (
				SELECT
					*
				FROM
					handler_register AS g_s
				WHERE
					g_s.show_code != ''
				AND
					g_s.payment_state = 2
				AND
					(g_s.show_leave = 0 OR g_s.show_leave = 1)
				AND
					g_s.leave_cancel = 0
				AND
					g_s.del_flag = 0
			) AS g_r_d
			WHERE
				base_num.id BETWEEN 1
			AND (
				SELECT
					1 + LENGTH(
						REPLACE(g_r_d.show_code,'，',',')
					) - LENGTH(
						REPLACE (
							REPLACE(g_r_d.show_code,'，',','),',',''
						)
					)
			)
			) as g_r_d
			ON
				g_s.show_code = g_r_d.show_code
			WHERE
				g_s.del_flag  = 0
			GROUP BY g_s.show_code
			) as countAll
			WHERE
				Locate(#{handleMainID},countAll.show_code)
			ORDER BY countAll.start_time DESC
		</select>


		<select id = "handleChildTotal" resultType="map">
		SELECT
			count(IF(g_s.payment_state = 2,1,NULL)) as numOfOrder,
			sum(g_s.fee) as receivables,
			count(IF(g_s.leave_cancel = 1,1,NULL)) as numOfAbsence,
			count(IF(g_s.show_leave = 2,1,NULL)) as numOfRefund,
			SUM(IF(g_s.show_leave = 2 or g_s.leave_cancel = 1,fee,0)) as refund
		FROM
			handler_register as g_s
		WHERE
			LOCATE(#{handleMainID},g_s.show_code)
		AND
			g_s.del_flag = 0
		AND g_s.payment_state='2'
		</select>
		
		
		<!-- 根据同一场赛事编号，查询相关赛事明细信息 -->
		<select id="handleChildAll" resultType="com.cku.oa.statistics.entity.HandleChildAll">
			SELECT
				LEFT(show_code,9) as handleMainID,
				show_code,
				start_time,
				end_time,
				show_name,
				show_address,
				numOfReg
			FROM
			(
			SELECT
				g_s.id,
				g_s.show_code,
				DATE_FORMAT(g_s.start_time,'%Y-%m-%d') as start_time,
				DATE_FORMAT(g_s.end_time,'%Y-%m-%d') as end_time,
				g_s.show_name,
				g_s.show_address,
				count(DISTINCT g_r_d.member_code) as numOfReg
			FROM
				handler_show as g_s
			LEFT JOIN
				(
					SELECT
				TRIM(SUBSTRING_INDEX(SUBSTRING_INDEX(REPLACE(g_r_d.show_code,'，',','), ',', base_num.id),',' ,-1))as show_code,
					g_r_d.member_code
			FROM
				(
					select
							 (@i:=@i+1) as id
							FROM
								show_apply,
								(select   @i:=0) as a
							LIMIT 1,20
				) AS base_num
			CROSS JOIN (
				SELECT
					*
				FROM
					handler_register AS g_s
				WHERE
					g_s.show_code != ''
				AND
					g_s.payment_state = 2
				AND
					(g_s.show_leave = 0 OR g_s.show_leave = 1)
				AND
					g_s.leave_cancel = 0
				AND
					g_s.del_flag = 0
			) AS g_r_d
			WHERE
				base_num.id BETWEEN 1
			AND (
				SELECT
					1 + LENGTH(
						REPLACE(g_r_d.show_code,'，',',')
					) - LENGTH(
						REPLACE (
							REPLACE(g_r_d.show_code,'，',','),',',''
						)
					)
			)
			) as g_r_d
			ON
				g_s.show_code = g_r_d.show_code
			WHERE
				g_s.del_flag  = 0
			GROUP BY g_s.show_code
			) as countAll
			<where>
				<foreach collection="handleMainIDList" item="handleMainID" separator="  or " open="(" close=")">
				    Locate(#{handleMainID},countAll.show_code)
				</foreach>
			</where>
			ORDER BY countAll.start_time DESC
		</select>

		<select id = "handleChildTotalAll"  resultType ="com.cku.oa.statistics.entity.HandleChildTotalAll">
			SELECT
				LEFT(g_s.show_code,9) as handleMainID,
				count(IF(g_s.payment_state = 2,1,NULL)) as numOfOrder,
				sum(g_s.fee) as receivables,
				count(IF(g_s.leave_cancel = 1,1,NULL)) as numOfAbsence,
				count(IF(g_s.show_leave = 2,1,NULL)) as numOfRefund,
				SUM(IF(g_s.show_leave = 2 or g_s.leave_cancel = 1,fee,0)) as refund
			FROM
				handler_register as g_s
			<where>
				<foreach collection="handleMainIDList" item="handleMainID" separator="  or " open="(" close=")">
				    LOCATE(#{handleMainID},g_s.show_code)
				</foreach>
				AND
					g_s.del_flag = 0
				AND g_s.payment_state='2'
			</where>
			GROUP BY LEFT(g_s.show_code,9)
		</select>
		
	<select id = "countHandleMember" resultType = "int">
		SELECT
			count(*) as count
		FROM
			handler_register as h_s
		WHERE
			LOCATE(#{showCode},h_s.show_code)
		AND
			h_s.del_flag = 0

		</select>

		<!-- 参赛美容师详情 -->
		<select id = "handleMemberView" resultType = "map">
		SELECT
			h_s.register_code,
			h_s.member_code,
			h_s.name,
			CASE WHEN s_m.gender = 'female' THEN '女' WHEN s_m.gender = 'male' THEN '男' END as gender,
			s_m.mobile,
			h_s.school_name_cn,
-- 			h_s.show_dog_breed,
			h_s.show_code,
			h_s.show_level
		FROM
			handler_register as h_s
		LEFT JOIN
			sys_member as s_m
		ON
			h_s.member_code = s_m.member_code
		WHERE
			LOCATE(#{showCode},h_s.show_code)
		AND
			h_s.del_flag = 0
		limit #{pageNo},#{pageSize}
		</select>

</mapper>