<?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.fs.crm.mapper.CrmEventMapper">
    
    <resultMap type="CrmEvent" id="CrmEventResult">
        <result property="eventId"    column="event_id"    />
        <result property="title"    column="title"    />
        <result property="eventType"    column="event_type"    />
        <result property="eventTime"    column="event_time"    />
        <result property="startTime"    column="start_time"    />
        <result property="finishTime"    column="finish_time"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateTime"    column="update_time"    />
        <result property="content"    column="content"    />
        <result property="companyId"    column="company_id"    />
        <result property="companyUserId"    column="company_user_id"    />
        <result property="status"    column="status"    />
    </resultMap>

    <sql id="selectCrmEventVo">
        select event_id, title, event_type, event_time, create_time, update_time, content, company_id, company_user_id,status,start_time,finish_time from crm_event
    </sql>

    <select id="selectCrmEventList" parameterType="CrmEvent" resultMap="CrmEventResult">
        <include refid="selectCrmEventVo"/>
        <where>  
            <if test="title != null  and title != ''"> and title = #{title}</if>
            <if test="eventType != null "> and event_type = #{eventType}</if>
            <if test="eventTime != null "> and event_time = #{eventTime}</if>
            <if test="content != null  and content != ''"> and content = #{content}</if>
            <if test="companyId != null "> and company_id = #{companyId}</if>
            <if test="companyUserId != null "> and company_user_id = #{companyUserId}</if>
        </where>
    </select>
    
    <select id="selectCrmEventById" parameterType="Long" resultMap="CrmEventResult">
        <include refid="selectCrmEventVo"/>
        where event_id = #{eventId}
    </select>
        
    <insert id="insertCrmEvent" parameterType="CrmEvent" useGeneratedKeys="true" keyProperty="eventId">
        insert into crm_event
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="title != null and title != ''">title,</if>
            <if test="eventType != null">event_type,</if>
            <if test="eventTime != null">event_time,</if>
            <if test="startTime != null">start_time,</if>
            <if test="finishTime != null">finish_time,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="content != null">content,</if>
            <if test="companyId != null">company_id,</if>
            <if test="companyUserId != null">company_user_id,</if>
            <if test="status != null">status,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="title != null and title != ''">#{title},</if>
            <if test="eventType != null">#{eventType},</if>
            <if test="eventTime != null">#{eventTime},</if>
            <if test="startTime != null">#{startTime},</if>
            <if test="finishTime != null">#{finishTime},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="content != null">#{content},</if>
            <if test="companyId != null">#{companyId},</if>
            <if test="companyUserId != null">#{companyUserId},</if>
            <if test="status != null">#{status},</if>
         </trim>
    </insert>

    <update id="updateCrmEvent" parameterType="CrmEvent">
        update crm_event
        <trim prefix="SET" suffixOverrides=",">
            <if test="title != null and title != ''">title = #{title},</if>
            <if test="eventType != null">event_type = #{eventType},</if>
            <if test="eventTime != null">event_time = #{eventTime},</if>
            <if test="startTime != null">start_time = #{startTime},</if>
            <if test="finishTime != null">finish_time = #{finishTime},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="content != null">content = #{content},</if>
            <if test="companyId != null">company_id = #{companyId},</if>
            <if test="companyUserId != null">company_user_id = #{companyUserId},</if>
            <if test="status != null">status = #{status},</if>
        </trim>
        where event_id = #{eventId}
    </update>

    <delete id="deleteCrmEventById" parameterType="Long">
        delete from crm_event where event_id = #{eventId}
    </delete>

    <delete id="deleteCrmEventByIds" parameterType="String">
        delete from crm_event where event_id in 
        <foreach item="eventId" collection="array" open="(" separator="," close=")">
            #{eventId}
        </foreach>
    </delete>
    
</mapper>