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

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

        select count(*)
        from finance_withdraw
        <include refid="selectCondition">
        </include>
    </select>

    <select id="pageList" resultType="com.crm.rely.backend.model.pojo.view.FinanceWithdrawView"
            parameterType="com.crm.rely.backend.model.entity.finance.withdraw.FinanceWithdrawSearchListEntity">

        select finance_withdraw.*
        from finance_withdraw
        <include refid="selectCondition">
        </include>
        order by finance_withdraw.add_time desc
        <if test="page != null">
            limit #{page.offset},#{page.row}
        </if>
    </select>

    <select id="listByIds" resultType="com.crm.rely.backend.model.pojo.view.FinanceWithdrawView">
        select finance_withdraw.* ,
        agent.ib_no as p_ib_no,
        ifnull(sales2.ib_no,sales.ib_no) sales_no
        from finance_withdraw
        left join user_info agent on agent.id=finance_withdraw.agent_id
        left join user_info sales on agent.sales_id=sales.id
        left join user_info sales2 on finance_withdraw.sales_id = sales2.id
        where finance_withdraw.id in
        <foreach collection="ids" index="idx" item="item" separator="," open="(" close=")">
            #{item}
        </foreach>

        <if test="stamp != null and stamp!=''">
            and agent.stamp like concat(#{stamp},"%")
        </if>
        order by finance_withdraw.add_time desc
    </select>

    <sql id="selectCondition">
        <where>
            <if test="salesStatus != null">
                and finance_withdraw.sales_status =#{salesStatus}
            </if>
            <if test="channelCode != null and channelCode!=''">
                and finance_withdraw.remit_channel_code =#{channelCode}
            </if>
            <if test="cId != null">
                and finance_withdraw.c_id =#{cId}
            </if>
            <if test="customId != null ">
                and finance_withdraw.custom_id=#{customId}
            </if>
            <if test="platform != null and platform != ''">
                and finance_withdraw.platform=#{platform}
            </if>
            <if test="channelCode != null and channelCode != ''">
                and finance_withdraw.remit_channel_code=#{channelCode}
            </if>
            <if test="channelCodes != null">
                and finance_withdraw.remit_channel_code in
                <foreach collection="channelCodes" index="idx" item="item" separator="," open="(" close=")">
                    #{item}
                </foreach>
            </if>
            <if test="loginType != null ">
                and finance_withdraw.login_type=#{loginType}
            </if>
            <if test="login != null ">
                and finance_withdraw.login=#{login}
            </if>
            <if test="serial != null and serial != ''">
                and finance_withdraw.serial=#{serial}
            </if>
            <if test="country != null and country != ''">
                and finance_withdraw.country=#{country}
            </if>
            <if test="status != null ">
                <if test="status==0">
                    and finance_withdraw.status=1
                </if>
                <if test="status==1">
                    and (finance_withdraw.status=2 )
                    <if test="mtStatus==null or (mtStatus != 1 and mtStatus!=0)  ">
                        and (finance_withdraw.withdraw_status in(0,1) or
                        (finance_withdraw.withdraw_status=2 and finance_withdraw.callback_status=0))
                    </if>
                    <if test="mtStatus == 0 ">
                        and finance_withdraw.withdraw_status in(0,1)
                    </if>
                    <if test="mtStatus == 1 ">
                        and (finance_withdraw.withdraw_status=2 and finance_withdraw.callback_status=0)
                    </if>
                </if>
                <if test="status==2">
                    and (finance_withdraw.status=2 and finance_withdraw.withdraw_status in(2) and
                    finance_withdraw.callback_status in(1))
                </if>
                <if test="status==3">
                    and (finance_withdraw.status=3 or finance_withdraw.withdraw_status=3 or
                    finance_withdraw.callback_status>=2 or finance_withdraw.submit_status=3)
                </if>
                <if test="status==5">
                    and (finance_withdraw.status=5)
                </if>
            </if>
            <if test="submitStatus != null ">
                and (finance_withdraw.submit_status=#{submitStatus})
            </if>
            <if test="backstageStatus != null ">
                and (finance_withdraw.backstage_status=#{backstageStatus})
            </if>
            <if test="infoStatus != null ">
                and (finance_withdraw.info_status=#{infoStatus})
            </if>
            <if test="startDate != null">
                and finance_withdraw.add_time <![CDATA[ >= ]]> #{startDate}
            </if>
            <if test="endDate != null">
                and finance_withdraw.add_time <![CDATA[ < ]]> #{endDate}
            </if>
            <if test="startSubmitDate != null">
                and finance_withdraw.submit_time <![CDATA[ >= ]]> #{startSubmitDate}
            </if>
            <if test="endSubmitDate != null">
                and finance_withdraw.submit_time <![CDATA[ < ]]> #{endSubmitDate}
            </if>
            <if test="name != null and name!=''">
                and finance_withdraw.name like concat(#{name},'%')
            </if>
        </where>
    </sql>


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

        select finance_withdraw.*,
               agent.ib_no as p_ib_no,
               sales.ib_no    sales_ib_no
        from finance_withdraw
                 left join user_info agent on agent.id = finance_withdraw.agent_id
                 left join user_info sales on agent.sales_id = sales.id
        where finance_withdraw.id = #{id}
    </select>

    <select id="getWithdrawAmountThatDayByCustomIdAndPayType" resultType="java.math.BigDecimal">
        SELECT ifnull(sum(amount),0)
        from finance_withdraw
        where custom_id = #{customId}
          and pay_type = #{payType}
          AND (add_time <![CDATA[ >= ]]> CURDATE() AND add_time <![CDATA[ < ]]> DATE_ADD(CURDATE(), INTERVAL 1 DAY))
          and status != 3 and status != 5
    </select>

    <select id="getCountByDateAndCId" resultType="java.lang.Integer">
        select count(*)
        from finance_withdraw
        where finance_withdraw.add_time <![CDATA[ >= ]]> #{startDate}
        and finance_withdraw.add_time <![CDATA[ < ]]> #{endDate}
        and finance_withdraw.c_id = #{cId}
        and finance_withdraw.status not in (3, 5)
        <if test="feeReduction != null ">
            and finance_withdraw.fee_reduction = 1
        </if>
    </select>

</mapper>