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

    <resultMap id="purchasingPlanMap" type="digital.laboratory.platform.reagent.entity.PurchasingPlan">
        <id property="purchasingPlanId" column="purchasing_plan_id"/>
        <result property="appropriationBudget" column="appropriation_budget"/>
        <result property="approvalOpinion" column="approval_opinion"/>
        <result property="approvalResult" column="approval_result"/>
        <result property="approvalTime" column="approval_time"/>
        <result property="auditOpinionOfPrimary" column="audit_opinion_of_primary"/>
        <result property="auditResultOfPrimary" column="audit_result_of_primary"/>
        <result property="auditTimeOfPrimary" column="audit_time_of_primary"/>
        <result property="approverId" column="approver_id"/>
        <result property="createTime" column="create_time"/>
        <result property="updateTime" column="update_time"/>
        <result property="createBy" column="create_by"/>
        <result property="updateBy" column="update_by"/>
        <result property="purchaseListId" column="purchase_list_id"/>
        <result property="releaseDate" column="release_date"/>
        <result property="status" column="status"/>
        <result property="primaryAuditorId" column="primary_auditor_id"/>
        <result property="createId" column="create_id"/>
        <result property="number" column="number"/>

    </resultMap>

    <resultMap id="purchasingPlanVO" type="digital.laboratory.platform.reagent.vo.PurchasingPlanVO"
               extends="purchasingPlanMap">
        <result property="primaryAuditorName" column="primary_auditor_name"></result>
        <result property="approverName" column="approver_name"></result>
        <result property="createName" column="create_name"></result>
        <result property="orgName" column="org_name"></result>
    </resultMap>

    <sql id="getPurchasingPlanVOSQL">
        SELECT pp.*,
               (select user.name
                from dlp_base.sys_user user
        where user.user_id = pp.primary_auditor_id) as primary_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 = pp.create_id)) AS org_name,
            (
        select user.name
        from dlp_base.sys_user user
        where user.user_id = pp.approver_id) as approver_name
            , (
        select user.name
        from dlp_base.sys_user user
        where user.user_id = pp.create_id) as create_name
        FROM purchasing_plan pp
    </sql>



    <select id="getPurchasingPlanVOList" resultMap="purchasingPlanVO" resultType="digital.laboratory.platform.reagent.vo.PurchasingPlanVO">
        <include refid="getPurchasingPlanVOSQL"></include>
        ${ew.customSqlSegment}
    </select>

    <select id="getPurchasingPlanVOPage" resultMap="purchasingPlanVO" resultType="digital.laboratory.platform.reagent.vo.PurchasingPlanVO">
        <include refid="getPurchasingPlanVOSQL"></include>
        ${ew.customSqlSegment}
    </select>

    <select id="getPurchasingPlanVO" resultMap="purchasingPlanVO" resultType="digital.laboratory.platform.reagent.vo.PurchasingPlanVO">
        SELECT pp.*,
               (select user.name
                from dlp_base.sys_user user
        where user.user_id = pp.primary_auditor_id) as primary_auditor_name
            , (
        select user.name
        from dlp_base.sys_user user
        where user.user_id = pp.approver_id) as approver_name
            , (
        select user.name
        from dlp_base.sys_user user
        where user.user_id = pp.create_id) as create_name
        FROM purchasing_plan pp
        WHERE pp.purchasing_plan_id = #{purchasingPlanId}
    </select>

</mapper>
