<?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.info.collection.mapper.CcdiAccountInfoMapper">

    <resultMap id="CcdiAccountInfoVOResult" type="com.ruoyi.info.collection.domain.vo.CcdiAccountInfoVO">
        <id property="id" column="id"/>
        <result property="ownerType" column="ownerType"/>
        <result property="ownerId" column="ownerId"/>
        <result property="staffId" column="staffId"/>
        <result property="staffName" column="staffName"/>
        <result property="relationId" column="relationId"/>
        <result property="relationType" column="relationType"/>
        <result property="relationName" column="relationName"/>
        <result property="relationCertNo" column="relationCertNo"/>
        <result property="accountNo" column="accountNo"/>
        <result property="accountType" column="accountType"/>
        <result property="bankScope" column="bankScope"/>
        <result property="accountName" column="accountName"/>
        <result property="openBank" column="openBank"/>
        <result property="bankCode" column="bankCode"/>
        <result property="currency" column="currency"/>
        <result property="status" column="status"/>
        <result property="effectiveDate" column="effectiveDate"/>
        <result property="invalidDate" column="invalidDate"/>
        <result property="isActualControl" column="isActualControl"/>
        <result property="avgMonthTxnCount" column="avgMonthTxnCount"/>
        <result property="avgMonthTxnAmount" column="avgMonthTxnAmount"/>
        <result property="txnFrequencyLevel" column="txnFrequencyLevel"/>
        <result property="debitSingleMaxAmount" column="debitSingleMaxAmount"/>
        <result property="creditSingleMaxAmount" column="creditSingleMaxAmount"/>
        <result property="debitDailyMaxAmount" column="debitDailyMaxAmount"/>
        <result property="creditDailyMaxAmount" column="creditDailyMaxAmount"/>
        <result property="txnRiskLevel" column="txnRiskLevel"/>
        <result property="createBy" column="createBy"/>
        <result property="createTime" column="createTime"/>
        <result property="updateBy" column="updateBy"/>
        <result property="updateTime" column="updateTime"/>
    </resultMap>

    <sql id="AccountInfoSelectColumns">
        ai.account_id AS id,
        ai.owner_type AS ownerType,
        ai.owner_id AS ownerId,
        CASE
            WHEN ai.owner_type = 'EMPLOYEE' THEN bs.staff_id
            WHEN ai.owner_type = 'RELATION' THEN bsRel.staff_id
            ELSE NULL
        END AS staffId,
        CASE
            WHEN ai.owner_type = 'EMPLOYEE' THEN bs.name
            WHEN ai.owner_type = 'RELATION' THEN bsRel.name
            ELSE NULL
        END AS staffName,
        CASE WHEN ai.owner_type = 'RELATION' THEN fr.id ELSE NULL END AS relationId,
        CASE WHEN ai.owner_type = 'RELATION' THEN fr.relation_type ELSE NULL END AS relationType,
        CASE WHEN ai.owner_type = 'RELATION' THEN fr.relation_name ELSE NULL END AS relationName,
        CASE WHEN ai.owner_type = 'RELATION' THEN fr.relation_cert_no ELSE NULL END AS relationCertNo,
        ai.account_no AS accountNo,
        ai.account_type AS accountType,
        ai.bank_scope AS bankScope,
        ai.account_name AS accountName,
        ai.bank AS openBank,
        ai.bank_code AS bankCode,
        ai.currency AS currency,
        ai.status AS status,
        ai.effective_date AS effectiveDate,
        ai.invalid_date AS invalidDate,
        ai.is_self_account AS isActualControl,
        ai.monthly_avg_trans_count AS avgMonthTxnCount,
        ai.monthly_avg_trans_amount AS avgMonthTxnAmount,
        ai.trans_freq_type AS txnFrequencyLevel,
        ai.dr_max_single_amount AS debitSingleMaxAmount,
        ai.cr_max_single_amount AS creditSingleMaxAmount,
        ai.dr_max_daily_amount AS debitDailyMaxAmount,
        ai.cr_max_daily_amount AS creditDailyMaxAmount,
        ai.trans_risk_level AS txnRiskLevel,
        ai.create_by AS createBy,
        ai.create_time AS createTime,
        ai.update_by AS updateBy,
        ai.update_time AS updateTime
    </sql>

    <sql id="AccountInfoWhereClause">
        WHERE 1 = 1
        AND ai.owner_type &lt;&gt; 'CREDIT_CUSTOMER'
        <if test="query.staffName != null and query.staffName != ''">
            AND (
                (ai.owner_type = 'EMPLOYEE' AND bs.name LIKE CONCAT('%', #{query.staffName}, '%'))
                OR
                (ai.owner_type = 'RELATION' AND bsRel.name LIKE CONCAT('%', #{query.staffName}, '%'))
            )
        </if>
        <if test="query.ownerType != null and query.ownerType != ''">
            AND ai.owner_type = #{query.ownerType}
        </if>
        <if test="query.bankScope != null and query.bankScope != ''">
            AND ai.bank_scope = #{query.bankScope}
        </if>
        <if test="query.relationType != null and query.relationType != ''">
            AND fr.relation_type = #{query.relationType}
        </if>
        <if test="query.accountName != null and query.accountName != ''">
            AND ai.account_name LIKE CONCAT('%', #{query.accountName}, '%')
        </if>
        <if test="query.accountType != null and query.accountType != ''">
            AND ai.account_type = #{query.accountType}
        </if>
        <if test="query.isActualControl != null">
            AND ai.is_self_account = #{query.isActualControl}
        </if>
        <if test="query.riskLevel != null and query.riskLevel != ''">
            AND ai.trans_risk_level = #{query.riskLevel}
        </if>
        <if test="query.status != null">
            AND ai.status = #{query.status}
        </if>
    </sql>

    <select id="selectAccountInfoPage" resultMap="CcdiAccountInfoVOResult">
        SELECT
        <include refid="AccountInfoSelectColumns"/>
        FROM ccdi_account_info ai
        LEFT JOIN ccdi_base_staff bs ON ai.owner_type = 'EMPLOYEE' AND ai.owner_id = bs.id_card
        LEFT JOIN ccdi_staff_fmy_relation fr ON ai.owner_type = 'RELATION' AND ai.owner_id = fr.relation_cert_no
        LEFT JOIN ccdi_base_staff bsRel ON fr.person_id = bsRel.id_card
        <include refid="AccountInfoWhereClause"/>
        ORDER BY ai.update_time DESC, ai.account_id DESC
    </select>

    <select id="selectAccountInfoListForExport" resultMap="CcdiAccountInfoVOResult">
        SELECT
        <include refid="AccountInfoSelectColumns"/>
        FROM ccdi_account_info ai
        LEFT JOIN ccdi_base_staff bs ON ai.owner_type = 'EMPLOYEE' AND ai.owner_id = bs.id_card
        LEFT JOIN ccdi_staff_fmy_relation fr ON ai.owner_type = 'RELATION' AND ai.owner_id = fr.relation_cert_no
        LEFT JOIN ccdi_base_staff bsRel ON fr.person_id = bsRel.id_card
        <include refid="AccountInfoWhereClause"/>
        ORDER BY ai.update_time DESC, ai.account_id DESC
    </select>

    <select id="selectAccountInfoById" resultMap="CcdiAccountInfoVOResult">
        SELECT
        <include refid="AccountInfoSelectColumns"/>
        FROM ccdi_account_info ai
        LEFT JOIN ccdi_base_staff bs ON ai.owner_type = 'EMPLOYEE' AND ai.owner_id = bs.id_card
        LEFT JOIN ccdi_staff_fmy_relation fr ON ai.owner_type = 'RELATION' AND ai.owner_id = fr.relation_cert_no
        LEFT JOIN ccdi_base_staff bsRel ON fr.person_id = bsRel.id_card
        WHERE ai.account_id = #{id}
    </select>

    <select id="selectRelationOptionsByStaffId" resultType="com.ruoyi.info.collection.domain.vo.CcdiAccountRelationOptionVO">
        SELECT
            fr.id,
            fr.relation_name AS relationName,
            fr.relation_type AS relationType,
            fr.relation_cert_no AS relationCertNo
        FROM ccdi_staff_fmy_relation fr
        INNER JOIN ccdi_base_staff bs ON fr.person_id = bs.id_card
        WHERE bs.staff_id = #{staffId}
          AND fr.is_emp_family = 1
          AND fr.status = 1
        ORDER BY fr.id DESC
    </select>
</mapper>
