<?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.shop.mapper.UserRefereeMapper">

    <!-- 关联查询sql -->
    <sql id="selectSql">
        SELECT a.*,
               b.nickname,b.avatar,b.sex,b.grade_id,b.offline,
               c.nickname as dealerName,c.avatar as dealerAvatar,c.phone as dealerPhone,
               d.name as gradeName
        FROM shop_user_referee a
        LEFT JOIN sys_user b ON a.user_id = b.user_id
        LEFT JOIN sys_user c ON a.dealer_id = c.user_id
        LEFT JOIN shop_user_grade d ON b.grade_id = d.grade_id
        <where>
            <if test="param.id != null">
                AND a.id = #{param.id}
            </if>
            <if test="param.dealerId != null">
                AND a.dealer_id = #{param.dealerId}
            </if>
            <if test="param.userId != null">
                AND a.user_id = #{param.userId}
            </if>
            <if test="param.level != null">
                AND a.level = #{param.level}
            </if>
            <if test="param.sex != null">
              AND b.sex = #{param.sex}
            </if>
            <if test="param.comments != null">
                AND a.comments LIKE CONCAT('%', #{param.comments}, '%')
            </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.gradeId != null">
              AND b.grade_id = #{param.gradeId}
            </if>
            <if test="param.offline != null">
              AND b.offline = #{param.offline}
            </if>
            <if test="param.gradeStart  != null">
              AND b.grade_id &gt;= #{param.gradeStart}
            </if>
            <if test="param.gradeEnd  != null">
              AND b.grade_id &lt;= #{param.gradeEnd}
            </if>
            <if test="param.keywords != null">
              AND (
              a.nickname LIKE CONCAT('%', #{param.keywords}, '%')
              OR b.nickname = #{param.keywords}
              OR c.nickname LIKE CONCAT('%', #{param.keywords}, '%')
              )
            </if>
        </where>
    </sql>

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

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

</mapper>
