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

    <resultMap id="infoMap" type="com.house365.beans.entity.CloudCustomerEntity">
        <result property="id" column="id"/>
        <result property="activeId" column="active_id"/>
        <result property="isOvertime" column="is_overtime"/>
        <result property="signupTime" column="signup_time"/>
        <result property="name" column="name"/>
        <result property="phone" column="phone"/>
        <result property="bakPhone1" column="bak_phone1"/>
        <result property="bakPhone2" column="bak_phone2"/>
        <result property="bakPhone3" column="bak_phone3"/>
        <result property="sex" column="sex"/>
        <result property="customerType" column="customer_type"/>
        <result property="actSource" column="act_source"/>
        <result property="firstSource" column="first_source"/>
        <result property="latestSource" column="latest_source"/>
        <result property="crmLatestCall" column="crm_latest_call"/>
        <result property="crmLatestCallResult" column="crm_latest_call_result"/>
        <result property="status" column="status"/>
        <result property="createSource" column="create_source"/>
        <result property="createTime" column="create_time"/>
        <result property="creater" column="creater"/>
        <result property="buyIntention" column="buy_intention"/>
        <result property="lastVisitTime" column="last_visit_time"/>
        <result property="lastCallTime" column="last_call_time"/>
        <result property="managerId" column="manager_id"/>
        <result property="managerName" column="manager_name"/>
        <result property="updateTime" column="update_time"/>
        <result property="updater" column="updater"/>
        <result property="cityCode" column="city_code"/>
        <result property="bizPort" column="biz_port"/>
        <result property="crmId" column="crm_id"/>
        <result property="cityName" column="city_name"/>
        <result property="actSourceId" column="act_source_id"/>
        <result property="remark" column="remark"/>
        <result property="traceStatus" column="trace_status"/>
        <result property="crmSync" column="crm_sync"/>
        <result property="hotlineCallTime" column="hotline_call_time"/>
        <result property="hotlineCallSpend" column="hotline_call_spend"/>
        <result property="oceanDate" column="ocean_date"/>
        <result property="isDelete" column="is_delete"/>
        <result property="lastManager" column="last_manager"/>
        <result property="bindTime" column="bind_time"/>
        <result property="signupHouseId" column="signup_house_id"/>
        <result property="signupHouseName" column="signup_house_name"/>
        <result property="signupSourceId" column="signup_source_id"/>
        <result property="signupSourceName" column="signup_source_name"/>
        <result property="customerId" column="customer_id"/>
        <result property="overTimeCount" column="over_time_count"/>
        <result property="visitBackCount" column="visit_back_count"/>
        <result property="waitVisitCount" column="wait_visit_count"/>
        <result property="boughtCount" column="bought_count"/>
        <result property="isabandon" column="is_abandon"/>
        <result property="abandonTime" column="abandon_time"/>
        <result property="abandonLogId" column="abandon_log_id"/>
    </resultMap>

    <sql id="sqlId">
        id,active_id,is_overtime,signup_time,NAME,phone,bak_phone1,bak_phone2,bak_phone3,sex,customer_type,act_source,
        first_source,latest_source,crm_latest_call,crm_latest_call_result,STATUS,create_source,create_time,creater,buy_intention,
        last_visit_time,last_call_time,manager_id,manager_name,update_time,updater,city_code,biz_port,crm_id,city_name,act_source_id,
        remark,trace_status,crm_sync,hotline_call_time,hotline_call_spend,is_delete,last_manager,bind_time,signup_house_id,signup_house_name,
        signup_source_id,signup_source_name,ocean_date,customer_id,over_time_count,visit_back_count,wait_visit_count,bought_count,is_abandon,
        abandon_time,abandon_log_id
    </sql>

    <insert id="save" parameterType="com.house365.beans.entity.CloudCustomerEntity">
	        INSERT INTO cloud_customer (
	        	active_id,
	        	is_overtime,
	        	signup_time,
	        	name,
	        	phone,
	        	bak_phone1,
	        	bak_phone2,
	        	bak_phone3,
	        	sex,
	        	customer_type,
	        	act_source,
	        	first_source,
	        	latest_source,
	        	crm_latest_call,
	        	crm_latest_call_result,
	        	status,
	        	create_source,
	        	create_time,
	        	creater,
	        	buy_intention,
	        	last_visit_time,
	        	last_call_time,
	        	manager_id,
	        	manager_name,
	        	update_time,
	        	updater,
	        	city_code,
	        	biz_port,
	        	crm_id,
	        	city_name,
	        	act_source_id,
	        	remark,
	        	trace_status,
	        	crm_sync,
	        	hotline_call_time,
	        	hotline_call_spend,
	        	ocean_date,
	        	is_delete,
	        	last_manager,
	        	bind_time,
	        	signup_house_id, signup_house_name, signup_source_id, signup_source_name, customer_id,
	        	over_time_count,
	        	visit_back_count,
	        	wait_visit_count,
	        	bought_count,
	        	abandon_time, is_abandon, abandon_log_id
	        ) VALUES (
	            #{activeId},
	        	#{isOvertime},
	        	#{signupTime},
	        	#{name},
	        	#{phone},
	        	#{bakPhone1},
	        	#{bakPhone2},
	        	#{bakPhone3},
	        	#{sex},
	        	#{customerType},
	        	#{actSource},
	        	#{firstSource},
	        	#{latestSource},
	        	#{crmLatestCall},
	        	#{crmLatestCallResult},
	        	#{status},
	        	#{createSource},
	        	#{createTime},
	        	#{creater},
	        	#{buyIntention},
	        	#{lastVisitTime},
	        	#{lastCallTime},
	        	#{managerId},
	        	#{managerName},
	        	#{updateTime},
	        	#{updater},
	        	#{cityCode},
	        	#{bizPort},
	        	#{crmId},
	        	#{cityName},
	        	#{actSourceId},
	        	#{remark},
	        	#{traceStatus},
	        	#{crmSync},
	        	#{hotlineCallTime},
	        	#{hotlineCallSpend},
	        	#{oceanDate},
	        	#{isDelete},
	        	#{lastManager},
	        	#{bindTime},
	        	#{signupHouseId}, #{signupHouseName}, #{signupSourceId}, #{signupSourceName}, #{customerId},
	        	#{overTimeCount},
	        	#{visitBackCount},
	        	#{waitVisitCount},
	        	#{boughtCount},
	        	#{abandonTime}, #{isabandon}, #{abandonLogId}
	        )
    </insert>

    <!-- 通过ID修改 -->
    <update id="update" parameterType="com.house365.beans.entity.CloudCustomerEntity">
	        UPDATE cloud_customer SET
		        active_id = #{activeId},
		        is_overtime = #{isOvertime},
		        signup_time = #{signupTime},
		        name = #{name},
		        phone = #{phone},
		        bak_phone1 = #{bakPhone1},
		        bak_phone2 = #{bakPhone2},
		        bak_phone3 = #{bakPhone3},
		        sex = #{sex},
		        customer_type = #{customerType},
		        act_source = #{actSource},
		        first_source = #{firstSource},
		        latest_source = #{latestSource},
		        crm_latest_call = #{crmLatestCall},
		        crm_latest_call_result = #{crmLatestCallResult},
		        status = #{status},
		        create_source = #{createSource},
		        create_time = #{createTime},
		        creater = #{creater},
		        buy_intention = #{buyIntention},
		        last_visit_time = #{lastVisitTime},
		        last_call_time = #{lastCallTime},
		        manager_id = #{managerId},
		        manager_name = #{managerName},
		        update_time = #{updateTime},
		        updater = #{updater},
		        city_code = #{cityCode},
		        biz_port = #{bizPort},
		        crm_id = #{crmId},
		        city_name = #{cityName},
		        act_source_id = #{actSourceId},
		        remark = #{remark},
		        trace_status = #{traceStatus},
		        crm_sync = #{crmSync},
		        hotline_call_time = #{hotlineCallTime},
		        hotline_call_spend = #{hotlineCallSpend},
		        ocean_date = #{oceanDate},
		        is_delete = #{isDelete},
		        last_manager = #{lastManager},
		        bind_time = #{bindTime},
		        signup_house_id = #{signupHouseId},
		        signup_house_name = #{signupHouseName},
		        signup_source_id = #{signupSourceId},
		        signup_source_name = #{signupSourceName},
		        over_time_count = #{overTimeCount},
		        visit_back_count = #{visitBackCount},
		        wait_visit_count = #{waitVisitCount},
		        bought_count = #{boughtCount},
		        customer_id = #{customerId},
		        abandon_time = #{abandonTime},
		        is_abandon = #{isabandon},
		        abandon_log_id = #{abandonLogId}
	        WHERE
		        id = #{id}
	</update>

    <update id="updateCustomerId" parameterType="map">
        UPDATE cloud_customer
        SET
        customerId = #{customerId}
        WHERE id = #{id}
    </update>

    <select id="getById" resultMap="infoMap" parameterType="Integer">
		SELECT
        <include refid="sqlId"/>
		FROM
		cloud_customer
		where is_delete = 0 AND id = #{id}
	</select>

    <select id="queryByConditions" resultMap="infoMap" parameterType="map">
        SELECT
        <include refid="sqlId"/>
        FROM
        cloud_customer
        where is_delete = 0
        <if test="actSource != null and actSource != ''">
            AND act_source LIKE CONCAT('%',#{actSource},'%')
        </if>
        <if test="phone != null and phone != '' ">
            AND phone LIKE CONCAT('%',#{phone},'%')
        </if>
        <if test="managerName != null and managerName != ''">
            AND manager_name LIKE CONCAT('%',#{managerName},'%')
        </if>
        <if test="latestSource != null and latestSource != ''">
            AND latest_source LIKE CONCAT('%',#{latestSource},'%')
        </if>
        <if test="signupHouseName != null and signupHouseName != ''">
            AND signup_house_name LIKE CONCAT('%',#{signupHouseName},'%')
        </if>
        <if test="city != null and city != ''">
            AND city_code = #{city}
        </if>
        <if test="mobile != null and mobile != ''">
            AND phone = #{mobile}
        </if>
        <if test="activeId != null and activeId >= 0 ">
            AND active_id = #{activeId}
        </if>
        <if test="isSea != null and isSea >= 0 ">
            AND manager_id = 0
        </if>
        <if test="customerId != null and customerId >= 0 ">
            AND customer_id = #{customerId}
        </if>
        <if test="type != null and type  == 1 ">
            AND status = 0
        </if>
        <if test="type != null and type  == 2 ">
            AND status = 1
        </if>
        <if test="idList != null and idList.size() > 0 ">
            AND id in
            <foreach collection="idList" index="index" item="item" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
        <if test="actIdList != null and actIdList.size() > 0 ">
            AND active_id in
            <foreach collection="actIdList" index="index" item="item" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
        <if test="group != null and group > 0 ">
            group by phone
        </if>
    </select>

    <select id="queryList" resultType="java.util.HashMap" parameterType="map">
        SELECT
            id, active_id as activeId, is_overtime as isOvertime, signup_time as signupTime, name, phone,
            bak_phone1, bak_phone2, bak_phone3,sex, customer_type as customerType, act_source as actSource,
            first_source as firstSource, latest_source as latestSource, crm_latest_call as crmLatestCall ,
            crm_latest_call_result as crmLatestCallResult,status, create_source as createSource, create_time as createTime,
            creater, buy_intention as buyIntention, last_visit_time as lastVisitTime, last_call_time as lastCallTime,
            manager_id as managerId,manager_name as managerName, update_time as updateTime, updater, city_code as cityCode,
            biz_port as bizPort,crm_id as crmId,city_name as cityName, act_source_id as actSourceId, remark,
            trace_status as traceStatus, crm_sync as crmSync, hotline_call_time as hotlineCallTime, hotline_call_spend as
            hotlineCallSpend,
            ocean_date as oceanDate,is_delete as isDelete, last_manager as lastManager, bind_time as bindTime,
            signup_house_id as signupHouseId,signup_house_name as signupHouseName,signup_source_id as signupSourceId,
            signup_source_name as signupSourceName, customer_id as customerId,
            over_time_count as overTimeCount, visit_back_count as visitBackCount,wait_visit_count as waitVisitCount,
            bought_count as boughtCount, abandon_time as boughtCount,is_abandon as isabandon,abandon_log_id as abandonLogId
        FROM
           cloud_customer
        where is_delete = 0
        <if test="actSource != null and actSource != ''">
            AND act_source LIKE CONCAT('%',#{actSource},'%')
        </if>
        <if test="phone != null and phone != '' ">
            AND phone LIKE CONCAT('%',#{phone},'%')
        </if>
        <if test="managerName != null and managerName != ''">
            AND manager_name LIKE CONCAT('%',#{managerName},'%')
        </if>
        <if test="latestSource != null and latestSource != ''">
            AND latest_source LIKE CONCAT('%',#{latestSource},'%')
        </if>
        <if test="signupHouseName != null and signupHouseName != ''">
            AND signup_house_name LIKE CONCAT('%',#{signupHouseName},'%')
        </if>
        <if test="city != null and city != ''">
            AND city_code = #{city}
        </if>
        <if test="activeId != null and activeId >= 0 ">
            AND active_id = #{activeId}
        </if>
        <if test="type != null and type  == 1 ">
            AND status = 0
        </if>
        <if test="type != null and type  == 2 ">
            AND status = 1
        </if>
        <if test="start != null and start >= 0 ">
            limit #{start},#{limit}
        </if>
    </select>

    <select id="queryCount" resultType="Integer" parameterType="map">
        SELECT count(*)
        FROM cloud_customer
        where is_delete = 0
        <if test="actSource != null and actSource != ''">
            AND act_source LIKE CONCAT('%',#{actSource},'%')
        </if>
        <if test="phone != null and phone != '' ">
            AND phone LIKE CONCAT('%',#{phone},'%')
        </if>
        <if test="managerName != null and managerName != ''">
            AND manager_name LIKE CONCAT('%',#{managerName},'%')
        </if>
        <if test="latestSource != null and latestSource != ''">
            AND latest_source LIKE CONCAT('%',#{latestSource},'%')
        </if>
        <if test="signupHouseName != null and signupHouseName != ''">
            AND signup_house_name LIKE CONCAT('%',#{signupHouseName},'%')
        </if>
        <if test="city != null and city != ''">
            AND city_code = #{city}
        </if>
        <if test="activeId != null and activeId >= 0 ">
            AND active_id = #{activeId}
        </if>
        <if test="type != null and type  == 1 ">
            AND status = 0
        </if>
        <if test="type != null and type  == 2 ">
            AND status = 1
        </if>
    </select>

</mapper>