<?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.fuyuanshen.customer.mapper.CustomerMapper">
    <resultMap id="BaseResultMap" type="com.fuyuanshen.customer.domain.Customer">
        <id column="user_user_id" property="customerId"/>
        <result column="user_dept_id" property="deptId"/>
        <result column="user_user_name" property="userName"/>
        <result column="user_nick_name" property="nickName"/>
        <result column="user_email" property="email"/>
        <result column="user_pid" property="pid"/>
        <result column="user_create_by" property="createBy"/>
        <result column="user_update_by" property="updateBy"/>
        <result column="user_create_time" property="createTime"/>
        <result column="user_update_time" property="updateTime"/>
        <result column="tenant_id" property="tenantId"/>

    </resultMap>

    <!-- 公共查询字段 -->
    <sql id="selectUserColumns">
        u
        .
        user_user_id
        AS user_user_id,
        u.user_name,
        u.nick_name AS nickName,
        u.email,
        u.phone,
        u.gender,
        u.avatar_name AS avatarName,
        u.avatar_path AS avatarPath,
        u.enabled,
        IF(u.is_admin = 1, true, false) AS isAdmin,
        u.pwd_reset_time AS pwdResetTime,
        u.dept_id AS deptId,
        u.tenant_id AS tenantId
    </sql>

    <sql id="Base_Column_List">
        u1
        .
        user_id
        as user_user_id, u1.dept_id as user_dept_id, u1.user_name as user_user_name,
           u1.nick_name as user_nick_name, u1.email as user_email, u1.phone as user_phone,
           u1.gender as user_gender, u1.avatar_name as user_avatar_name, u1.avatar_path as user_avatar_path,
           u1.enabled as user_enabled, u1.pwd_reset_time as user_pwd_reset_time, u1.create_by as user_create_by,
           u1.update_by as user_update_by, u1.create_time as user_create_time, u1.update_time as user_update_time,
           u1.user_level, u1.pid as user_pid,u1.is_admin AS admin,
           d.dept_id as dept_id, d.name as dept_name
    </sql>

    <sql id="Job_Column_List">
        j
        .
        job_id
        as job_id, j.name as job_name
    </sql>

    <sql id="Role_Column_List">
        r
        .
        role_id
        as role_id, r.name as role_name, r.level as role_level, r.data_scope as role_data_scope
    </sql>

    <sql id="Whrer_Sql">
        <where>
            <if test="criteria.id != null">
                and u1.user_id = #{criteria.id}
            </if>
            <if test="criteria.enabled != null">
                and u1.enabled = #{criteria.enabled}
            </if>
            <if test="criteria.deptIds != null and criteria.deptIds.size() != 0">
                and u1.dept_id in
                <foreach collection="criteria.deptIds" item="deptId" open="(" separator="," close=")">
                    #{deptId}
                </foreach>
            </if>
            <if test="criteria.blurry != null and criteria.blurry != ''">
                and (
                u1.user_name like concat('%', #{criteria.blurry}, '%')
                or u1.nick_name like concat('%', #{criteria.blurry}, '%')
                or u1.email like concat('%', #{criteria.blurry}, '%')
                )
            </if>
            <if test="criteria.createTime != null and criteria.createTime.size() != 0">
                and u1.create_time between #{criteria.createTime[0]} and #{criteria.createTime[1]}
            </if>
        </where>
    </sql>


    <!-- 分页查询客户 -->
    <select id="findCustomers" resultType="com.fuyuanshen.customer.domain.Customer">
        select
        u.user_id as customerId, u.nick_name , u.user_name, u.enabled, u.create_time
        from sys_user u
        <where>
            <if test="criteria.ids != null and !criteria.ids.isEmpty()">
                and u.pid IN
                <foreach item="item" collection="criteria.ids" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
            <if test="criteria.pid != null">
                and u.pid = #{criteria.pid}
            </if>
            <if test="criteria.blurry != null and criteria.blurry.trim() != ''">
                and u.nick_name like concat('%', TRIM(#{criteria.blurry}), '%')
            </if>
            <if test="criteria.enabled != null">
                and u.enabled = #{criteria.enabled}
            </if>
            <if test="criteria.params.beginTime != null and criteria.params.endTime != null">
                and u.create_time between #{criteria.params.beginTime} and #{criteria.params.endTime}
            </if>
        </where>
        order by u.user_id desc
        <if test="criteria.offset != null">
            limit #{criteria.offset}, #{criteria.pageSize}
        </if>
    </select>

    <!-- 获取分页总数 -->
    <select id="countCustomers" resultType="java.lang.Long">
        select count(*)
        from sys_user u
        <where>
            <if test="criteria.ids != null and !criteria.ids.isEmpty()">
                and u.pid IN
                <foreach item="item" collection="criteria.ids" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
            <if test="criteria.pid != null">
                and u.pid = #{criteria.pid}
            </if>
            <if test="criteria.blurry != null and criteria.blurry.trim() != ''">
                and u.nick_name like concat('%', TRIM(#{criteria.blurry}), '%')
            </if>
            <if test="criteria.enabled != null">
                and u.enabled = #{criteria.enabled}
            </if>
            <if test="criteria.params.beginTime != null and criteria.params.endTime != null">
                and u.create_time between #{criteria.params.beginTime} and #{criteria.params.endTime}
            </if>
        </where>
    </select>

    <!-- 查询所有客户 -->
    <select id="queryAllCustomers" resultType="com.fuyuanshen.customer.domain.Customer">
        select u.user_id as customerId,
        u.nick_name,
        u.user_name,
        u.enabled,
        u.create_time
        from sys_user u
        <where>
            <!-- 增加非空判断 -->
            <if test="criteria.ids != null and !criteria.ids.isEmpty()">
                u.pid IN
                <foreach item="item" collection="criteria.ids" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
            <if test="criteria.pid != null">
                and u.pid = #{criteria.pid}
            </if>
        </where>
    </select>


    <!-- 根据条件查询客户 -->
    <select id="queryCustomers" resultType="com.fuyuanshen.customer.domain.Customer"
            parameterType="com.fuyuanshen.customer.domain.query.UserQueryCriteria">
        select u.user_id as customerId,
        u.nick_name,
        u.user_name,
        u.enabled,
        u.create_time
        from sys_user u
        <where>
            <if test="criteria.customerName != null and criteria.customerName.trim() != ''">
                and u.user_name = TRIM(#{criteria.customerName})
            </if>
        </where>
    </select>

    <!-- 根据id查询客户 -->
    <select id="queryCustomerById" resultType="com.fuyuanshen.customer.domain.Customer">
        select u.*,u.user_id as customerId
        from sys_user u
        <where>
            <if test="customerId!= null">
                and u.user_id = #{customerId}
            </if>
            <if test="pid != null">
                and u.pid = #{pid}
            </if>
        </where>
    </select>


</mapper>