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

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="cn.pluss.platform.entity.Account">
        <id column="id" property="id" />
        <result column="userId" property="userId" />
        <result column="idcardId" property="idCardId" />
        <result column="bankCardId" property="bankCardId" />
        <result column="createTime" property="createTime" />
        <result column="updateTime" property="updateTime" />
    </resultMap>

    <select id="selectMicroMbiAccountNum" resultType="java.lang.Integer">
        SELECT count(DISTINCT mbi.userId) count FROM tb_pluss_merchant_base_info mbi
        LEFT JOIN tb_pluss_account account ON account.userId = mbi.userId
        LEFT JOIN tb_pluss_id_card ic ON account.userId = ic.userId AND account.idCardId = ic.id
        LEFT JOIN tb_pluss_merchant_channel_status mcs ON mcs.merchantCode = mbi.merchantCode
        WHERE merchantType = 1
        AND mcs.`status` IN (3)
        AND ic.certNo = #{certNo}
    </select>

    <select id="selectAccountNum" resultType="java.lang.Integer">
        SELECT count(DISTINCT mbi.userId) count FROM tb_pluss_merchant_base_info mbi
            LEFT JOIN tb_pluss_account account ON account.userId = mbi.userId
            LEFT JOIN tb_pluss_id_card ic ON account.userId = ic.userId AND account.idCardId = ic.id
            LEFT JOIN tb_pluss_merchant_channel_status mcs ON mcs.merchantCode = mbi.merchantCode
        WHERE mcs.`status` IN (3, 7)
          AND ic.certNo IN
        <foreach collection="certNoList" item="item" open="(" close=")" separator=",">
            #{item}
        </foreach>
    </select>

    <select id="selectGroupMaps" resultType="java.util.Map">
        SELECT count(1) count, ic.certName, ic.certNo FROM tb_pluss_merchant_base_info mbi
        LEFT JOIN tb_pluss_id_card ic ON mbi.userId = ic.userId
        LEFT JOIN tb_pluss_merchant_channel_status mcs ON mcs.merchantCode = mbi.merchantCode
        WHERE merchantType = 1
        AND ic.userType = 1
        AND mcs.`status` IN (3, 4)
        GROUP BY ic.certName, ic.certNo
    </select>


    <select id="selectYSAuditSmallCount" resultType="int">
        SELECT count(DISTINCT a.userId) FROM tb_pluss_account a
            LEFT JOIN tb_pluss_merchant_base_info mbi ON a.userId = mbi.userId
            LEFT JOIN tb_pluss_merchant_channel_status mcs ON mbi.merchantCode = mcs.merchantCode
            LEFT JOIN tb_pluss_id_card ic ON ic.id = a.idcardId
        WHERE mcs.`status` IN (3, 7) AND mbi.merchantType = 1 AND mcs.channel = 4
          AND ic.certNo = (
            SELECT DISTINCT ic.certNo FROM tb_pluss_account a
                   LEFT JOIN tb_pluss_merchant_base_info mbi ON a.userId = mbi.userId
                   LEFT JOIN tb_pluss_merchant_channel_status mcs ON mbi.merchantCode = mcs.merchantCode
                   LEFT JOIN tb_pluss_id_card ic ON ic.id = a.idcardId
            WHERE mbi.userId = #{userId}
        )
    </select>

    <select id="selectMbiListByCertNo" resultType="cn.pluss.platform.entity.MerchantBaseInfo">
        SELECT DISTINCT account.userId, mbi.merchantCode FROM tb_pluss_account account
        LEFT JOIN tb_pluss_id_card ic ON account.userId = ic.userId AND account.idcardId = ic.id
        LEFT JOIN tb_pluss_merchant_base_info mbi ON mbi.userId = account.userId
        WHERE ic.certNo = #{certNo};
    </select>

</mapper>
