<?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="net.ferrum.business.mapper.QsProjectsMapper">

    <resultMap type="net.ferrum.business.domain.QsProjects" id="QsProjectsResult">
        <result property="projectId" column="project_id"/>
        <result property="batchId" column="batch_id"/>
        <result property="projectName" column="project_name"/>
        <result property="createBy" column="create_by"/>
        <result property="createTime" column="create_time"/>
        <result property="updateBy" column="update_by"/>
        <result property="updateTime" column="update_time"/>
        <result property="remark" column="remark"/>
        <result property="delFlag" column="del_flag"/>
        <result property="currentMasterStageId" column="current_master_stage_id"/>
        <result property="projectCompletion" column="project_completion"/>
        <result property="startDate" column="start_date"/>
        <result property="endTime" column="end_time"/>

        <result property="specInfo" column="specInfo"/>
        <result property="batchName" column="batch_name"/>
        <result property="stageName" column="stage_name"/>
        <result property="stageDisplayName" column="display_name"/>
        <result property="isParallelContainer" column="is_parallel_container"/>
        <result property="parallelProgress" column="parallel_Progress"/>

        <association property="batch" column="batch_id" javaType="net.ferrum.business.domain.QsBatch" resultMap="QsBatchResult"/>
    </resultMap>

    <resultMap type="net.ferrum.business.domain.QsBatch" id="QsBatchResult">
        <result property="batchId" column="batch_id"/>
        <result property="batchNo" column="batch_no"/>
        <result property="batchName" column="batch_name"/>
        <result property="batchStartDate" column="batch_start_date"/>
        <result property="batchPlanEndDate" column="batch_plan_end_date"/>
        <result property="batchEndDate" column="batch_end_date"/>
        <result property="delFlag" column="del_flag"/>
        <result property="createBy" column="create_by"/>
        <result property="createTime" column="create_time"/>
        <result property="updateBy" column="update_by"/>
        <result property="updateTime" column="update_time"/>
        <result property="remark" column="remark"/>
    </resultMap>


    <select id="selectPageQsProjectsList" resultMap="QsProjectsResult">
        select a.batch_id, a.project_id, a.project_name,a.current_master_stage_id,a.project_completion,
               a.start_date,a.end_time, b.batch_name,d.specInfo,
               a.create_by, a.create_time, a.update_by, a.update_time, a.remark, a.del_flag,
               ms.stage_name, ms.display_name, ms.is_parallel_container,
               IF(ms.is_parallel_container,
                  CONCAT(
                      (SELECT COUNT(*)
                       FROM qs_project_stage_status pss
                       WHERE pss.project_id = a.project_id
                         AND pss.master_stage_id = a.current_master_stage_id
                         AND pss.status = '已完成'),
                      '/',
                      (SELECT COUNT(*)
                       FROM qs_project_sub_stages pss
                       WHERE pss.master_stage_id = a.current_master_stage_id)
                  ),
                  '1/1'
               ) AS parallel_Progress
        from qs_projects a
                left join qs_batch b on a.batch_id = b.batch_id
                left join (SELECT p.project_id,
                                  GROUP_CONCAT(
                                      CONCAT(pa.id, ':',at.type_id, ':',at.type_name, ':', av.value_id,':', av.value)
                                          ORDER BY at.type_id SEPARATOR '|'
                                  ) AS specInfo
                           FROM qs_projects p
                                    LEFT JOIN qs_project_attributes pa ON p.project_id = pa.project_id
                                    LEFT JOIN qs_spec_attribute_values av ON pa.value_id = av.value_id
                                    LEFT JOIN qs_spec_attribute_types at ON av.type_id = at.type_id
                           GROUP BY p.project_id) as d on d.project_id = a.project_id
                left JOIN qs_project_master_stages ms ON a.current_master_stage_id = ms.master_stage_id
        ${ew.getCustomSqlSegment}
        order by a.start_date desc
    </select>
</mapper>
