<?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.crontab.mapper.EmailRecipientMapper">

    <!-- 结果映射 -->
    <resultMap id="BaseResultMap" type="org.xyzh.common.dto.crontab.TbCrontabEmailRecipient">
        <id column="id" property="ID" />
        <result column="recipient_id" property="recipientId" />
        <result column="task_id" property="taskId" />
        <result column="user_id" property="userId" />
        <result column="email" property="email" />
        <result column="name" property="name" />
        <result column="creator" property="creator" />
        <result column="updater" property="updater" />
        <result column="create_time" property="createTime" />
        <result column="update_time" property="updateTime" />
        <result column="delete_time" property="deleteTime" />
        <result column="deleted" property="deleted" />
    </resultMap>

    <!-- 字段列表 -->
    <sql id="Base_Column_List">
        id, recipient_id, task_id, user_id, email, name, 
        creator, updater, create_time, update_time, delete_time, deleted
    </sql>

    <!-- 查询条件 -->
    <sql id="Base_Where_Clause">
        <where>
            deleted = 0
            <if test="filter.recipientId != null and filter.recipientId != ''">
                AND recipient_id = #{filter.recipientId}
            </if>
            <if test="filter.taskId != null and filter.taskId != ''">
                AND task_id = #{filter.taskId}
            </if>
            <if test="filter.userId != null and filter.userId != ''">
                AND user_id = #{filter.userId}
            </if>
            <if test="filter.email != null and filter.email != ''">
                AND email LIKE CONCAT('%', #{filter.email}, '%')
            </if>
        </where>
    </sql>

    <!-- 插入邮件接收人 -->
    <insert id="insertRecipient" parameterType="org.xyzh.common.dto.crontab.TbCrontabEmailRecipient">
        INSERT INTO tb_crontab_email_recipient (
            id, recipient_id, task_id, user_id, email, name, creator, create_time
        ) VALUES (
            #{recipient.ID}, #{recipient.recipientId}, #{recipient.taskId}, 
            #{recipient.userId}, #{recipient.email}, #{recipient.name}, 
            #{recipient.creator}, NOW()
        )
    </insert>

    <!-- 批量插入邮件接收人 -->
    <insert id="batchInsertRecipient">
        INSERT INTO tb_crontab_email_recipient (
            id, recipient_id, task_id, user_id, email, name, creator, create_time
        ) VALUES
        <foreach collection="recipients" item="item" separator=",">
            (#{item.ID}, #{item.recipientId}, #{item.taskId}, 
             #{item.userId}, #{item.email}, #{item.name}, #{item.creator}, NOW())
        </foreach>
    </insert>

    <!-- 更新邮件接收人 -->
    <update id="updateRecipient" parameterType="org.xyzh.common.dto.crontab.TbCrontabEmailRecipient">
        UPDATE tb_crontab_email_recipient
        SET task_id = #{recipient.taskId},
            user_id = #{recipient.userId},
            email = #{recipient.email},
            name = #{recipient.name},
            updater = #{recipient.updater},
            update_time = NOW()
        WHERE recipient_id = #{recipient.recipientId}
          AND deleted = 0
    </update>

    <!-- 删除邮件接收人（逻辑删除） -->
    <update id="deleteRecipient">
        UPDATE tb_crontab_email_recipient
        SET deleted = 1,
            delete_time = NOW()
        WHERE recipient_id = #{recipientId}
          AND deleted = 0
    </update>

    <!-- 根据ID查询 -->
    <select id="selectRecipientById" resultMap="BaseResultMap">
        SELECT <include refid="Base_Column_List" />
        FROM tb_crontab_email_recipient
        WHERE recipient_id = #{recipientId}
          AND deleted = 0
    </select>

    <!-- 根据任务ID查询接收人列表 -->
    <select id="selectRecipientsByTaskId" resultMap="BaseResultMap">
        SELECT <include refid="Base_Column_List" />
        FROM tb_crontab_email_recipient
        WHERE task_id = #{taskId}
          AND deleted = 0
        ORDER BY create_time ASC
    </select>

    <!-- 查询所有接收人 -->
    <select id="selectAllRecipients" resultMap="BaseResultMap">
        SELECT <include refid="Base_Column_List" />
        FROM tb_crontab_email_recipient
        WHERE deleted = 0
        ORDER BY create_time ASC
    </select>

    <!-- 分页查询 -->
    <select id="selectRecipientPage" resultMap="BaseResultMap">
        SELECT <include refid="Base_Column_List" />
        FROM tb_crontab_email_recipient
        <include refid="Base_Where_Clause" />
        ORDER BY create_time DESC
        LIMIT #{pageParam.offset}, #{pageParam.pageSize}
    </select>

    <!-- 查询总数 -->
    <select id="countSelectRecipient" resultType="int">
        SELECT COUNT(*)
        FROM tb_crontab_email_recipient
        <include refid="Base_Where_Clause" />
    </select>


    <!-- 删除任务的所有接收人 -->
    <update id="deleteRecipientsByTaskId">
        UPDATE tb_crontab_email_recipient
        SET deleted = 1,
            delete_time = NOW()
        WHERE task_id = #{taskId}
          AND deleted = 0
    </update>

</mapper>