<?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.web.modules.entry.mapper.BizMemberMessageMapper">

    <resultMap id="BizMemberMessageResult" type="com.ruoyi.web.modules.entry.domain.BizMemberMessage">
        <id property="messageId" column="message_id"/>
        <result property="memberId" column="member_id"/>
        <result property="bizType" column="biz_type"/>
        <result property="bizId" column="biz_id"/>
        <result property="title" column="title"/>
        <result property="content" column="content"/>
        <result property="readFlag" column="read_flag"/>
        <result property="createTime" column="create_time"/>
    </resultMap>

    <insert id="insert" useGeneratedKeys="true" keyProperty="messageId">
        insert into biz_member_message (
            member_id, biz_type, biz_id, title, content, read_flag, create_time
        ) values (
            #{memberId}, #{bizType}, #{bizId}, #{title}, #{content}, #{readFlag}, sysdate()
        )
    </insert>

    <select id="countUnreadByMemberId" resultType="int">
        select count(1)
        from biz_member_message
        where member_id = #{memberId}
          and read_flag = '0'
    </select>

    <select id="selectListByMemberId" resultMap="BizMemberMessageResult">
        select message_id, member_id, biz_type, biz_id, title, content, read_flag, create_time
        from biz_member_message
        where member_id = #{memberId}
        order by create_time desc
    </select>

    <select id="selectByIdAndMemberId" resultMap="BizMemberMessageResult">
        select message_id, member_id, biz_type, biz_id, title, content, read_flag, create_time
        from biz_member_message
        where message_id = #{messageId}
          and member_id = #{memberId}
    </select>

    <update id="updateReadFlag">
        update biz_member_message
        set read_flag = #{readFlag}
        where message_id = #{messageId}
          and member_id = #{memberId}
    </update>

</mapper>
