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

    <resultMap id="sampleInboundAndOutboundTableMap" type="digital.laboratory.platform.imr.entity.SampleInboundAndOutboundTable">
        <id property="id" column="id"/>
        <result property="appraiserId" column="appraiser_id"/>
        <result property="outWarehouseDate" column="out_warehouse_date"/>
        <result property="remarks" column="remarks"/>
        <result property="sampleId" column="sample_id"/>
        <result property="warehousingDate" column="warehousing_date"/>
        <result property="imAdministrators" column="im_administrators"/>
        <result property="recipient" column="recipient_id"/>
        <result property="inboundSupervisor" column="inboundSupervisor"/>
        <result property="outboundSupervisor" column="outboundSupervisor"/>
    </resultMap>

    <resultMap id="tableMap" type="digital.laboratory.platform.imr.vo.SampleTableVO">
        <result property="appraiserName" column="appraiser_name"/>
        <result property="imName" column="im_name"/>
        <result property="recipientName" column="recipient_name"/>
        <result property="outboundSupervisorName" column="outbound_supervisor_name"/>
        <result property="inboundSupervisorName" column="inbound_supervisor_name"/>
    </resultMap>


    <!--List<SampleTableVO> getTableVO(String sampleId);-->
    <select id="getTableVO" resultMap="tableMap">
        select t.*,
               (
                   SELECT user.name
                   FROM dlp_base.sys_user user
                   WHERE user.user_id=t.appraiser_id
               ) AS appraiser_name,
               (
                   SELECT user.name
                   FROM dlp_base.sys_user user
                   WHERE user.user_id=t.im_administrators
               ) AS im_name,
               (
                   SELECT user.name
                   FROM dlp_base.sys_user user
                   WHERE user.user_id=t.recipient
               ) AS recipient_name,
               (
                   SELECT user.name
                   FROM dlp_base.sys_user user
                   WHERE user.user_id=t.outbound_supervisor
               ) AS outbound_supervisor_name,
               (
                   SELECT user.name
                   FROM dlp_base.sys_user user
                   WHERE user.user_id=t.inbound_supervisor
               ) AS inbound_supervisor_name
        from dlp_identification_material_repository.sample_inbound_and_outbound_table t
        where t.sample_id=#{id}
    </select>




    <!--List<InAndOutInfos> getInandOutTableInfo(String sampleId)-->

    <resultMap id="printTableMap" type="digital.laboratory.platform.imr.vo.InAndOutInfos">
        <result property="sampleNo" column="sample_no"/>
        <result property="sampleType" column="sample_type"/>
        <result property="warehousingDate" column="warehousing_date"/>
        <result property="outWarehouseDate" column="out_warehouse_date"/>
        <result property="appraiserName" column="appraiser_name"/>
        <result property="imName" column="im_name"/>
        <result property="returnTime" column="return_time"/>
        <result property="outboundSupervisorName" column="outbound_supervisor_name"/>
        <result property="inboundSupervisorName" column="inbound_supervisor_name"/>
    </resultMap>

    <select id="getInandOutTableInfo" resultMap="printTableMap">
        select sample.sample_no,
               sample.sample_type,
               sampleTable.return_time,
               sampleTable.out_warehouse_date,
               sampleTable.warehousing_date,
               (
                   SELECT user.name
                   FROM dlp_base.sys_user user
                   WHERE user.user_id=sampleTable.appraiser_id
               ) AS appraiser_name,
               (
                   SELECT user.name
                   FROM dlp_base.sys_user user
                   WHERE user.user_id=sampleTable.im_administrators
               ) AS im_name,
               (
                   SELECT user.name
                   FROM dlp_base.sys_user user
                   WHERE user.user_id=sampleTable.outbound_supervisor
               ) AS outbound_supervisor_name,
               (
                   SELECT user.name
                   FROM dlp_base.sys_user user
                   WHERE user.user_id=sampleTable.inbound_supervisor
               ) AS inbound_supervisor_name
        from dlp_identification_material_repository.sample_inbound_and_outbound_table sampleTable
        join dlp_identification_material_transfer.b_sample sample
        on sampleTable.sample_id = sample.id
        where sampleTable.sample_id = #{id} order by sampleTable.create_time desc
    </select>




    <!--List<InAndOutInfos> getInandOutTableInfoByDate(LocalDate beginDate, LocalDate finishDate);-->
    <select id="getInandOutTableInfoByDate" resultMap="printTableMap">
        select sample.sample_no,
               sample.sample_type,
               sampleTable.return_time,
               sampleTable.out_warehouse_date,
               sampleTable.warehousing_date,
               sampleTable.create_time,
               (
                   SELECT user.name
                   FROM dlp_base.sys_user user
                   WHERE user.user_id=sampleTable.appraiser_id
               ) AS appraiser_name,
               (
                   SELECT user.name
                   FROM dlp_base.sys_user user
                   WHERE user.user_id=sampleTable.im_administrators
               ) AS im_name,
               (
                   SELECT user.name
                   FROM dlp_base.sys_user user
                   WHERE user.user_id=sampleTable.outbound_supervisor
               ) AS outbound_supervisor_name,
               (
                   SELECT user.name
                   FROM dlp_base.sys_user user
                   WHERE user.user_id=sampleTable.inbound_supervisor
               ) AS inbound_supervisor_name
        from dlp_identification_material_repository.sample_inbound_and_outbound_table sampleTable
        join dlp_identification_material_transfer.b_sample sample
        on sampleTable.sample_id = sample.id
        where sampleTable.warehousing_date between #{beginDate} and #{finishDate} order by sampleTable.create_time desc
    </select>











    <!--  String getEntrustmentName(@Param("id")String entrustmentId);-->
    <select id="getEntrustmentName" resultType="String">
        select event.case_name
        from dlp_entrustment.b_entrustment entrustment
        join dlp_entrustment.b_case_event event
        on entrustment.case_id = event.id
        <where>
            and entrustment.id = #{id}
            <if test="name!=null and name!=''">
                and event.case_name like concat('%',#{name},'%')
            </if>
        </where>
        group by event.id
    </select>

    <!--  String getHairJobName(@Param("id")String hairJobId);-->

    <select id="getHairJobName" resultType="String">
        select hairJob.task_name
        from dlp_hair_job.b_hair_job hairJob
        <where>
            and hairJob.id = #{id}
            <if test="name!=null and name!=''">
                and hairJob.task_name like concat('%',#{name},'%')
            </if>
        </where>
    </select>

    <!--  String getSewageJobName(@Param("id")String sewageJobId);-->

    <select id="getSewageJobName" resultType="String">
        select sewageJob.name
        from dlp_sewage_job.b_sewage_job sewageJob
        <where>
            and sewageJob.id = #{id}
            <if test="name!=null and name!=''">
                and sewageJob.name like concat('%',#{name},'%')
            </if>
        </where>
    </select>

    <!--String getEventName(@Param("id")String eventId,@Param("name")String name);-->
    <select id="getEventName" resultType="String">
        select event.event_name
        from dlp_im_acceptance.b_event event
        <where>
            and event.id = #{id}
            <if test="name!=null and name!=''">
                and event.event_name like concat('%',#{name},'%')
            </if>
        </where>
    </select>


    <!--getFeatureName-->
    <select id="getFeatureName" resultType="String">
        select feature.name
        from dlp_feature_analysis.b_feature_analysis_job feature
        <where>
            and feature.id = #{id}
            <if test="name!=null and name!=''">
                and feature.name like concat('%',#{name},'%')
            </if>
        </where>
    </select>



    <!--分组查询毛发id
    List<String> queryHairJob();-->

    <select id="queryHairJob" resultType="String">
        select sample.hair_job_id
        from dlp_identification_material_transfer.b_sample sample
        where sample.hair_job_id is not null
        group by sample.hair_job_id
    </select>

    <!--//分组查询污水任务id
    List<String> querySewageJob();-->
    <select id="querySewageJob" resultType="String">
        select sample.sewage_job_id
        from dlp_identification_material_transfer.b_sample sample
        where sample.sewage_job_id is not null
        group by sample.sewage_job_id
    </select>
    <!--//分组查询委托id
    List<String> queryEntrustment();-->
    <select id="queryEntrustment" resultType="String">
        select sample.entrustment_id
        from dlp_identification_material_transfer.b_sample sample
        where sample.entrustment_id is not null
        group by sample.entrustment_id
    </select>

    <!--
    分组查询事件id
    List<String> queryEvent();-->
    <select id="queryEvent" resultType="String">
        select sample.event_id
        from dlp_identification_material_transfer.b_sample sample
        where sample.event_id is not null
        group by sample.event_id
    </select>

    <!--List<String> queryFeature();-->
    <select id="queryFeature" resultType="String">
        select sample.feature_analysis_job_id
        from dlp_identification_material_transfer.b_sample sample
        where sample.feature_analysis_job_id is not null
        group by sample.feature_analysis_job_id
    </select>


    <!--//委托样本
    List<String> getSampleIdsByEw(String eventId);-->
    <select id="getSampleIdsByEw" resultType="String">
        select sample.id
        from dlp_identification_material_transfer.b_sample sample
        where sample.entrustment_id = #{eventId}
    </select>

    <!--//毛发样本
    List<String> getSampleIdsByHj(String eventId);-->
    <select id="getSampleIdsByHj" resultType="String">
        select sample.id
        from dlp_identification_material_transfer.b_sample sample
        where sample.hair_job_id = #{eventId}
    </select>
    <!--//污水样本
    List<String> getSampleIdsBySj(String eventId);-->
    <select id="getSampleIdsBySj" resultType="String">
        select sample.id
        from dlp_identification_material_transfer.b_sample sample
        where sample.sewage_job_id = #{eventId}
    </select>

    <!--
    事件
    List<String> getSampleIdsByTy(@Param("eventId")String eventId);-->
    <select id="getSampleIdsByTy" resultType="String">
        select sample.id
        from dlp_identification_material_transfer.b_sample sample
        where sample.event_id = #{eventId}
    </select>


    <!--List<String> getSampleIdsByTz(@Param("eventId")String eventId);-->
    <select id="getSampleIdsByTz" resultType="String">
        select sample.id
        from dlp_identification_material_transfer.b_sample sample
        where sample.feature_analysis_job_id = #{eventId}
    </select>
</mapper>
