<?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.ruoyi.ehsMyMessage.mapper.EhsMyMessageMapper">

    <resultMap type="EhsMyMessage" id="EhsMyMessageResult">
            <result property="myMessageId"    column="my_message_id"    />
            <result property="myMessageKey"    column="my_message_key"    />
            <result property="myMessageType"    column="my_message_type"    />
            <result property="myMessageDesc"    column="my_message_desc"    />
            <result property="myMessageState"    column="my_message_state"    />
            <result property="myUserId"    column="my_user_id"    />
            <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="deptId"    column="dept_id"    />
            <result property="createUserId"    column="create_user_id"    />
            <result property="updateUserId"    column="update_user_id"    />
        <result property="noticeStatus"    column="notice_status"    />
        <result property="noticeMesgType"    column="notice_mesg_type"    />
        <result property="noticeDeptId"    column="notice_dept_id"    />
    </resultMap>

    <sql id="selectEhsMyMessageVo">
        select my_message_id, my_message_key, my_message_type, my_message_desc,
        my_message_state, my_user_id, create_by, create_time, update_by, update_time, remark, dept_id,
        create_user_id, update_user_id from ehs_my_message
    </sql>

    <select id="selectEhsMyMessageList" parameterType="EhsMyMessage" resultMap="EhsMyMessageResult">
        <include refid="selectEhsMyMessageVo"/>
        <where>
            <if test="myMessageKey != null  and myMessageKey != ''"> and my_message_key = #{myMessageKey}</if>
            <if test="myMessageType != null  and myMessageType != ''"> and my_message_type = #{myMessageType}</if>
            <if test="myMessageDesc != null  and myMessageDesc != ''"> and my_message_desc = #{myMessageDesc}</if>
            <if test="myMessageState != null  and myMessageState != ''"> and my_message_state = #{myMessageState}</if>
            <if test="myUserId != null "> and my_user_id = #{myUserId}</if>
        </where>
    </select>

    <select id="selectEhsMyMessageByMyMessageId" parameterType="Long" resultMap="EhsMyMessageResult">
            <include refid="selectEhsMyMessageVo"/>
            where my_message_id = #{myMessageId}
    </select>

    <select id="selectEhsMyMessageByMyMessageMax" parameterType="EhsMyMessage"  resultMap="EhsMyMessageResult">
        <include refid="selectEhsMyMessageVo"/>
        where   my_message_id =(
        select max(my_message_id) from ehs_my_message
            <where>
                <if test="myMessageKey != null  and myMessageKey != ''"> and my_message_key = #{myMessageKey}</if>
                <if test="myMessageType != null  and myMessageType != ''"> and my_message_type = #{myMessageType}</if>
                <if test="myMessageDesc != null  and myMessageDesc != ''"> and my_message_desc = #{myMessageDesc}</if>
                <if test="myMessageState != null  and myMessageState != ''"> and my_message_state = #{myMessageState}</if>
                <if test="myUserId != null "> and my_user_id = #{myUserId}</if>
            </where>
         )
        <if test="myMessageKey != null  and myMessageKey != ''"> and my_message_key = #{myMessageKey}</if>
        <if test="myMessageType != null  and myMessageType != ''"> and my_message_type = #{myMessageType}</if>
        <if test="myMessageDesc != null  and myMessageDesc != ''"> and my_message_desc = #{myMessageDesc}</if>
        <if test="myMessageState != null  and myMessageState != ''"> and my_message_state = #{myMessageState}</if>
        <if test="myUserId != null "> and my_user_id = #{myUserId}</if>
    </select>

    <select id="selectUnReadNotice" parameterType="EhsMyMessage" resultMap="EhsMyMessageResult">
        SELECT     m.notice_message_id  my_message_id  FROM  ehs_notice_message m
        LEFT JOIN sys_user e ON e.user_id = m.CREATE_USER_ID
        LEFT JOIN sys_dept d ON m.dept_id = d.dept_id AND d.parent_id != 0
        WHERE m.STATUS = #{noticeStatus}
        AND ( FIND_IN_SET( #{noticeDeptId}, m.dept_list ) OR m.all_dept = 1 )
        AND m.mesg_type = #{noticeMesgType}
        and   m.notice_message_id  not in (
        select my_message_key from ehs_my_message
        <where>
            <if test="myMessageKey != null  and myMessageKey != ''"> and my_message_key = #{myMessageKey}</if>
            <if test="myMessageType != null  and myMessageType != ''"> and my_message_type = #{myMessageType}</if>
            <if test="myMessageDesc != null  and myMessageDesc != ''"> and my_message_desc = #{myMessageDesc}</if>
            <if test="myMessageState != null  and myMessageState != ''"> and my_message_state = #{myMessageState}</if>
            <if test="myUserId != null "> and my_user_id = #{myUserId}</if>
        </where>
        )

    </select>
    <update id="updatebyKeyUserId" parameterType="GenTableColumn">
        update ehs_my_message
          set my_message_state = #{myMessageState}
        where my_message_key = #{myMessageKey} and  my_user_id = #{myUserId}
    </update>
    <update id="updateHDbyKeyUserId" parameterType="GenTableColumn">
        update ehs_my_message
          set my_message_state = #{myMessageState}
        where my_message_type='0'   and  my_user_id = #{myUserId}
    </update>
</mapper>