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

    <!-- 结果映射 -->
    <resultMap id="BaseResultMap" type="org.xyzh.common.dto.crontab.TbCrontabTaskMeta">
        <id column="id" property="ID" />
        <result column="meta_id" property="metaId" />
        <result column="name" property="name" />
        <result column="description" property="description" />
        <result column="category" property="category" />
        <result column="bean_name" property="beanName" />
        <result column="method_name" property="methodName" />
        <result column="script_path" property="scriptPath" />
        <result column="param_schema" property="paramSchema" />
        <result column="auto_publish" property="autoPublish" />
        <result column="sort_order" property="sortOrder" />
        <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, meta_id, name, description, category, bean_name, method_name, 
        script_path, param_schema, auto_publish, sort_order, 
        creator, updater, create_time, update_time, delete_time, deleted
    </sql>

    <!-- 查询条件 -->
    <sql id="Base_Where_Clause">
        <where>
            deleted = 0
            <if test="filter.metaId != null and filter.metaId != ''">
                AND meta_id = #{filter.metaId}
            </if>
            <if test="filter.name != null and filter.name != ''">
                AND name LIKE CONCAT('%', #{filter.name}, '%')
            </if>
            <if test="filter.category != null and filter.category != ''">
                AND category = #{filter.category}
            </if>
        </where>
    </sql>

    <!-- 插入任务元数据 -->
    <insert id="insertTaskMeta" parameterType="org.xyzh.common.dto.crontab.TbCrontabTaskMeta">
        INSERT INTO tb_crontab_task_meta (
            id, meta_id, name, description, category, bean_name, method_name, 
            script_path, param_schema, auto_publish, sort_order, creator, create_time
        ) VALUES (
            #{taskMeta.ID}, #{taskMeta.metaId}, #{taskMeta.name}, #{taskMeta.description}, 
            #{taskMeta.category}, #{taskMeta.beanName}, #{taskMeta.methodName}, 
            #{taskMeta.scriptPath}, #{taskMeta.paramSchema}, #{taskMeta.autoPublish}, #{taskMeta.sortOrder}, 
            #{taskMeta.creator}, NOW()
        )
    </insert>

    <!-- 更新任务元数据 -->
    <update id="updateTaskMeta" parameterType="org.xyzh.common.dto.crontab.TbCrontabTaskMeta">
        UPDATE tb_crontab_task_meta
        SET name = #{taskMeta.name},
            description = #{taskMeta.description},
            category = #{taskMeta.category},
            bean_name = #{taskMeta.beanName},
            method_name = #{taskMeta.methodName},
            script_path = #{taskMeta.scriptPath},
            param_schema = #{taskMeta.paramSchema},
            auto_publish = #{taskMeta.autoPublish},
            sort_order = #{taskMeta.sortOrder},
            updater = #{taskMeta.updater},
            update_time = NOW()
        WHERE meta_id = #{taskMeta.metaId}
          AND deleted = 0
    </update>

    <!-- 删除任务元数据（逻辑删除） -->
    <update id="deleteTaskMeta">
        UPDATE tb_crontab_task_meta
        SET deleted = 1,
            delete_time = NOW()
        WHERE meta_id = #{metaId}
          AND deleted = 0
    </update>

    <!-- 根据ID查询 -->
    <select id="selectTaskMetaById" resultMap="BaseResultMap">
        SELECT <include refid="Base_Column_List" />
        FROM tb_crontab_task_meta
        WHERE meta_id = #{metaId}
          AND deleted = 0
    </select>

    <!-- 根据TaskID查询 -->
    <select id="selectTaskMetaByTaskId" resultMap="BaseResultMap">
        SELECT tcte.*
        FROM tb_crontab_task tct
        LEFT JOIN tb_crontab_task_meta tcte ON tct.meta_id = tcte.meta_id
        WHERE tct.task_id = #{taskId}
          AND tcte.deleted = 0
    </select>

    <!-- 查询所有任务元数据 -->
    <select id="selectAllTaskMeta" resultMap="BaseResultMap">
        SELECT <include refid="Base_Column_List" />
        FROM tb_crontab_task_meta
        WHERE deleted = 0
        ORDER BY sort_order ASC, create_time DESC
    </select>

    <!-- 根据分类查询 -->
    <select id="selectTaskMetaByCategory" resultMap="BaseResultMap">
        SELECT <include refid="Base_Column_List" />
        FROM tb_crontab_task_meta
        WHERE category = #{category}
          AND deleted = 0
        ORDER BY sort_order ASC
    </select>

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

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

</mapper>