<?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.imr.mapper.CabinetOpeningRecordMapper">

    <resultMap id="openRecordMap" type="digital.laboratory.platform.imr.entity.CabinetOpeningRecord">
        <id column="id" property="id"/>
        <result column="storage_location" property="storageLocation"/>
        <result column="open_time" property="openTime"/>
        <result column="open_person" property="openPerson"/>
        <result column="open_type" property="openType"/>
        <result column="apply_person" property="applyPerson"/>
        <result column="openPurpose" property="openPurpose"/>
        <result column="warehouse" property="warehouse"/>
        <result column="supervisor" property="supervisor"/>
        <result property="createTime" column="create_time"/>
        <result property="updateTime" column="update_time"/>
        <result property="createBy" column="create_by"/>
        <result property="updateBy" column="update_by"/>
    </resultMap>



    <!--CabinetSampleVO scanCode(String number);-->

    <select id="scanCode" resultType="digital.laboratory.platform.imr.vo.CabinetSampleVO">
        select sample.name as sample_name,
               sample.source,
               sample.id as sample_id,
               sample.sample_type,
               sample.unit,
               sample.sample_no,
               (
                   select b1.weight
                   from dlp_base.b_balance_r_material b1
                   where b1.create_time =
                   (select max(b2.create_time)
                    from dlp_base.b_balance_r_material b2
                    where b2.identification_material_id = sample.id
                    )
               )as quality,
               im.inspection_materials_no as im_nu,
               (select d.dictionary_describe
                from dlp_identification_material_transfer.sample_dictionary d
                where sample.source = d.business_status
                )as source_name
        from dlp_identification_material_transfer.b_sample sample
        join dlp_identification_material_transfer.identification_material im
        on sample.identification_material_id = im.id
        where im.inspection_materials_no = #{number} or sample.sample_no= #{number} or sample.box_id = #{number}
               and sample.status not in(2,4,9,13)
    </select>



    <!--CabinetSampleVO getSampleById(@Param("sampleId")String sampleId);-->
    <select id="getSampleById" resultType="digital.laboratory.platform.imr.vo.CabinetSampleVO">
        select sample.name as sample_name,
               sample.source,
               sample.id as sample_id,
               sample.sample_type,
               sample.sample_no,
               im.inspection_materials_no ,
               (select d.dictionary_describe as source_name
                from dlp_identification_material_transfer.sample_dictionary d
                where sample.source = d.business_status
               )as im_nu,
            st.storage_location,
            st.room_no
        from dlp_identification_material_transfer.b_sample sample
        join dlp_identification_material_transfer.identification_material im
        on sample.identification_material_id = im.id
        join dlp_identification_material_repository.b_sample_storage st
        on st.sample_id = sample.id
        where sample.id = #{sampleId}
    </select>



    <!--List<CabinetSampleVO> batchScanCode(@Param("number")String number);-->

    <select id="batchScanCode" resultType="digital.laboratory.platform.imr.vo.CabinetSampleVO">
        SELECT
            sample.name AS sample_name,
            sample.source,
            sample.id AS sample_id,
            sample.sample_type,
            sample.sample_no,
            (
                SELECT b1.weight
                FROM dlp_base.b_balance_r_material b1
                WHERE b1.create_time = (
                    SELECT MAX(b2.create_time)
                    FROM dlp_base.b_balance_r_material b2
                    WHERE b2.identification_material_id = sample.id
                )
            ) AS quality,
            im.inspection_materials_no,
            (
                SELECT d.dictionary_describe
                FROM dlp_identification_material_transfer.sample_dictionary d
                WHERE sample.source = d.business_status
            ) AS source_name
        FROM
            dlp_identification_material_transfer.b_sample sample
                JOIN
            dlp_identification_material_transfer.identification_material im
            ON
                sample.identification_material_id = im.id
        WHERE
            (sample.sample_no LIKE CONCAT('%', #{number}, '%')
                OR im.inspection_materials_no = #{number}
                OR sample.box_id = #{number})
          AND sample.status NOT IN (2, 4, 9)
    </select>



    <!--void updateSampleQuality(String sampleId, double sampleQuality);-->

    <update id="updateSampleQuality">
        update dlp_identification_material_transfer.b_sample
        set existing_quality = #{sampleQuality}
        where id = #{sampleId}
    </update>


</mapper>
