<?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="cn.edu.whu.boot.photozip.mapper.PhotoZipMapper">

    <!-- 1. 查询 public.task 表，获取 database_name 和 xml_id -->
    <select id="getTaskInfoByTaskName" resultType="map">
        SELECT database_name, xml_id
        FROM public.task
        WHERE task_name = #{taskName}
    </select>

    <!-- 2. 根据 xml_id 查询 public.xml 表，获取 file_path -->
    <select id="getXmlFilePathByXmlId" resultType="string">
        SELECT file_path
        FROM public.xml
        WHERE id = #{xmlId}
    </select>

    <!-- 3. 根据拼接后的 tableName 查询 media_path，注意这里使用动态 SQL -->
    <select id="getMediaPathsByTask" resultType="string">
        SELECT media_path
        FROM ${tableName}
    </select>
<!--    维护下载状态表 -->
    <insert id="insertOrUpdateZipTask" parameterType="map">
        INSERT INTO zip_task_status (task_name, status, updated_at)
        VALUES (#{taskName}, #{status}, NOW())
        ON CONFLICT (task_name) DO UPDATE
        SET status = #{status}, updated_at = NOW();
    </insert>
    <!--  查询状态 -->
    <select id="getZipTaskStatus" resultType="String">
        SELECT status FROM zip_task_status WHERE task_name = #{taskName}
    </select>
    <select id="getUserMap" resultType="java.util.Map">
        SELECT id, real_name FROM public.user_staff
    </select>
</mapper>
