<?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.crm.manager.dao.mapper.SalesDwCommissionMapper">


    <select id="countSearch" parameterType="com.crm.rely.backend.core.entity.sales.SalesDwCommissionListEntity" resultType="Integer">
        select count(sdc.id)
        from sales_dw_commission_${ym} sdc
        inner join user_info leader on sdc.sales_leader_id = leader.id
        <include refid="whereSql"></include>
    </select>

    <select id="listSearch" parameterType="com.crm.rely.backend.core.entity.sales.SalesDwCommissionListEntity" resultType="com.crm.rely.backend.core.pojo.view.SalesDwCommissionView">
        select sdc.*,sales_no,leader.ib_no sales_leader_no
        from sales_dw_commission_${ym} sdc
        inner join user_info leader on sdc.sales_leader_id = leader.id
        <include refid="whereSql"></include>
        order by sdc.close_time desc
        <if test="page != null">
            limit #{page.offset},#{page.row}
        </if>
    </select>

    <select id="sumSearch" parameterType="com.crm.rely.backend.core.entity.sales.SalesDwCommissionListEntity" resultType="com.crm.rely.backend.core.pojo.view.SalesDwCommissionView">
        select sum(sdc.amount) amount
        from sales_dw_commission_${ym} sdc
        inner join user_info leader on sdc.sales_leader_id = leader.id
        <include refid="whereSql"></include>
    </select>

    <sql id="whereSql" >
        <where>
            <if test="dwType == 2">
                and sdc.dw_type=0
            </if>
            <if test="dwType == 1">
                and sdc.dw_type <![CDATA[ > ]]> 0
            </if>
            <if test="salesNo != null and salesNo != ''">
                and sdc.sales_no like concat('%',#{salesNo},'%')
            </if>
            <if test="salesType == 1">
                and sdc.sales_id = #{salesId}
            </if>
            <if test="salesType == 2">
                and sdc.sales_leader_id = #{salesId}
                <if test="reportType == 1 or reportType == 3">
                    and sdc.sales_id != #{salesId}
                </if>
                <if test="reportType == 2">
                    and sdc.sales_id = #{salesId}
                </if>
            </if>
            <if test="salesType == 0">
                and sdc.sales_id_stamp like concat(#{salesIdStamp},'%')
            </if>
        </where>
    </sql>
</mapper>