<?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.xyzh.message.mapper.MessageTargetMapper">

    <!-- Result Map -->
    <resultMap id="BaseResultMap" type="org.xyzh.common.dto.message.TbSysMessageTarget">
        <id column="id" property="id" jdbcType="VARCHAR"/>
        <result column="message_id" property="messageID" jdbcType="VARCHAR"/>
        <result column="send_method" property="sendMethod" jdbcType="VARCHAR"/>
        <result column="target_type" property="targetType" jdbcType="VARCHAR"/>
        <result column="target_id" property="targetID" jdbcType="VARCHAR"/>
        <result column="target_name" property="targetName" jdbcType="VARCHAR"/>
        <result column="scope_dept_id" property="scopeDeptID" jdbcType="VARCHAR"/>
        <result column="creator" property="creator" jdbcType="VARCHAR"/>
        <result column="updater" property="updater" jdbcType="VARCHAR"/>
        <result column="create_time" property="createTime" jdbcType="TIMESTAMP"/>
        <result column="update_time" property="updateTime" jdbcType="TIMESTAMP"/>
        <result column="delete_time" property="deleteTime" jdbcType="TIMESTAMP"/>
        <result column="deleted" property="deleted" jdbcType="BOOLEAN"/>
    </resultMap>

    <!-- 根据消息ID查询接收对象列表 -->
    <select id="selectByMessageID" resultMap="BaseResultMap">
        SELECT *
        FROM tb_sys_message_target
        WHERE message_id = #{messageID}
        AND deleted = 0
        ORDER BY create_time ASC
    </select>

    <!-- 批量插入接收对象 -->
    <insert id="batchInsert">
        INSERT INTO tb_sys_message_target
        (id, message_id, send_method, target_type, target_id, target_name, scope_dept_id, creator, create_time, update_time, deleted)
        VALUES
        <foreach collection="targets" item="item" separator=",">
            (#{item.id}, #{item.messageID}, #{item.sendMethod}, #{item.targetType}, #{item.targetID},
             #{item.targetName}, #{item.scopeDeptID}, #{item.creator}, NOW(), NOW(), 0)
        </foreach>
    </insert>

    <!-- 根据消息ID删除接收对象 -->
    <update id="deleteByMessageID">
        UPDATE tb_sys_message_target
        SET deleted = 1,
            delete_time = NOW(),
            update_time = NOW()
        WHERE message_id = #{messageID}
        AND deleted = 0
    </update>

</mapper>
