<?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.reagent.mapper.CentralizedRequestMapper">

    <resultMap id="centralizedRequestMap" type="digital.laboratory.platform.reagent.entity.CentralizedRequest">
        <id property="centralizedRequestId" column="centralized_request_id"/>
        <result property="status" column="status"/>
        <result property="purchasingPlanId" column="purchasing_plan_id"/>
        <result property="dateOfApplication" column="date_of_application"/>
        <result property="auditOpinion" column="audit_opinion"/>
        <result property="applicantId" column="applicant_id"/>
        <result property="orgName" column="org_name"/>
        <result property="applicantName" column="applicant_name"/>
        <result property="auditorId" column="auditor_id"/>
        <result property="number" column="number"/>
        <result property="orgName" column="org_name"/>
        <result property="applicantName" column="applicant_name"/>

    </resultMap>

    <resultMap id="CentralizedRequestVO" type="digital.laboratory.platform.reagent.vo.CentralizedRequestVO"
               extends="centralizedRequestMap">
        <result property="orgName" column="org_name"/>
        <result property="applicantName" column="applicant_name"/>
    </resultMap>

    <sql id="getCentralizedRequestVOSQL">
        SELECT cr.*,
               (SELECT org.name
                FROM dlp_base.sys_org org
                WHERE org.org_id in (select org_id from dlp_base.sys_user where user_id = cr.applicant_id)) AS org_name,
               (SELECT user.name
                FROM dlp_base.sys_user user
        WHERE user.user_id=cr.applicant_id
            ) AS applicant_name
        FROM centralized_request cr
    </sql>

    <sql id="getCentralizedRequestSQL">

        select cr.*,
               (SELECT org.name
                FROM dlp_base.sys_org org
                WHERE org.org_id in (select org_id from dlp_base.sys_user where user_id = cr.applicant_id)) AS org_name,
               (SELECT user.name
                FROM dlp_base.sys_user user
        WHERE user.user_id=cr.applicant_id
            ) AS applicant_name
        from centralized_request cr
        where cr.centralized_request_id =#{centralizedRequestId}
    </sql>

    <!-- 根据条件取 CentralizedRequestVO 列表分页 -->
    <select id="getCentralizedRequestVOPage" resultMap="CentralizedRequestVO"
            resultType="digital.laboratory.platform.reagent.vo.CentralizedRequestVO">
        <include refid="getCentralizedRequestVOSQL"/>
        ${ew.customSqlSegment}
    </select>

    <!-- 根据条件取 CentralizedRequestVO 列表 -->
    <select id="getCentralizedRequestVOList" resultMap="CentralizedRequestVO"
            resultType="digital.laboratory.platform.reagent.vo.CentralizedRequestVO">
        <include refid="getCentralizedRequestVOSQL"/>
        ${ew.customSqlSegment}
    </select>
    <!-- 根据条件取 CentralizedRequestVO  -->
    <select id="getCentralizedRequestVO" resultMap="CentralizedRequestVO"
            resultType="digital.laboratory.platform.reagent.vo.CentralizedRequestVO">
        <include refid="getCentralizedRequestSQL"></include>
    </select>

</mapper>
