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

    <resultMap id="infoMap" type="com.house365.beans.entity.CustomerStatusLogEntity">
        <result property="id" column="id"/>
        <result property="fromStatusId" column="from_status_id"/>
        <result property="toStatusId" column="to_status_id"/>
        <result property="fromStatusName" column="from_status_name"/>
        <result property="toStatusName" column="to_status_name"/>
        <result property="remark" column="remark"/>
        <result property="creater" column="creater"/>
        <result property="createTime" column="create_time"/>
        <result property="occurTime" column="occur_time"/>
        <result property="customerId" column="customer_id"/>
        <result property="projectId" column="project_id"/>
        <result property="isRepeat" column="isRepeat"/>
        <result property="nonStatistic" column="nonStatistic"/>
        <result property="customerManagerId" column="customerManagerId"/>
        <result property="customerManagerName" column="customerManagerName"/>
        <result property="isDelete" column="is_delete"/>
    </resultMap>

    <insert id="save" parameterType="com.house365.beans.entity.CustomerStatusLogEntity">
	        INSERT INTO customer_status_log (
	        	from_status_id,
	        	to_status_id,
	        	from_status_name,
	        	to_status_name,
	        	remark,
	        	creater,
	        	create_time,
	        	customer_id,
	        	project_id,
	        	isRepeat,
	        	nonStatistic,
	        	customerManagerId,
	        	customerManagerName,
	        	occur_time,
	        	is_delete
	        ) VALUES (
	        	#{fromStatusId},
	        	#{toStatusId},
	        	#{fromStatusName},
	        	#{toStatusName},
	        	#{remark},
	        	#{creater},
	        	#{createTime},
	        	#{customerId},
	        	#{projectId},
	        	#{isRepeat},
	        	#{nonStatistic},
	        	#{customerManagerId},
	        	#{customerManagerName},
	        	#{occurTime},
	        	#{isDelete}
	        )
    </insert>

    <select id="queryById" resultMap="infoMap" parameterType="int">
        SELECT
        *
        FROM customer_status_log
        WHERE
        id = #{id}
    </select>

    <select id="queryByConditions" resultMap="infoMap" parameterType="map">
        SELECT *
        FROM customer_status_log
        where is_delete = 0
        <if test="fromStatusId != null and fromStatusId > 0 ">
            AND from_status_id = #{fromStatusId}
        </if>
        <if test="toStatusId != null and toStatusId > 0 ">
            AND to_status_id = #{toStatusId}
        </if>
        <if test="customerId != null and customerId != '' ">
            AND customer_id = #{customerId}
        </if>
        <if test="projectId != null and projectId != '' ">
            AND project_id = #{projectId}
        </if>
        ORDER by occur_time DESC
    </select>

    <update id="updateIsDelete" parameterType="map">
        UPDATE customer_status_log
        SET
        is_delete = 1
        where 1 = 1
        <if test="toStatusId != null and toStatusId > 0 ">
            AND to_status_id >= #{toStatusId}
        </if>
        <if test="customerId != null and customerId != '' ">
            AND customer_id = #{customerId}
        </if>
        <if test="id != null and id > 0 ">
            AND id = #{id}
        </if>
    </update>

    <update id="updateNewProjectId" parameterType="map">
        UPDATE customer_status_log
        SET
        project_id = #{newPid}
        where 1 = 1
        <if test="customerId != null and customerId != '' ">
            AND customer_id = #{customerId}
        </if>
        <if test="oldPid != null and oldPid != '' ">
            AND project_id = #{oldPid}
        </if>
    </update>

    <select id="queryByCustomer" resultMap="infoMap">
       SELECT customer_id ,project_id FROM customer_status_log
       WHERE to_status_id IN (1,2,3,4,5,6)
       AND is_delete = 0
       GROUP BY customer_id,project_id
    </select>

    <select id="queryByCustomerId" resultMap="infoMap">
        SELECT *
        FROM customer_status_log
        where is_delete = 0
        AND customer_id = #{customerId}
        <if test="toStatusId != null and toStatusId > 0 ">
            AND to_status_id = #{toStatusId}
        </if>
        <if test="projectId != null and projectId > 0 ">
            AND project_id = #{projectId}
        </if>
        AND to_status_id IN (1,2,3,4,5,6)
        ORDER BY id DESC
    </select>

    <select id="queryByStatus" resultMap="infoMap" parameterType="map">
        SELECT *
        FROM customer_status_log
        where is_delete = 0
        <if test="customerId != null and customerId != '' ">
            AND customer_id = #{customerId}
        </if>
        <if test="projectId != null and projectId != '' ">
            AND project_id = #{projectId}
        </if>
        <if test="toStatusId != null and toStatusId > 0 ">
            AND #{toStatusId} > to_status_id
        </if>
        <if test="toStatusId != null and toStatusId == 2 ">
            ORDER BY create_time DESC
        </if>
        <if test="toStatusId != null and toStatusId > 2 ">
            ORDER BY occur_time DESC
        </if>
    </select>

    <update id="updateRepeatStatus" parameterType="map">
        UPDATE customer_status_log
        SET
        isRepeat = 1
        WHERE customer_id = #{customerId} AND project_id = #{projectId}
    </update>

    <update id="updateMajorCustomer" parameterType="map">
        UPDATE customer_status_log
        SET
        is_delete = 1,
        nonStatistic = 0
        WHERE customer_id = #{customerId} AND project_id = #{projectId} AND to_status_id > 1
    </update>

    <select id="queryCloudCount" resultType="map" parameterType="map">
        SELECT
        csl.id as id,c.phone as phone,csl.customer_id as customerId,c.manager_name as managerName,c.name as name,
        csl.project_id as projectId,csl.occur_time as occurTime
        FROM customer_status_log csl
        left JOIN customer c on c.id = csl.customer_id
        where csl.is_delete = 0
        AND c.is_delete = 0
        <if test="status != null and status > 0 ">
            AND to_status_id = #{status}
        </if>
        <if test="buyStatus != null and buyStatus > 0 ">
            AND to_status_id in (4,5)
            AND (csl.occur_time BETWEEN #{startTime} AND #{endTime})
        </if>
        <if test="idList != null and idList.size() > 0 ">
            AND csl.customer_id in
            <foreach collection="idList" index="index" item="item" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
        <if test="startTime != null and startTime != '' and status == 1">
            AND (csl.create_time BETWEEN #{startTime} AND #{endTime})
        </if>
        <if test="startTime != null and startTime != '' and status > 1">
            AND (csl.occur_time BETWEEN #{startTime} AND #{endTime})
        </if>
        GROUP by c.phone
        <if test="start != null and start >= 0 ">
            limit #{start},#{limit}
        </if>
    </select>

</mapper>