<?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="digital.laboratory.platform.inspection.mapper.TestRecordSampleDataMapper">
    <resultMap id="BaseResultMap" type="digital.laboratory.platform.inspection.entity.TestRecordSampleData">
        <id property="id" column="id" jdbcType="VARCHAR"/>
        <result property="name" column="name" jdbcType="VARCHAR"/>
        <result property="sampleNo" column="sample_no" jdbcType="VARCHAR"/>
        <result property="testId" column="test_id" jdbcType="VARCHAR"/>
        <result property="stdConcentration" column="std_concentration" jdbcType="VARCHAR"/>
        <result property="sampleConcentration" column="sample_concentration" jdbcType="VARCHAR"/>
        <result property="compoundName" column="compound_name" jdbcType="VARCHAR"/>
        <result property="rtTimeWithinError" column="rt_time_within_error" jdbcType="VARCHAR"/>
        <result property="rtTimeError" column="rt_time_error" jdbcType="DOUBLE"/>
        <result property="targetRtTime" column="target_rt_time" jdbcType="DOUBLE"/>
        <result property="stdRtTime" column="std_rt_time" jdbcType="DOUBLE"/>
        <result property="isDetected" column="is_detected" jdbcType="INTEGER"/>
        <result property="sampleType" column="sample_type" jdbcType="VARCHAR"/>
        <result property="dataJson" column="data_json" jdbcType="VARCHAR"/>
        <result property="dataResultJson" column="data_result_json" jdbcType="VARCHAR"/>
        <result property="status" column="status" jdbcType="TINYINT"/>
        <result property="createTime" column="create_time" jdbcType="TIMESTAMP"/>
        <result property="createBy" column="create_by" jdbcType="VARCHAR"/>
        <result property="updateTime" column="update_time" jdbcType="TIMESTAMP"/>
        <result property="updateBy" column="update_by" jdbcType="VARCHAR"/>
        <result property="compoundCnName" column="compound_cn_name" jdbcType="VARCHAR"/>
    </resultMap>

    <sql id="Base_Column_List">
            id,
            name,
            sample_no,
            test_id,
            std_concentration,
            sample_concentration,
            compound_name,
            rt_time_within_error,
            rt_time_error,
            target_rt_time,
            std_rt_time,
            is_detected,
            sample_type,
            data_json,
            data_result_json,
            status,
            create_time,
            create_by,
            update_time,
            update_by,
            compound_cn_name
    </sql>
<!--    合并委托、任务、筛查表-->
    <sql id="queryCommoneBusinessSql">
        SELECT T.id,
               T.material_type,
               T.business_type,
               T.case_name,
               T.type,
               T.compounds,
               T.report_config,
               T.create_time,
               T.create_by,
               T.status,
               T.task_start_date
        FROM (SELECT id, material_type, case_name, business_type, "" AS compounds, "" AS report_config, 10000 AS type, create_time, create_by, status, "" AS task_start_date
              FROM b_entrustinfo
              UNION ALL
              SELECT id, "" AS material_type, case_name, business_type, "" AS compounds, "" AS report_config,30000 AS type, create_time, create_by, 3 AS status, "" AS task_start_date
              FROM b_case_screen
              UNION ALL
              SELECT id, "" AS material_type, task_name AS case_name, business_type, compounds, report_config, 20000 AS type, create_time, create_by, 3 AS status, task_start_date
              FROM b_taskinfo) T
    </sql>

<!--    通过业务id查询委托表、筛查表、任务表-->
    <select id="queryESTBusinessInfoList" resultType="digital.laboratory.platform.inspection.vo.ESTBusinessInfoVO">
        <include refid="queryCommoneBusinessSql"></include>
        ${ew.customSqlSegment}
    </select>

    <!--    实验结果查询中分页查询-->
    <select id="queryTestResultTaskPage" resultType="digital.laboratory.platform.inspection.vo.TestResultBusinessVO">
        <include refid="queryCommoneBusinessSql"></include>
        ${ew.customSqlSegment}
    </select>

    <!--    实验结果查询中列表查询-->
    <select id="queryTestResultTaskList" resultType="digital.laboratory.platform.inspection.vo.TestResultBusinessVO">
        <include refid="queryCommoneBusinessSql"></include>
        ${ew.customSqlSegment}
    </select>

