<?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="org.dromara.member.mapper.AppMessageMapper">

    <resultMap type="org.dromara.member.domain.AppMessage" id="AppMessageResult">
        <result property="messageId" column="message_id"/>
        <result property="title" column="title"/>
        <result property="content" column="content"/>
        <result property="messageType" column="message_type"/>
        <result property="pushTarget" column="push_target"/>
        <result property="targetIds" column="target_ids"/>
        <result property="sendTime" column="send_time"/>
        <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"/>
    </resultMap>

    <resultMap type="org.dromara.member.domain.vo.AppMessageVo" id="AppMessageVoResult">
        <result property="messageId" column="message_id"/>
        <result property="title" column="title"/>
        <result property="content" column="content"/>
        <result property="messageType" column="message_type"/>
        <result property="pushTarget" column="push_target"/>
        <result property="targetIds" column="target_ids"/>
        <result property="sendTime" column="send_time"/>
        <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"/>
    </resultMap>

    <sql id="selectAppMessageVo">
        select message_id,
               title,
               content,
               message_type,
               push_target,
               target_ids,
               send_time,
               status,
               create_by,
               create_time,
               update_by,
               update_time,
               remark
        from app_message
    </sql>

    <select id="selectMessagesByUserId" parameterType="Long" resultMap="AppMessageResult">
        <include refid="selectAppMessageVo"/>
        where (push_target = 'user' and FIND_IN_SET(#{userId}, target_ids))
        or push_target = 'all'
        order by send_time desc
    </select>

    <select id="selectMessagesByRoleId" parameterType="Long" resultMap="AppMessageResult">
        <include refid="selectAppMessageVo"/>
        where push_target = 'role' and FIND_IN_SET(#{roleId}, target_ids)
        order by send_time desc
    </select>

    <select id="selectMessagesForAll" resultMap="AppMessageResult">
        <include refid="selectAppMessageVo"/>
        where push_target = 'all'
        order by send_time desc
    </select>

    <select id="selectAppMessageList" parameterType="AppMessage" resultMap="AppMessageVoResult">
        <include refid="selectAppMessageVo"/>
        <where>
            <if test="title != null and title != ''">and title like concat('%', #{title}, '%')</if>
            <if test="messageType != null and messageType != ''">and message_type = #{messageType}</if>
            <if test="pushTarget != null and pushTarget != ''">and push_target = #{pushTarget}</if>
            <if test="status != null and status != ''">and status = #{status}</if>
            <if test="params.beginTime != null and params.beginTime != ''"><!-- 开始时间检索 -->
                and date_format(send_time,'%y%m%d') &gt;= date_format(#{params.beginTime},'%y%m%d')
            </if>
            <if test="params.endTime != null and params.endTime != ''"><!-- 结束时间检索 -->
                and date_format(send_time,'%y%m%d') &lt;= date_format(#{params.endTime},'%y%m%d')
            </if>
        </where>
        order by send_time desc
    </select>

    <select id="selectAppMessageByMessageId" parameterType="Long" resultMap="AppMessageVoResult">
        <include refid="selectAppMessageVo"/>
        where message_id = #{messageId}
    </select>

    <select id="selectAppMessageByStatus" parameterType="String" resultMap="AppMessageVoResult">
        <include refid="selectAppMessageVo"/>
        where status = #{status}
        order by send_time desc
    </select>

    <insert id="insertAppMessage" parameterType="AppMessage" useGeneratedKeys="true" keyProperty="messageId">
        insert into app_message
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="title != null and title != ''">title,</if>
            <if test="content != null">content,</if>
            <if test="messageType != null and messageType != ''">message_type,</if>
            <if test="pushTarget != null and pushTarget != ''">push_target,</if>
            <if test="targetIds != null">target_ids,</if>
            <if test="sendTime != null">send_time,</if>
            <if test="status != null">status,</if>
            <if test="createBy != null">create_by,</if>
            <if test="remark != null">remark,</if>
            create_time
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="title != null and title != ''">#{title},</if>
            <if test="content != null">#{content},</if>
            <if test="messageType != null and messageType != ''">#{messageType},</if>
            <if test="pushTarget != null and pushTarget != ''">#{pushTarget},</if>
            <if test="targetIds != null">#{targetIds},</if>
            <if test="sendTime != null">#{sendTime},</if>
            <if test="status != null">#{status},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="remark != null">#{remark},</if>
            sysdate()
        </trim>
    </insert>

    <update id="updateAppMessage" parameterType="AppMessage">
        update app_message
        <trim prefix="SET" suffixOverrides=",">
            <if test="title != null and title != ''">title = #{title},</if>
            <if test="content != null">content = #{content},</if>
            <if test="messageType != null and messageType != ''">message_type = #{messageType},</if>
            <if test="pushTarget != null and pushTarget != ''">push_target = #{pushTarget},</if>
            <if test="targetIds != null">target_ids = #{targetIds},</if>
            <if test="sendTime != null">send_time = #{sendTime},</if>
            <if test="status != null">status = #{status},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="remark != null">remark = #{remark},</if>
            update_time = sysdate()
        </trim>
        where message_id = #{messageId}
    </update>

    <update id="updateMessageStatus" parameterType="AppMessage">
        update app_message
        set status      = #{status},
            update_by   = #{updateBy},
            update_time = sysdate()
        where message_id = #{messageId}
    </update>

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

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

    <delete id="deleteAppMessageByStatus" parameterType="String">
        delete
        from app_message
        where status = #{status}
    </delete>

</mapper>