<?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="com.fs.survey.mapper.SurveyTaskPeriodMapper">

    <resultMap type="SurveyTaskPeriod" id="SurveyTaskPeriodResult">
        <result property="id"              column="id" />
        <result property="taskId"          column="task_id" />
        <result property="periodNum"       column="period_num" />
        <result property="periodStartTime" column="period_start_time" />
        <result property="periodEndTime"   column="period_end_time" />
        <result property="createTime"      column="create_time" />
        <result property="companyId"       column="company_id" />
        <result property="isPeriod"       column="is_period" />
        <!-- 添加任务名称字段映射 -->
        <result property="taskTitle"       column="task_title" />
        <!-- 添加公司名称字段映射 -->
        <result property="companyName"     column="company_name" />
    </resultMap>

    <!-- 修改基础查询SQL，添加关联查询 -->
    <sql id="selectSurveyTaskPeriodVo">
        select
        stp.id,
        stp.task_id,
        stp.period_num,
        stp.period_start_time,
        stp.period_end_time,
        stp.create_time,
        stp.company_id,
        stp.is_period,
        <!-- 添加任务名称 -->
        st.title as task_title,
        <!-- 添加公司名称 -->
        c.company_name as company_name
        from survey_task_period stp
        left join survey_task st on stp.task_id = st.id and st.deleted = 0
        left join company c on stp.company_id = c.company_id and c.is_del = 0
    </sql>

    <select id="selectSurveyTaskPeriodList"
            parameterType="SurveyTaskPeriod"
            resultMap="SurveyTaskPeriodResult">
        <include refid="selectSurveyTaskPeriodVo"/>
        <where>
            <if test="taskId != null">and stp.task_id = #{taskId}</if>
            <if test="periodNum != null">and stp.period_num = #{periodNum}</if>
            <if test="periodStartTime != null">and stp.period_start_time = #{periodStartTime}</if>
            <if test="periodEndTime != null">and stp.period_end_time = #{periodEndTime}</if>
            <if test="companyId != null">and stp.company_id = #{companyId}</if>
            <!-- 可以添加任务名称作为查询条件 -->
            <if test="taskTitle != null and taskTitle != ''">and st.title like concat('%', #{taskTitle}, '%')</if>
            <!-- 可以添加公司名称作为查询条件 -->
            <if test="companyName != null and companyName != ''">and c.company_name like concat('%', #{companyName}, '%')</if>
        </where>
        order by stp.period_num asc
    </select>

    <select id="selectSurveyTaskPeriodById"
            parameterType="Long"
            resultMap="SurveyTaskPeriodResult">
        <include refid="selectSurveyTaskPeriodVo"/>
        where stp.id = #{id}
    </select>

    <!-- 原有的插入、更新、删除方法保持不变 -->
    <insert id="insertSurveyTaskPeriod"
            parameterType="SurveyTaskPeriod"
            useGeneratedKeys="true"
            keyProperty="id">
        insert into survey_task_period
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="taskId != null">task_id,</if>
            <if test="periodNum != null">period_num,</if>
            <if test="periodStartTime != null">period_start_time,</if>
            <if test="periodEndTime != null">period_end_time,</if>
            <if test="createTime != null">create_time,</if>
            <if test="companyId != null">company_id,</if>
            <if test="isPeriod != null">is_period,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="taskId != null">#{taskId},</if>
            <if test="periodNum != null">#{periodNum},</if>
            <if test="periodStartTime != null">#{periodStartTime},</if>
            <if test="periodEndTime != null">#{periodEndTime},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="companyId != null">#{companyId},</if>
            <if test="isPeriod != null">#{isPeriod},</if>
        </trim>
    </insert>

    <update id="updateSurveyTaskPeriod" parameterType="SurveyTaskPeriod">
        update survey_task_period
        <trim prefix="SET" suffixOverrides=",">
            <if test="taskId != null">task_id = #{taskId},</if>
            <if test="periodNum != null">period_num = #{periodNum},</if>
            <if test="periodStartTime != null">period_start_time = #{periodStartTime},</if>
            <if test="periodEndTime != null">period_end_time = #{periodEndTime},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="companyId != null">company_id = #{companyId},</if>
            <if test="isPeriod != null">is_period = #{isPeriod},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteSurveyTaskPeriodById" parameterType="Long">
        delete from survey_task_period where id = #{id}
    </delete>

    <delete id="deleteSurveyTaskPeriodByIds" parameterType="String">
        delete from survey_task_period where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>

    <!-- 修改：根据任务ID查询期数列表，已包含任务名称 -->
    <select id="selectSurveyTaskPeriodListByTaskId"
            parameterType="Long"
            resultMap="SurveyTaskPeriodResult">
        <include refid="selectSurveyTaskPeriodVo"/>
        where stp.task_id = #{taskId}
        order by stp.period_num asc
    </select>
    <select id="getPeriodData" resultType="com.fs.survey.dto.PeriodDataDto">
        SELECT
            COUNT(*) AS totalPeriodNum,
            SUM(CASE WHEN is_period = 1 THEN 1 ELSE 0 END) AS alreadyPeriodNum
        FROM survey_task_period
        WHERE task_id = #{taskId};
    </select>
    <select id="getPeriodNum" resultType="java.lang.Long">
    <![CDATA[
        SELECT IFNULL(
                       (SELECT id
                        FROM survey_task_period
                        WHERE task_id = #{taskId}
                          AND NOW() >= period_start_time
                          AND NOW() <= period_end_time
                          AND is_period = 0
                       ),
                       0
               ) AS id;
        ]]>
</select>

    <!-- 添加：根据任务ID删除期数 -->
    <delete id="deleteSurveyTaskPeriodByTaskId" parameterType="Long">
        delete from survey_task_period where task_id = #{taskId}
    </delete>

</mapper>