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

    <select id="listCustom"
            parameterType="com.crm.rely.backend.model.entity.custom.info.CustomSearchEntity"
            resultType="com.crm.rely.backend.model.pojo.view.CustomView">
        select custom.id,first_name,middle
        middle,last_name,custom.email,custom.country,custom.country_name,custom.country_en_name,custom.identity,
        custom.name_en,custom.head_picture,custom.phone,
        custom.add_ip, custom.approve_time, custom.
        last_time,custom.birth,custom.valid,custom.add_time,custom.last_ip,custom.`status`,ifnull(custom.apply_real_status,0)
        apply_real_status,custom.apply_real_time,custom.approve_desc,custom.approve_time,custom.c_id,agent.ib_no as
        agent_ib_no,sales.ib_no sales_ib_no,
        custom.agent_id, custom.sno
        from custom_info custom
        left join user_info agent on agent.id = custom.agent_id
        left join user_info sales on sales.id = agent.sales_id
        <where>
            <if test="stamp != null">
                and agent.stamp like binary concat(#{stamp},'%')
            </if>
            <!--            <if test="(groupId!=null and groupId > 0)">-->
            <!--                and agent.group_id = #{groupId}-->
            <!--            </if>-->

            <if test="email != null and email !=''">
                and custom.email =#{email}
            </if>
            <if test="email != null and email !=''">
                and custom.email =#{email}
            </if>
            <if test="agentIbNo != null and agentIbNo !=''">
                and agent.ib_no like binary concat(#{agentIbNo},'%')
            </if>
            <if test="salesIbNo != null and salesIbNo !=''">
                and sales.ib_no like binary concat(#{salesIbNo},'%')
            </if>
            <if test="lastIp != null and lastIp !=''">
                and custom.last_ip like binary concat(#{lastIp},'%')
            </if>
            <if test="identity != null and identity !=''">
                and custom.identity =#{identity}
            </if>
            <if test="cId != null">
                and custom.c_id =#{cId}
            </if>
            <if test="startDate != null">
                <if test="status!=null">
                    and custom.add_time <![CDATA[>=]]>#{startDate}
                </if>
                <if test="status==null">
                    and custom.approve_time <![CDATA[>=]]>#{startDate}
                </if>
            </if>
            <if test="endDate != null">
                <if test="status!=null">
                    and custom.add_time <![CDATA[<=]]>#{endDate}
                </if>
                <if test="status==null">
                    and custom.approve_time <![CDATA[<=]]>#{endDate}
                </if>
            </if>
            <if test="status!=null">
                <if test="status==0">
                    <if test="potentialStatus==null">
                        and (custom.status in (1,3) and ( custom.apply_real_status in(0,3) or custom.apply_real_status
                        is null))
                    </if>
                    <if test="potentialStatus!=null">
                        <if test="potentialStatus==0">
                            and (custom.apply_real_status =0 or custom.apply_real_status is null)
                        </if>
                        <if test="potentialStatus==1">
                            and (custom.status = 3 or custom.apply_real_status=3)
                        </if>
                    </if>
                </if>
                <if test="status==1">
                    and (custom.status =1 and custom.apply_real_status=1)
                </if>
                <if test="status==2">
                    and (custom.status =2 )
                </if>
                <if test="status==3">
                    and (custom.status = 3 or custom.apply_real_status=3)
                </if>
            </if>

        </where>
        <if test="status!=null">
            <if test="status==1">
                order by
                <if test="sortType!=null and sortName!=null and sortName !=''">
                    <if test="sortType==1">
                        custom.${sortName},
                    </if>
                    <if test="sortType==2">
                        custom.${sortName} desc,
                    </if>
                </if>
                custom.apply_real_time desc,custom.status asc
            </if>
            <if test="status!=1">
                order by
                <if test="sortType!=null and sortName!=null and sortName !=''">
                    <if test="sortType==1">
                        custom.${sortName},
                    </if>
                    <if test="sortType==2">
                        custom.${sortName} desc,
                    </if>
                </if>
                custom.add_time desc,custom.status asc
            </if>
        </if>
        <if test="status==null">
            order by
            <if test="sortType!=null and sortName!=null and sortName !=''">
                <if test="sortType==1">
                    custom.${sortName},
                </if>
                <if test="sortType==2">
                    custom.${sortName} desc,
                </if>
            </if>
            custom.approve_time desc, custom.add_time desc,custom.status asc
        </if>
        <if test="page != null">
            limit #{page.offset},#{page.row}
        </if>
    </select>

    <select id="countCustom"
            parameterType="com.crm.rely.backend.model.entity.custom.info.CustomSearchEntity"
            resultType="Integer">
        select count(*)
        from custom_info custom
        left join user_info agent on agent.id = custom.agent_id
        left join user_info sales on sales.id = agent.sales_id
        <where>
            <if test="stamp != null">
                and agent.stamp like binary concat(#{stamp},'%')
            </if>
            <!--            <if test="groupId != null and groupId > 0">-->
            <!--                and agent.group_id = #{groupId}-->
            <!--            </if>-->
            <if test="email != null and email !=''">
                and custom.email =#{email}
            </if>
            <if test="agentIbNo != null and agentIbNo !=''">
                and agent.ib_no like binary concat(#{agentIbNo},'%')
            </if>
            <if test="salesIbNo != null and salesIbNo !=''">
                and sales.ib_no like binary concat(#{salesIbNo},'%')
            </if>
            <if test="lastIp != null and lastIp !=''">
                and custom.last_ip like binary concat(#{lastIp},'%')
            </if>
            <if test="identity != null and identity !=''">
                and custom.identity =#{identity}
            </if>
            <if test="cId != null">
                and custom.c_id =#{cId}
            </if>
            <if test="startDate != null">
                <if test="status!=null">
                    and custom.add_time <![CDATA[>=]]>#{startDate}
                </if>
                <if test="status==null">
                    and custom.approve_time <![CDATA[>=]]>#{startDate}
                </if>
            </if>
            <if test="endDate != null">
                <if test="status!=null">
                    and custom.add_time <![CDATA[<=]]>#{endDate}
                </if>
                <if test="status==null">
                    and custom.approve_time <![CDATA[<=]]>#{endDate}
                </if>
            </if>
            <if test="status!=null">
                <if test="status==0">
                    <if test="potentialStatus==null">
                        and (custom.status in (1,3) and ( custom.apply_real_status in(0,3) or custom.apply_real_status
                        is null))
                    </if>
                    <if test="potentialStatus!=null">
                        <if test="potentialStatus==0">
                            and (custom.apply_real_status =0 or custom.apply_real_status is null)
                        </if>
                        <if test="potentialStatus==1">
                            and (custom.status = 3 or custom.apply_real_status=3)
                        </if>
                    </if>
                </if>
                <if test="status==1">
                    and (custom.status =1 and custom.apply_real_status=1)
                </if>
                <if test="status==2">
                    and (custom.status =2 )
                </if>
                <if test="status==3">
                    and (custom.status = 3 or custom.apply_real_status=3)
                </if>
            </if>

        </where>
    </select>

    <select id="getAgentIdById" parameterType="Long" resultType="Long">
        select agent_id
        from custom_info
        where id = #{id}
    </select>

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

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

    <select id="getOneByCId" resultType="com.crm.rely.backend.model.pojo.view.CustomView">
        SELECT custom_info.id,
               custom_info.ib_id,
               custom_info.first_name,
               custom_info.middle,
               custom_info.last_name,
               custom_info.name_en,
               custom_info.head_picture,
               custom_info.email,
               custom_info.`password`,
               custom_info.lang,
               custom_info.area_code,
               custom_info.phone,
               custom_info.agent_id,
               custom_info.c_id,
               custom_info.ib_id,
               custom_info.gender,
               custom_info.birth,
               custom_info.identity,
               custom_info.country,
               custom_info.country_name,
               custom_info.country_en_name,
               custom_info.state,
               custom_info.city,
               custom_info.zip_code,
               custom_info.address_line,
               custom_info.tax_number,
               custom_info.`status`,
               custom_info.add_time,
               custom_info.add_ip,
               custom_info.apply_real_status,
               custom_info.apply_real_time,
               custom_info.valid,
               custom_info.approve_desc,
               custom_info.usd_amount,
               custom_info.cny_amount,
               custom_info.agent_id,
               custom_info.sno,
               agent.ib_no    agent_ib_no,
               sales.ib_no    sales_ib_no,
               agent.ib_no as p_ib_no,
               IF(
                       agent.id IS NULL,
                       '',
                       concat(agent.ib_no, '-', agent.NAME)
                   )       AS agent
        FROM custom_info custom_info
                 LEFT JOIN user_info agent ON custom_info.agent_id = agent.id
                 left join user_info sales on sales.id = agent.sales_id
        WHERE custom_info.c_id = #{id}
    </select>

</mapper>