<?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.qs.mp.channel.mapper.ChannelCommissionMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.qs.mp.channel.domain.ChannelCommission">
        <id column="id" property="id" />
        <result column="channel_id" property="channelId" />
        <result column="order_id" property="orderId" />
        <result column="box_id" property="boxId" />
        <result column="sale_amt" property="saleAmt" />
        <result column="sale_comm_rate" property="saleCommRate" />
        <result column="sale_comm_amt" property="saleCommAmt" />
        <result column="comm_rate" property="commRate" />
        <result column="comm_amt" property="commAmt" />
        <result column="created_time" property="createdTime" />
        <result column="updated_time" property="updatedTime" />
    </resultMap>

    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        id, channel_id, order_id, box_id, sale_amt, sale_comm_rate, sale_comm_amt, comm_rate, comm_amt, created_time, updated_time
    </sql>

    <!-- 查询子渠道一定时间范围内的佣金金额 -->
    <select id="getChannelCommAmtCnt" resultType="integer">
		select IFNULL(SUM(t1.comm_amt),0) as commAmt
		from mp_channel_commission t1
		left join mp_channel t2 on t1.channel_id = t2.channel_id
		where (t2.channel_no like concat(#{channelNo},'.%') or t2.channel_no = #{channelNo})
		AND t1.created_time > DATE_SUB(CURDATE(),INTERVAL #{days} DAY)
	</select>


    <!-- 查询子渠道一定时间范围内的销售金额 -->
    <select id="getChannelSaleAmtCnt" resultType="integer">
		select IFNULL(SUM(t3.sale_amt),0) as saleAmt
		from (
			select t1.order_id, MIN(t1.sale_amt) sale_amt
			from mp_channel_commission t1
			left join mp_channel t2 on t1.channel_id = t2.channel_id
			where (t2.channel_no like concat(#{channelNo},'.%') or t2.channel_no = #{channelNo})
			AND t1.created_time > DATE_SUB(CURDATE(),INTERVAL #{days} DAY)
			GROUP BY t1.order_id
		) t3
	</select>


	  <!-- 查询子渠道全部的佣金金额 -->
    <select id="getChannelTotalCommAmtCnt" resultType="integer">
		select IFNULL(SUM(t1.comm_amt),0) as commAmt
		from mp_channel_commission t1
		left join mp_channel t2 on t1.channel_id = t2.channel_id
		where (t2.channel_no like concat(#{channelNo},'.%') or t2.channel_no = #{channelNo})
	</select>


    <!-- 查询子渠道全部的销售金额 -->
    <select id="getChannelTotalSaleAmtCnt" resultType="integer">
		select IFNULL(SUM(t3.sale_amt),0) as saleAmt
		from (
			select t1.order_id, MIN(t1.sale_amt) sale_amt
			from mp_channel_commission t1
			left join mp_channel t2 on t1.channel_id = t2.channel_id
			where (t2.channel_no like concat(#{channelNo},'.%') or t2.channel_no = #{channelNo})
			GROUP BY t1.order_id
		) t3
	</select>

		<!-- 查询有佣金收入的渠道列表 -->
		<select id="getChannelList" resultMap="BaseResultMap">
		select distinct t1.channel_id
		from mp_channel_commission t1
					 left join mp_channel t2 on t1.channel_id = t2.channel_id
			${ew.customSqlSegment}
	</select>

</mapper>
