<?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="com.ruoyi.ccdi.project.mapper.CcdiProjectSpecialCheckMapper">

    <resultMap id="FamilyAssetLiabilityListItemResultMap"
               type="com.ruoyi.ccdi.project.domain.vo.CcdiProjectFamilyAssetLiabilityListItemVO">
        <id property="staffIdCard" column="staff_id_card"/>
        <result property="staffCode" column="staff_code"/>
        <result property="staffName" column="staff_name"/>
        <result property="deptName" column="dept_name"/>
        <result property="totalIncome" column="total_income"/>
        <result property="totalAsset" column="total_asset"/>
        <result property="totalDebt" column="total_debt"/>
        <result property="comparisonAmount" column="comparison_amount"/>
        <result property="riskLevelCode" column="risk_level_code"/>
        <result property="riskLevelName" column="risk_level_name"/>
    </resultMap>

    <resultMap id="FamilyAssetItemResultMap"
               type="com.ruoyi.ccdi.project.domain.vo.CcdiProjectFamilyAssetItemVO">
        <result property="assetName" column="asset_name"/>
        <result property="assetMainType" column="asset_main_type"/>
        <result property="assetSubType" column="asset_sub_type"/>
        <result property="holderName" column="holder_name"/>
        <result property="holderIdCard" column="holder_id_card"/>
        <result property="currentValue" column="current_value"/>
        <result property="valuationDate" column="valuation_date"/>
    </resultMap>

    <resultMap id="FamilyDebtItemResultMap"
               type="com.ruoyi.ccdi.project.domain.vo.CcdiProjectFamilyDebtItemVO">
        <result property="debtName" column="debt_name"/>
        <result property="debtMainType" column="debt_main_type"/>
        <result property="debtSubType" column="debt_sub_type"/>
        <result property="creditorType" column="creditor_type"/>
        <result property="ownerName" column="owner_name"/>
        <result property="ownerIdCard" column="owner_id_card"/>
        <result property="principalBalance" column="principal_balance"/>
        <result property="queryDate" column="query_date"/>
    </resultMap>

    <resultMap id="ExtendedPurchaseListItemResultMap"
               type="com.ruoyi.ccdi.project.domain.vo.CcdiProjectExtendedPurchaseListItemVO">
        <id property="purchaseId" column="purchase_id"/>
        <result property="projectName" column="project_name"/>
        <result property="subjectName" column="subject_name"/>
        <result property="applicantName" column="applicant_name"/>
        <result property="applyDate" column="apply_date"/>
    </resultMap>

    <resultMap id="ExtendedRecruitmentListItemResultMap"
               type="com.ruoyi.ccdi.project.domain.vo.CcdiProjectExtendedRecruitmentListItemVO">
        <id property="recruitId" column="recruit_id"/>
        <result property="recruitName" column="recruit_name"/>
        <result property="posName" column="pos_name"/>
        <result property="interviewerNameSummary" column="interviewer_name_summary"/>
        <result property="admitStatus" column="admit_status"/>
    </resultMap>

    <resultMap id="ExtendedTransferListItemResultMap"
               type="com.ruoyi.ccdi.project.domain.vo.CcdiProjectExtendedTransferListItemVO">
        <id property="id" column="id"/>
        <result property="staffName" column="staff_name"/>
        <result property="transferType" column="transfer_type"/>
        <result property="deptNameBefore" column="dept_name_before"/>
        <result property="deptNameAfter" column="dept_name_after"/>
        <result property="transferDate" column="transfer_date"/>
    </resultMap>

    <resultMap id="FamilyAssetLiabilityDetailResultMap"
               type="com.ruoyi.ccdi.project.domain.vo.CcdiProjectFamilyAssetLiabilityDetailVO">
        <association property="incomeDetail"
                     javaType="com.ruoyi.ccdi.project.domain.vo.CcdiProjectFamilyIncomeDetailVO">
            <result property="selfIncome" column="income_self_income"/>
            <result property="spouseIncome" column="income_spouse_income"/>
            <result property="totalIncome" column="income_total_income"/>
        </association>
        <association property="assetDetail"
                     javaType="com.ruoyi.ccdi.project.domain.vo.CcdiProjectFamilyAssetDetailVO">
            <result property="missingSelfAssetInfo" column="asset_missing_self_asset_info"/>
            <result property="selfTotalAsset" column="asset_self_total_asset"/>
            <result property="spouseTotalAsset" column="asset_spouse_total_asset"/>
            <result property="totalAsset" column="asset_total_asset"/>
            <collection property="items"
                        column="{projectId=project_id,staffIdCard=staff_id_card,spouseIdCard=spouse_id_card}"
                        ofType="com.ruoyi.ccdi.project.domain.vo.CcdiProjectFamilyAssetItemVO"
                        select="selectFamilyAssetItemsByScope"/>
        </association>
        <association property="debtDetail"
                     javaType="com.ruoyi.ccdi.project.domain.vo.CcdiProjectFamilyDebtDetailVO">
            <result property="missingSelfDebtInfo" column="debt_missing_self_debt_info"/>
            <result property="selfTotalDebt" column="debt_self_total_debt"/>
            <result property="spouseTotalDebt" column="debt_spouse_total_debt"/>
            <result property="totalDebt" column="debt_total_debt"/>
            <collection property="items"
                        column="{projectId=project_id,staffIdCard=staff_id_card,spouseIdCard=spouse_id_card}"
                        ofType="com.ruoyi.ccdi.project.domain.vo.CcdiProjectFamilyDebtItemVO"
                        select="selectFamilyDebtItemsByScope"/>
        </association>
        <association property="summary"
                     javaType="com.ruoyi.ccdi.project.domain.vo.CcdiProjectFamilyAssetLiabilityListItemVO">
            <id property="staffIdCard" column="summary_staff_id_card"/>
            <result property="staffCode" column="summary_staff_code"/>
            <result property="staffName" column="summary_staff_name"/>
            <result property="deptName" column="summary_dept_name"/>
            <result property="totalIncome" column="summary_total_income"/>
            <result property="totalAsset" column="summary_total_asset"/>
            <result property="totalDebt" column="summary_total_debt"/>
            <result property="comparisonAmount" column="summary_comparison_amount"/>
            <result property="riskLevelCode" column="summary_risk_level_code"/>
            <result property="riskLevelName" column="summary_risk_level_name"/>
        </association>
    </resultMap>

    <sql id="projectEmployeeScopeSql">
        select distinct
            statement_staff.id_card as staff_id_card,
            cast(statement_staff.staff_id as char) as staff_code,
            statement_staff.name as staff_name,
            dept.dept_name
        from ccdi_bank_statement bs
        inner join ccdi_base_staff statement_staff
            on statement_staff.id_card = trim(bs.cret_no)
        left join sys_dept dept
            on dept.dept_id = statement_staff.dept_id
        where bs.project_id = #{projectId}
          and bs.cret_no is not null
          and trim(bs.cret_no) != ''
    </sql>

    <sql id="spouseRelationSql">
        select
            person_id,
            max(relation_name) as spouse_name,
            min(relation_cert_no) as spouse_id_card,
            max(annual_income) as spouse_income
        from ccdi_staff_fmy_relation
        where status = 1
          and is_emp_family = 1
          and relation_type = '配偶'
        group by person_id
    </sql>

    <select id="selectFamilyAssetLiabilityList" resultMap="FamilyAssetLiabilityListItemResultMap">
        select
            aggregated.staff_id_card,
            aggregated.staff_code,
            aggregated.staff_name,
            aggregated.dept_name,
            aggregated.total_income,
            aggregated.total_asset,
            aggregated.total_debt,
            aggregated.comparison_amount,
            case
                when aggregated.self_asset_record_count = 0 or aggregated.self_debt_record_count = 0 then 'MISSING_INFO'
                when comparison_amount &lt;= total_asset * 1.5 then 'NORMAL'
                when comparison_amount &gt; total_asset * 1.5 and comparison_amount &lt;= total_asset * 3 then 'RISK'
                when comparison_amount &gt; total_asset * 3 then 'HIGH'
                else 'HIGH'
            end as risk_level_code,
            case
                when aggregated.self_asset_record_count = 0 or aggregated.self_debt_record_count = 0 then '缺少信息'
                when comparison_amount &lt;= total_asset * 1.5 then '正常'
                when comparison_amount &gt; total_asset * 1.5 and comparison_amount &lt;= total_asset * 3 then '存在风险'
                when comparison_amount &gt; total_asset * 3 then '高风险'
                else '高风险'
            end as risk_level_name
        from (
            select
                scope.staff_id_card,
                scope.staff_code,
                scope.staff_name,
                scope.dept_name,
                coalesce(base_staff.annual_income, 0) + coalesce(spouse.spouse_income, 0) as total_income,
                coalesce((
                    select count(1)
                    from ccdi_asset_info asset
                    where asset.family_id = scope.staff_id_card
                      and asset.person_id = scope.staff_id_card
                ), 0) as self_asset_record_count,
                coalesce((
                    select count(1)
                    from ccdi_debts_info debt
                    where debt.person_id = scope.staff_id_card
                ), 0) as self_debt_record_count,
                coalesce((
                    select sum(coalesce(asset.current_value, 0))
                    from ccdi_asset_info asset
                    where asset.family_id = scope.staff_id_card
                      and (
                            asset.person_id = scope.staff_id_card
                            or (spouse.spouse_id_card is not null and asset.person_id = spouse.spouse_id_card)
                      )
                ), 0) as total_asset,
                coalesce((
                    select sum(coalesce(debt.principal_balance, 0))
                    from ccdi_debts_info debt
                    where debt.person_id = scope.staff_id_card
                       or (spouse.spouse_id_card is not null and debt.person_id = spouse.spouse_id_card)
                ), 0) as total_debt,
                coalesce(base_staff.annual_income, 0)
                    + coalesce(spouse.spouse_income, 0)
                    + coalesce((
                        select sum(coalesce(debt.principal_balance, 0))
                        from ccdi_debts_info debt
                        where debt.person_id = scope.staff_id_card
                           or (spouse.spouse_id_card is not null and debt.person_id = spouse.spouse_id_card)
                    ), 0) as comparison_amount,
                case
                    when coalesce((
                        select count(1)
                        from ccdi_asset_info asset
                        where asset.family_id = scope.staff_id_card
                          and asset.person_id = scope.staff_id_card
                    ), 0) = 0
                        or coalesce((
                            select count(1)
                            from ccdi_debts_info debt
                            where debt.person_id = scope.staff_id_card
                        ), 0) = 0 then 4
                    when (
                        coalesce(base_staff.annual_income, 0)
                        + coalesce(spouse.spouse_income, 0)
                        + coalesce((
                            select sum(coalesce(debt.principal_balance, 0))
                            from ccdi_debts_info debt
                            where debt.person_id = scope.staff_id_card
                               or (spouse.spouse_id_card is not null and debt.person_id = spouse.spouse_id_card)
                        ), 0)
                    ) &lt;= coalesce((
                        select sum(coalesce(asset.current_value, 0))
                        from ccdi_asset_info asset
                        where asset.family_id = scope.staff_id_card
                          and (
                                asset.person_id = scope.staff_id_card
                                or (spouse.spouse_id_card is not null and asset.person_id = spouse.spouse_id_card)
                          )
                    ), 0) * 1.5 then 1
                    when (
                        coalesce(base_staff.annual_income, 0)
                        + coalesce(spouse.spouse_income, 0)
                        + coalesce((
                            select sum(coalesce(debt.principal_balance, 0))
                            from ccdi_debts_info debt
                            where debt.person_id = scope.staff_id_card
                               or (spouse.spouse_id_card is not null and debt.person_id = spouse.spouse_id_card)
                        ), 0)
                    ) &lt;= coalesce((
                        select sum(coalesce(asset.current_value, 0))
                        from ccdi_asset_info asset
                        where asset.family_id = scope.staff_id_card
                          and (
                                asset.person_id = scope.staff_id_card
                                or (spouse.spouse_id_card is not null and asset.person_id = spouse.spouse_id_card)
                          )
                    ), 0) * 3 then 2
                    when (
                        coalesce(base_staff.annual_income, 0)
                        + coalesce(spouse.spouse_income, 0)
                        + coalesce((
                            select sum(coalesce(debt.principal_balance, 0))
                            from ccdi_debts_info debt
                            where debt.person_id = scope.staff_id_card
                               or (spouse.spouse_id_card is not null and debt.person_id = spouse.spouse_id_card)
                        ), 0)
                    ) &gt; coalesce((
                        select sum(coalesce(asset.current_value, 0))
                        from ccdi_asset_info asset
                        where asset.family_id = scope.staff_id_card
                          and (
                                asset.person_id = scope.staff_id_card
                                or (spouse.spouse_id_card is not null and asset.person_id = spouse.spouse_id_card)
                          )
                    ), 0) * 3 then 3
                    else 3
                end as risk_level_sort
            from (
                <include refid="projectEmployeeScopeSql"/>
            ) scope
            left join ccdi_base_staff base_staff
                on base_staff.id_card = scope.staff_id_card
            left join (
                <include refid="spouseRelationSql"/>
            ) spouse
                on spouse.person_id = scope.staff_id_card
        ) aggregated
        order by risk_level_sort desc, comparison_amount desc, staff_name asc
    </select>

    <select id="selectFamilyAssetLiabilityDetail" resultMap="FamilyAssetLiabilityDetailResultMap">
        select
            aggregated.project_id,
            aggregated.staff_id_card,
            aggregated.spouse_id_card,
            aggregated.staff_code,
            aggregated.staff_name,
            aggregated.dept_name,
            aggregated.self_income as income_self_income,
            aggregated.spouse_income as income_spouse_income,
            aggregated.total_income as income_total_income,
            aggregated.missing_self_asset_info as asset_missing_self_asset_info,
            aggregated.self_total_asset as asset_self_total_asset,
            aggregated.spouse_total_asset as asset_spouse_total_asset,
            aggregated.total_asset as asset_total_asset,
            aggregated.missing_self_debt_info as debt_missing_self_debt_info,
            aggregated.self_total_debt as debt_self_total_debt,
            aggregated.spouse_total_debt as debt_spouse_total_debt,
            aggregated.total_debt as debt_total_debt,
            aggregated.staff_id_card as summary_staff_id_card,
            aggregated.staff_code as summary_staff_code,
            aggregated.staff_name as summary_staff_name,
            aggregated.dept_name as summary_dept_name,
            aggregated.total_income as summary_total_income,
            aggregated.total_asset as summary_total_asset,
            aggregated.total_debt as summary_total_debt,
            aggregated.comparison_amount as summary_comparison_amount,
            case
                when aggregated.missing_self_asset_info = 1 or aggregated.missing_self_debt_info = 1 then 'MISSING_INFO'
                when comparison_amount &lt;= total_asset * 1.5 then 'NORMAL'
                when comparison_amount &gt; total_asset * 1.5 and comparison_amount &lt;= total_asset * 3 then 'RISK'
                when comparison_amount &gt; total_asset * 3 then 'HIGH'
                else 'HIGH'
            end as summary_risk_level_code,
            case
                when aggregated.missing_self_asset_info = 1 or aggregated.missing_self_debt_info = 1 then '缺少信息'
                when comparison_amount &lt;= total_asset * 1.5 then '正常'
                when comparison_amount &gt; total_asset * 1.5 and comparison_amount &lt;= total_asset * 3 then '存在风险'
                when comparison_amount &gt; total_asset * 3 then '高风险'
                else '高风险'
            end as summary_risk_level_name
        from (
            select
                #{projectId} as project_id,
                scope.staff_id_card,
                scope.staff_code,
                scope.staff_name,
                scope.dept_name,
                spouse.spouse_id_card,
                coalesce(base_staff.annual_income, 0) as self_income,
                coalesce(spouse.spouse_income, 0) as spouse_income,
                coalesce(base_staff.annual_income, 0) + coalesce(spouse.spouse_income, 0) as total_income,
                case
                    when coalesce((
                        select count(1)
                        from ccdi_asset_info asset
                        where asset.family_id = scope.staff_id_card
                          and asset.person_id = scope.staff_id_card
                    ), 0) = 0 then 1
                    else 0
                end as missing_self_asset_info,
                coalesce((
                    select sum(coalesce(asset.current_value, 0))
                    from ccdi_asset_info asset
                    where asset.family_id = scope.staff_id_card
                      and asset.person_id = scope.staff_id_card
                ), 0) as self_total_asset,
                coalesce((
                    select sum(coalesce(asset.current_value, 0))
                    from ccdi_asset_info asset
                    where asset.family_id = scope.staff_id_card
                      and spouse.spouse_id_card is not null
                      and asset.person_id = spouse.spouse_id_card
                ), 0) as spouse_total_asset,
                coalesce((
                    select sum(coalesce(asset.current_value, 0))
                    from ccdi_asset_info asset
                    where asset.family_id = scope.staff_id_card
                      and (
                            asset.person_id = scope.staff_id_card
                            or (spouse.spouse_id_card is not null and asset.person_id = spouse.spouse_id_card)
                      )
                ), 0) as total_asset,
                case
                    when coalesce((
                        select count(1)
                        from ccdi_debts_info debt
                        where debt.person_id = scope.staff_id_card
                    ), 0) = 0 then 1
                    else 0
                end as missing_self_debt_info,
                coalesce((
                    select sum(coalesce(debt.principal_balance, 0))
                    from ccdi_debts_info debt
                    where debt.person_id = scope.staff_id_card
                ), 0) as self_total_debt,
                coalesce((
                    select sum(coalesce(debt.principal_balance, 0))
                    from ccdi_debts_info debt
                    where spouse.spouse_id_card is not null
                      and debt.person_id = spouse.spouse_id_card
                ), 0) as spouse_total_debt,
                coalesce((
                    select sum(coalesce(debt.principal_balance, 0))
                    from ccdi_debts_info debt
                    where debt.person_id = scope.staff_id_card
                       or (spouse.spouse_id_card is not null and debt.person_id = spouse.spouse_id_card)
                ), 0) as total_debt,
                coalesce(base_staff.annual_income, 0)
                    + coalesce(spouse.spouse_income, 0)
                    + coalesce((
                        select sum(coalesce(debt.principal_balance, 0))
                        from ccdi_debts_info debt
                        where debt.person_id = scope.staff_id_card
                           or (spouse.spouse_id_card is not null and debt.person_id = spouse.spouse_id_card)
                    ), 0) as comparison_amount
            from (
                <include refid="projectEmployeeScopeSql"/>
            ) scope
            left join ccdi_base_staff base_staff
                on base_staff.id_card = scope.staff_id_card
            left join (
                <include refid="spouseRelationSql"/>
            ) spouse
                on spouse.person_id = scope.staff_id_card
            where scope.staff_id_card = #{staffIdCard}
        ) aggregated
    </select>

    <select id="selectFamilyAssetItemsByScope" resultMap="FamilyAssetItemResultMap">
        select
            asset.asset_name,
            asset.asset_main_type,
            asset.asset_sub_type,
            case
                when asset.person_id = #{staffIdCard} then base_staff.name
                else spouse.relation_name
            end as holder_name,
            asset.person_id as holder_id_card,
            asset.current_value,
            asset.valuation_date
        from ccdi_asset_info asset
        left join ccdi_base_staff base_staff
            on base_staff.id_card = #{staffIdCard}
        left join ccdi_staff_fmy_relation spouse
            on spouse.person_id = #{staffIdCard}
           and spouse.status = 1
           and spouse.relation_type = '配偶'
           and spouse.relation_cert_no = asset.person_id
        where asset.family_id = #{staffIdCard}
          and (
                asset.person_id = #{staffIdCard}
                or (#{spouseIdCard} is not null and asset.person_id = #{spouseIdCard})
          )
        order by
            case when asset.person_id = #{staffIdCard} then 1 else 2 end,
            asset.valuation_date desc,
            asset.asset_name asc
    </select>

    <select id="selectFamilyDebtItemsByScope" resultMap="FamilyDebtItemResultMap">
        select
            debt.debt_name,
            debt.debt_main_type,
            debt.debt_sub_type,
            debt.creditor_type,
            case
                when debt.person_id = #{staffIdCard} then base_staff.name
                else spouse.relation_name
            end as owner_name,
            debt.person_id as owner_id_card,
            debt.principal_balance,
            debt.query_date
        from ccdi_debts_info debt
        left join ccdi_base_staff base_staff
            on base_staff.id_card = #{staffIdCard}
        left join ccdi_staff_fmy_relation spouse
            on spouse.person_id = #{staffIdCard}
           and spouse.status = 1
           and spouse.relation_type = '配偶'
           and spouse.relation_cert_no = debt.person_id
        where debt.person_id = #{staffIdCard}
           or (#{spouseIdCard} is not null and debt.person_id = #{spouseIdCard})
        order by
            case when debt.person_id = #{staffIdCard} then 1 else 2 end,
            debt.query_date desc,
            debt.debt_name asc
    </select>

    <select id="selectExtendedPurchasePage" resultMap="ExtendedPurchaseListItemResultMap">
        <bind name="projectId" value="query.projectId"/>
        select
            p.purchase_id,
            p.project_name,
            p.subject_name,
            p.applicant_name,
            p.apply_date
        from ccdi_purchase_transaction p
        inner join (
            select distinct scope.staff_name
            from (
                <include refid="projectEmployeeScopeSql"/>
            ) scope
            where scope.staff_name is not null
              and scope.staff_name != ''
        ) scoped_staff
            on scoped_staff.staff_name = p.applicant_name
        <where>
            <if test="query.applicantName != null and query.applicantName != ''">
                and p.applicant_name like concat('%', #{query.applicantName}, '%')
            </if>
            <if test="query.applyDateStart != null and query.applyDateStart != ''">
                and p.apply_date &gt;= #{query.applyDateStart}
            </if>
            <if test="query.applyDateEnd != null and query.applyDateEnd != ''">
                and p.apply_date &lt;= #{query.applyDateEnd}
            </if>
        </where>
        order by p.apply_date desc, p.create_time desc, p.purchase_id desc
    </select>

    <select id="selectExtendedPurchaseDetail"
            resultType="com.ruoyi.ccdi.project.domain.vo.CcdiProjectExtendedPurchaseDetailVO">
        select
            p.purchase_id,
            p.purchase_category,
            p.project_name,
            p.subject_name,
            p.subject_desc,
            p.purchase_qty,
            p.budget_amount,
            p.bid_amount,
            p.actual_amount,
            p.contract_amount,
            p.settlement_amount,
            p.purchase_method,
            p.supplier_name,
            p.contact_person,
            p.contact_phone,
            p.supplier_uscc,
            p.supplier_bank_account,
            p.apply_date,
            p.plan_approve_date,
            p.announce_date,
            p.bid_open_date,
            p.contract_sign_date,
            p.expected_delivery_date,
            p.actual_delivery_date,
            p.acceptance_date,
            p.settlement_date,
            p.applicant_id,
            p.applicant_name,
            p.apply_department,
            p.purchase_leader_id,
            p.purchase_leader_name,
            p.purchase_department,
            p.created_by,
            p.create_time,
            p.updated_by,
            p.update_time
        from ccdi_purchase_transaction p
        inner join (
            select distinct scope.staff_name
            from (
                <include refid="projectEmployeeScopeSql"/>
            ) scope
            where scope.staff_name is not null
              and scope.staff_name != ''
        ) scoped_staff
            on scoped_staff.staff_name = p.applicant_name
        where p.purchase_id = #{purchaseId}
    </select>

    <select id="selectExtendedPurchaseSuppliers"
            resultType="com.ruoyi.ccdi.project.domain.vo.CcdiProjectExtendedPurchaseSupplierVO">
        select
            s.id,
            s.purchase_id,
            s.supplier_name,
            s.supplier_uscc,
            s.contact_person,
            s.contact_phone,
            s.supplier_bank_account,
            s.is_bid_winner,
            s.sort_order
        from ccdi_purchase_transaction_supplier s
        inner join ccdi_purchase_transaction p on p.purchase_id = s.purchase_id
        inner join (
            select distinct scope.staff_name
            from (
                <include refid="projectEmployeeScopeSql"/>
            ) scope
            where scope.staff_name is not null
              and scope.staff_name != ''
        ) scoped_staff
            on scoped_staff.staff_name = p.applicant_name
        where s.purchase_id = #{purchaseId}
        order by s.sort_order asc, s.id asc
    </select>

    <select id="selectExtendedRecruitmentPage" resultMap="ExtendedRecruitmentListItemResultMap">
        <bind name="projectId" value="query.projectId"/>
        select distinct r.recruit_id,
            r.recruit_name,
            r.pos_name,
            concat_ws(' / ',
                nullif(trim(r.interviewer_name1), ''),
                nullif(trim(r.interviewer_name2), '')
            ) as interviewer_name_summary,
            r.admit_status
        from ccdi_staff_recruitment r
        where exists (
            select 1
            from (
                <include refid="projectEmployeeScopeSql"/>
            ) scope
            where scope.staff_name is not null
              and scope.staff_name != ''
              and (
                    scope.staff_name = r.interviewer_name1
                    or scope.staff_name = r.interviewer_name2
              )
        )
        <if test="query.interviewerName != null and query.interviewerName != ''">
            and (
                r.interviewer_name1 like concat('%', #{query.interviewerName}, '%')
                or r.interviewer_name2 like concat('%', #{query.interviewerName}, '%')
            )
        </if>
        order by r.create_time desc, r.recruit_id desc
    </select>

    <select id="selectExtendedRecruitmentDetail"
            resultType="com.ruoyi.ccdi.project.domain.vo.CcdiProjectExtendedRecruitmentDetailVO">
        select
            r.recruit_id,
            r.recruit_name,
            r.pos_name,
            r.pos_category,
            r.pos_desc,
            r.cand_name,
            r.cand_edu,
            r.cand_id,
            r.cand_school,
            r.cand_major,
            r.cand_grad,
            r.admit_status,
            r.interviewer_name1,
            r.interviewer_id1,
            r.interviewer_name2,
            r.interviewer_id2,
            r.created_by,
            r.create_time,
            r.updated_by,
            r.update_time
        from ccdi_staff_recruitment r
        where r.recruit_id = #{recruitId}
          and exists (
            select 1
            from (
                <include refid="projectEmployeeScopeSql"/>
            ) scope
            where scope.staff_name is not null
              and scope.staff_name != ''
              and (
                    scope.staff_name = r.interviewer_name1
                    or scope.staff_name = r.interviewer_name2
              )
        )
    </select>

    <select id="selectExtendedTransferPage" resultMap="ExtendedTransferListItemResultMap">
        <bind name="projectId" value="query.projectId"/>
        select
            t.id,
            s.name as staff_name,
            t.transfer_type,
            t.dept_name_before,
            t.dept_name_after,
            t.transfer_date
        from ccdi_staff_transfer t
        inner join ccdi_base_staff s
            on t.staff_id = s.staff_id
        inner join (
            select distinct scope.staff_name
            from (
                <include refid="projectEmployeeScopeSql"/>
            ) scope
            where scope.staff_name is not null
              and scope.staff_name != ''
        ) scope
            on scope.staff_name = s.name
        <where>
            <if test="query.staffName != null and query.staffName != ''">
                and s.name like concat('%', #{query.staffName}, '%')
            </if>
            <if test="query.transferDateStart != null and query.transferDateStart != ''">
                and t.transfer_date &gt;= #{query.transferDateStart}
            </if>
            <if test="query.transferDateEnd != null and query.transferDateEnd != ''">
                and t.transfer_date &lt;= #{query.transferDateEnd}
            </if>
        </where>
        order by t.transfer_date desc, t.create_time desc, t.id desc
    </select>

    <select id="selectExtendedTransferDetail"
            resultType="com.ruoyi.ccdi.project.domain.vo.CcdiProjectExtendedTransferDetailVO">
        select
            t.id,
            t.staff_id,
            s.name as staff_name,
            t.transfer_type,
            t.transfer_sub_type,
            t.dept_id_before,
            t.dept_name_before,
            t.grade_before,
            t.position_before,
            t.salary_level_before,
            t.dept_id_after,
            t.dept_name_after,
            t.grade_after,
            t.position_after,
            t.salary_level_after,
            t.transfer_date,
            t.created_by,
            t.create_time,
            t.updated_by,
            t.update_time
        from ccdi_staff_transfer t
        inner join ccdi_base_staff s
            on t.staff_id = s.staff_id
        where t.id = #{id}
          and exists (
            select 1
            from (
                <include refid="projectEmployeeScopeSql"/>
            ) scope
            where scope.staff_name = s.name
          )
    </select>

</mapper>
