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

    <resultMap id="infoMap" type="com.house365.beans.entity.CustomerProjectEntity">
        <result property="id" column="id"/>
        <result property="consultantName" column="consultant_name"/>
        <result property="status" column="status"/>
        <result property="consultantPhone" column="consultant_phone"/>
        <result property="isRepeat" column="is_repeat"/>
        <result property="recordedStatus" column="recorded_status"/>
        <result property="recordedTime" column="recorded_time"/>
        <result property="visitTime" column="visit_time"/>
        <result property="loanStatus" column="loan_status"/>
        <result property="creater" column="creater"/>
        <result property="createTime" column="create_time"/>
        <result property="projectId" column="project_id"/>
        <result property="remark" column="remark"/>
        <result property="customerId" column="customer_id"/>
        <result property="requireArea" column="require_area"/>
        <result property="requireHouse" column="require_house"/>
        <result property="requireHouseType" column="require_house_type"/>
        <result property="requirePrice" column="require_price"/>
        <result property="updateTime" column="update_time"/>
        <result property="updater" column="updater"/>
        <result property="customerPhone" column="customer_phone"/>
        <result property="contract" column="contract"/>
        <result property="houseName" column="house_name"/>
        <result property="projectName" column="project_name"/>
        <result property="lastChangeStatusTime" column="last_change_status_time"/>
        <result property="jgStatus" column="jg_status"/>
    </resultMap>

    <select id="getPhonesById" resultMap="infoMap" parameterType="String">
        SELECT customer_phone
        FROM customer_project
        where project_id = #{id}
        group by customer_phone
    </select>

    <select id="queryById" resultMap="infoMap" parameterType="map">
        SELECT *
        FROM customer_project
        where id = #{id}
    </select>

    <select id="getCustomerProjectInfo" resultMap="infoMap" parameterType="map">
        select t.id,cp.last_change_status_time
        from (
        select c.id
        from customer c
        where c.phone= #{phone}
        and c.manager_id= #{managerId} ) t inner join customer_project cp
        where t.id =cp.customer_id
    </select>

    <select id="queryPlatCustomerStatus" resultType="Integer" parameterType="map">
        select ifnull(MAX(cp.STATUS),0) status
        from
        (
        select c.id
        from customer c
        where c.phone= #{phone}
        and c.manager_id= #{managerId}
        and c.id in (select customer_id from customer_project)
        ) t, customer_project cp
        where t.id=cp.customer_id
    </select>

    <select id="queryByConditions" resultMap="infoMap" parameterType="map">
        SELECT
        id, consultant_name, status, consultant_phone, is_repeat, recorded_status, recorded_time,
        visit_time, loan_status, create_time,creater, project_id, remark, customer_id, require_area, require_house,
        require_house_type, require_price, update_time, updater, customer_phone, contract, house_name, project_name,
        last_change_status_time,jg_status
        FROM customer_project
        where 1 = 1
        <if test="projectId != null and projectId >= 0 ">
            AND project_id = #{projectId}
        </if>
        <if test="manager != null and manager != ''">
            AND EXISTS (select id from customer where id=customer_id and manager_id is not null and manager_id != '' and
            is_delete = 0)
        </if>
        <if test="createTime != null and createTime != '' ">
            AND create_time > #{createTime}
        </if>
        <if test="isRepeat != null and isRepeat >= 0 ">
            AND is_repeat = #{isRepeat}
        </if>
        <if test="customerPhone != null and customerPhone != '' ">
            AND customer_phone = #{customerPhone}
        </if>
        <if test="contract != null and contract != '' ">
            AND contract = #{contract}
        </if>
        <if test="id != null and id >= 0 ">
            AND id != #{id}
        </if>
        <if test="customerId != null and customerId >= 0 ">
            AND customer_id != #{customerId}
        </if>
        <if test="customerIdNew != null and customerIdNew >= 0 ">
            AND customer_id = #{customerIdNew}
        </if>
        <if test="isVisit != null and isVisit >= 0 ">
            AND status > 1
        </if>
        <if test="idList != null and idList.size() > 0 ">
            AND customer_id in
            <foreach collection="idList" index="index" item="item" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
        <if test="projectIdList != null and projectIdList.size() > 0 ">
            AND project_id in
            <foreach collection="projectIdList" index="index" item="item" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
        ORDER BY create_time DESC
        <if test="limit != null and limit >= 0 ">
            limit 1
        </if>
    </select>


    <update id="updateOldPid2NewPid" parameterType="map">
        UPDATE customer_project
        SET project_id = #{newPid}, project_name = #{newProjectName}, contract = #{newContract}, house_name = #{newHouseName}
        WHERE project_id = #{oldPid}
        and customer_id = #{customerId}
    </update>

    <update id="updateCustomerStatus" parameterType="map">
        UPDATE customer_project
        SET
        <if test="isRepeat != null and isRepeat >= 0 ">
            is_repeat = #{isRepeat},
        </if>
        <if test="jgStatus != null and jgStatus >= 0 ">
            jg_status = #{jgStatus},
        </if>
        <if test="status != null and status >= 0 ">
            status = #{status},
        </if>
        update_time = #{updateTime}
        WHERE id = #{id}
    </update>

    <select id="queryReport" resultType="java.util.HashMap" parameterType="map">
        SELECT temp.* FROM
        (SELECT c.id ,cp.id as cpId,c.isWaitCall,c.status AS customerStatus,c.manager_id,cp.project_id AS projectId,
        cp.project_name as projectName,
        cp.contract,c.name,c.phone,c.manager_name as managerName,cp.status,cp.jg_status AS jgStatus,
        cp.is_repeat AS isRepeat,
        (SELECT cs.create_time AS status1_time FROM customer_status_log cs WHERE cs.project_id = cp.project_id AND
        cs.customer_id = cp.customer_id AND cs.to_status_id = 1 AND cs.is_delete = 0 LIMIT 1) AS status1 ,
        (SELECT cs.occur_time AS status2_time FROM customer_status_log cs WHERE cs.project_id = cp.project_id AND
        cs.customer_id = cp.customer_id AND cs.to_status_id = 2 AND cs.is_delete = 0 LIMIT 1) AS status2 ,
        (SELECT cs.occur_time AS status3_time FROM customer_status_log cs WHERE cs.project_id = cp.project_id AND
        cs.customer_id = cp.customer_id AND cs.to_status_id = 3 AND cs.is_delete = 0 LIMIT 1) AS status3 ,
        (SELECT cs.occur_time AS status1_time FROM customer_status_log cs WHERE cs.project_id = cp.project_id AND
        cs.customer_id = cp.customer_id AND cs.to_status_id = 4 AND cs.is_delete = 0 LIMIT 1) AS status4 ,
        (SELECT cs.occur_time AS status2_time FROM customer_status_log cs WHERE cs.project_id = cp.project_id AND
        cs.customer_id = cp.customer_id AND cs.to_status_id = 5 AND cs.is_delete = 0 LIMIT 1) AS status5 ,
        (SELECT cs.occur_time AS status3_time FROM customer_status_log cs WHERE cs.project_id = cp.project_id AND
        cs.customer_id = cp.customer_id AND cs.to_status_id = 6 AND cs.is_delete = 0 LIMIT 1) AS status6
        FROM customer_project cp
        LEFT JOIN customer c ON c.id = cp.customer_id
        WHERE  c.cityCode = #{city}
        ) AS temp
        WHERE 1 = 1
        <if test="startTime != null and startTime != '' ">
            <if test="dateType != null and dateType == 1 ">
                AND (status1 BETWEEN #{startTime} AND #{endTime})
            </if>
            <if test="dateType != null and dateType == 2 ">
                AND (status2 BETWEEN #{startTime} AND #{endTime})
            </if>
            <if test="dateType != null and dateType == 3 ">
                AND (status3 BETWEEN #{startTime} AND #{endTime})
            </if>
            <if test="dateType != null and dateType == 4 ">
                AND (status4 BETWEEN #{startTime} AND #{endTime})
            </if>
            <if test="dateType != null and dateType == 5 ">
                AND (status5 BETWEEN #{startTime} AND #{endTime})
            </if>
            <if test="dateType != null and dateType == 6 ">
                AND (status6 BETWEEN #{startTime} AND #{endTime})
            </if>
        </if>
        <if test="name != null and name != '' ">
            AND name LIKE CONCAT('%', #{name},'%')
        </if>
        <if test="phone != null and phone != '' ">
            AND phone LIKE CONCAT('%', #{phone},'%')
        </if>
        <if test="projectName != null and projectName != '' ">
            AND projectName LIKE CONCAT('%', #{projectName},'%')
        </if>
        <if test="isMatch != null and isMatch == 1 ">
            AND status != jgStatus
        </if>
        <if test="isMatch != null and isMatch == 2 ">
            AND status = jgStatus
        </if>
        ORDER by cpId DESC
        <if test="start != null and start >= 0 ">
            limit #{start},#{limit}
        </if>
    </select>

    <select id="queryReportCount" resultType="Integer" parameterType="map">
        SELECT count(*) FROM
        (SELECT c.id ,cp.id as cpId,c.isWaitCall,c.status AS customerStatus,c.manager_id,cp.project_name as projectName,
        cp.contract,c.name,c.phone,c.manager_name as managerName,cp.status,cp.jg_status AS jgStatus,
        cp.is_repeat AS isRepeat,
        (SELECT cs.create_time AS status1_time FROM customer_status_log cs WHERE cs.project_id = cp.project_id AND
        cs.customer_id = cp.customer_id AND cs.to_status_id = 1 AND cs.is_delete = 0 LIMIT 1) AS status1 ,
        (SELECT cs.occur_time AS status2_time FROM customer_status_log cs WHERE cs.project_id = cp.project_id AND
        cs.customer_id = cp.customer_id AND cs.to_status_id = 2 AND cs.is_delete = 0 LIMIT 1) AS status2 ,
        (SELECT cs.occur_time AS status3_time FROM customer_status_log cs WHERE cs.project_id = cp.project_id AND
        cs.customer_id = cp.customer_id AND cs.to_status_id = 3 AND cs.is_delete = 0 LIMIT 1) AS status3 ,
        (SELECT cs.occur_time AS status1_time FROM customer_status_log cs WHERE cs.project_id = cp.project_id AND
        cs.customer_id = cp.customer_id AND cs.to_status_id = 4 AND cs.is_delete = 0 LIMIT 1) AS status4 ,
        (SELECT cs.occur_time AS status2_time FROM customer_status_log cs WHERE cs.project_id = cp.project_id AND
        cs.customer_id = cp.customer_id AND cs.to_status_id = 5 AND cs.is_delete = 0 LIMIT 1) AS status5 ,
        (SELECT cs.occur_time AS status3_time FROM customer_status_log cs WHERE cs.project_id = cp.project_id AND
        cs.customer_id = cp.customer_id AND cs.to_status_id = 6 AND cs.is_delete = 0 LIMIT 1) AS status6
        FROM customer_project cp
        LEFT JOIN customer c ON c.id = cp.customer_id
        WHERE c.cityCode = #{city}
        ) AS temp
        WHERE 1 = 1
        <if test="startTime != null and startTime != '' ">
            <if test="dateType != null and dateType == 1 ">
                AND (status1 BETWEEN #{startTime} AND #{endTime})
            </if>
            <if test="dateType != null and dateType == 2 ">
                AND (status2 BETWEEN #{startTime} AND #{endTime})
            </if>
            <if test="dateType != null and dateType == 3 ">
                AND (status3 BETWEEN #{startTime} AND #{endTime})
            </if>
            <if test="dateType != null and dateType == 4 ">
                AND (status4 BETWEEN #{startTime} AND #{endTime})
            </if>
            <if test="dateType != null and dateType == 5 ">
                AND (status5 BETWEEN #{startTime} AND #{endTime})
            </if>
            <if test="dateType != null and dateType == 6 ">
                AND (status6 BETWEEN #{startTime} AND #{endTime})
            </if>
        </if>
        <if test="name != null and name != '' ">
            AND name LIKE CONCAT('%', #{name},'%')
        </if>
        <if test="phone != null and phone != '' ">
            AND phone LIKE CONCAT('%', #{phone},'%')
        </if>
        <if test="projectName != null and projectName != '' ">
            AND projectName LIKE CONCAT('%', #{projectName},'%')
        </if>
        <if test="isMatch != null and isMatch == 1 ">
            AND status != jgStatus
        </if>
        <if test="isMatch != null and isMatch == 2 ">
            AND status = jgStatus
        </if>
    </select>

    <update id="updateNameAndCode" parameterType="map">
        UPDATE customer_project
        SET
        <if test="projectName != null and projectName != '' ">
            project_name = #{projectName},
        </if>
        <if test="contract != null and contract != '' ">
            contract = #{contract},
        </if>
        update_time = #{updateTime}
        WHERE project_id = #{projectId}
    </update>

    <select id="querySuccessList" resultType="map" parameterType="map">
        SELECT
        cp.status,
        c.id,
        c.name,
        c.phone,
        c.manager_name AS managerName,
        c.manager_id AS managerId,
        c.buy_intention AS buyIntention,
        c.create_time AS createTime,
        c.bind_time AS bindTime,
        c.cityCode
        FROM
        customer_project cp
        LEFT JOIN customer c
        ON cp.customer_id = c.id
        WHERE c.is_delete = 0
        AND cp.status BETWEEN 4 AND 5
        AND cp.is_repeat = 0
        <if test="city != null and city != '' ">
            AND c.cityCode = #{city}
        </if>
        <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="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 c.last_call_time > #{callStartTime}
        </if>
        <if test="callEndTime != null and callEndTime != ''">
            AND #{callEndTime} > c.last_call_time
        </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}))
            OR c.last_manager IN (SELECT realName FROM user WHERE depturlpath LIKE (SELECT CONCAT(depturlpath,'/%') FROM user WHERE id = #{createId}))
            )
        </if>
        <if test="projectName != null and projectName != '' ">
            AND cp.project_name LIKE CONCAT('%', #{projectName},'%')
        </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="sort != null and sort == 5 ">
            ORDER BY c.bind_time DESC
        </if>
        <if test="sort != null and sort == 6 ">
            ORDER BY c.bind_time ASC
        </if>
        <if test="start != null and start >= 0 ">
            limit #{start},#{limit}
        </if>
    </select>

    <select id="querySuccessListCount" resultType="Integer" parameterType="map">
        SELECT
        COUNT(DISTINCT(c.id))
        FROM
        customer_project cp
        LEFT JOIN customer c
        ON cp.customer_id = c.id
        WHERE c.is_delete = 0
        AND cp.status BETWEEN 4 AND 5
        AND cp.is_repeat = 0
        <if test="city != null and city != '' ">
            AND c.cityCode = #{city}
        </if>
        <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="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 c.last_call_time > #{callStartTime}
        </if>
        <if test="callEndTime != null and callEndTime != ''">
            AND #{callEndTime} > c.last_call_time
        </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}))
            OR c.last_manager IN (SELECT realName FROM user WHERE depturlpath LIKE (SELECT CONCAT(depturlpath,'/%') FROM user WHERE id = #{createId}))
            )        </if>
        <if test="projectName != null and projectName != '' ">
            AND cp.project_name LIKE CONCAT('%', #{projectName},'%')
        </if>
    </select>

</mapper>