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

    <resultMap id="purchaseCatalogueMap" type="digital.laboratory.platform.reagent.entity.PurchaseCatalogue">
        <id property="purchaseCatalogueId" column="purchase_catalogue_id"/>
        <result property="auditOpinionOfPrimary" column="audit_opinion_of_primary"/>
        <result property="auditOpinionOfSecondary" column="audit_opinion_of_secondary"/>
        <result property="auditResultOfPrimary" column="auditResult_of_primary"/>
        <result property="auditResultOfSecondary" column="audit_result_of_secondary"/>
        <result property="auditTimeOfPrimary" column="audit_time_of_primary"/>
        <result property="auditTimeOfSecondary" column="audit_time_of_secondary"/>
        <result property="primaryAuditorId" column="primary_auditor_id"/>
        <result property="releaseDate" column="release_date"/>
        <result property="status" column="status"/>
        <result property="secondaryAuditorId" column="secondary_auditor_id"/>
        <result property="number" column="number"/>
        <result property="createTime" column="create_time"/>
        <result property="updateTime" column="update_time"/>
        <result property="createBy" column="create_by"/>
        <result property="updateBy" column="update_by"/>

    </resultMap>

    <resultMap id="purchaseCatalogueVO" type="digital.laboratory.platform.reagent.vo.PurchaseCatalogueVO"
               extends="purchaseCatalogueMap">
        <result property="primaryAuditorName" column="primary_auditor_name"/>
        <result property="secondaryAuditorName" column="secondary_auditor_name"/>
        <result property="createName" column="create_name"/>
        <result property="orgName" column="org_name"/>
    </resultMap>

    <sql id="getPurchaseCatalogueVOSQL">
        SELECT pc.*,
               (SELECT user.name
                FROM dlp_base.sys_user user
        WHERE user.user_id = pc.primary_auditor_id) AS primary_auditor_name
            , (
        SELECT user.name
        FROM dlp_base.sys_user user
        WHERE user.user_id=pc.secondary_auditor_id
            ) AS secondary_auditor_name
            , (
        SELECT user.name
        FROM dlp_base.sys_user user
        WHERE user.user_id=pc.create_by
            ) AS create_name
            , (
        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 = pc.create_by)) AS org_name
        FROM purchase_catalogue pc
    </sql>

    <sql id="getPurchaseCatalogueSQL">

        SELECT pc.*,
               (SELECT user.name
                FROM dlp_base.sys_user user
        WHERE user.user_id = pc.primary_auditor_id) AS primary_auditor_name
            , (
        SELECT user.name
        FROM dlp_base.sys_user user
        WHERE user.user_id=pc.secondary_auditor_id
            ) AS secondary_auditor_name
            , (
        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 = pc.create_by)) AS org_name
            , (
        SELECT user.name
        FROM dlp_base.sys_user user
        WHERE user.user_id=pc.create_by
            ) AS create_name
        FROM purchase_catalogue pc
        WHERE pc.purchase_catalogue_id = #{purchaseCatalogueId}
    </sql>

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

    <!-- 根据条件取 PurchaseCatalogueVO 列表 -->
    <select id="getPurchaseCatalogueVOList" resultMap="purchaseCatalogueVO"
            resultType="digital.laboratory.platform.reagent.vo.PurchaseCatalogueVO">
        <include refid="getPurchaseCatalogueVOSQL"/>
        ${ew.customSqlSegment}
    </select>
    <!-- 根据条件取 CentralizedRequestVO  -->
    <select id="getPurchaseCatalogueVO" resultMap="purchaseCatalogueVO"
            resultType="digital.laboratory.platform.reagent.vo.PurchaseCatalogueVO">
        <include refid="getPurchaseCatalogueSQL"></include>
    </select>

</mapper>
