<?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.gxwebsoft.common.system.mapper.UserVerifyMapper">

    <!-- 关联查询sql -->
    <sql id="selectSql">
        SELECT a.*, b.phone, c.organization_name, d.nickname as adminName
        FROM sys_user_verify a
        LEFT JOIN sys_user b ON a.user_id = b.user_id
        LEFT JOIN sys_organization c ON a.organization_id = c.organization_id
        LEFT JOIN sys_user d ON a.admin_id = d.user_id
        <where>
            <if test="param.id != null">
                AND a.id = #{param.id}
            </if>
            <if test="param.userId != null">
                AND a.user_id = #{param.userId}
            </if>
            <if test="param.type != null">
              AND a.type = #{param.type}
            </if>
            <if test="param.name != null">
              AND a.name LIKE CONCAT('%', #{param.name}, '%')
            </if>
            <if test="param.realName != null">
                AND a.real_name LIKE CONCAT('%', #{param.realName}, '%')
            </if>
            <if test="param.idCard != null">
                AND a.id_card LIKE CONCAT('%', #{param.idCard}, '%')
            </if>
            <if test="param.birthday != null">
                AND a.birthday LIKE CONCAT('%', #{param.birthday}, '%')
            </if>
            <if test="param.sfz1 != null">
                AND a.sfz1 LIKE CONCAT('%', #{param.sfz1}, '%')
            </if>
            <if test="param.sfz2 != null">
                AND a.sfz2 LIKE CONCAT('%', #{param.sfz2}, '%')
            </if>
            <if test="param.zzCode != null">
                AND a.zz_code = #{param.zzCode}
            </if>
            <if test="param.adminId != null">
              AND a.admin_id = #{param.adminId}
            </if>
            <if test="param.organizationId != null">
              AND a.organization_id = #{param.organizationId}
            </if>
            <if test="param.comments != null">
                AND a.comments LIKE CONCAT('%', #{param.comments}, '%')
            </if>
            <if test="param.status != null">
                AND a.status = #{param.status}
            </if>
            <if test="param.deleted != null">
                AND a.deleted = #{param.deleted}
            </if>
            <if test="param.deleted == null">
                AND a.deleted = 0
            </if>
            <if test="param.createTimeStart != null">
                AND a.create_time &gt;= #{param.createTimeStart}
            </if>
            <if test="param.createTimeEnd != null">
                AND a.create_time &lt;= #{param.createTimeEnd}
            </if>
            <if test="param.organizationIds != null">
              AND a.organization_id IN
              <foreach collection="param.organizationIds" item="item" separator="," open="(" close=")">
                #{item}
              </foreach>
            </if>
            <if test="param.keywords != null">
              AND (a.name LIKE CONCAT('%', #{param.keywords}, '%')
              OR a.real_name LIKE CONCAT('%', #{param.keywords}, '%')
              OR b.phone = #{param.keywords}
              OR a.id_card = #{param.keywords}
              OR a.zz_code = #{param.keywords}
              )
            </if>
            <!--
              防止同一个 user_id 在 sys_user_verify 中存在多条记录时，列表查询返回 userId 重复。
              这里保留每个 user_id 的最新一条记录（按 id 最大）。
              注意：当按主键 id 精确查询时不做去重，避免把历史记录过滤掉。
            -->
            <if test="param.id == null">
                AND a.id IN (
                    SELECT MAX(v.id)
                    FROM sys_user_verify v
                    LEFT JOIN sys_user u ON v.user_id = u.user_id
                    <where>
                        <if test="param.userId != null">
                            AND v.user_id = #{param.userId}
                        </if>
                        <if test="param.type != null">
                            AND v.type = #{param.type}
                        </if>
                        <if test="param.name != null">
                            AND v.name LIKE CONCAT('%', #{param.name}, '%')
                        </if>
                        <if test="param.realName != null">
                            AND v.real_name LIKE CONCAT('%', #{param.realName}, '%')
                        </if>
                        <if test="param.idCard != null">
                            AND v.id_card LIKE CONCAT('%', #{param.idCard}, '%')
                        </if>
                        <if test="param.birthday != null">
                            AND v.birthday LIKE CONCAT('%', #{param.birthday}, '%')
                        </if>
                        <if test="param.sfz1 != null">
                            AND v.sfz1 LIKE CONCAT('%', #{param.sfz1}, '%')
                        </if>
                        <if test="param.sfz2 != null">
                            AND v.sfz2 LIKE CONCAT('%', #{param.sfz2}, '%')
                        </if>
                        <if test="param.zzCode != null">
                            AND v.zz_code = #{param.zzCode}
                        </if>
                        <if test="param.adminId != null">
                            AND v.admin_id = #{param.adminId}
                        </if>
                        <if test="param.organizationId != null">
                            AND v.organization_id = #{param.organizationId}
                        </if>
                        <if test="param.comments != null">
                            AND v.comments LIKE CONCAT('%', #{param.comments}, '%')
                        </if>
                        <if test="param.status != null">
                            AND v.status = #{param.status}
                        </if>
                        <if test="param.deleted != null">
                            AND v.deleted = #{param.deleted}
                        </if>
                        <if test="param.deleted == null">
                            AND v.deleted = 0
                        </if>
                        <if test="param.createTimeStart != null">
                            AND v.create_time &gt;= #{param.createTimeStart}
                        </if>
                        <if test="param.createTimeEnd != null">
                            AND v.create_time &lt;= #{param.createTimeEnd}
                        </if>
                        <if test="param.organizationIds != null">
                            AND v.organization_id IN
                            <foreach collection="param.organizationIds" item="item" separator="," open="(" close=")">
                                #{item}
                            </foreach>
                        </if>
                        <if test="param.keywords != null">
                            AND (v.name LIKE CONCAT('%', #{param.keywords}, '%')
                            OR v.real_name LIKE CONCAT('%', #{param.keywords}, '%')
                            OR u.phone = #{param.keywords}
                            OR v.id_card = #{param.keywords}
                            OR v.zz_code = #{param.keywords}
                            )
                        </if>
                    </where>
                    GROUP BY v.user_id
                )
            </if>
        </where>
    </sql>

    <!-- 分页查询 -->
    <select id="selectPageRel" resultType="com.gxwebsoft.common.system.entity.UserVerify">
        <include refid="selectSql"></include>
    </select>

    <!-- 查询全部 -->
    <select id="selectListRel" resultType="com.gxwebsoft.common.system.entity.UserVerify">
        <include refid="selectSql"></include>
    </select>

</mapper>
