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

    <resultMap id="identificationMaterialTransferRetainedAnnexMap" type="digital.laboratory.platform.imr.entity.IdentificationMaterialTransferRetainedAnnex">
        <id property="id" column="id"/>
        <result property="retainedSampleInboundDate" column="retained_sample_inbound_date"/>
        <result property="retainedSampleOutboundDate" column="retained_sample_outbound_date"/>
        <result property="retainedSampleInboundQuality" column="retained_sample_inbound_quality"/>
        <result property="retainedSampleOutboundQuality" column="retained_sample_outbound_quality"/>
        <result property="inboundAdminId" column="inbound_admin_id"/>
        <result property="outboundAdminId" column="outbound_admin_id"/>
        <result property="inboundAppraiserId" column="inbound_appraiser_id"/>
        <result property="outboundAppraiserId" column="outbound_appraiser_id"/>
        <result property="inboundHandledBy" column="inbound_handled_by"/>
        <result property="outboundHandledBy" column="outbound_handled_by"/>
        <result property="sampleId" column="sample_id"/>
        <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>


    <resultMap id="retainedAnnexMap" type="digital.laboratory.platform.imr.vo.RetainedSample" extends="identificationMaterialTransferRetainedAnnexMap">
        <result property="inboundAppraiserName" column="inbound_appraiser_name"/>
        <result property="outboundAppraiserName" column="outbound_appraiser_name"/>
        <result property="inboundAdminName" column="inbound_admin_name"/>
        <result property="outboundAdminName" column="outbound_admin_name"/>
        <result property="inboundHandledByName" column="inbound_handled_by_name"/>
        <result property="outboundHandledByName" column="outbound_handled_by_name"/>
    </resultMap>


<!--    List<RetainedSample> getSampleTransfer(String sampleId);-->


    <select id="getSampleTransfer" resultMap="retainedAnnexMap">
        select retained.*,
               (
                   SELECT user.name
                   FROM dlp_base.sys_user user
                   WHERE user.user_id=retained.inbound_appraiser_id
               ) AS inbound_appraiser_name,
               (
                   SELECT user.name
                   FROM dlp_base.sys_user user
                   WHERE user.user_id=retained.outbound_appraiser_id
               ) AS outbound_appraiser_name,
               (
                   SELECT user.name
                   FROM dlp_base.sys_user user
                   WHERE user.user_id=retained.inbound_admin_id
               ) AS inbound_admin_name,
               (
                   SELECT user.name
                   FROM dlp_base.sys_user user
                   WHERE user.user_id=retained.outbound_admin_id
               ) AS outbound_admin_name,
               (
                   SELECT user.name
                   FROM dlp_base.sys_user user
                   WHERE user.user_id=retained.inbound_handled_by
               ) AS inbound_handled_by_name,
               (
                   SELECT user.name
                   FROM dlp_base.sys_user user
                   WHERE user.user_id=retained.outbound_handled_by
               ) AS outbound_handled_by_name
        from dlp_identification_material_repository.b_identification_material_transfer_retained_annex retained
        join dlp_identification_material_transfer.b_sample sample
        on sample.id = retained.sample_id
        where retained.sample_id = #{sampleId}
    </select>


</mapper>
