<?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.pay.dao.mapper.FinanceTrainMapper">

    <select id="countList" resultType="integer"
            parameterType="com.crm.rely.backend.model.entity.finance.train.FinanceTrainSearchListEntity">

        select count(*)
        from finance_train finance
        <if test="stamp != null">
            left join user_info agent on finance.agent_id = agent.id
        </if>
        <where>
            <if test="stamp != null">
                and agent.stamp like binary concat(#{stamp},'%')
            </if>
            <if test="cId != null">
                and finance.c_id =#{cId}
            </if>
            <if test="customId != null ">
                and finance.custom_id=#{customId}
            </if>
            <if test="login != null ">
                and finance.login=#{login}
            </if>
            <if test="serial != null and serial != ''">
                and finance.serial=#{serial}
            </if>
            <if test="status != null ">
                and finance.`status`=#{status}
            </if>
            <if test="startDate != null">
                and finance.add_time <![CDATA[ >= ]]> #{startDate}
            </if>
            <if test="endDate != null">
                and finance.add_time <![CDATA[ < ]]> #{endDate}
            </if>
        </where>

    </select>

    <select id="searchList" resultType="com.crm.rely.backend.model.pojo.view.FinanceTrainView"
            parameterType="com.crm.rely.backend.model.entity.finance.train.FinanceTrainSearchListEntity">

        select finance.* ,
        agent.ib_no as agent_ib_no,
        sales.ib_no sales_ib_no
        from finance_train finance
        left join user_info agent on agent.id=finance.agent_id
        left join user_info sales on agent.sales_id=sales.id
        <where>
            <if test="stamp != null">
                and agent.stamp like binary concat(#{stamp},'%')
            </if>
            <if test="cId != null">
                and finance.c_id =#{cId}
            </if>
            <if test="customId != null ">
                and finance.custom_id=#{customId}
            </if>
            <if test="login != null ">
                and finance.login=#{login}
            </if>
            <if test="serial != null and serial != ''">
                and finance.serial=#{serial}
            </if>
            <if test="status != null ">
                and finance.`status`=#{status}
            </if>
            <if test="startDate != null">
                and finance.add_time <![CDATA[ >= ]]> #{startDate}
            </if>
            <if test="endDate != null">
                and finance.add_time <![CDATA[ < ]]> #{endDate}
            </if>
        </where>
        order by finance.add_time desc
        <if test="page != null">
            limit #{page.offset},#{page.row}
        </if>

    </select>
    <select id="getOne" resultType="com.crm.rely.backend.model.pojo.view.FinanceTrainView">

        select finance.*,
               agent.ib_no as agent_ib_no
        from finance_train finance
                 left join user_info agent on agent.id = finance.agent_id
        where finance.id = #{id}
    </select>


    <select id="countReport"
            parameterType="com.crm.rely.backend.model.entity.finance.train.FinanceTrainSearchListEntity"
            resultType="Integer">
        select count(train.id)
        from finance_train train
        left join custom_info ci on train.custom_id = ci.id
        left join user_info agent on ci.agent_id = agent.id
        left join user_info sales on agent.sales_id = sales.id
        <where>
            and train.`status` = 2
            <if test="stamp != null and stamp != ''">
                and agent.stamp like binary concat(#{stamp},'%')
            </if>
            <if test="ibNo != null and ibNo != ''">
                and agent.ib_no like concat('%',#{ibNo},'%')
            </if>
            <if test="salesNo != null and salesNo != ''">
                and sales.ib_no like concat('%',#{salesNo},'%')
            </if>
            <if test="startDate != null">
                and train.add_time <![CDATA[ >= ]]> #{startDate}
            </if>
            <if test="endDate != null">
                and train.add_time <![CDATA[ < ]]> #{endDate}
            </if>
        </where>
    </select>


    <select id="listReport"
            parameterType="com.crm.rely.backend.model.entity.finance.train.FinanceTrainSearchListEntity"
            resultType="com.crm.rely.backend.model.pojo.view.FinanceTrainView">
        select train.*,agent.ib_no agent_ib_no,sales.ib_no sales_ib_no,
        ci.first_name,ci.middle,ci.last_name,ci.country,sales.`name` sales_name
        from finance_train train
        left join custom_info ci on train.custom_id = ci.id
        left join user_info agent on ci.agent_id = agent.id
        left join user_info sales on agent.sales_id = sales.id
        <where>
            and train.`status` = 2
            <if test="stamp != null and stamp != ''">
                and agent.stamp like binary concat(#{stamp},'%')
            </if>
            <if test="ibNo != null and ibNo != ''">
                and agent.ib_no like concat('%',#{ibNo},'%')
            </if>
            <if test="salesNo != null and salesNo != ''">
                and sales.ib_no like concat('%',#{salesNo},'%')
            </if>
            <if test="startDate != null">
                and train.add_time <![CDATA[ >= ]]> #{startDate}
            </if>
            <if test="endDate != null">
                and train.add_time <![CDATA[ < ]]> #{endDate}
            </if>
        </where>
        order by train.add_time desc
        <if test="page != null">
            limit #{page.offset},#{page.row}
        </if>
    </select>

    <select id="sumReport"
            parameterType="com.crm.rely.backend.model.entity.finance.train.FinanceTrainSearchListEntity"
            resultType="com.crm.rely.backend.model.pojo.view.FinanceTrainView">
        select sum(train.amount) amount
        from finance_train train
        left join custom_info ci on train.custom_id = ci.id
        left join user_info agent on ci.agent_id = agent.id
        left join user_info sales on agent.sales_id = sales.id
        <where>
            and train.`status` = 2
            <if test="stamp != null and stamp != ''">
                and agent.stamp like binary concat(#{stamp},'%')
            </if>
            <if test="ibNo != null and ibNo != ''">
                and agent.ib_no like concat('%',#{ibNo},'%')
            </if>
            <if test="salesNo != null and salesNo != ''">
                and sales.ib_no like concat('%',#{salesNo},'%')
            </if>
            <if test="startDate != null">
                and train.add_time <![CDATA[ >= ]]> #{startDate}
            </if>
            <if test="endDate != null">
                and train.add_time <![CDATA[ < ]]> #{endDate}
            </if>
        </where>
    </select>

</mapper>