<?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.SalesVolumeCommissionMapper">

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

    <select id="sumSearch" parameterType="com.crm.rely.backend.core.entity.sales.SalesVolumeCommissionListEntity" resultType="com.crm.rely.backend.core.pojo.view.SalesVolumeCommissionView">
        select sum(svc.volume) volume,sum(svc.team_volume) team_volume,sum(svc.personal_commission) personal_commission,sum(svc.team_commission) team_commission
        from sales_volume_commission_${ym} svc
        inner join user_info leader on svc.sales_leader_id = leader.id
        <include refid="whereSql"></include>
    </select>

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

    <sql id="whereSql" >
        <where>
            <if test="salesNo != null and salesNo != ''">
                and svc.sales_no like concat('%',#{salesNo},'%')
            </if>
            <if test="salesType == 1">
                and svc.sales_id = #{salesId}
            </if>
            <if test="salesType == 2">
                and svc.sales_leader_id = #{salesId}
                <if test="reportType == 1">
                    and svc.sales_id = #{salesId}
                </if>
                <if test="reportType == 2">
                    and svc.sales_id != #{salesId}
                </if>
            </if>
        </where>
    </sql>
</mapper>