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

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


    <select id="queryWaitingDataForReport" resultType="com.zt.plat.module.qms.business.bus.dal.dataobject.BusinessSampleEntrustRegistrationDO">
        select
        m.ID as id,
        m.ENTT_ID as entrustId,
        m.ENTT_NUM as entrustNumber,
        m.ENTT_UNT as entrustUnit,
        m.CFG_ENTT_SRC_ID as configEntrustSourceId,
        m.SMP_QTY as sampleQuantity,
        m.TSK_QTY as taskQuantity,
        m.TSK_FIND_QTY as taskFinishedQuantity,
        m.RMNE_SMP_REQM as remaineSampleRequirement,
        m.CRT_WY as createWay,
        m.ENTT_TP as entrustType,
        m.ENTT_TM as entrustTime,
        m.SMP_SNDR as sampleSender,
        m.SMP_SND_DT as sampleSendDate,
        m.SMP_RCVR as sampleReceiver,
        m.SMP_RCV_DT as sampleReceiveDate,
        m.SMP_STS as sampleStatus,
        m.EXT_INF_DAT_COLT_ID as externalInfomationDataCollectionId,
        m.EXT_INF as externalInfomation,
        m.CFG_RPT_TMPL_ID as configReportTemplateId,
        m.CFG_RPT_TMPL_KY as configReportTemplateKey,
        m.TEL as tel,
        m.FAX as fax,
        m.ADR as address,
        m.PSTL as postal,
        m.EM as email,
        m.ENTT_CTGR as entrustCategory,
        m.SMP_CTGR as sampleCategory,
        m.SMP_ORGN as sampleOrigin,
        m.SCRY_REQR as secrecyRequire,
        m.ASY_MTHD as assayMethod,
        m.REG_STS as registrationStatus,
        m.DAT_CHK_STS as dataCheckStatus,
        m.ASY_STS as assayStatus,
        m.DAT_STS as dataStatus,
        m.DOC_MAIN_ID as documentMainId,
        m.DOC_SND_WY as documentSendWay,
        m.DOC_PUBR as documentPublisher,
        m.DOC_PUBR_DT as documentPublisherDate,
        m.DOC_RCVR as documentReceiver,
        m.DOC_RCV_DT as documentReceiveDate,
        m.SYS_DEPT_CD as systemDepartmentCode,
        m.UPD_CNT as updateCount,
        m.RMK as remark,
        m.CREATE_TIME as createTime,
        es.name as configEntrustSourceName
        FROM T_BSN_SMP_ENTT_REG m
        left join T_CFG_ENTT_SRC es on m.CFG_ENTT_SRC_ID = es.id
        <where>
            and NOT EXISTS (
            select 1 from T_RPT_DOC_MAIN dm left join T_RPT_DOC_MAIN_CORR dc on dm.id = dc.MAIN_ID
            where dc.CORR_ID = m.id and dm.FLW_STS in ('not_start','in_progress','completed','rejected') and dm.deleted = 0
            )

            <if test="param.configReportTypeId != null">
                <!-- 通过主样配置id过滤-->
                and exists (
                select 1 from T_BSN_SMP_ENTT_DTL d
                inner join T_BSN_BSE_SMP s on d.BSN_BSE_SMP_ID = s.id
                where
                    d.BSN_SMP_ENTT_REG_ID = m.id
                    and s.CFG_BSE_SMP_ID in (
                        select csr.CFG_BSE_SMP_ID from T_CFG_SMP_RPT csr where
                        csr.CFG_RPT_TP_ID = ${param.configReportTypeId}
                        and csr.DELETED = 0
                    )
                    and d.DELETED = 0
                )
            </if>
            <if test="param.assayStatus != null and param.assayStatus != ''">
                and m.ASY_STS = #{param.assayStatus}
            </if>
            <if test="param.entrustType != null and param.entrustType != ''">
                and m.ENTT_TP = #{param.entrustType}
            </if>
            <if test="param.entrustTime != null and param.entrustTime.length > 0">
                and m.ENTT_TM between #{param.entrustTime[0]} and #{param.entrustTime[1]}
            </if>
            <if test="param.entrustNumber != null and param.entrustNumber != ''">
                and m.ENTT_NUM like concat('%',#{param.entrustNumber},'%')
            </if>
            <if test="param.entrustUnit != null and param.entrustUnit != ''">
                and m.ENTT_UNT like concat('%',#{param.entrustUnit},'%')
            </if>
            and m.deleted = 0
        </where>
    </select>



</mapper>