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,
ar.is_self_account AS isActualControl,
ar.monthly_avg_trans_count AS avgMonthTxnCount,
ar.monthly_avg_trans_amount AS avgMonthTxnAmount,
ar.trans_freq_type AS txnFrequencyLevel,
ar.dr_max_single_amount AS debitSingleMaxAmount,
ar.cr_max_single_amount AS creditSingleMaxAmount,
ar.dr_max_daily_amount AS debitDailyMaxAmount,
ar.cr_max_daily_amount AS creditDailyMaxAmount,
ar.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
WHERE 1 = 1
AND (
(ai.owner_type = 'EMPLOYEE' AND bs.name LIKE CONCAT('%', #{query.staffName}, '%'))
OR
(ai.owner_type = 'RELATION' AND bsRel.name LIKE CONCAT('%', #{query.staffName}, '%'))
)
AND ai.owner_type = #{query.ownerType}
AND ai.bank_scope = #{query.bankScope}
AND fr.relation_type = #{query.relationType}
AND ai.account_name LIKE CONCAT('%', #{query.accountName}, '%')
AND ai.account_type = #{query.accountType}
AND ar.is_self_account = #{query.isActualControl}
AND ar.trans_risk_level = #{query.riskLevel}
AND ai.status = #{query.status}