<?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.custom.dao.mapper.CustomRecommendMapper">

    <select id="countList"
            parameterType="com.crm.rely.backend.model.entity.custom.CustomInfoLoginListEntity"
            resultType="java.lang.Integer">

        SELECT
        count(*)
        from custom_login custom_login
        <if test="stamp != null or stamp!=null">
            left join user_info agent on agent.id=custom_login.agent_id
        </if>
        left join custom_info custom on custom.id=custom_login.custom_id
        <where>
            <if test="stamp != null and stamp!=''">
                and agent.stamp like binary concat(#{stamp},'%')
            </if>
            <if test="agentIbNo != null and agentIbNo!=''">
                and agent.ib_no like binary concat(#{agentIbNo},'%')
            </if>
            <if test="email != null and email!=''">
                and custom.`email` =#{email}
            </if>
            <if test="agentId != null">
                and custom_login.`agent_id` =#{agentId}
            </if>
            <if test="customId!=null">
                and custom_login.`custom_id` =#{customId}
            </if>
            <if test="login != null">
                and custom_login.login =#{login}
            </if>
            <if test="cId != null">
                and custom_login.c_id =#{cId}
            </if>
            <if test="startDate != null">
                and custom_login.add_time <![CDATA[ >= ]]> #{startDate}
            </if>
            <if test="endDate != null">
                and custom_login.add_time <![CDATA[ < ]]> #{endDate}
            </if>
        </where>
    </select>

    <select id="pageList"
            parameterType="com.crm.rely.backend.model.entity.custom.CustomInfoLoginListEntity"
            resultType="com.crm.rely.backend.model.pojo.view.CustomInfoLoginView">

        SELECT
        custom.*,account.`BALANCE`,EQUITY,floating,CREDIT,agent.ib_no as agent_ib_no,
        sales.ib_no sales_ib_no,custom.first_name,custom.middle,custom.last_name,custom_login.login
        FROM
        custom_login custom_login
        left join account_view_mt4 account on account.login=custom_login.login
        left join custom_info custom on custom.id=custom_login.custom_id
        left join user_info agent on agent.id=custom_login.agent_id
        left join user_info sales on sales.id=agent.sales_id
        <where>
            <if test="stamp != null and stamp!=''">
                and agent.stamp like binary concat(#{stamp},'%')
            </if>
            <if test="agentIbNo != null and agentIbNo!=''">
                and agent.ib_no like binary concat(#{agentIbNo},'%')
            </if>
            <if test="email != null and email!=''">
                and custom.`email` =#{email}
            </if>
            <if test="agentId != null">
                and custom_login.`agent_id` =#{agentId}
            </if>
            <if test="customId!=null">
                and custom_login.`custom_id` =#{customId}
            </if>
            <if test="login != null">
                and custom_login.login =#{login}
            </if>
            <if test="cId != null">
                and custom_login.c_id =#{cId}
            </if>
            <if test="startDate != null">
                and custom_login.add_time <![CDATA[ >= ]]> #{startDate}
            </if>
            <if test="endDate != null">
                and custom_login.add_time <![CDATA[ < ]]> #{endDate}
            </if>
        </where>
        order by custom_login.add_time desc
        <if test="page!=null">
            limit #{page.offset},#{page.row}
        </if>
    </select>
    <select id="getById"
            resultType="com.crm.rely.backend.model.pojo.view.CustomLoginView">

        SELECT custom_login.*,
               account.`BALANCE`,
               EQUITY,
               floating,
               CREDIT,
               agent.ib_no as agent_ib_no,
               sales.ib_no    sales_ib_no,
               custom.first_name,
               custom.middle,
               custom.last_name
        FROM custom_login custom_login
                 left join account_view_mt4 account on account.login = custom_login.login
                 left join user_info agent on agent.id = custom_login.agent_id
                 left join user_info sales on sales.id = agent.sales_id
                 left join custom_info custom on custom.id = custom_login.custom_id
        where custom_login.id = #{id}
    </select>
    <select id="getByLogin"
            resultType="com.crm.rely.backend.model.pojo.view.CustomLoginView">

        SELECT custom_login.*,
               account.`BALANCE`,
               EQUITY,
               floating,
               CREDIT,
               agent.ib_no as agent_ib_no,
               sales.ib_no    sales_ib_no,
               custom.first_name,
               custom.middle,
               custom.last_name
        FROM custom_login custom_login
                 left join account_view_mt4 account on account.login = custom_login.login
                 left join user_info agent on agent.id = custom_login.agent_id
                 left join user_info sales on sales.id = agent.sales_id
                 left join custom_info custom on custom.id = custom_login.custom_id
        where custom_login.login = #{login}
    </select>


    <select id="existByCustomIdAndEquity"
            resultType="java.lang.Integer">
        SELECT count(custom_login.id)
        FROM custom_login custom_login
                 left join account_view_mt4 account on account.login = custom_login.login
        where custom_login.custom_id = #{customId}
          and account.equity >= #{equity}
    </select>
</mapper>