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

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

        select count(*)
        from finance_transfer
        left join custom_info on finance_transfer.custom_id=custom_info.id
        <if test="stamp != null">
            join user_info on custom_info.agent_id = user_info.id
        </if>
        <where>
            <if test="stamp != null">
                and user_info.stamp like binary concat(#{stamp},'%')
            </if>
            <if test="cId != null">
                and finance_transfer.c_id =#{cId}
            </if>
            <if test="serial != null and serial != ''">
                and serial=#{serial}
            </if>
            <if test="customId != null">
                and custom_id=#{customId}
            </if>
            <if test="fromLogin != null and fromLogin > 0">
                and withdraw_login=#{fromLogin}
            </if>
            <if test="toLogin != null and toLogin > 0">
                and deposit_login=#{toLogin}
            </if>
            <if test="status != null and status != ''">
                <if test="status==0">
                    and finance_transfer.status=1
                </if>
                <if test="status==1">
                    and finance_transfer.status=2 and (finance_transfer.withdraw_status in(0,1) or
                    finance_transfer.deposit_status in(0,1) )
                </if>
                <if test="status==2">
                    and finance_transfer.status=2 and finance_transfer.withdraw_status in(2) and
                    finance_transfer.deposit_status in(2)
                </if>
                <if test="status==3">
                    and (finance_transfer.status=3 or finance_transfer.withdraw_status=3 or
                    finance_transfer.deposit_status=3)
                </if>
            </if>
            <if test="startDate != null">
                and finance_transfer.add_time <![CDATA[ >= ]]> #{startDate}
            </if>
            <if test="endDate != null">
                and finance_transfer.add_time <![CDATA[ < ]]> #{endDate}
            </if>
        </where>

    </select>

    <select id="searchList" resultType="com.crm.rely.backend.model.pojo.view.FinanceTransferView"
            parameterType="com.crm.rely.backend.model.entity.finance.transfer.FinanceTransferSearchEntity">

        select finance_transfer.*,
        agent.ib_no as agent_ib_no,
        sales.ib_no sales_ib_no
        from finance_transfer
        join custom_info custom on finance_transfer.custom_id = custom.id
        left join user_info agent on agent.id=custom.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_transfer.c_id =#{cId}
            </if>
            <if test="serial != null and serial != ''">
                and finance_transfer.serial=#{serial}
            </if>
            <if test="customId != null">
                and finance_transfer.custom_id=#{customId}
            </if>
            <if test="fromLogin != null and fromLogin > 0">
                and finance_transfer.withdraw_login=#{fromLogin}
            </if>
            <if test="toLogin != null and toLogin > 0">
                and finance_transfer.deposit_login=#{toLogin}
            </if>
            <if test="status != null  and status != ''">
                <if test="status==0">
                    and finance_transfer.status=1
                </if>
                <if test="status==1">
                    and finance_transfer.status=2 and (finance_transfer.withdraw_status in(0,1) or
                    finance_transfer.deposit_status in(0,1) )
                </if>
                <if test="status==2">
                    and finance_transfer.status=2 and finance_transfer.withdraw_status in(2) and
                    finance_transfer.deposit_status in(2)
                </if>
                <if test="status==3">
                    and (finance_transfer.status=3 or finance_transfer.withdraw_status=3 or
                    finance_transfer.deposit_status=3)
                </if>
            </if>
            <if test="startDate != null">
                and finance_transfer.add_time <![CDATA[ >= ]]> #{startDate}
            </if>
            <if test="endDate != null">
                and finance_transfer.add_time <![CDATA[ < ]]> #{endDate}
            </if>
        </where>
        order by finance_transfer.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.FinanceTransferView">

        select finance_transfer.*,
               agent.ib_no as agent_ib_no,
               sales.ib_no    sales_ib_no
        from finance_transfer
                 join custom_info custom on finance_transfer.custom_id = custom.id
                 left join user_info agent on agent.id = custom.agent_id
                 left join user_info sales on agent.sales_id = sales.id
        where finance_transfer.id = #{id}
    </select>
</mapper>