<?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.jlt.crm.mapper.VisitorMapper">
    
    <resultMap type="Visitor" id="VisitorResult">
        <result property="visitorId"    column="visitor_id"    />
        <result property="visitorName"    column="visitor_name"    />
        <result property="phone"    column="phone"    />
        <result property="matter"    column="matter"    />
        <result property="backlog"    column="backlog"    />
        <result property="visitTime"    column="visit_time"    />
        <result property="status"    column="status"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />
        <result property="remark"    column="remark"    />
    </resultMap>

    <sql id="selectVisitorVo">
        select visitor_id, visitor_name, phone, matter, backlog, visit_time, status, create_by, create_time, update_by, update_time, remark from crm_visitor
    </sql>

    <select id="selectVisitorList" parameterType="Visitor" resultMap="VisitorResult">
        <include refid="selectVisitorVo"/>
        <where>  
            <if test="visitorName != null  and visitorName != ''"> and visitor_name like concat('%', #{visitorName}, '%')</if>
            <if test="phone != null  and phone != ''"> and phone like concat('%', #{phone}, '%')</if>
            <if test="matter != null  and matter != ''"> and matter like concat('%', #{matter}, '%')</if>
            <if test="backlog != null  and backlog != ''"> and backlog like concat('%', #{backlog}, '%')</if>
            <if test="params.beginVisitTime != null and params.beginVisitTime != '' and params.endVisitTime != null and params.endVisitTime != ''"> and visit_time between #{params.beginVisitTime} and #{params.endVisitTime}</if>
            <if test="status != null  and status != ''"> and status = #{status}</if>
        </where>
    </select>
    
    <select id="selectVisitorByVisitorId" parameterType="Long" resultMap="VisitorResult">
        <include refid="selectVisitorVo"/>
        where visitor_id = #{visitorId}
    </select>
        
    <insert id="insertVisitor" parameterType="Visitor" useGeneratedKeys="true" keyProperty="visitorId">
        insert into crm_visitor
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="visitorName != null and visitorName != ''">visitor_name,</if>
            <if test="phone != null and phone != ''">phone,</if>
            <if test="matter != null and matter != ''">matter,</if>
            <if test="backlog != null">backlog,</if>
            <if test="visitTime != null">visit_time,</if>
            <if test="status != null and status != ''">status,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="remark != null">remark,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="visitorName != null and visitorName != ''">#{visitorName},</if>
            <if test="phone != null and phone != ''">#{phone},</if>
            <if test="matter != null and matter != ''">#{matter},</if>
            <if test="backlog != null">#{backlog},</if>
            <if test="visitTime != null">#{visitTime},</if>
            <if test="status != null and status != ''">#{status},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="remark != null">#{remark},</if>
         </trim>
    </insert>

    <update id="updateVisitor" parameterType="Visitor">
        update crm_visitor
        <trim prefix="SET" suffixOverrides=",">
            <if test="visitorName != null and visitorName != ''">visitor_name = #{visitorName},</if>
            <if test="phone != null and phone != ''">phone = #{phone},</if>
            <if test="matter != null and matter != ''">matter = #{matter},</if>
            <if test="backlog != null">backlog = #{backlog},</if>
            <if test="visitTime != null">visit_time = #{visitTime},</if>
            <if test="status != null and status != ''">status = #{status},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="remark != null">remark = #{remark},</if>
        </trim>
        where visitor_id = #{visitorId}
    </update>

    <delete id="deleteVisitorByVisitorId" parameterType="Long">
        delete from crm_visitor where visitor_id = #{visitorId}
    </delete>

    <delete id="deleteVisitorByVisitorIds" parameterType="String">
        delete from crm_visitor where visitor_id in
        <foreach item="visitorId" collection="array" open="(" separator="," close=")">
            #{visitorId}
        </foreach>
    </delete>
</mapper>