<!--    检验数据、样本溶液、检材连表查询-->
    <sql id="joinDataSolutionSampleQuerySql">
        SELECT
            rs.id AS sample_data_id, -- 检验数据id
            rs.data_result_json, -- 数据json
            rs.compound_name, -- 化合物名称
            rs.is_detected, -- 是否检出
            rs.test_id, -- 实验id
            rs.name, -- name
            ss.sample_no, -- 溶液编号
            si.id AS material_id, -- 检材id
            si.sample_name, -- 检材名称
            si.business_id, -- 业务id，可能是委托、任务、筛查
            si.order_no -- 委托中检材的序号
        FROM
            `b_test_record_sampledata` rs
                LEFT JOIN `b_test_record_sample_solution` ss ON rs.sample_no = ss.sample_no
                LEFT JOIN `b_sampleinfo` si ON si.id = ss.material_id OR rs.sample_no = si.accept_no -- 如果溶液关联id 不存在则通过检材受理编号关联
    </sql>

    <select id="queryDataSolutionSampleDTOList" resultType="digital.laboratory.platform.inspection.dto.DataSolutionSampleDTO">
        <include refid="joinDataSolutionSampleQuerySql"></include>
        ${ew.customSqlSegment}
    </select>

<!--    根据业务id 查询检材内容，然后通过检材id关联查询溶液，最后通过溶液编号查询数据-->
    <sql id="getTestDataByBusinessSql">
        SELECT
            rs.id AS sample_data_id, -- 检验数据id
            rs.data_result_json, -- 数据json
            rs.compound_name, -- 化合物名称
            rs.is_detected, -- 是否检出
            rs.test_id, -- 实验id
            si.accept_no AS sample_no, -- 检材受理编号
            si.id AS material_id, -- 检材id
            si.sample_name, -- 检材名称
            rs.sample_concentration, -- 对应的检材化合物浓度
            si.business_id -- 业务id，可能是委托、任务、筛查
        FROM
            `b_sampleinfo` si
                LEFT JOIN `b_test_record_sampledata` rs ON rs.sample_no = si.accept_no
    </sql>

    <select id="getTestDataListByBusiness" resultType="digital.laboratory.platform.inspection.dto.DataSolutionSampleDTO">
        <include refid="getTestDataByBusinessSql"></include>
        ${ew.customSqlSegment}
    </select>

<!--    获取任务检验数据的sql-->
    <sql id="queryTaskTestDataSql">
        SELECT
            rs.id AS sample_data_id,-- 检验数据id
            rs.data_result_json,-- 数据json
            rs.compound_name,-- 化合物名称
            rs.is_detected,-- 是否检出
            rs.test_id,-- 实验id
            rs.status, -- 任务数据的审核状态
            rs.sample_concentration, -- 对应的检材化合物浓度
            ss.sample_no,-- 溶液编号
            si.id AS material_id,-- 检材id
            si.accept_no, -- 检材编号
            si.sample_name,-- 检材名称
            si.business_id, -- 业务id，可能是委托、任务、筛查
            ti.task_name,
            ti.business_type,
            ti.create_time
        FROM
            `b_test_record_sampledata` rs
                LEFT JOIN `b_test_record_sample_solution` ss ON rs.sample_no = ss.sample_no
                LEFT JOIN `b_sampleinfo` si ON si.id = ss.material_id
                JOIN b_taskinfo ti ON si.business_id = ti.Id
    </sql>

    <select id="queryWaitApproveTaskTestDataList" resultType="digital.laboratory.platform.inspection.dto.TaskTestDataDTO">
        <include refid="queryTaskTestDataSql"></include>
        ${ew.customSqlSegment}
    </select>

</mapper>
