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

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="cn.pluss.platform.entity.IdCard">
        <id column="id" property="id"/>
        <result column="userId" property="userId"/>
        <result column="certType" property="certType"/>
        <result column="userType" property="userType"/>
        <result column="certNo" property="certNo"/>
        <result column="certName" property="certName"/>
        <result column="certStartTime" property="certStartTime"/>
        <result column="certEndTime" property="certEndTime"/>
        <result column="certAddress" property="certAddress"/>
    </resultMap>

    <insert id="insertUniqueData">
        INSERT INTO `tb_pluss_id_card`
        (`userId`, `userType`, `certType`, `certNo`, `certName`, `certStartTime`,
        `certEndTime`, `certAddress`, `imgPositive`, `imgNegative`)
        SELECT #{data.userId}, #{data.userType}, #{data.certType}, #{data.certNo}, #{data.certName},
        #{data.certStartTime}, #{data.certEndTime}, #{data.certAddress}, #{data.imgPositive}, #{data.imgNegative}
        FROM `tb_pluss_id_card`
        <if test="uniqueData != null">
            WHERE NOT EXISTS
            (
            SELECT 1 FROM `tb_pluss_id_card`
            <where>
                <foreach collection="uniqueData.entrySet()" index="key" item="value">
                    and `${key}` = #{value}
                </foreach>
            </where>
            )
        </if>
        LIMIT 1
    </insert>

    <select id="getAccountIdCard" resultType="cn.pluss.platform.entity.IdCard">
        SELECT t1.`id`,
               t1.`userId`,
               t1.`userType`,
               t1.`certType`,
               t1.`certNo`,
               t1.`certName`,
               t1.`certStartTime`,
               t1.`certEndTime`,
               t1.`certAddress`,
               t1.`createTime`,
               t1.`updateTime`,
               t1.`imgPositive`,
               t1.`imgNegative`
        FROM tb_pluss_id_card t1
                 LEFT JOIN tb_pluss_account t2 ON t1.id = t2.idcardId
        WHERE t2.userId = #{userId}
        <if test="_parameter.containsKey('channelType')">
            <if test="channelType != null and channelType != ''">
                AND t2.channelType = #{channelType}
            </if>
        </if>
    </select>

    <select id="selectAgentCount" resultType="integer">
        SELECT count(1)
        FROM tb_pluss_id_card t1
        LEFT JOIN tb_pluss_user_app ua ON t1.userId = ua.userId
        WHERE ua.userType IN ('agent', 'promoter')
          AND ua.`status` = '3' AND ua.`bankStatus` = '3'
          AND t1.`certNo` = #{certNo}
          AND ua.`userId` != #{userId}
          AND t1.`userType` = '01'
    </select>
</mapper>
