<?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="cn.pluss.platform.mapper.RiskBlacklistMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="cn.pluss.platform.entity.RiskBlacklist">
        <id column="id" property="id" />
        <result column="idcardNo" property="idcardNo" />
        <result column="certName" property="certName" />
        <result column="userId" property="userId" />
        <result column="remark" property="remark" />
        <result column="channel" property="channel" />
        <result column="payWay" property="payWay" />
        <result column="createTime" property="createTime" />
        <result column="updateTime" property="updateTime" />
    </resultMap>


    <select id="selectPageVO" resultType="cn.pluss.platform.vo.RiskBlacklistVO">
        SELECT rb.*, ua.userName, mbi.alias, ui.phone regPhone, bc.phone accountPhone, bc.bankHolder accountName,
               pua.userId parentId, pua.userName parentName, gpua.userId grandParentId, gpua.userName grandParentName
        FROM tb_pluss_risk_blacklist rb
             LEFT JOIN tb_pluss_user_info ui ON ui.id = rb.userId
             LEFT JOIN tb_pluss_merchant_base_info mbi ON mbi.userId = rb.userId
             LEFT JOIN tb_pluss_account ac ON ac.userId = rb.userId
             LEFT JOIN tb_pluss_bank_card bc ON ac.userId = bc.userId AND bc.id = ac.bankCardId
             LEFT JOIN tb_pluss_user_app ua ON ua.userId = rb.userId
             LEFT JOIN tb_pluss_user_app pua ON pua.userId = ua.parentId
             LEFT JOIN tb_pluss_user_app gpua ON gpua.userId = LEFT(pua.parentId, LOCATE(',', pua.parentId) - 1)
        <where>
            <if test="condition.userId != null and condition.userId != ''">
                AND rb.userId = #{condition.userId}
            </if>
            <if test="condition.certName != null and condition.certName != ''">
                AND rb.certName LIKE CONCAT(#{condition.certName}, '%')
            </if>
            <if test="condition.idcardNo != null and condition.idcardNo != ''">
                AND rb.idcardNo LIKE CONCAT(#{condition.idcardNo}, '%')
            </if>
            <if test="condition.alias != null and condition.alias != ''">
                AND mbi.alias LIKE CONCAT(#{condition.alias}, '%')
            </if>
            <if test="condition.channel != null and condition.channel != ''">
                AND rb.channel = #{condition.channel}
            </if>
            <if test="condition.payWay != null and condition.payWay != ''">
                AND rb.payWay = #{condition.channel}
            </if>

            <if test="condition.userName != null and condition.userName != ''">
                AND ua.userName LIKE CONCAT(#{condition.userName}, '%')
            </if>
            <if test="condition.regPhone != null and condition.regPhone != ''">
                AND ui.phone LIKE CONCAT(#{condition.regPhone}, '%')
            </if>

            <if test="condition.accountPhone != null and condition.accountPhone != ''">
                AND bc.phone LIKE CONCAT(#{condition.accountPhone}, '%')
            </if>

            <if test="condition.accountName != null and condition.accountName != ''">
                AND bc.bankHolder LIKE CONCAT(#{condition.accountName}, '%')
            </if>

            <if test="condition.parentId != null and condition.parentId != ''">
                AND pua.userId = #{condition.parentId}
            </if>

            <if test="condition.parentName != null and condition.parentName != ''">
                AND pua.userName LIKE CONCAT(#{condition.parentName}, '%')
            </if>

            <if test="condition.grandParentId != null and condition.grandParentId != ''">
                AND gpua.userId = #{condition.grandParentId}
            </if>

            <if test="condition.grandParentName != null and condition.grandParentName != ''">
                AND gpua.userName LIKE CONCAT(#{condition.grandParentName}, '%')
            </if>
        </where>
    </select>
</mapper>
