<?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.CrmMsgMapper">
    
    <resultMap type="CrmMsg" id="CrmMsgResult">
        <result property="msgId"    column="msg_id"    />
        <result property="msgType"    column="msg_type"    />
        <result property="title"    column="title"    />
        <result property="content"    column="content"    />
        <result property="createTime"    column="create_time"    />
        <result property="isRead"    column="is_read"    />
        <result property="companyId"    column="company_id"    />
        <result property="companyUserId"    column="company_user_id"    />
        <result property="updateTime"    column="update_time"    />
        <result property="objId"    column="obj_id"    />
    </resultMap>

    <sql id="selectCrmMsgVo">
        select msg_id, msg_type, title, content, create_time, is_read, company_id, company_user_id, update_time,obj_id from crm_msg
    </sql>

    <select id="selectCrmMsgList" parameterType="CrmMsg" resultMap="CrmMsgResult">
        <include refid="selectCrmMsgVo"/>
        <where>  
            <if test="msgType != null "> and msg_type = #{msgType}</if>
            <if test="title != null  and title != ''"> and title = #{title}</if>
            <if test="content != null  and content != ''"> and content = #{content}</if>
            <if test="isRead != null "> and is_read = #{isRead}</if>
            <if test="companyId != null "> and company_id = #{companyId}</if>
            <if test="companyUserId != null "> and company_user_id = #{companyUserId}</if>
        </where>
        order by msg_id desc
    </select>
    
    <select id="selectCrmMsgById" parameterType="Long" resultMap="CrmMsgResult">
        <include refid="selectCrmMsgVo"/>
        where msg_id = #{msgId}
    </select>
        
    <insert id="insertCrmMsg" parameterType="CrmMsg" useGeneratedKeys="true" keyProperty="msgId">
        insert into crm_msg
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="msgType != null">msg_type,</if>
            <if test="title != null">title,</if>
            <if test="content != null">content,</if>
            <if test="createTime != null">create_time,</if>
            <if test="isRead != null">is_read,</if>
            <if test="companyId != null">company_id,</if>
            <if test="companyUserId != null">company_user_id,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="objId != null">obj_id,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="msgType != null">#{msgType},</if>
            <if test="title != null">#{title},</if>
            <if test="content != null">#{content},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="isRead != null">#{isRead},</if>
            <if test="companyId != null">#{companyId},</if>
            <if test="companyUserId != null">#{companyUserId},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="objId != null">#{objId},</if>
         </trim>
    </insert>

    <update id="updateCrmMsg" parameterType="CrmMsg">
        update crm_msg
        <trim prefix="SET" suffixOverrides=",">
            <if test="msgType != null">msg_type = #{msgType},</if>
            <if test="title != null">title = #{title},</if>
            <if test="content != null">content = #{content},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="isRead != null">is_read = #{isRead},</if>
            <if test="companyId != null">company_id = #{companyId},</if>
            <if test="companyUserId != null">company_user_id = #{companyUserId},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="objId != null">obj_id = #{objId},</if>
        </trim>
        where msg_id = #{msgId}
    </update>

    <delete id="deleteCrmMsgById" parameterType="Long">
        delete from crm_msg where msg_id = #{msgId}
    </delete>

    <delete id="deleteCrmMsgByIds" parameterType="String">
        delete from crm_msg where msg_id in 
        <foreach item="msgId" collection="array" open="(" separator="," close=")">
            #{msgId}
        </foreach>
    </delete>
    
</mapper>