<?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.zehong.system.mapper.TMessageInfoMapper">
    
    <resultMap type="TMessageInfo" id="TMessageInfoResult">
        <result property="messageId"    column="message_id"    />
        <result property="messageTitle"    column="message_title"    />
        <result property="messageContent"    column="message_content"    />
        <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"    />
        <result property="topping"  column="topping"/>
        <result property="fujianUrl"  column="fujian_url"/>
    </resultMap>
    <resultMap type="TMessageInfo" id="TMessageInfoResultapp">
        <result property="messageId"    column="message_id"    />
        <result property="messageTitle"    column="message_title"    />
        <result property="messageContent"    column="message_content"    />
        <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"    />
        <result property="messageDetailStatus" column="message_detail_status" />
        <result property="messageDetailId" column="message_detail_id"/>
        <result property="topping"  column="topping"/>
        <result property="fujianUrl"  column="fujian_url"/>
    </resultMap>

    <sql id="selectTMessageInfoVo">
        SELECT
            msg.message_id,
            msg.message_title,
            msg.message_content,
            msg.STATUS,
            msg.create_by,
            msg.create_time,
            msg.update_by,
            msg.update_time,
            msg.remark,
            msg.topping,
            msg.fujian_url
        FROM
            t_message_info msg
    </sql>

    <select id="selectTMessageInfoList" parameterType="TMessageInfo" resultMap="TMessageInfoResult">
        <include refid="selectTMessageInfoVo"/>
        <where>  
            <if test="messageTitle != null  and messageTitle != ''"> and msg.message_title like concat('%', #{messageTitle}, '%')</if>
            <if test="messageContent != null  and messageContent != ''"> and msg.message_content = #{messageContent}</if>
            <if test="status != null  and status != ''"> and msg.status = #{status}</if>
        </where>
        order by msg.topping desc, msg.create_time desc
    </select>
    <select id="selectTMessageInfoListapp" parameterType="TMessageInfo" resultMap="TMessageInfoResultapp">
        SELECT
            msg.message_id,
            msg.message_title,
            msg.message_content,
            msg.STATUS,
            msg.create_by,
            msg.create_time,
            msg.update_by,
            msg.update_time,
            msg.remark,
            msg.topping,
            msg.fujian_url,
            detail.message_detail_id,
            detail.message_detail_status
        FROM
            t_message_info msg
        LEFT JOIN t_message_detail_info detail ON detail.message_id = msg.message_id
        WHERE detail.person_id =#{personId}
        and msg.`status` = 0
        order by msg.topping desc,detail.message_detail_status ,msg.create_time desc
    </select>
    <select id="selectTMessageInfoById" parameterType="Long" resultMap="TMessageInfoResult">
        <include refid="selectTMessageInfoVo"/>
        where msg.message_id = #{messageId}
    </select>
        
    <insert id="insertTMessageInfo" parameterType="TMessageInfo" useGeneratedKeys="true" keyProperty="messageId">
        insert into t_message_info
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="messageTitle != null and messageTitle != ''">message_title,</if>
            <if test="messageContent != null">message_content,</if>
            <if test="status != null">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>
            <if test="topping != null">topping,</if>
            <if test="fujianUrl != null">fujian_url,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="messageTitle != null and messageTitle != ''">#{messageTitle},</if>
            <if test="messageContent != null">#{messageContent},</if>
            <if test="status != null">#{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>
            <if test="topping != null">#{topping},</if>
            <if test="fujianUrl != null">#{fujianUrl},</if>
         </trim>
    </insert>

    <update id="updateTMessageInfo" parameterType="TMessageInfo">
        update t_message_info
        <trim prefix="SET" suffixOverrides=",">
            <if test="messageTitle != null and messageTitle != ''">message_title = #{messageTitle},</if>
            <if test="messageContent != null">message_content = #{messageContent},</if>
            <if test="status != null">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>
            <if test="topping != null">topping = #{topping},</if>
            <if test="fujianUrl != null">fujian_url = #{fujianUrl},</if>
        </trim>
        where message_id = #{messageId}
    </update>

    <delete id="deleteTMessageInfoById" parameterType="Long">
        delete from t_message_info where message_id = #{messageId}
    </delete>

    <delete id="deleteTMessageInfoByIds" parameterType="String">
        delete from t_message_info where message_id in 
        <foreach item="messageId" collection="array" open="(" separator="," close=")">
            #{messageId}
        </foreach>
    </delete>
</mapper>