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

    <resultMap id="infoMap" type="com.house365.beans.entity.CustomerRemindEntity">
        <result property="id" column="id"/>
        <result property="customerId" column="customer_id"/>
        <result property="customerName" column="customer_name"/>
        <result property="cityCode" column="city_code"/>
        <result property="createUserId" column="create_user_id"/>
        <result property="createTime" column="create_time"/>
        <result property="returnRemindTime" column="return_remind_time"/>
        <result property="isDelete" column="is_delete"/>
    </resultMap>

    <insert id="save" parameterType="com.house365.beans.entity.CustomerRemindEntity">
        INSERT INTO customer_remind (
            customer_id,
            customer_name,
            city_code,
            create_user_id,
            create_time,
            return_remind_time,
            is_delete
        )
        VALUES (
            #{customerId},
            #{customerName},
            #{cityCode},
            #{createUserId},
            #{createTime},
            #{returnRemindTime},
            #{isDelete}
        )
    </insert>

    <update id="update" parameterType="com.house365.beans.entity.CustomerRemindEntity">
        UPDATE customer_remind
        SET
        return_remind_time = #{returnRemindTime}
        WHERE id= #{id}
    </update>

    <delete id="delete" parameterType="Integer">
        delete FROM customer_remind
        WHERE id = #{id}
    </delete>

    <select id="getById" resultMap="infoMap" resultType="Integer">
        SELECT
        *
        FROM customer_remind
        where
        is_delete = 0 AND id = #{id}
    </select>

    <select id="queryByConditions" resultMap="infoMap" resultType="java.util.HashMap">
        SELECT
         *
        FROM customer_remind
        where
        is_delete = 0
        <if test="cityCode != null and cityCode != '' ">
            AND city_code = #{cityCode}
        </if>
        <if test="userId != null and userId >=0">
            AND create_user_id = #{userId}
        </if>
        <if test="customerId != null and customerId >=0">
            AND customer_id = #{customerId}
        </if>
        <if test="returnRemindTime != null and returnRemindTime !='' ">
            AND return_remind_time BETWEEN (SELECT SUBDATE(NOW(),INTERVAL 5 MINUTE)) AND (SELECT SUBDATE(NOW(),INTERVAL -5 MINUTE))
        </if>
        <if test="time != null and time !='' ">
            AND NOW() > return_remind_time
        </if>
        order by return_remind_time ASC
    </select>

    <update id="updateUser" parameterType="java.util.HashMap">
        UPDATE customer_remind
        SET
        is_delete = 1
        create_user_id = #{userId}
        WHERE id= #{id}
    </update>

    <update id="updateStatus" parameterType="java.util.HashMap">
        UPDATE customer_remind
        SET
        is_delete = 1
        <if test="returnRemindTime != null and returnRemindTime !='' ">
            , return_remind_time = ''
        </if>
        WHERE customer_id = #{customerId}
    </update>

</mapper>