<?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.hotent.surveyinfo.dao.CostSurveyTemplateUploadDao">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.hotent.surveyinfo.model.CostSurveyTemplateUpload">
        <id column="id" property="id" />
        <result column="catalog_id" property="catalogId" />
        <result column="survey_template_id" property="surveyTemplateId" />
        <result column="is_required" property="isRequired" />
        <result column="is_upload" property="isUpload" />
        <result column="upload_time" property="uploadTime" />
        <result column="upload_by" property="uploadBy" />
        <result column="file_url" property="fileUrl" />
        <result column="order_num" property="orderNum" />
        <result column="is_deleted" property="isDeleted" />
        <result column="create_time" property="createTime" />
        <result column="create_by" property="createBy" />
        <result column="update_time" property="updateTime" />
        <result column="update_by" property="updateBy" />
        <!-- 关联查询字段 -->
        <result column="survey_template_name" property="surveyTemplateName" />
        <result column="audited_status" property="auditedStatus" />
        <result column="template_type" property="templateType" />
        <result column="task_id" property="taskId" />
    </resultMap>

    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        id, catalog_id, survey_template_id, is_required, is_upload, upload_time, upload_by, 
        file_url, order_num, is_deleted, create_time, create_by, update_time, update_by,audited_status,task_id
    </sql>

    <!-- 根据目录ID查询上传记录列表（关联模板信息） -->
    <select id="selectListByCatalogId" resultMap="BaseResultMap">
        SELECT 
            COALESCE(u.id, '') as id,
            t.catalog_id,
            t.survey_template_id,
            COALESCE(u.is_required, '1') as is_required,
            COALESCE(u.is_upload, '0') as is_upload,
            u.upload_time,
            u.upload_by,
            u.file_url,
            COALESCE(u.order_num, 0) as order_num,
            COALESCE(u.is_deleted, '0') as is_deleted,
            COALESCE(u.create_time, t.create_time) as create_time,
            COALESCE(u.create_by, t.create_by) as create_by,
            u.update_time,
            u.update_by,
            t.survey_template_name,
            t.template_type,
            '模版定制' as data_type,
            u.audited_status
        FROM 
            cost_survey_template t
        LEFT JOIN 
            cost_survey_template_upload u ON t.survey_template_id = u.survey_template_id 
                AND u.catalog_id = #{catalogId} 
        WHERE
            t.catalog_id = #{catalogId}
            AND t.status = '0'
        ORDER BY
            COALESCE(u.order_num, 0) ASC, t.create_time ASC
    </select>

    <select id="selectPage" resultMap="BaseResultMap">
        select 
            <include refid="Base_Column_List"/> 
        from
            cost_survey_template_upload
        ${ew.customSqlSegment}
    </select>
    
    <select id="selectList" resultMap="BaseResultMap">
        select 
            <include refid="Base_Column_List"/> 
        from
            cost_survey_template_upload
        ${ew.customSqlSegment}
    </select>
    
    <select id="selectById" resultMap="BaseResultMap">
        select 
            <include refid="Base_Column_List"/> 
        from
            cost_survey_template_upload
        where 
            id = #{id}
    </select>
    <select id="selectListByTaskId" resultType="com.hotent.surveyinfo.model.CostSurveyTemplateUpload">
        SELECT
            COALESCE(u.id, '') as id,
            u.task_id,
            t.catalog_id,
            t.survey_template_id,
            COALESCE(u.is_required, '1') as is_required,
            COALESCE(u.is_upload, '0') as is_upload,
            u.upload_time,
            u.upload_by,
            u.file_url,
            COALESCE(u.order_num, 0) as order_num,
            COALESCE(u.is_deleted, '0') as is_deleted,
            COALESCE(u.create_time, t.create_time) as create_time,
            COALESCE(u.create_by, t.create_by) as create_by,
            u.update_time,
            u.update_by,
            t.survey_template_name,
            t.template_type,
            '模版定制' as data_type,
            u.audited_status
        FROM
            cost_survey_template t
                LEFT JOIN
            cost_survey_template_upload u ON t.survey_template_id = u.survey_template_id
        WHERE
            u.task_id = #{taskId}
            AND t.status = '0'
        ORDER BY
            COALESCE(u.order_num, 0) ASC, t.create_time ASC
    </select>
</mapper>

