<?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.zt.plat.module.qms.business.bus.dal.mapper.BusinessSubParentSampleAssessmentProjectMapper">

    <!--
        一般情况下，尽可能使用 Mapper 进行 CRUD 增删改查即可。
        无法满足的场景，例如说多表关联查询，才使用 XML 编写 SQL。
        代码生成器暂时只生成 Mapper XML 文件本身，更多推荐 MybatisX 快速开发插件来生成查询。
        文档可见：https://www.iocoder.cn/MyBatis/x-plugins/
     -->

    <select id="selectUnReportMethodGroupList" resultType="com.zt.plat.module.qms.business.bus.controller.vo.BusinessSubParentSampleAssessmentGroupRespVO" >
        SELECT
                t.CFG_ASY_MTHD_ID AS configAssayMethodId,
                t.NAME AS configAssayMethodName,
                t.DIC_BSN_KY AS configAssayMethodDictionaryBusinessKey,
                t.MTHD_NAME_CTGR AS configAssayMethodNameAndCategory,
                t.ASY_DEPT_ID AS assayDepartmentId,
                t.ASY_DEPT_NAME AS assayDepartmentName,
                COUNT(DISTINCT t.BSN_SB_PRN_SMP_ASMT_ID) AS sampleCount
        FROM
            (
            SELECT
                tbspsa.TENANT_ID ,
                tbssa.BSN_SB_PRN_SMP_ASMT_ID ,
                tbssa.CFG_ASY_MTHD_ID,
                tcam.NAME,
                tcam.DIC_BSN_KY,
                tcam.MTHD_NAME_CTGR,
                tcam.ASY_DEPT_ID ,
                tcam.ASY_DEPT_NAME
            FROM
                T_BSN_SB_SMP_ASMT tbssa
            LEFT JOIN T_BSN_SB_PRN_SMP_ASMT tbspsa ON
                tbssa.BSN_SB_PRN_SMP_ASMT_ID = tbspsa.ID
            LEFT JOIN T_CFG_ASY_MTHD tcam ON
                tbssa.CFG_ASY_MTHD_ID = tcam.ID
            LEFT JOIN T_BSN_SB_SMP_ANL_GRP tbssag ON
                tbssa.BSN_SB_SMP_ID = tbssag.BSN_SB_SMP_ID
                AND tcam.ASY_DEPT_ID = tbssag.ASY_DEPT_ID
            WHERE
                tbssa.DELETED = 0
                AND tbspsa.DELETED = 0
                AND tbssa.RPOD_STS = 'reported'
                AND tbspsa.IS_RPOD = 0
          <if test="reqVO.configAssayMethodName != null and reqVO.configAssayMethodName != ''">
                AND tcam.NAME like  '%' || #{reqVO.configAssayMethodName} || '%'
          </if>
          <if test="reqVO.configAssayMethodNameAndCategory != null and reqVO.configAssayMethodNameAndCategory != ''">
                AND tcam.MTHD_NAME_CTGR  like '%' || #{reqVO.configAssayMethodNameAndCategory} || '%'
          </if>
          <if test="reqVO.assayDepartmentId != null and reqVO.assayDepartmentId != ''">
                AND tcam.ASY_DEPT_ID = #{reqVO.assayDepartmentId}
          </if>
          <if test="reqVO.sampleReceiveTime != null and reqVO.sampleReceiveTime.length == 2">
                AND tbssag.SMP_RCV_TM BETWEEN #{reqVO.sampleReceiveTime[0]} AND #{reqVO.sampleReceiveTime[1]}
          </if>
            GROUP BY
                tbspsa.TENANT_ID ,
                tbssa.BSN_SB_PRN_SMP_ASMT_ID ,
                tbssa.CFG_ASY_MTHD_ID ,
                tcam.NAME,
                tcam.DIC_BSN_KY,
                tcam.MTHD_NAME_CTGR,
                tcam.ASY_DEPT_ID ,
                tcam.ASY_DEPT_NAME
            ) t
        GROUP BY
                t.CFG_ASY_MTHD_ID,
                t.NAME,
                t.DIC_BSN_KY,
                t.MTHD_NAME_CTGR,
                t.ASY_DEPT_ID,
                t.ASY_DEPT_NAME
        ORDER BY t.DIC_BSN_KY ASC
    </select>
    
    <select id="selectNoReportSubParentSampleAssessment" resultType="com.zt.plat.module.qms.business.bus.controller.vo.NoReportSubParentSampleAssessmentRespVO" >                
        SELECT
            t.ID AS businessSubParentSampleAssessmentId,
            tcssp.BSE_SMP_ID AS baseSampleId,
            tbsps.BSN_BSE_SMP_ID AS businessBaseSampleId,
            tbssa.BSN_SB_PRN_SMP_ID AS businessSubParentSampleId,
            tbssa.CFG_ASY_MTHD_ID AS configAssayMethodId ,
            tbssa.TSK_TP AS taskType,
            tbssa.ASY_TP AS assayType,
            tbssa.RTST_CNT AS retestCount,
            tbsps.CFG_SB_SMP_PRN_ID AS configSubSampleParentId,
            tbsps.SMP_NAME AS sampleName,
            tbs.NAME AS baseSampleName,
            tbssag.SMP_RCV_TM AS sampleReceiveTime
        FROM
            T_BSN_SB_SMP_ASMT tbssa
        LEFT JOIN T_BSN_SB_PRN_SMP tbsps ON
            tbssa.BSN_SB_PRN_SMP_ID = tbsps.ID
        LEFT JOIN T_CFG_SB_SMP_PRN tcssp ON
            tbsps.CFG_SB_SMP_PRN_ID = tcssp.ID
        LEFT JOIN T_BSE_SMP tbs ON
            tcssp.BSE_SMP_ID = tbs.ID
        LEFT JOIN T_CFG_ASY_MTHD tcam ON
            tbssa.CFG_ASY_MTHD_ID = tcam.ID
        LEFT JOIN T_BSN_SB_SMP_ANL_GRP tbssag ON
            tbssa.BSN_SB_SMP_ID = tbssag.BSN_SB_SMP_ID
            AND tcam.ASY_DEPT_ID = tbssag.ASY_DEPT_ID
        LEFT JOIN (
            SELECT
                tbspsa.ID,
                tbspsa.BSN_SB_PRN_SMP_ID ,
                tbspsa.CFG_ASY_MTHD_ID ,
                tbspsa.IS_RPOD, 
                tbspsa.RTST_CNT
            FROM
                T_BSN_SB_PRN_SMP_ASMT tbspsa
            WHERE
                tbspsa.IS_RPOD = 0
                AND tbspsa.DELETED = 0
                AND tbspsa.CFG_ASY_MTHD_ID = #{reqVO.configAssayMethodId}
        ) t ON
            tbssa.BSN_SB_PRN_SMP_ID = t.BSN_SB_PRN_SMP_ID 
            AND tbssa.CFG_ASY_MTHD_ID = t.CFG_ASY_MTHD_ID 
            AND tbssa.RTST_CNT = t.RTST_CNT
        WHERE
            tbssa.IS_RPOD = 1
            AND t.IS_RPOD = 0
            AND tbssa.CFG_ASY_MTHD_ID = #{reqVO.configAssayMethodId}
          <if test="reqVO.sampleReceiveTime != null and reqVO.sampleReceiveTime.length == 2">
            AND tbssag.SMP_RCV_TM BETWEEN #{reqVO.sampleReceiveTime[0]} AND #{reqVO.sampleReceiveTime[1]}
          </if>
        GROUP BY
            t.ID,
            tcssp.BSE_SMP_ID ,
            tbsps.BSN_BSE_SMP_ID ,
            tbssa.BSN_SB_PRN_SMP_ID,
            tbssa.CFG_ASY_MTHD_ID,
            tbssa.TSK_TP ,
            tbssa.ASY_TP ,
            tbssa.RTST_CNT,
            tbsps.CFG_SB_SMP_PRN_ID,
            tbsps.SMP_NAME ,
            tbs.NAME,
            tbssag.SMP_RCV_TM
    </select>
    
    <select id="getRecheckAssayMethodList" resultType="com.zt.plat.module.qms.business.bus.controller.vo.RecheckSubSampleParentMethodRespVO">
        SELECT
            tmasd.DIC_PRJ_ID dictionaryProjectId,
            tdp.NAME AS dictionaryProjectName,
            tdp.SMPL_NAME AS dictionaryProjectSimpleName,
            tdp.SHW_NAME AS dictionaryProjectShowName,
            tmasm.CFG_ASY_MTHD_ID AS configAssayMethodId,
            tcam.NAME AS configAssayMethodName,
            tcam.MTHD_NAME_CTGR AS configAssayMethodNameAndCategory,
            tmas.BSE_SMP_ID AS baseSampleId,
            tbs.NAME AS baseSampleName,
            tmasm.IS_RCHK_DFT AS isRecheckDefault
        FROM
            T_MTRL_ASY_STD_MTHD tmasm
        LEFT JOIN T_MTRL_ASY_STD_DTL tmasd ON
            tmasm.MTRL_ASY_STD_DTL_ID = tmasd.ID
        LEFT JOIN T_MTRL_ASY_STD tmas ON
            tmasd.MTRL_ASY_STD_ID = tmas.ID
        LEFT JOIN T_CFG_ASY_MTHD tcam ON
            tmasm.CFG_ASY_MTHD_ID = tcam.ID
        LEFT JOIN T_BSE_SMP tbs ON
            tmas.BSE_SMP_ID = tbs.ID
        LEFT JOIN T_DIC_PRJ tdp ON tmasd.DIC_PRJ_ID = tdp.ID 
        WHERE
            tmasm.DELETED = 0
            AND tmas.BSE_SMP_ID = #{baseSampleId}
            AND tcam.ASY_DEPT_ID = #{assayDepartmentId}
            AND tmasd.DIC_PRJ_ID IN (
            SELECT
                    tbspsap.DIC_PRJ_ID
            FROM
                    T_BSN_SB_PRN_SMP_ASMT_PRJ tbspsap 
            WHERE
                tbspsap.DELETED = 0
                AND tbspsap.ASMT_STS = 'normal'
                AND tbspsap.BSN_SB_PRN_SMP_ID = #{businessSubParentSampleId}
                AND tbspsap.CFG_ASY_MTHD_ID = #{configAssayMethodId}
            )

    </select>
    
    
</mapper>