<?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.CustomAllocationRosterRecordMapper">
    <select id="selectList" resultType="com.crm.rely.backend.core.pojo.table.CustomAllocationRosterRecordTable"
            parameterType="com.crm.rely.backend.core.entity.custom.allocation.roster.record.CustomAllocationRosterRecordSearchEntity">
        select *
        from custom_allocation_roster_record
        <include refid="selectCondition"></include>
        order by add_time desc
        <if test="page != null">
            limit #{page.offset},#{page.row}
        </if>
    </select>

    <select id="selectCount" resultType="java.lang.Integer"
            parameterType="com.crm.rely.backend.core.entity.custom.allocation.roster.record.CustomAllocationRosterRecordSearchEntity">
        select count(*)
        from custom_allocation_roster_record
        <include refid="selectCondition"></include>

    </select>

    <sql id="selectCondition">
        <where>
            <if test="allocationRosterId!=null">
                and allocation_roster_id =#{allocationRosterId}
            </if>
            <if test="startDate != null">
                and add_time <![CDATA[>=]]>#{startDate}
            </if>
            <if test="endDate != null">
                and add_time <![CDATA[<=]]>#{endDate}
            </if>
        </where>
    </sql>

    <insert id="clickAllocationRoster"
            parameterType="com.crm.rely.backend.core.entity.custom.allocation.roster.CustomAllocationRosterClickEntity">
        INSERT INTO `custom_allocation_roster_record` (`allocation_roster_id`,
        `custom_id`,
        `c_id`,
        `agent_id`,
        `allocation_agent_id`,
        `add_time`,
        `add_user`,
        `add_ip`)
        SELECT custom_allocation_roster.id,
        custom_allocation_roster.custom_id,
        custom_allocation_roster.c_id,
        custom_allocation_roster.agent_id,
        agent.id,
        NOW(),
        1,
        '127.0.0.1'
        FROM custom_allocation_roster
        LEFT JOIN custom_info ON custom_info.id = custom_allocation_roster.custom_id,
        (SELECT user_info.id
        FROM user_info
        WHERE ib_no = #{ibNo}) agent
        WHERE custom_allocation_roster.`status` = 0
        AND custom_info.country in (SELECT `code`
        FROM sys_country
        WHERE `code` IN
        <foreach collection="country" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>)
    </insert>

    <insert id="clickAllocationRosterByCountrys"
            parameterType="com.crm.rely.backend.core.entity.custom.allocation.roster.CustomAllocationRosterClickByCountrysEntity">
        <if test="countrys != null and ibNo != null and ibNo!=''">
            INSERT INTO `custom_allocation_roster_record` (`allocation_roster_id`,
            `custom_id`,
            `c_id`,
            `agent_id`,
            `allocation_agent_id`,
            `add_time`,
            `add_user`,
            `add_ip`)
            SELECT custom_allocation_roster.id,
            custom_allocation_roster.custom_id,
            custom_allocation_roster.c_id,
            custom_allocation_roster.agent_id,
            agent.id,
            NOW(),
            1,
            '127.0.0.1'
            FROM custom_allocation_roster
            LEFT JOIN custom_info ON custom_info.id = custom_allocation_roster.custom_id,
            (SELECT user_info.id
            FROM user_info
            WHERE ib_no = #{ibNo}) agent
            WHERE custom_allocation_roster.`status` = 0
            AND custom_info.country in (SELECT `code`
            FROM sys_country
            WHERE
            `code` in
            <if test="countrys != null">
                <foreach collection="countrys" index="idx" item="item" separator="," open="(" close=")">
                    #{item}
                </foreach>
            </if>
            )
        </if>
    </insert>
</mapper>