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

    <select id="countList" resultType="integer"
            parameterType="com.crm.rely.backend.core.entity.account.leverage.AccountLeverageChangeSearchEntity">

        select count(*)
        from account_leverage_change
        <if test="stamp != null or groupId!=null">
            JOIN `custom_info` ON `account_leverage_change`.`custom_id` = `custom_info`.`id`
            left JOIN `user_info` agent ON `custom_info`.`agent_id` = agent.`id`
        </if>
        <where>
            <if test="stamp != null">
                and agent.stamp like binary concat(#{stamp},'%')
            </if>
            <if test="groupId != null and groupId != -1">
                and agent.group_id = #{groupId}
            </if>
            <if test="status != null ">
                <if test="status==0">
                    and account_leverage_change.status=1
                </if>
                <if test="status==1">
                    and (account_leverage_change.status=2 and account_leverage_change.leverage_status in(0,1))
                </if>
                <if test="status==2">
                    and (account_leverage_change.status=2 and account_leverage_change.leverage_status =2)
                </if>
                <if test="status==3">
                    and (account_leverage_change.status=3 or account_leverage_change.leverage_status=3)
                </if>
            </if>
            <if test="customId != null ">
                and custom_id=#{customId}
            </if>
            <if test="platform != null and platform != ''">
                and platform=#{platform}
            </if>
            <if test="loginType != null ">
                and `type`=#{loginType}
            </if>
            <if test="login != null ">
                and login=#{login}
            </if>
            <if test="serial != null and serial != ''">
                and serial=#{serial}
            </if>
            <if test="startDate != null">
                and add_time <![CDATA[ >= ]]> #{startDate}
            </if>
            <if test="endDate != null">
                and add_time <![CDATA[ < ]]> #{endDate}
            </if>
        </where>

    </select>

    <select id="pageList" resultType="com.crm.rely.backend.core.pojo.view.AccountLeverageChangeView"
            parameterType="com.crm.rely.backend.core.entity.account.leverage.AccountLeverageChangeSearchEntity">

        select account_leverage_change.*,
        `user_info`.`ib_no` AS `ib_no`,
        `agent`.`ib_no` AS `p_ib_no`,
        `custom_info`.`first_name` AS `first_name`,
        `custom_info`.`middle` AS `middle`,
        `custom_info`.`last_name` AS `last_name`,
        custom_info.c_id from account_leverage_change
        LEFT JOIN `user_info` ON `account_leverage_change`.`custom_id` = `user_info`.`custom_id`
         JOIN `custom_info` ON `account_leverage_change`.`custom_id` = `custom_info`.`id`
        LEFT JOIN `user_info` agent ON `custom_info`.`agent_id` = agent.`id`
        <where>
            <if test="stamp != null">
                and agent.stamp like binary concat(#{stamp},'%')
            </if>
            <if test="groupId != null and groupId != -1">
                and agent.group_id = #{groupId}
            </if>
            <if test="status != null ">
                <if test="status==0">
                    and account_leverage_change.status=1
                </if>
                <if test="status==1">
                    and (account_leverage_change.status=2 and account_leverage_change.leverage_status in(0,1))
                </if>
                <if test="status==2">
                    and (account_leverage_change.status=2 and account_leverage_change.leverage_status =2)
                </if>
                <if test="status==3">
                    and (account_leverage_change.status=3 or account_leverage_change.leverage_status=3)
                </if>
            </if>
            <if test="customId != null ">
                and account_leverage_change.custom_id=#{customId}
            </if>
            <if test="platform != null and platform != ''">
                and account_leverage_change.platform=#{platform}
            </if>
            <if test="loginType != null ">
                and account_leverage_change.`type`=#{loginType}
            </if>
            <if test="login != null ">
                and account_leverage_change.login=#{login}
            </if>
            <if test="serial != null and serial != ''">
                and account_leverage_change.serial=#{serial}
            </if>
            <if test="startDate != null">
                and account_leverage_change.add_time <![CDATA[ >= ]]> #{startDate}
            </if>
            <if test="endDate != null">
                and account_leverage_change.add_time <![CDATA[ < ]]> #{endDate}
            </if>
        </where>
        order by account_leverage_change.add_time desc
        limit #{page.offset},#{page.row}
    </select>
    <select id="getOne" resultType="com.crm.rely.backend.core.pojo.view.AccountLeverageChangeView">

        select account_leverage_change.*,
        `user_info`.`ib_no` AS `ib_no`,
        `agent`.`ib_no` AS `p_ib_no`,
        `custom_info`.`first_name` AS `first_name`,
        `custom_info`.`middle` AS `middle`,
        `custom_info`.`last_name` AS `last_name`,
        custom_info.c_id from account_leverage_change
        LEFT JOIN `user_info` ON `account_leverage_change`.`custom_id` = `user_info`.`custom_id`
         JOIN `custom_info` ON `account_leverage_change`.`custom_id` = `custom_info`.`id`
        LEFT JOIN `user_info` agent ON `custom_info`.`agent_id` = agent.`id`
       where account_leverage_change.id=#{id}
    </select>

</mapper>