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


    <select id="getAgentIdByLogin" parameterType="Long" resultType="Long">
        select agent_id from custom_login where login=#{login} limit 0,1
    </select>

    <select id="getCustomIdByLogin" parameterType="Long" resultType="Long">
        select custom_id from custom_login where login=#{login} limit 0,1
    </select>

    <select id="getCustomIdsByLogins"
            parameterType="java.util.List"
            resultType="Long">
        select custom_id
        from custom_login
        where login in
        <foreach collection="list" index="idx" item="item" separator="," open="(" close=")">
            #{item}
        </foreach>
    </select>

    <delete id="deleteInBatchByCustomId" parameterType="java.util.List">
        delete
        from custom_login
        where login in
        <foreach collection="list" index="idx" item="item" separator="," open="(" close=")">
            #{item}
        </foreach>
    </delete>

    <select id="findAllLoginBySalesId"
            parameterType="Long"
            resultType="com.crm.rely.backend.core.pojo.table.CustomLoginTable">
        select login
        from custom_login cl
        inner join user_info agent on cl.agent_id=agent.id
        where agent.sales_id=#{salesId}
    </select>

    <update id="batchUpdateLoginStatusByAgentId" parameterType="java.util.List">
        <foreach collection="list" item="item" index="idx" open="" close="" separator=";">
            update custom_login
            <set>
                login_status=#{item.loginStatus},
                login_status_number=#{item.loginStatusNumber}
            </set>
            where agent_id=#{item.agentId}
        </foreach>
    </update>

    <select id="getCustomLoginByLogin" parameterType="Long" resultType="com.crm.rely.backend.core.pojo.table.CustomLoginTable">
        select * from custom_login where login=#{login} limit 0,1
    </select>

    <select id="getEmailCustomByLogin" resultType="com.crm.rely.backend.core.pojo.view.CustomLoginView">
        select
        custom_login.id,custom_login.c_id,custom_login.login,
        custom_login.platform,custom.email,custom.country,custom.name_en
        from custom_login
        join custom_info custom on custom.id=custom_login.custom_id
        where custom_login.platform=#{platform} and custom_login.login in
        <foreach collection="tradeList" item="item" index="index" separator="," open="(" close=")">
            #{item}
        </foreach>
    </select>

    <select id="getCustomLoginByLoginAndPlatform" resultType="com.crm.rely.backend.core.pojo.view.AccountBalanceView">
        select login.login, account.balance
        from custom_login login
        left join account_view_${platform} account on login.login = account.login
        where login.login=#{login} and login.platform = #{platform}
        and account.balance <![CDATA[<]]> 0
        limit 0,1
    </select>

</mapper>