<?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.house365.ws.dao.mapper.ExternalCustomerMapper">

    <resultMap id="infoMap" type="com.house365.beans.entity.ExternalCustomerEntity">
        <result property="id" column="id"/>
        <result property="name" column="name"/>
        <result property="phone" column="phone"/>
        <result property="bakPhone" column="bak_phone"/>
        <result property="sex" column="sex"/>
        <result property="actSource" column="act_source"/>
        <result property="status" column="status"/>
        <result property="createSource" column="create_source"/>
        <result property="buyIntention" column="buy_intention"/>
        <result property="lastCallTime" column="last_call_time"/>
        <result property="managerId" column="manager_id"/>
        <result property="managerName" column="manager_name"/>
        <result property="cityCode" column="city_code"/>
        <result property="cityName" column="city_name"/>
        <result property="actSourceId" column="act_source_id"/>
        <result property="traceStatus" column="trace_status"/>
        <result property="remark" column="remark"/>
        <result property="lastManager" column="last_manager"/>
        <result property="bindTime" column="bind_time"/>
        <result property="crmSync" column="crm_sync"/>
        <result property="createTime" column="create_time"/>
        <result property="creater" column="creater"/>
        <result property="updateTime" column="update_time"/>
        <result property="updater" column="updater"/>
        <result property="isDelete" column="is_delete"/>
    </resultMap>

    <select id="queryById" resultMap="infoMap" parameterType="int">
        SELECT
        id, name, phone, bak_phone, sex, act_source,
        status, create_source,
        create_time, creater, buy_intention,  last_call_time, manager_id, manager_name,
        update_time, updater, city_code,city_name,act_source_id, trace_status, remark,
        is_delete,last_manager,bind_time,crm_sync
        FROM external_customer
        WHERE is_delete = 0
        AND  id = #{id}
    </select>

    <insert id="save" useGeneratedKeys="true" keyProperty="id"  keyColumn="id" parameterType="com.house365.beans.entity.ExternalCustomerEntity">
        insert into external_customer
        (id,name,phone,bak_phone,
        act_source,status,create_source,
        create_time,creater,buy_intention,last_call_time,manager_id,manager_name,
        update_time,updater,city_code,city_name,act_source_id,trace_status,remark,
        last_manager,is_delete,bind_time)
        values
        (
        #{id},#{name},#{phone},#{bakPhone},
        #{actSource},#{status},#{createSource},
        #{createTime},#{creater},#{buyIntention},#{lastCallTime},#{managerId},#{managerName},
        #{updateTime},#{updater},#{cityCode},#{cityName},#{actSourceId},#{traceStatus},#{remark},
        #{lastManager},#{isDelete},#{bindTime}
        )
    </insert>

    <update id="update" parameterType="Map">
        UPDATE external_customer
        SET
        <if test="name != null and name != ''">
            name = #{name},
        </if>
        <if test="sex != null and sex >= 0">
            sex = #{sex},
        </if>
        <if test="managerId != null and managerId >= 0">
            manager_id = #{managerId},
        </if>
        <if test="phone != null and phone != ''">
            phone = #{phone},
        </if>
        <if test="bakPhone != null and bakPhone != ''">
            bak_phone = #{bakPhone},
        </if>
        <if test="traceStatus != null and traceStatus >= 0">
            trace_status = #{traceStatus},
        </if>
        <if test="buyIntention != null and buyIntention != ''">
            buy_intention = #{buyIntention},
        </if>
        <if test="lastCallTime != null and lastCallTime != ''">
            last_call_time = #{lastCallTime},
        </if>
        <if test="remark != null and remark != ''">
            remark = #{remark},
        </if>
        <if test="isDelete != null and isDelete >= 0">
            is_delete = #{isDelete},
        </if>
        <if test="crmSync != null and crmSync >= 0">
            crm_sync = #{crmSync},
        </if>
        update_time = #{updateTime},
        updater = #{updater}
        WHERE id = #{id}
    </update>

    <select id="queryByConditions" resultMap="infoMap" resultType="java.util.HashMap">
        SELECT
        *
        FROM external_customer
        where is_delete = 0
        <if test="phone != null and phone >=0">
            AND phone = #{phone}
        </if>
        <if test="managerId != null and managerId >=0">
            AND manager_id = #{managerId}
        </if>
        <if test="cityCode != null and cityCode != ''">
            AND city_code = #{cityCode}
        </if>
        <if test="crmSync != null and crmSync >=0">
            AND crm_sync = #{crmSync}
        </if>
        <if test="putTime != null and putTime != ''">
            AND #{putTime} > create_time
        </if>
        <if test="limit != null and limit > 0 ">
            limit 10
        </if>
    </select>

    <select id="queryList" resultType="java.util.HashMap" parameterType="map">
        SELECT
        c.id, c.name, c.phone, c.bak_phone AS bakPhone, c.sex, c.act_source AS actSource,
        c.status, c.create_source AS createSource,
        c.create_time AS createTime, c.creater, c.buy_intention AS buyIntention, c.last_call_time AS lastCallTime,
        c.manager_id AS managerId, c.manager_name AS managerName,
        c.update_time AS updateTime, c.updater, c.city_code AS cityCode, c.city_name AS cityName,c.act_source_id AS actSourceId,
        c.trace_status AS traceStatus, c.remark,
        c.is_delete AS isDelete,c.last_manager AS lastManager,c.bind_time AS bindTime
        FROM external_customer c
        left JOIN external_customer_callback_log ccl ON ccl.customer_id = c.id
        where c.is_delete = 0
        <if test="name != null and name != ''">
            AND c.name LIKE CONCAT('%',#{name},'%')
        </if>
        <if test="phone != null and phone != ''">
            AND c.phone LIKE CONCAT('%',#{phone},'%')
        </if>
        <if test="managerName != null and managerName != ''">
            AND c.manager_name LIKE CONCAT('%',#{managerName},'%')
        </if>
        <if test="createId != null and createId > 0 ">
            AND (c.manager_id = #{createId} OR c.manager_id IN (SELECT id FROM user WHERE depturlpath LIKE (SELECT CONCAT(depturlpath,'/%') FROM user WHERE id = #{createId})))
        </if>
        <if test="createName != null and createName != ''">
            AND c.create_name = #{createName}
        </if>
        <if test="startTime != null and startTime != ''">
            AND c.create_time > #{startTime}
        </if>
        <if test="endTime != null and endTime != ''">
            AND #{endTime} > c.create_time
        </if>
        <if test="callStartTime != null and callStartTime != ''">
            AND ccl.create_time > #{callStartTime}
        </if>
        <if test="callEndTime != null and callEndTime != ''">
            AND #{callEndTime} > ccl.create_time
        </if>
        <if test="city != null and city != ''">
            AND c.city_code = #{city}
        </if>
        <if test="content != null and content != ''">
            AND ccl.content LIKE CONCAT('%',#{content},'%')
        </if>
        <if test="buyIntention != null and buyIntention != ''">
            AND c.buy_intention = #{buyIntention}
        </if>
        GROUP BY c.id
        <if test="sort != null and sort == 1 ">
            ORDER BY c.create_time DESC
        </if>
        <if test="sort != null and sort == 2 ">
            ORDER BY c.create_time ASC
        </if>
        <if test="sort != null and sort == 3 ">
            ORDER BY c.last_call_time DESC
        </if>
        <if test="sort != null and sort == 4 ">
            ORDER BY c.last_call_time ASC
        </if>
        <if test="start != null and start >= 0 ">
            limit #{start},#{limit}
        </if>
    </select>

    <select id="queryCount" resultType="Integer" parameterType="map">
        SELECT COUNT( DISTINCT c.id)
        FROM external_customer c
        left JOIN external_customer_callback_log ccl ON ccl.customer_id = c.id
        where c.is_delete = 0
        <if test="name != null and name != ''">
            AND c.name LIKE CONCAT('%',#{name},'%')
        </if>
        <if test="phone != null and phone != ''">
            AND c.phone LIKE CONCAT('%',#{phone},'%')
        </if>
        <if test="managerName != null and managerName != ''">
            AND c.manager_name LIKE CONCAT('%',#{managerName},'%')
        </if>
        <if test="createId != null and createId > 0 ">
            AND (c.manager_id = #{createId} OR c.manager_id IN (SELECT id FROM user WHERE depturlpath LIKE (SELECT CONCAT(depturlpath,'/%') FROM user WHERE id = #{createId})))
        </if>
        <if test="createName != null and createName != ''">
            AND c.create_name = #{createName}
        </if>
        <if test="startTime != null and startTime != ''">
            AND c.create_time > #{startTime}
        </if>
        <if test="endTime != null and endTime != ''">
            AND #{endTime} > c.create_time
        </if>
        <if test="callStartTime != null and callStartTime != ''">
            AND ccl.create_time > #{callStartTime}
        </if>
        <if test="callEndTime != null and callEndTime != ''">
            AND #{callEndTime} > ccl.create_time
        </if>
        <if test="city != null and city != ''">
            AND c.city_code = #{city}
        </if>
        <if test="buyIntention != null and buyIntention != ''">
            AND c.buy_intention = #{buyIntention}
        </if>
        <if test="content != null and content != ''">
            AND ccl.content  LIKE CONCAT('%',#{content},'%')
        </if>
    </select>

</mapper>
