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

    <resultMap id="BaseResultMap" type="digital.laboratory.platform.imr.entity.DrugMaterialInfo">
            <id property="id" column="id" jdbcType="VARCHAR"/>
            <result property="caseId" column="case_id" jdbcType="VARCHAR"/>
            <result property="drugNo" column="drug_no" jdbcType="VARCHAR"/>
            <result property="drugName" column="drug_name" jdbcType="VARCHAR"/>
            <result property="type" column="type" jdbcType="VARCHAR" typeHandler="com.baomidou.mybatisplus.extension.handlers.FastjsonTypeHandler"/>
            <result property="originMassVolume" column="origin_mass_volume" jdbcType="VARCHAR"/>
            <result property="originUnit" column="origin_unit" jdbcType="VARCHAR"/>
            <result property="recheckMassVolume" column="recheck_mass_volume" jdbcType="VARCHAR"/>
            <result property="recheckUnit" column="recheck_unit" jdbcType="VARCHAR"/>
            <result property="packageComplete" column="package_complete" jdbcType="TINYINT"/>
            <result property="status" column="status" jdbcType="INTEGER"/>
            <result property="createTime" column="create_time" jdbcType="TIMESTAMP"/>
            <result property="updateTime" column="update_time" jdbcType="TIMESTAMP"/>
            <result property="createBy" column="create_by" jdbcType="VARCHAR"/>
            <result property="updateBy" column="update_by" jdbcType="VARCHAR"/>
    </resultMap>

    <resultMap id="voMap" type="digital.laboratory.platform.imr.vo.DrugMaterialInfoVO">
        <id property="id" column="id" jdbcType="VARCHAR"/>
        <result property="caseId" column="case_id" jdbcType="VARCHAR"/>
        <result property="drugNo" column="drug_no" jdbcType="VARCHAR"/>
        <result property="drugName" column="drug_name" jdbcType="VARCHAR"/>
        <result property="type" column="type" jdbcType="VARCHAR" typeHandler="com.baomidou.mybatisplus.extension.handlers.FastjsonTypeHandler"/>
        <result property="originMassVolume" column="origin_mass_volume" jdbcType="VARCHAR"/>
        <result property="originUnit" column="origin_unit" jdbcType="VARCHAR"/>
        <result property="recheckMassVolume" column="recheck_mass_volume" jdbcType="VARCHAR"/>
        <result property="recheckUnit" column="recheck_unit" jdbcType="VARCHAR"/>
        <result property="packageComplete" column="package_complete" jdbcType="TINYINT"/>
        <result property="status" column="status" jdbcType="INTEGER"/>
        <result property="sampleStorageId" column="sample_storage_id" jdbcType="VARCHAR"/>
        <result property="storageLocation" column="storage_location" jdbcType="VARCHAR"/>
        <result property="canineId" column="canineId" jdbcType="VARCHAR"/>
        <result property="storageCellId" column="storage_cell_id" jdbcType="VARCHAR"/>
        <result property="caseName" column="case_name" jdbcType="VARCHAR"/>
        <result property="caseNo" column="case_no" jdbcType="VARCHAR"/>
        <result property="handingOverOrg" column="handing_over_org" jdbcType="VARCHAR"/>
        <result property="handingOverDate" column="handing_over_date" jdbcType="TIMESTAMP"/>
    </resultMap>

    <sql id="Base_Column_List">
        dm.id,
        dm.case_id,
        dm.drug_no,
        dm.drug_name,
        dm.type,
        dm.origin_mass_volume,
        dm.origin_unit,
        dm.recheck_mass_volume,
        dm.recheck_unit,
        dm.package_complete,
        dm.status,
        dm.create_time,
        dm.update_time,
        dm.create_by,
        dm.update_by
    </sql>

    <sql id="queryVOSQL">
        SELECT
            <include refid="Base_Column_List"/>,
            dc.case_name,
            dc.case_no,
            dhoa.handing_over_org,
            dhoa.handing_over_date,
            ss.id AS sample_storage_id,
            ss.storage_location,
            ss.box_id AS canineId,
            ss.storage_cell_id,
            dur.id AS usage_record_id
        FROM b_drug_material_info dm
                 LEFT JOIN  b_drug_case_info dc ON dm.case_id = dc.id
                 LEFT JOIN b_drug_handing_over_apply dhoa ON dc.apply_id = dhoa.id
                 LEFT JOIN b_sample_storage ss ON dm.id = ss.sample_id
                 LEFT JOIN b_drug_usage_record dur ON dur.drug_id LIKE CONCAT('%',dm.id,'%')
    </sql>

    <select id="getDrugMaterialVOPage" resultType="digital.laboratory.platform.imr.vo.DrugMaterialInfoVO" resultMap="voMap">
        <include refid="queryVOSQL"/>
        <where>
            <if test="query.keywords != null and query.keywords != ''">
                AND (
                dc.case_name LIKE CONCAT('%', #{query.keywords}, '%')
                OR dm.drug_name LIKE CONCAT('%', #{query.keywords}, '%')
                OR dm.drug_no LIKE CONCAT('%', #{query.keywords}, '%')
                )
            </if>
            <if test="query.orgId != null and query.orgId != ''">
                AND dhoa.handing_over_org = #{query.orgId}
            </if>
            <if test="query.applyId != null and query.applyId != ''">
                AND dc.apply_id = #{query.applyId}
            </if>
            <if test="query.usageRecordId != null and query.usageRecordId != ''">
                AND dur.id = #{query.usageRecordId}
            </if>
            <if test="query.status != null">
                AND dm.status = #{query.status}
            </if>
            <if test="query.startDate != null">
                AND dhoa.handing_over_date &gt;= #{query.startDate}
            </if>
            <if test="query.endDate != null">
                AND dhoa.handing_over_date &lt;= #{query.endDate}
            </if>
        </where>
        ORDER BY dm.update_time DESC
    </select>

    <select id="getDrugMaterialVO" resultType="digital.laboratory.platform.imr.vo.DrugMaterialInfoVO">
        <include refid="queryVOSQL"/>
        ${ew.customSqlSegment}
    </select>
</mapper>
