<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE sqlMap PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN"
        "http://ibatis.apache.org/dtd/sql-map-2.dtd">
<sqlMap namespace="com.house365.beans.entity.CustomerCallbackRecordEntity">
    <typeAlias alias="customerCallbackRecordEntity"
               type="com.house365.beans.entity.CustomerCallbackRecordEntity"/>
    <resultMap id="customerCallbackRecord"
               class="com.house365.beans.entity.CustomerCallbackRecordEntity">
        <result property="id" column="id"/>
        <result property="name" column="name"/>
        <result property="phone" column="phone"/>
        <result property="content" column="content"/>
        <result property="callRecordName" column="call_record_name"/>
        <result property="managerName" column="manager_name"/>
        <result property="createTime" column="create_time"/>
    </resultMap>


    <!-- 用于分页查询抽取的公共的条件 -->
    <sql id="customerCallbackRecord.queryCondition">
        <!-- 默认不开启实体作为条件 -->
        <isNotNull property="searchFilters">
            <isNotEmpty prepend="AND" property="searchFilters.id">
                <![CDATA[
					id $searchFilters.id.operation$ #searchFilters.id.value#
		            ]]>
            </isNotEmpty>
            <isNotEmpty prepend="AND" property="searchFilters.name">
                <![CDATA[
					`name` $searchFilters.name.operation$ #searchFilters.name.value#
		            ]]>
            </isNotEmpty>
            <isNotEmpty prepend="AND" property="searchFilters.phone">
                <![CDATA[
					phone $searchFilters.phone.operation$ #searchFilters.phone.value#
		            ]]>
                <isNotEmpty prepend="AND" property="searchFilters.callRecordName">
                    <![CDATA[
                call_record_name $searchFilters.callRecordName.operation$ #searchFilters.callRecordName.value#
                ]]>
                </isNotEmpty>
            </isNotEmpty>
            <isNotEmpty prepend="AND" property="searchFilters.managerName">
                <![CDATA[
					manager_name $searchFilters.managerName.operation$ #searchFilters.managerName.value#
		            ]]>
            </isNotEmpty>
            <isNotEmpty prepend="AND" property="searchFilters.createTimeStart">
                <![CDATA[
					a.create_time $searchFilters.createTimeStart.operation$ #searchFilters.createTimeStart.value#
		            ]]>
            </isNotEmpty>
            <isNotEmpty prepend="AND" property="searchFilters.createTimeEnd">
                <![CDATA[
					a.create_time $searchFilters.createTimeEnd.operation$ #searchFilters.createTimeEnd.value#
		            ]]>
            </isNotEmpty>
            <isNotEmpty prepend="AND" property="searchFilters.keyword">
                <![CDATA[
					(b.`name` $searchFilters.keyword.operation$ #searchFilters.keyword.value# OR b.phone $searchFilters.keyword.operation$ #searchFilters.keyword.value# OR a.manager_name $searchFilters.keyword.operation$ #searchFilters.keyword.value#)
		            ]]>
            </isNotEmpty>
            <isNotEmpty prepend="AND" property="searchFilters.userId">
                <![CDATA[
					 (a.manager_id = $searchFilters.userId.value$ or a.manager_id in (select id from user where depturlpath like (select concat(depturlpath,'/%') from user where id = $searchFilters.userId.value$)))
		            ]]>
            </isNotEmpty>
            <isNotEmpty prepend="AND" property="searchFilters.userIds">
                <![CDATA[
					 (a.manager_id in ($searchFilters.userIds.value$) )
		            ]]>
            </isNotEmpty>
            <isNotEmpty prepend="AND" property="searchFilters.cityCode">
                <![CDATA[
					b.cityCode $searchFilters.cityCode.operation$ #searchFilters.cityCode.value#
		            ]]>
            </isNotEmpty>
            <isNotEmpty prepend="AND" property="searchFilters.cityName">
                <![CDATA[
					b.cityName $searchFilters.cityName.operation$ #searchFilters.cityName.value#
		            ]]>
            </isNotEmpty>

        </isNotNull>
        <dynamic prepend="ORDER BY">
            <isNotNull property="orderColumn">
                <![CDATA[
				a.$orderColumn$
				]]>
                <isNotNull property="orderMode">
                    $orderMode$
                </isNotNull>
            </isNotNull>
        </dynamic>
    </sql>

    <!-- 回访录音页面查询所有含有录音的记录 -->
    <select id="queryByPage" resultMap="customerCallbackRecord">
        SELECT
        a.id,
        b.`name`,
        b.phone,
        a.content,
        a.manager_name,
        a.call_record_name,
        a.create_time
        FROM customer_callback_log a LEFT JOIN customer b ON a.customer_id=b.id
        WHERE a.call_record_name IS NOT NULL
        <include refid="customerCallbackRecord.queryCondition"/>
        <![CDATA[
		    LIMIT #paging.startRecord#, #paging.pageSize#
        ]]>
    </select>

    <!-- 为分页查询符合条件的记录数 -->
    <select id="queryByPage_count" resultClass="int">
        SELECT
        count(*)
        FROM customer_callback_log a LEFT JOIN customer b ON a.customer_id=b.id
        WHERE a.call_record_name IS NOT NULL
        <include refid="customerCallbackRecord.queryCondition"/>
    </select>
</sqlMap>
