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

  <resultMap id="sampleOutWarehouseApplyMap" type="digital.laboratory.platform.imr.entity.SampleOutWarehouseApply">
    <id property="id" column="id"/>
    <result property="applicantId" column="applicant_id"/>
    <result property="applicationDate" column="application_date"/>
    <result property="purpose" column="purpose"/>
    <result property="status" column="status"/>
    <result property="returnDate" column="return_date"/>
    <result property="sampleCodeNumber" column="sample_code_number"/>
    <result property="imAdministratorsId" column="im_administrators_id"/>
    <result property="appraiserId" column="appraiser_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="outApplyVOMap" type="digital.laboratory.platform.imr.vo.SampleOutWarehouseApplyVO" extends="sampleOutWarehouseApplyMap">
    <result property="applicantName" column="applicant_name"/>
    <result property="assigneeName" column="assignee_name"/>
    <collection property="sampleList" select="digital.laboratory.platform.imr.mapper.OutWarehouseDetailedMapper.getOutWarehouseApplyDetailed" column="id"/>
  </resultMap>

  <!--SampleOutWarehouseApplyVO getOutApplyDetail(String id);-->
  <select id="getOutApplyDetail" resultMap="outApplyVOMap">
    select sowa.*,
           (
             SELECT user.name
             FROM dlp_base.sys_user user
             WHERE user.user_id=sowa.applicant_id
           ) AS applicant_name,
           (
             SELECT user.name
             FROM dlp_base.sys_user user
             WHERE user.user_id=sowa.appraiser_id
           ) AS assignee_name
    from dlp_identification_material_repository.sample_out_warehouse_apply sowa
    where sowa.id = #{id}
  </select>

  <sql id="getOutwarehouseApply">
    select sowa.*,
           (
             SELECT user.name
             FROM dlp_base.sys_user user
             WHERE user.user_id=sowa.applicant_id
           ) AS applicant_name,
           (
             SELECT user.name
             FROM dlp_base.sys_user user
             WHERE user.user_id=sowa.appraiser_id
           ) AS assignee_name
    from dlp_identification_material_repository.sample_out_warehouse_apply sowa
  </sql>




<!--  IPage<SampleOutWarehouseApply> getSampleOutWarehouseApplyVOPage(@Param("page") Page<SampleOutWarehouseApply> page, @Param("qw")QueryWrapper<SampleOutWarehouseApply> qw);-->
  <select id="getSampleOutWarehouseApplyVOPage" resultMap="outApplyVOMap" resultType="digital.laboratory.platform.imr.vo.SampleOutWarehouseApplyVO">
    <include refid="getOutwarehouseApply"></include>
    ${ew.customSqlSegment}
  </select>


<!--  IPage<SampleOutWarehouseApply> getSampleOutWarehouseApplyVOList(@Param("qw")QueryWrapper<SampleOutWarehouseApply> qw);-->
  <select id="getSampleOutWarehouseApplyVOList" resultMap="outApplyVOMap" resultType="digital.laboratory.platform.imr.vo.SampleOutWarehouseApplyVO">
  <include refid="getOutwarehouseApply"/>
  ${ew.customSqlSegment}
  </select>

  <select id="getCellLocation" resultType="digital.laboratory.platform.imr.vo.CellVO" >
      SELECT c.cabinet_type, b.location
      FROM dlp_cabinet.b_cabinet c,
           dlp_cabinet.b_cabinetcell b
      WHERE c.id = b.cabinet_Id
        and b.id =  #{id}
  </select>





  <!-- +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-->


  <resultMap id="sampleOutVOMap" type="digital.laboratory.platform.imr.vo.SampleOutWarehouseApplyVO" extends="sampleOutWarehouseApplyMap">
    <result property="applicantName" column="applicant_name"/>
    <result property="assigneeName" column="assignee_name"/>
    <collection property="sampleList" select="digital.laboratory.platform.imr.mapper.SampleOutWarehouseApplyMapper.getOutDetailed" column="id"/>
  </resultMap>

<!--  SampleOutWarehouseApplyVO getSampleOutRepositoryInfo(String id);-->
  <select id="getSampleOutRepositoryInfo" resultMap="sampleOutVOMap">
    select sowa.*,
           (
             SELECT user.name
             FROM dlp_base.sys_user user
             WHERE user.user_id=sowa.applicant_id
           ) AS applicant_name,
           (
             SELECT user.name
             FROM dlp_base.sys_user user
             WHERE user.user_id=sowa.appraiser_id
           ) AS assignee_name
    from dlp_identification_material_repository.sample_out_warehouse_apply sowa
    where sowa.id = #{id}
  </select>




  <resultMap id="outSampleVo" type="digital.laboratory.platform.imr.vo.OutSampleVO">
    <id property="id" column="id"/>
    <result property="sampleNo" column="sample_no"/>
    <result property="name" column="name"/>
    <result property="storageMethod" column="storage_method"/>
    <result property="category" column="category"/>
    <result property="sampleType" column="sample_type"/>
    <result property="boxNo" column="box_no"/>
    <result property="form" column="form"/>
    <result property="storageLocation" column="storage_location"/>
  </resultMap>

  <!--List<OutSampleVO> getOutWarehouseApplyDetailed(String id);-->
  <select id="getOutDetailed" resultMap="outSampleVo">
    select s.sample_type,
           s.id,
           s.storage_method,
           im.type as category,
           im.name,
           s.sample_no,
           im.form,
           (
             SELECT box.box_no
             FROM dlp_entrustment.b_sample_box box
             WHERE box.id=s.box_id
           ) AS box_no
    from dlp_identification_material_repository.out_warehouse_detailed od
    join dlp_entrustment.b_sample s
    on od.sample_id = s.id
    join dlp_entrustment.b_entrustment_identification_material im
    on s.identification_material_id = im.id
    where od.apply_id = #{id}
  </select>

</mapper>
