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

  <resultMap id="sampleApplyMap" type="digital.laboratory.platform.imr.entity.SampleApply">
    <id property="id" column="id"/>
    <result property="applicantId" column="applicant_id"/>
    <result property="applicationDate" column="application_date"/>
    <result property="applicationStatus" column="application_status"/>
    <result property="applyQuality" column="apply_quality"/>
    <result property="applyQuantity" column="apply_quantity"/>
    <result property="approvalDate" column="approval_date"/>
    <result property="approverId" column="approver_id"/>
    <result property="approverOpinion" column="approver_opinion"/>
    <result property="firstAuditorId" column="first_auditor_id"/>
    <result property="firstAuditDate" column="first_audit_date"/>
    <result property="firstAuditOpinion" column="first_audit_opinion"/>
    <result property="reason" column="reason"/>
    <result property="secondaryAuditDate" column="secondary_audit_date"/>
    <result property="secondaryAuditOpinion" column="secondary_audit_opinion"/>
    <result property="secondaryAuditorId" column="secondary_auditor_id"/>
    <result property="applyType" column="apply_type"/>
    <result property="createTime" column="create_time"/>
    <result property="updateTime" column="update_time"/>
    <result property="createBy" column="create_by"/>
    <result property="updateBy" column="update_by"/>
    <result property="unit" column="unit"/>
  </resultMap>

  <resultMap id="getSampleApplyInfo" type="digital.laboratory.platform.imr.vo.SampleApplyVO" extends="sampleApplyMap">
    <result property="applicantName" column="applicant_name"/>
    <result property="firstAuditorName" column="first_auditor_name"/>
    <result property="secondaryAuditName" column="secondary_audit_name"/>
    <result property="approverName" column="approve_name"/>
    <result property="applicantDepartment" column="department"/>
  </resultMap>

  <sql id="sampleApplySql">
    select sa.*,
           (
             SELECT user.name
             FROM dlp_base.sys_user user
             WHERE user.user_id=sa.applicant_id
           ) AS applicant_name,
           (
             SELECT user.name
             FROM dlp_base.sys_user user
             WHERE user.user_id=sa.approver_id
           ) AS approve_name,
           (
             SELECT user.name
             FROM dlp_base.sys_user user
             WHERE user.user_id=sa.first_auditor_id
           ) AS first_auditor_name,
           (
             SELECT user.name
             FROM dlp_base.sys_user user
             WHERE user.user_id=sa.secondary_auditor_id
           ) AS secondary_audit_name,
           (
             SELECT user.department
             FROM dlp_base.sys_user user
             WHERE user.user_id=sa.applicant_id
           ) AS department
    from dlp_identification_material_repository.sample_apply sa
  </sql>



<!-- 获取检材/样本申请vo(简单信息,创建时返回)  SampleApplyVO getSampleApplyInfo(String id);-->

  <select id="getSampleApply" resultMap="getSampleApplyInfo">
    <include refid="sampleApplySql"/>
    where id= #{id}
  </select>


<!--  IPage<SampleApplyVO> getApplyPage(@Param("page") Page<SampleApply> page, @Param(Constants.WRAPPER)QueryWrapper<SampleApply> qw);-->
  <select id="getApplyPage" resultMap="getSampleApplyInfo">
    <include refid="sampleApplySql"/>
    ${ew.customSqlSegment}
  </select>

  <resultMap id="statusSampleMap" type="digital.laboratory.platform.imr.vo.StatusSampleVO">
    <id column="id" property="id"/>
    <result property="name" column="name"/>
    <result column="sample_no" property="sampleNo"/>
    <result property="status" column="status"/>
  </resultMap>


  <select id="getSampleInfo" resultMap="statusSampleMap">
        select sample.id,
               sample.name,
               sample.status,
               sample.sample_no
        from dlp_identification_material_transfer.b_sample sample
        where sample.id = #{id}
  </select>


</mapper>
