<?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.ss.realName.mapper.RealNameMapper">

    <resultMap type="RealNameVO" id="RealNameResult" autoMapping="true"/>

    <sql id="selectRealNameVo">
        select
            srn.id,
            srn.user_id,
            srn.name,
            srn.id_card,
            srn.mobile,
            srn.create_time,
            srn.score,
            srn.face_image,
            srn.type,
            su.user_name as user_name
        from ss_real_name srn
            left join sm_user su on su.user_id = srn.user_id
    </sql>

    <sql id="searchCondition">
        <if test="query.id != null "> and srn.id = #{query.id}</if>
        <if test="query.userId != null "> and srn.user_id = #{query.userId}</if>
        <if test="query.excludeUserId != null "> and srn.user_id != #{query.excludeUserId}</if>
        <if test="query.name != null  and query.name != ''"> and srn.name like concat('%', #{query.name}, '%')</if>
        <if test="query.idCard != null  and query.idCard != ''"> and srn.id_card like concat('%', #{query.idCard}, '%')</if>
        <if test="query.eqIdCard != null  and query.eqIdCard != ''"> and srn.id_card = #{query.eqIdCard}</if>
        <if test="query.mobile != null  and query.mobile != ''"> and srn.mobile like concat('%', #{query.mobile}, '%')</if>
        <if test="query.userName != null  and query.userName != ''"> and su.user_name like concat('%', #{query.userName}, '%')</if>
        <if test="query.type != null  and query.type != ''"> and srn.type = #{query.type}</if>
    </sql>

    <select id="selectRealNameList" parameterType="RealNameQuery" resultMap="RealNameResult">
        <include refid="selectRealNameVo"/>
        <where>
            <include refid="searchCondition"/>
        </where>
    </select>

    <select id="selectRealNameById" parameterType="Long" resultMap="RealNameResult">
        <include refid="selectRealNameVo"/>
        where srn.id = #{id}
    </select>

    <select id="selectCount" resultType="java.lang.Integer">
        select count(srn.id)
        from ss_real_name srn
        <where>
            <include refid="searchCondition"/>
        </where>
    </select>

    <insert id="insertRealName" parameterType="RealName" useGeneratedKeys="true" keyProperty="id">
        <selectKey keyProperty="id" order="AFTER" resultType="Long">
            SELECT LAST_INSERT_ID();
        </selectKey>
        insert into ss_real_name
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="userId != null">user_id,</if>
            <if test="name != null">`name`,</if>
            <if test="idCard != null">id_card,</if>
            <if test="mobile != null">mobile,</if>
            <if test="createTime != null">create_time,</if>
            <if test="score != null">score,</if>
            <if test="faceImage != null">face_image,</if>
            <if test="type != null and type != ''">type,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="userId != null">#{userId},</if>
            <if test="name != null">#{name},</if>
            <if test="idCard != null">#{idCard},</if>
            <if test="mobile != null">#{mobile},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="score != null">#{score},</if>
            <if test="faceImage != null">#{faceImage},</if>
            <if test="type != null and type != ''">#{type},</if>
         </trim>
    </insert>

    <update id="updateRealName" parameterType="RealName">
        update ss_real_name
        <trim prefix="SET" suffixOverrides=",">
            <include refid="updateColumns"/>
        </trim>
        where id = #{data.id}
    </update>

    <sql id="updateColumns">
        <if test="data.userId != null">user_id = #{data.userId},</if>
        <if test="data.name != null">`name` = #{data.name},</if>
        <if test="data.idCard != null">id_card = #{data.idCard},</if>
        <if test="data.mobile != null">mobile = #{data.mobile},</if>
        <if test="data.createTime != null">create_time = #{data.createTime},</if>
        <if test="data.score != null">score = #{data.score},</if>
        <if test="data.faceImage != null">face_image = #{data.faceImage},</if>
        <if test="data.type != null and data.type != ''">type = #{data.type},</if>
    </sql>

    <delete id="deleteRealNameById" parameterType="Long">
        delete from ss_real_name where id = #{id}
    </delete>

    <delete id="deleteRealNameByIds" parameterType="String">
        delete from ss_real_name where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>
</mapper>
