<?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.ruoyi.oa.mapper.OaProjectScheduleStepMapper">

    <resultMap id="OaProjectScheduleStepResult"
               type="com.ruoyi.oa.domain.vo.OaProjectScheduleStepVo" >
        <!-- 主键最好用 <id/>，以便 MyBatis 去重 -->
        <id     property="trackId"   column="track_id"/>

        <!-- ========== 原有字段 ========= -->
        <result property="accessory"     column="accessory"/>
        <result property="scheduleId"    column="schedule_id"/>
        <result property="stepOrder"     column="step_order"/>
        <result property="stepName"      column="step_name"/>
        <result property="planStart"     column="plan_start"/>
        <result property="planEnd"       column="plan_end"/>
        <result property="actualStart"   column="actual_start"/>
        <result property="actualEnd"     column="actual_end"/>
        <result property="status"        column="status"/>
        <result property="expectedDays"        column="expected_days"/>
        <result property="useFlag"        column="use_flag"/>
        <result property="header"        column="header"/>
        <result property="tabNode" column="tab_node"/>
        <result property="firstLevelNode" column="first_level_node"/>
        <result property="secondLevelNode" column="second_level_node"/>
        <result property="startTime" column="start_time"/>
        <result property="originalEndTime" column="original_end_time"/>
        <result property="endTime" column="end_time"/>
        <result property="nodeHeader" column="node_header"/>
        <result property="relatedDocs" column="related_docs"/>
        <result property="relatedImages" column="related_images"/>
        <result property="specification" column="specification"/>

        <!-- ========== 附件列表：多对一 折叠 ========= -->
        <collection property="fileList"
                    ofType="com.ruoyi.system.domain.SysOss"
                    javaType="java.util.ArrayList">
            <result     property="ossId"    column="oss_id"/>
            <result property="url"      column="attach_url"/>
            <result property="fileName" column="attach_file_name"/>
            <result property="createBy" column="create_by"/>
            <result property="isPublic" column="is_public"/>
            <result property="ownerId" column="owner_id"/>
        </collection>
    </resultMap>

    <sql id="OssVisibleCondition">
        ( so.is_public = 1
            OR so.owner_id = #{userId}
            OR EXISTS (SELECT 1 FROM sys_oss_acl a
            WHERE a.oss_id = so.oss_id
            AND a.user_id = #{userId}) )
    </sql>
    <insert id="saveBatch">
        INSERT INTO oa_project_schedule_step (
        schedule_id,
        step_order,
        step_name,
        tab_node,
        first_level_node,
        second_level_node,
        specification,
        sort_num
        ) VALUES
        <foreach item="item" index="index" collection="list" separator=",">
            (
            #{item.scheduleId},
            #{item.stepOrder},
            #{item.stepName},
            #{item.tabNode},
            #{item.firstLevelNode},
            #{item.secondLevelNode},
            #{item.specification},
            #{item.sortNum}
            )
        </foreach>
    </insert>

    <update id="updateByStepAndScheduleId">
        UPDATE oa_project_schedule_step
        SET
            status = 1,
            actual_end = NOW()
        WHERE
            schedule_id = #{scheduleId}
          AND step_order = #{currentStep}
          AND del_flag = '0'
        and use_flag = '1'
    </update>
    <delete id="deleteByScheduleIds">
        DELETE FROM oa_project_schedule_step
        WHERE schedule_id IN
        <foreach collection="scheduleIds" item="id" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>
    <select id="maxStepByScheduleId" resultMap="OaProjectScheduleStepResult">
        SELECT opss.track_id,
               opss.accessory,
               opss.schedule_id,
               opss.step_order,
               opss.step_name,
               opss.plan_start,
               opss.plan_end,
               opss.actual_start,
               opss.actual_end,
               opss.status,
               opss.del_flag,
               opss.header,
               opss.use_flag,
               osts.expected_days,
               osts.description
        FROM oa_project_schedule_step opss
                 LEFT JOIN fad_oa.oa_project_schedule ops ON ops.schedule_id = opss.schedule_id
                 LEFT JOIN fad_oa.oa_schedule_template_step osts ON osts.template_id = ops.template_id AND osts.step_order = opss.step_order
        WHERE opss.schedule_id = #{scheduleId}
          AND opss.del_flag = '0'
          and opss.use_flag = '1'
          AND opss.step_order = (
            SELECT MAX(step_order)
            FROM oa_project_schedule_step
            WHERE schedule_id = #{scheduleId}
              AND del_flag = '0'
            and use_flag = '1'
        )

    </select>
    <select id="selectVoPagePlus" resultMap="OaProjectScheduleStepResult">
        select opss.track_id,
               opss.accessory,
               opss.schedule_id,
               opss.step_order,
               opss.step_name,
               opss.plan_start,
               opss.plan_end,
               opss.actual_start,
               opss.actual_end,
               opss.status,
               opss.del_flag,
               opss.header,
               opss.use_flag,
               osts.expected_days,
               osts.description,
               so.oss_id          AS oss_id,
               so.url             AS attach_url,
               so.original_name       AS attach_file_name,     -- 如有需要
               so.create_by,
               so.is_public,
               so.owner_id,
               batch_id
        from oa_project_schedule_step opss
                 left join fad_oa.oa_project_schedule ops on ops.schedule_id = opss.schedule_id
                 left join fad_oa.oa_schedule_template_step osts on osts.template_id = ops.template_id and osts.step_order = opss.step_order
        LEFT JOIN sys_oss            so  ON FIND_IN_SET(so.oss_id, opss.accessory)
        AND <include refid="OssVisibleCondition"/>
            ${ew.getCustomSqlSegment}
    </select>
    <select id="selectVoPlusById" resultMap="OaProjectScheduleStepResult">
        select opss.track_id,
        opss.accessory,
        opss.schedule_id,
        opss.step_order,
        opss.step_name,
        opss.plan_start,
        opss.plan_end,
        opss.actual_start,
        opss.actual_end,
        opss.status,
        opss.use_flag,
        opss.del_flag,
        opss.header,
        osts.expected_days,
        osts.description,
        so.oss_id AS oss_id,
        so.url AS attach_url,
        so.original_name AS attach_file_name, -- 如有需要
        so.create_by,
        so.owner_id,
        so.is_public
        from oa_project_schedule_step opss
        left join fad_oa.oa_project_schedule ops on ops.schedule_id = opss.schedule_id
        left join fad_oa.oa_schedule_template_step osts on osts.template_id = ops.template_id and osts.step_order =
        opss.step_order
        LEFT JOIN sys_oss so ON FIND_IN_SET(so.oss_id, opss.accessory)
        AND
        <include refid="OssVisibleCondition"/>
        where opss.track_id = #{trackId} and opss.del_flag='0' and opss.use_flag = '1'
    </select>


    <select id="selectByCurrentStepAndScheduleId" resultType="com.ruoyi.oa.domain.vo.OaProjectScheduleStepVo">
        select opss.track_id,
               accessory,
               schedule_id,
               step_order,
               step_name,
               plan_start,
               plan_end,
               actual_start,
               actual_end,
               opss.use_flag,
               status,
               header
        from oa_project_schedule_step opss
        WHERE schedule_id = #{scheduleId}
          AND step_order = #{currentStep}
          and use_flag = '1'
          AND del_flag = '0'
    </select>
    <select id="selectVoPageNew" resultType="com.ruoyi.oa.domain.vo.OaProjectScheduleStepVo">
        SELECT
            opss.track_id,
            opss.accessory,
            opss.schedule_id,
            opss.step_order,
            opss.step_name,
            opss.plan_start,
            opss.plan_end,
            opss.actual_start,
            opss.actual_end,
            opss.status,
            opss.create_by,
            opss.create_time,
            opss.update_by,
            opss.update_time,
            opss.del_flag,
            opss.header,
            opss.use_flag,
            opss.batch_id,
            opss.tab_node,
            opss.first_level_node,
            opss.second_level_node,
            opss.start_time,
            opss.original_end_time,
            opss.end_time,
            opss.node_header,
            opss.related_docs,
            opss.related_images,
            opss.supplier_id,
            opss.requirement_file,
            opss.other,
            opss.specification,
            opss.sort_num,
            schedule.project_id AS projectId,
            project.project_name AS projectName,
            supplier.supplier_name AS supplierName
        FROM
            oa_project_schedule_step opss
                INNER JOIN oa_project_schedule schedule
                           ON opss.schedule_id = schedule.schedule_id
                INNER JOIN sys_oa_project project
                           ON schedule.project_id = project.project_id
--             根据供应商id拿供应商名字
                LEFT JOIN oa_supplier supplier
                           ON opss.supplier_id = supplier.supplier_id
            ${ew.customSqlSegment}
    </select>


</mapper>
