<?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.app.mapper.AppUserMapper">

    <!-- 关联查询sql（已移除跨库 JOIN gxwebsoft_core.sys_user，用户信息从冗余字段读取） -->
    <sql id="selectSql">
        SELECT a.*, w.website_name, w.website_code, w.website_icon
        FROM app_user a
        LEFT JOIN cms_website w ON a.website_id = w.website_id AND w.deleted = 0
        <where>
            <if test="param.id != null">
                AND a.id = #{param.id}
            </if>
            <if test="param.websiteId != null">
                AND a.website_id = #{param.websiteId}
            </if>
            <if test="param.userId != null">
                AND a.user_id = #{param.userId}
            </if>
            <if test="param.username != null and param.username != ''">
                AND (a.username LIKE CONCAT('%', #{param.username}, '%')
                  OR a.nickname LIKE CONCAT('%', #{param.username}, '%'))
            </if>
            <if test="param.role != null and param.role != ''">
                AND a.role = #{param.role}
            </if>
            <if test="param.inviteBy != null">
                AND a.invite_by = #{param.inviteBy}
            </if>
            <if test="param.sortNumber != null">
                AND a.sort_number = #{param.sortNumber}
            </if>
            <if test="param.status != null">
                AND a.status = #{param.status}
            </if>
            <if test="param.tenantId != null">
                AND a.tenant_id = #{param.tenantId}
            </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.keywords != null and param.keywords != ''">
                AND (a.username LIKE CONCAT('%', #{param.keywords}, '%')
                  OR a.nickname LIKE CONCAT('%', #{param.keywords}, '%'))
            </if>
        </where>
    </sql>

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

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

</mapper>
