<?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.EmailDefaultMapper">

    <!-- 结果映射 -->
    <resultMap id="BaseResultMap" type="org.xyzh.common.dto.crontab.TbCrontabEmailDefault">
        <id column="id" property="ID" />
        <result column="default_id" property="defaultId" />
        <result column="meta_id" property="metaId" />
        <result column="user_id" property="userId" />
        <result column="user_email" property="userEmail" />
        <result column="username" property="username" />
        <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, default_id, meta_id, user_id, 
        creator, updater, create_time, update_time, delete_time, deleted
    </sql>

    <!-- 插入默认接收人 -->
    <insert id="insertDefault" parameterType="org.xyzh.common.dto.crontab.TbCrontabEmailDefault">
        INSERT INTO tb_crontab_email_default (
            id, default_id, meta_id, user_id, creator, create_time
        ) VALUES (
            #{emailDefault.ID}, #{emailDefault.defaultId}, #{emailDefault.metaId}, 
            #{emailDefault.userId}, #{emailDefault.creator}, NOW()
        )
    </insert>

    <!-- 更新默认接收人 -->
    <update id="updateDefault" parameterType="org.xyzh.common.dto.crontab.TbCrontabEmailDefault">
        UPDATE tb_crontab_email_default
        SET meta_id = #{emailDefault.metaId},
            user_id = #{emailDefault.userId},
            updater = #{emailDefault.updater},
            update_time = NOW()
        WHERE default_id = #{emailDefault.defaultId}
          AND deleted = 0
    </update>

    <!-- 删除默认接收人（逻辑删除） -->
    <update id="deleteDefault">
        UPDATE tb_crontab_email_default
        SET deleted = 1,
            delete_time = NOW()
        WHERE default_id = #{defaultId}
          AND deleted = 0
    </update>

    <!-- 根据defaultId查询 -->
    <select id="selectDefaultById" resultMap="BaseResultMap">
        SELECT tced.id, tced.default_id, tced.meta_id, tced.user_id, tu.email as user_email, tu.username
        FROM tb_crontab_email_default tced
        LEFT JOIN tb_sys_user tu ON tced.user_id = tu.id
        WHERE tced.default_id = #{defaultId}
          AND tced.deleted = 0
    </select>

    <!-- 根据metaId查询 -->
    <select id="selectDefaultByMetaId" resultMap="BaseResultMap">
        SELECT tced.id, tced.default_id, tced.meta_id, tced.user_id, tu.email as user_email, tu.username, tced.create_time
        FROM tb_crontab_email_default tced
        LEFT JOIN tb_sys_user tu ON tced.user_id = tu.id
        WHERE tced.meta_id = #{metaId}
          AND tced.deleted = 0
    </select>

</mapper>