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

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

    <select id="selectPageForDispatch" resultType="com.zt.plat.module.qms.business.bus.controller.vo.BusinessSubSampleExtendRespVO">
    select
        smp.ID,
        smp.TENANT_ID as tenantId,
        smp.BSN_BSE_SMP_ID as businessBaseSampleId,
        smp.BSN_SB_PRN_SMP_ID as businessSubParentSampleId,
        smp.CFG_SB_SMP_ID as configSubSampleId,
        smp.DIC_BSN_ID as dictionaryBusinessId,
        smp.CFG_WRH_LOC_INF_ID as configWarehouseLocationInfomationId,
        smp.SMP_ID as sampleId,
        smp.GRP_ID as groupId,
        smp.SMP_NAME as sampleName,
        smp.SMP_CD as sampleCode,
        smp.SMP_ASY_CD as sampleAssayCode,
        smp.SMP_RTN_CD as sampleReturnCode,
        smp.IS_GEN_RTN_CD as isGenerateReturnCode,
        smp.IS_WG as isWeighing,
        smp.LST_SMP_WGT as lastSampleWeight,
        smp.LST_BAL_CD as lastBalanceCode,
        smp.CFG_SMP_FLW_ID as configSampleFlowId,
        smp.SMP_FLW_NDE_KY as sampleFlowNodeKey,
        smp.SMP_FLW_NDE_TM as sampleFlowNodeTime,
        smp.NXT_SMP_FLW_NDE as nextSampleFlowNode,
        smp.SMP_STS as sampleStatus,
        smp.ASY_OPTR as sampleOperator,
        smp.IS_ASN_TSKD as isAssignTasked,
        smp.ASN_TSK_TM as assignTaskTime,
        smp.IS_RPOD as isReported,
        smp.RPTR as reporter,
        smp.RPT_TM as reportTime,
        smp.RTN_TM as returnTime,
        smp.RTN_STS as returnStatus,
        smp.DST_STS as dispatchStatus,
        smp.RTN_CD_PRNT_CNT as returnCodePrintCount,
        smp.PRNT_LST_TM as printLastTime,
        smp.UP_SMP_RCD_ID as upSampleRecordId,
        smp.ANL_RMK as analysisRemark,
        smp.IS_ENBD as isEnabled,
        smp.SYS_DEPT_CD as systemDepartmentCode,
        smp.UPD_CNT as updateCount,
        smp.RMK as remark,
        loc.CD as warehouseLocationCode,
        wrh.CD as warehouseCode
    from t_bsn_sb_smp smp
    left join t_cfg_wrh_loc loc on smp.CFG_WRH_LOC_INF_ID = loc.id
    left join t_cfg_wrh_loc wrh on loc.PRN_ID = wrh.id
    <where>
        <if test="param.sampleFlowNodeKey != null and param.sampleFlowNodeKey != ''">
            and smp.SMP_FLW_NDE_KY = #{param.sampleFlowNodeKey}
        </if>
        <if test="param.sampleReturnCode != null and param.sampleReturnCode != ''">
            and smp.SMP_RTN_CD = #{param.sampleReturnCode}
        </if>
        <if test="param.returnStatus != null and param.returnStatus != ''">
            and smp.RTN_STS = #{param.returnStatus}
        </if>
        <if test="param.dispatchStatus != null and param.dispatchStatus != ''">
            and smp.DST_STS = #{param.dispatchStatus}
        </if>
        <if test="param.warehouseLocationCode != null and param.warehouseLocationCode != ''">
            and loc.CD = #{param.warehouseLocationCode}
        </if>
        <if test="param.warehouseCode != null and param.warehouseCode != ''">
            and wrh.CD = #{param.warehouseCode}
        </if>
        <if test="param.hideDispatchData != null and param.hideDispatchData == '1'.toString()">
            and not exists(
                select 1 from T_BSN_SMP_DST_DTL d left join T_BSN_SMP_DST m on d.PRN_ID = m.ID
                                where d.BSN_SB_SMP_ID = smp.ID
                                and d.deleted = 0
                                and m.FIN_STS in ('0', 'pending')
                                and m.CNL_STS = 0
                                and m.deleted = 0
            )
        </if>
    </where>
    </select>


</mapper>