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

    <resultMap id="infoMap" type="com.house365.beans.entity.CustomerLogEntity">
        <result property="id" column="id"/>
        <result property="relateId" column="relate_id"/>
        <result property="customerPhone" column="customer_phone"/>
        <result property="managerId" column="manager_id"/>
        <result property="type" column="type"/>
        <result property="modifyColumn" column="modify_column"/>
        <result property="modifyName" column="modify_name"/>
        <result property="oldValue" column="old_value"/>
        <result property="newValue" column="new_value"/>
        <result property="createUserId" column="create_user_id"/>
        <result property="createUserName" column="create_user_name"/>
        <result property="createTime" column="create_time"/>
    </resultMap>

    <insert id="save" parameterType="com.house365.beans.entity.CustomerLogEntity">
        INSERT INTO customer_log (
            relate_id,
            customer_phone,
            manager_id,
            type,
            modify_column,
            modify_name,
            old_value,
            new_value,
            create_user_id,
            create_user_name,
            create_time
        )
        VALUES (
            #{relateId},
            #{customerPhone},
            #{managerId},
            #{type},
            #{modifyColumn},
            #{modifyName},
            #{oldValue},
            #{newValue},
            #{createUserId},
            #{createUserName},
            #{createTime}
        )
    </insert>

    <select id="queryByConditions" resultMap="infoMap" parameterType="map">
        SELECT *
        FROM customer_log
        where 1 = 1
        <if test="relateId != null and relateId != ''">
            AND relate_id = #{relateId}
        </if>
        <if test="phone != null and phone != ''">
            AND customer_phone = #{phone}
        </if>
        <if test="managerId != null and managerId != ''">
            AND manager_id = #{managerId}
        </if>
        <if test="createTime != null and createTime != ''">
            AND create_time = #{createTime}
        </if>
        <if test="type != null and type != ''">
            AND type = #{type}
        </if>
        <if test="idList != null and idList.size() > 0 ">
            AND relate_id in
            <foreach collection="idList" index="index" item="item" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
        <if test="time != null and time != ''">
            GROUP BY create_time
        </if>
        ORDER BY create_time DESC
    </select>

    <update id="updateManagerId" parameterType="map">
        UPDATE customer_log
        SET
        manager_id = #{managerId}
        where customer_phone = #{phone}
        AND manager_id = #{oldManagerId}
    </update>

</mapper>