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

    <resultMap id="decentralizedRequestMap" type="digital.laboratory.platform.reagent.entity.DecentralizedRequest">
        <id property="decentralizedRequestId" column="decentralized_request_id"/>
        <result property="applicantId" column="applicant_id"/>
        <result property="opinionOfApproval" column="opinion_of_approval"/>
        <result property="resultOfApproval" column="result_of_approval"/>
        <result property="approvalOfTime" column="approval_of_time"/>
        <result property="auditOpinionOfPrimary" column="audit_opinion_of_primary"/>
        <result property="auditOpinionOfSecondary" column="audit_opinion_of_secondary"/>
        <result property="auditOpinionOfThreeLevel" column="audit_opinion_of_three_level"/>
        <result property="auditResultOfPrimary" column="audit_result_of_primary"/>
        <result property="auditResultOfSecondary" column="audit_result_of_secondary"/>
        <result property="auditResultOfThreeLevel" column="audit_result_of_three_level"/>
        <result property="auditTimeOfPrimary" column="audit_time_of_primary"/>
        <result property="auditTimeOfSecondary" column="audit_time_of_secondary"/>
        <result property="auditTimeOfThreeLevel" column="audit_time_of_three_level"/>
        <result property="complianceTesting" column="compliance_testing"/>
        <result property="dateOfApplication" column="date_of_application"/>
        <result property="approverId" column="approver_id"/>
        <result property="primaryAuditorId" column="primary_auditor_id"/>
        <result property="purchaseListId" column="purchase_list_id"/>
        <result property="secondaryAuditorId" column="secondary_auditor_id"/>
        <result property="status" column="status"/>
        <result property="threeLevelAuditId" column="three_level_audit_id"/>

    </resultMap>

    <resultMap id="decentralizedRequestVO" type="digital.laboratory.platform.reagent.vo.DecentralizedRequestVO"
               extends="decentralizedRequestMap">
        <result property="threeLevelAuditName" column="three_level_audit_name"/>
        <result property="secondaryAuditorName" column="secondary_auditor_name"/>
        <result property="primaryAuditorName" column="primary_auditor_name"/>
        <result property="applicantName" column="applicant_name"/>
        <result property="approverName" column="approver_name"/>
        <result property="ogrName" column="ogr_name"/>
    </resultMap>

    <sql id="getDecentralizedRequestVOSQL">
        SELECT dr.*,
               (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 = dr.applicant_id)) AS org_name,
               (SELECT user.name
                FROM dlp_base.sys_user user
        WHERE user.user_id =dr.three_level_audit_id) AS three_level_audit_name
            , (
        SELECT user.name
        FROM dlp_base.sys_user user
        WHERE user.user_id =dr.secondary_auditor_id) AS secondary_auditor_name
            , (
        SELECT user.name
        FROM dlp_base.sys_user user
        WHERE user.user_id =dr.primary_auditor_id) AS primary_auditor_name
            , (
        SELECT user.name
        FROM dlp_base.sys_user user
        WHERE user.user_id =dr.applicant_id) AS applicant_name
            , (
        SELECT user.name
        FROM dlp_base.sys_user user
        WHERE user.user_id =dr.approver_id) AS approver_name
        FROM decentralized_request dr
    </sql>
    <!-- 根据条件取DecentralizedRequestVO列表  -->
    <select id="getDecentralizedRequestVOList" resultMap="decentralizedRequestVO"
            resultType="digital.laboratory.platform.reagent.vo.DecentralizedRequestVO">
<include refid="getDecentralizedRequestVOSQL"></include>
    </select>
    <!-- 根据条件取 DecentralizedRequestVOPage列表  -->
    <select id="getDecentralizedRequestVOPage" resultMap="decentralizedRequestVO" resultType="digital.laboratory.platform.reagent.vo.DecentralizedRequestVO">
        <include refid="getDecentralizedRequestVOSQL"></include>
    </select>

    <select id="getDecentralizedRequest" resultMap="decentralizedRequestVO" resultType="digital.laboratory.platform.reagent.vo.DecentralizedRequestVO">
        SELECT dr.*,
               (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 = dr.applicant_id)) AS org_name,
               (SELECT user.name
                FROM dlp_base.sys_user user
        WHERE user.user_id =dr.three_level_audit_id) As three_level_audit_name
            , (
        SELECT user.name
        FROM dlp_base.sys_user user
        WHERE user.user_id =dr.secondary_auditor_id) As secondary_auditor_name
            , (
        SELECT user.name
        FROM dlp_base.sys_user user
        WHERE user.user_id =dr.primary_auditor_id) As primary_auditor_name
            , (
        SELECT user.name
        FROM dlp_base.sys_user user
        WHERE user.user_id =dr.applicant_id) As applicant_name
            , (
        SELECT user.name
        FROM dlp_base.sys_user user
        WHERE user.user_id =dr.approver_id) As approver_name
        FROM decentralized_request dr
        WHERE dr.decentralized_request_id = #{decentralizedRequestId}
    </select>
</mapper>

