<?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.SampleResultQueryMapper">

    <select id="sampleProjectResultQuery" resultType="com.zt.plat.module.qms.business.bus.controller.vo.SampleProjectResultRespVO">
        SELECT
            tbspsap.ID AS ID,
            tbsps.SMP_NAME AS sampleName,
            tbsps.SB_SMP_CD AS sampleCode,
            tbsps.SB_SMP_RTN_CD AS sampleReturnCode,
            tdp.NAME AS projectName,
            tdp.SMPL_NAME AS projectSimpleName,
            tdp.SHW_NAME AS projectShowName,
            tdp.KY AS projectKey,
            tbspsap.ASMT_VAL AS assessmentValue,
            tcamp.DIC_PRJ_UNT AS dictionaryProjectUnit,
            tbssag.ASY_DEPT_ID AS assayDepartmentId,
            tbssag.ASY_DEPT_NAME AS assayDepartmentName,
            tbssag.SMP_RCVR AS sampleReceiver,
            tbssag.SMP_RCV_TM AS sampleReceiveTime,
            tbspsap.MIN_LIM_VAL AS minimumLimitValue,
            tbspsap.IS_RPOD AS isReported,
            tbspsap.RPTR AS reporter,
            tbspsap.RPT_TM AS reportTime,
            tbspsap.CFG_ASY_MTHD_ID AS configAssayMethodId,
            tbspsap.CFG_ASY_MTHD_PRJ_ID AS configAssayMethodProjectId,
            tbspsap.BSN_SB_PRN_SMP_ID AS businessSubParentSampleId
        FROM
            T_BSN_SB_PRN_SMP_ASMT_PRJ tbspsap
        LEFT JOIN T_BSN_SB_PRN_SMP_ASMT tbspsa ON
            tbspsap.BSN_SB_PRN_SMP_ASMT_ID = tbspsa.ID
        LEFT JOIN T_BSN_SB_PRN_SMP tbsps ON
            tbspsap.BSN_SB_PRN_SMP_ID = tbsps.ID
        LEFT JOIN T_DIC_PRJ tdp ON
            tbspsap.DIC_PRJ_ID = tdp.ID
        LEFT JOIN T_BSN_SB_SMP_ANL_GRP tbssag ON
            tbspsap.BSN_SB_PRN_SMP_ID = tbssag.BSN_SB_PRN_SMP_ID
            AND tbssag.ASY_DEPT_ID = tbspsap.DEPT_ID
        LEFT JOIN T_CFG_ASY_MTHD_PRJ tcamp ON
            tbspsap.CFG_ASY_MTHD_PRJ_ID = tcamp.ID
        WHERE
            tbspsa.IS_RPOD = 1
            AND tbspsap.IS_RPOD = 1
        <if test="reqVO.assayDepartmentId != null">
            AND tbssag.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>
        <if test="reqVO.sampleName != null and reqVO.sampleName != ''">
            AND tbsps.SMP_NAME like '%' || #{reqVO.sampleName} || '%'
        </if>
        <if test="reqVO.sampleCode != null and reqVO.sampleCode != ''">
            AND tbsps.SB_SMP_CD like '%' || #{reqVO.sampleCode} || '%'
        </if>
        <if test="reqVO.projectSimpleName != null and reqVO.projectSimpleName != ''">
            AND tdp.SMPL_NAME like '%' || #{reqVO.projectSimpleName} || '%'
        </if>
        ORDER BY
            tbssag.SMP_RCV_TM ,
            tbsps.SB_SMP_CD,
            tbsps.SB_SMP_RTN_CD
        
    </select>
    
    <select id="sampleProjectResultDetailQuery" resultType="com.zt.plat.module.qms.business.bus.controller.vo.SampleProjectResultDetailRespVO">
        SELECT
            tbapd.ID ,
            tbss.SMP_NAME AS sampleName,
            tbss.SMP_ASY_CD AS sampleAssayCode,
            tbss.SMP_CD AS sampleCode,
            tbss.SMP_RTN_CD AS sampleReturnCode ,
            tdp.SHW_NAME AS projectShowName,
            tdp.SMPL_NAME AS projectSimpleName,
            tbat.TSK_NO AS taskNo,
            tbat.TSK_NAME AS taskName,
            tbatd.TSK_TP AS taskType,
            tcamp.DIC_PRJ_UNT AS dictionaryProjectUnit,
            tbapd.SMB AS symbol,
            tbapd.VAL AS value,
            tbapd.COEF AS coefficient,
            tbapd.VAL_AFT AS valueAfter,
            tbapd.MIN_LIM_VAL AS minimumLimitValue,
            tbapd.IS_NT_ASMT AS isNotAssessment,
            tbatd.ASY_OPTR AS assayOperator,
            tbatd.RPT_TM AS reportTime,
            tbatd.RPTR AS reporter,
            tbatd.ASY_DEPT_ID AS assayDepartmentId,
            tbatd.ASY_DEPT_NAME AS assayDepartmentName,
            tbatd.BSN_ASY_TSK_ID AS businessAssayTaskId,
            tcam.MTHD_NAME_CTGR AS methodNameCategory,
            tcam.CFG_RPT_TMPL_KY AS configReportTemplateKey,
            tbatd.BSN_BSE_SMP_ID AS businessBaseSampleId,
            tbatd.BSN_SB_PRN_SMP_ID AS businessSubParentSampleId,
            tbatd.BSN_SB_SMP_ID AS businessSubSampleId,
            tbatd.BSN_SB_SMP_ASMT_ID AS businessSubSampleAssessmentId,
            tbapd.CFG_ASY_MTHD_PRJ_ID AS configAssayMethodProjectId
        FROM
            T_BSN_ASY_PRJ_DAT tbapd
        LEFT JOIN
        T_BSN_ASY_TSK_DAT tbatd ON
            tbapd.BSN_ASY_TSK_DAT_ID = tbatd.ID
        LEFT JOIN 
        T_BSN_ASY_TSK tbat ON
            tbatd.BSN_ASY_TSK_ID = tbat.ID
        LEFT JOIN 
        T_BSN_SB_SMP tbss ON
            tbatd.BSN_SB_SMP_ID = tbss.ID
        LEFT JOIN 
        T_CFG_ASY_MTHD_PRJ tcamp ON
            tbapd.CFG_ASY_MTHD_PRJ_ID = tcamp.ID
        LEFT JOIN 
        T_CFG_ASY_MTHD tcam ON
            tcamp.CFG_ASY_MTHD_ID = tcam.ID
        LEFT JOIN 
        T_DIC_PRJ tdp ON
            tbapd.DIC_PRJ_ID = tdp.ID
        WHERE
            tbatd.BSN_ASY_TSK_ID IS NOT NULL
            AND tbapd.DELETED = 0
            AND tbss.BSN_SB_PRN_SMP_ID = #{reqVO.businessSubParentSampleId}
            AND tbapd.CFG_ASY_MTHD_PRJ_ID = #{reqVO.configAssayMethodProjectId}
        ORDER BY
            tbatd.RPT_TM
        
    </select>

</mapper>