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

    <!--
        一般情况下，尽可能使用 Mapper 进行 CRUD 增删改查即可。
        无法满足的场景，例如说多表关联查询，才使用 XML 编写 SQL。
        代码生成器暂时只生成 Mapper XML 文件本身，更多推荐 MybatisX 快速开发插件来生成查询。
        文档可见：https://www.iocoder.cn/MyBatis/x-plugins/
     -->
     
     <select id="selectUnAssayTaskGroupList" resultType="com.zt.plat.module.qms.business.bus.controller.vo.BusinessAssayTaskDataGroupRespVO">
        SELECT
            tbatd.CFG_ASY_MTHD_ID AS configAssayMethodId,
            tcam.NAME AS configAssayMethodName,
            tcam.DIC_BSN_KY AS configAssayMethodDictionaryBusinessKey,
            tcam.MTHD_NAME_CTGR AS configAssayMethodNameAndCategory,
            COUNT(tbatd.ID) AS sampleTaskCount
        FROM
            T_BSN_ASY_TSK_DAT tbatd
        LEFT JOIN T_CFG_ASY_MTHD tcam ON
            tbatd.CFG_ASY_MTHD_ID = tcam.ID
        LEFT JOIN T_BSN_SB_SMP_ANL_GRP tbssag ON
            tbatd.BSN_SB_SMP_ID = tbssag.BSN_SB_SMP_ID
            AND tbatd.ASY_DEPT_ID = tbssag.ASY_DEPT_ID
        WHERE
            tbatd.DELETED = 0
            AND tcam.DELETED = 0
            AND tbssag.DELETED = 0
            AND tbatd.IS_ASN_TSKD = 0
            AND tbatd.IS_RPOD = 0
            AND tbssag.SMP_STS = '已收样'
      <if test="reqVO.assayDepartmentId != null and reqVO.assayDepartmentId != ''">
            AND tbatd.ASY_DEPT_ID = #{reqVO.assayDepartmentId}
      </if>
      <if test="reqVO.sampleFlowNodeKey != null and reqVO.sampleFlowNodeKey != ''">
            AND tbatd.SMP_FLW_NDE_KY = #{reqVO.sampleFlowNodeKey}
      </if>
      <if test="reqVO.sampleFlowNodeTime != null and reqVO.sampleFlowNodeTime.length == 2">
            AND tbatd.SMP_FLW_NDE_TM BETWEEN #{reqVO.sampleFlowNodeTime[0]} AND #{reqVO.sampleFlowNodeTime[1]}
      </if>
      <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>
        GROUP BY
            tbatd.CFG_ASY_MTHD_ID,
            tcam.NAME,
            tcam.DIC_BSN_KY,
            tcam.MTHD_NAME_CTGR
        ORDER BY tcam.DIC_BSN_KY ASC
     </select>
     
     <select id="selectUnAssayTaskGroupTypeList" resultType="com.zt.plat.module.qms.business.bus.controller.vo.BusinessAssayTaskDataGroupRespVO">
        SELECT
            tcam.DIC_BSN_ID AS configAssayMethodDictionaryBusinessId,
            tcam.DIC_BSN_KY AS configAssayMethodDictionaryBusinessKey,
            tdb.NAME AS configAssayMethodDictionaryBusinessName,
            COUNT(tbatd.ID) AS sampleTaskCount
        FROM
            T_BSN_ASY_TSK_DAT tbatd
        LEFT JOIN T_CFG_ASY_MTHD tcam ON
            tbatd.CFG_ASY_MTHD_ID = tcam.ID
        LEFT JOIN T_DIC_BSN tdb ON
            tcam.DIC_BSN_ID = tdb.ID
        LEFT JOIN T_BSN_SB_SMP_ANL_GRP tbssag ON
            tbatd.BSN_SB_SMP_ID = tbssag.BSN_SB_SMP_ID
            AND tbatd.ASY_DEPT_ID = tbssag.ASY_DEPT_ID
        WHERE
            tbatd.DELETED = 0
            AND tcam.DELETED = 0
            AND tbssag.DELETED = 0
            AND tbatd.IS_ASN_TSKD = 0
            AND tbatd.IS_RPOD = 0
            AND tbssag.SMP_STS = '已收样'
      <if test="reqVO.assayDepartmentId != null and reqVO.assayDepartmentId != ''">
            AND tbatd.ASY_DEPT_ID = #{reqVO.assayDepartmentId}
      </if>
      <if test="reqVO.sampleFlowNodeKey != null and reqVO.sampleFlowNodeKey != ''">
            AND tbatd.SMP_FLW_NDE_KY = #{reqVO.sampleFlowNodeKey}
      </if>
      <if test="reqVO.sampleFlowNodeTime != null and reqVO.sampleFlowNodeTime.length == 2">
            AND tbatd.SMP_FLW_NDE_TM BETWEEN #{reqVO.sampleFlowNodeTime[0]} AND #{reqVO.sampleFlowNodeTime[1]}
      </if>
      <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>
        GROUP BY
            tcam.DIC_BSN_ID,
            tcam.DIC_BSN_KY,
            tdb.NAME
        ORDER BY tcam.DIC_BSN_KY ASC
     </select>

     <select id="selectUnAuditTaskGroupList" resultType="com.zt.plat.module.qms.business.bus.controller.vo.BusinessAssayTaskDataGroupRespVO">
         SELECT
            t.CFG_ASY_MTHD_ID AS configAssayMethodId,
            t.ASY_TP AS assayType,
            tcam.NAME AS configAssayMethodName,
            tcam.DIC_BSN_KY AS configAssayMethodDictionaryBusinessKey,
            tcam.MTHD_NAME_CTGR AS configAssayMethodNameAndCategory,
            count(CASE WHEN t.RPOD_STS = 'initial' THEN 1 END) AS asmt_initial_count,
            count(CASE WHEN t.RPOD_STS = 'pending_report' THEN 1 END) AS asmt_pending_report_count,
            count(CASE WHEN t.RPOD_STS = 'pending_assessment' THEN 1 END) AS asmt_pending_assessment_count,
            count(CASE WHEN t.RPOD_STS = 're_inspected' THEN 1 END) AS asmt_re_inspected_count,
            count(CASE WHEN t.RPOD_STS = 'reported' THEN 1 END) AS asmt_reported_count
        FROM
            (
            SELECT
                tbatd.TENANT_ID ,
                tbatd.BSN_SB_PRN_SMP_ID ,
                tbatd.BSN_SB_SMP_ID ,
                tbatd.CFG_ASY_MTHD_ID,
                tbatd.ASY_TP,
                tbssag.ASY_DEPT_ID ,
                tbssag.ASY_DEPT_NAME ,
                tbssa.RPOD_STS
            FROM
                T_BSN_ASY_TSK_DAT tbatd
            LEFT JOIN T_BSN_SB_SMP_ANL_GRP tbssag ON
                tbatd.BSN_SB_SMP_ID = tbssag.BSN_SB_SMP_ID
                AND tbatd.ASY_DEPT_ID = tbssag.ASY_DEPT_ID
                AND tbatd.TENANT_ID = tbssag.TENANT_ID
            LEFT JOIN T_BSN_SB_SMP_ASMT tbssa ON
                tbatd.BSN_SB_SMP_ASMT_ID = tbssa.ID
                AND tbatd.TENANT_ID = tbssa.TENANT_ID
            WHERE
                tbatd.DELETED = 0
                AND tbatd.TENANT_ID = 1
                AND tbatd.IS_ASN_TSKD = 1
                AND tbssag.SMP_STS = '已收样'
          <if test="reqVO.assayDepartmentId != null and reqVO.assayDepartmentId != ''">
                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>
            GROUP BY
                tbatd.TENANT_ID ,
                tbatd.BSN_SB_PRN_SMP_ID ,
                tbatd.BSN_SB_SMP_ID ,
                tbatd.CFG_ASY_MTHD_ID,
                tbatd.ASY_TP,
                tbssag.ASY_DEPT_ID ,
                tbssag.ASY_DEPT_NAME ,
                tbssa.RPOD_STS
        ) t
        LEFT JOIN T_CFG_ASY_MTHD tcam ON
            t.CFG_ASY_MTHD_ID = tcam.ID
            AND t.TENANT_ID = tcam.TENANT_ID
        WHERE t.RPOD_STS in ('pending_report', 'pending_assessment', 're_inspected')
          <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>
        GROUP BY
            t.CFG_ASY_MTHD_ID,
            t.ASY_TP,
            tcam.NAME,
            tcam.DIC_BSN_KY,
            tcam.MTHD_NAME_CTGR
        ORDER BY tcam.DIC_BSN_KY ASC
     </select>
     
     <select id="selectUnAssignTaskedSubSample" resultType="com.zt.plat.module.qms.business.bus.controller.vo.UnAssignTaskedSubSampleRespVO">
         SELECT
            tbss.ID || ',' || tbatd.CFG_ASY_MTHD_ID AS id ,
            tbss.SMP_NAME AS sampleName,
            tbss.SMP_CD AS sampleCode,
            tbss.SMP_ASY_CD AS sampleAssayCode,
            tbss.SMP_RTN_CD AS sampleReturnCode,
            tbss.SMP_FLW_NDE_TM AS sampleFlowNodeTime,
            tcbs.BSE_SMP_ID AS baseSampleId,
            tbbs.BSE_SMP_NAME AS baseSampleName,
            tbatd.BSN_SB_SMP_ID AS businessSubSampleId,
            tbatd.CFG_ASY_MTHD_ID AS configAssayMethodId,
            tcam.NAME AS configAssayMethodName,
            tcam.MTHD_NAME_CTGR AS configAssayMethodNameAndCategory,
            tbatd.ASY_TP AS assayType,
            tbatd.ASY_PRJ AS assayProject,
            tbatd.ASY_DEPT_ID AS assayDepartmentId,
            tbatd.ASY_DEPT_NAME AS assayDepartmentName
        FROM
            T_BSN_ASY_TSK_DAT tbatd
        LEFT JOIN T_BSN_SB_SMP tbss ON
            tbatd.BSN_SB_SMP_ID = tbss.ID
        LEFT JOIN T_CFG_ASY_MTHD tcam ON 
            tbatd.CFG_ASY_MTHD_ID = tcam.ID  
        LEFT JOIN T_BSN_BSE_SMP tbbs ON 
            tbatd.BSN_BSE_SMP_ID = tbbs.ID 
        LEFT JOIN T_BSN_SB_SMP_ANL_GRP tbssag ON
            tbatd.BSN_SB_SMP_ID = tbssag.BSN_SB_SMP_ID
            AND tbatd.ASY_DEPT_ID = tbssag.ASY_DEPT_ID
        LEFT JOIN T_CFG_BSE_SMP tcbs ON 
            tbbs.CFG_BSE_SMP_ID = tcbs.ID 
        WHERE 
            tbatd.DELETED = 0
            AND tbatd.IS_ASN_TSKD = 0
      <if test="reqVO.assayDepartmentId != null and reqVO.assayDepartmentId != ''">
            AND tbatd.ASY_DEPT_ID = #{reqVO.assayDepartmentId}
      </if>
      <if test="reqVO.sampleFlowNodeKey != null and reqVO.sampleFlowNodeKey != ''">
            AND tbatd.SMP_FLW_NDE_KY = #{reqVO.sampleFlowNodeKey}
      </if>
      <if test="reqVO.sampleFlowNodeTime != null and reqVO.sampleFlowNodeTime.length == 2">
            AND tbatd.SMP_FLW_NDE_TM BETWEEN #{reqVO.sampleFlowNodeTime[0]} AND #{reqVO.sampleFlowNodeTime[1]}
      </if>
      <if test="reqVO.sampleStatus != null and reqVO.sampleStatus != ''">
            AND tbssag.SMP_STS = #{reqVO.sampleStatus}
      </if>
      <if test="reqVO.sampleCode != null and reqVO.sampleCode != ''">
            AND tbss.SMP_CD = #{reqVO.sampleCode}
      </if>
      <if test="reqVO.sampleAssayCode != null and reqVO.sampleAssayCode != ''">
            AND tbss.SMP_ASY_CD = #{reqVO.sampleAssayCode}
      </if>
      <if test="reqVO.sampleReturnCode != null and reqVO.sampleReturnCode != ''">
            AND tbss.SMP_RTN_CD = #{reqVO.sampleReturnCode}
      </if>
      <if test="reqVO.sampleName != null and reqVO.sampleName != ''">
            AND tbss.SMP_NAME  like '%' || #{reqVO.sampleName} || '%'
      </if>
      <if test="reqVO.configAssayMethodNameAndCategory != null and reqVO.configAssayMethodNameAndCategory != ''">
            AND tcam.MTHD_NAME_CTGR  like '%' || #{reqVO.configAssayMethodNameAndCategory} || '%'
      </if>
        GROUP BY tbss.ID,
            tbss.SMP_NAME,
            tbss.SMP_CD,
            tbss.SMP_ASY_CD,
            tbss.SMP_RTN_CD,
            tbss.SMP_FLW_NDE_TM,
            tcbs.BSE_SMP_ID,
            tbbs.BSE_SMP_NAME,
            tbatd.BSN_SB_SMP_ID,
            tbatd.CFG_ASY_MTHD_ID,
            tcam.NAME,
            tcam.MTHD_NAME_CTGR,
            tbatd.ASY_TP,
            tbatd.ASY_PRJ,
            tbatd.ASY_DEPT_ID,
            tbatd.ASY_DEPT_NAME
     </select>

</mapper>