<?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.ankaibei.workFlow.webadmin.upms.dao.SysUserMapper">
    <resultMap id="BaseResultMap" type="com.ankaibei.workFlow.webadmin.upms.model.SysUser">
        <id column="user_id" jdbcType="BIGINT" property="userId"/>
        <result column="login_name" jdbcType="VARCHAR" property="loginName"/>
        <result column="password" jdbcType="VARCHAR" property="password"/>
        <result column="show_name" jdbcType="VARCHAR" property="showName"/>
        <result column="dept_id" jdbcType="BIGINT" property="deptId"/>
        <result column="user_type" jdbcType="INTEGER" property="userType"/>
        <result column="head_image_url" jdbcType="VARCHAR" property="headImageUrl"/>
        <result column="user_status" jdbcType="INTEGER" property="userStatus"/>
        <result column="email" jdbcType="VARCHAR" property="email"/>
        <result column="mobile" jdbcType="VARCHAR" property="mobile"/>
        <result column="deleted_flag" jdbcType="INTEGER" property="deletedFlag"/>
        <result column="create_user_id" jdbcType="BIGINT" property="createUserId"/>
        <result column="update_user_id" jdbcType="BIGINT" property="updateUserId"/>
        <result column="create_time" jdbcType="TIMESTAMP" property="createTime"/>
        <result column="update_time" jdbcType="TIMESTAMP" property="updateTime"/>
        <result column="region" jdbcType="VARCHAR" property="region"/>
        <result column="job_number" jdbcType="VARCHAR" property="jobNumber"/>
    </resultMap>

    <insert id="insertList">
        INSERT INTO work_sys_user
        (user_id,
        login_name,
        password,
        show_name,
        dept_id,
        user_type,
        head_image_url,
        user_status,
        email,
        mobile,
        deleted_flag,
        create_user_id,
        update_user_id,
        create_time,
        update_time)
        VALUES
        <foreach collection="list" index="index" item="item" separator=",">
            (#{item.userId},
            #{item.loginName},
            #{item.password},
            #{item.showName},
            #{item.deptId},
            #{item.userType},
            #{item.headImageUrl},
            #{item.userStatus},
            #{item.email},
            #{item.mobile},
            #{item.deletedFlag},
            #{item.createUserId},
            #{item.updateUserId},
            #{item.createTime},
            #{item.updateTime})
        </foreach>
    </insert>

    <!-- 如果有逻辑删除字段过滤，请写到这里 -->
    <sql id="filterRef">
        <!-- 这里必须加上全包名，否则当filterRef被其他Mapper.xml包含引用的时候，就会调用Mapper.xml中的该SQL片段 -->
        <include refid="com.ankaibei.workFlow.webadmin.upms.dao.SysUserMapper.inputFilterRef"/>
        AND work_sys_user.deleted_flag = ${@com.ankaibei.workFlow.common.core.constant.GlobalDeletedFlag@NORMAL}
    </sql>

    <!-- 这里仅包含调用接口输入的主表过滤条件 -->
    <sql id="inputFilterRef">
        <if test="sysUserFilter != null">
            <if test="sysUserFilter.loginName != null and sysUserFilter.loginName != ''">
                <bind name="safeSysUserLoginName" value="'%' + sysUserFilter.loginName + '%'"/>
                AND work_sys_user.login_name LIKE #{safeSysUserLoginName}
            </if>
            <if test="sysUserFilter.showName != null and sysUserFilter.showName != ''">
                <bind name="safeSysUserShowName" value="'%' + sysUserFilter.showName + '%'"/>
                AND work_sys_user.show_name LIKE #{safeSysUserShowName}
            </if>
            <if test="sysUserFilter.deptId != null">
                AND (EXISTS (SELECT 1 FROM work_sys_dept_relation WHERE
                work_sys_dept_relation.parent_dept_id = #{sysUserFilter.deptId}
                AND work_sys_user.dept_id = work_sys_dept_relation.dept_id))
            </if>
            <if test="sysUserFilter.userStatus != null">
                AND work_sys_user.user_status = #{sysUserFilter.userStatus}
            </if>
            <if test="sysUserFilter.createTimeStart != null and sysUserFilter.createTimeStart != ''">
                AND work_sys_user.create_time &gt;= #{sysUserFilter.createTimeStart}
            </if>
            <if test="sysUserFilter.createTimeEnd != null and sysUserFilter.createTimeEnd != ''">
                AND work_sys_user.create_time &lt;= #{sysUserFilter.createTimeEnd}
            </if>
        </if>
    </sql>

    <select id="getSysUserList" resultMap="BaseResultMap"
            parameterType="com.ankaibei.workFlow.webadmin.upms.model.SysUser">
        SELECT * FROM work_sys_user
        <where>
            <include refid="filterRef"/>
        </where>
        ORDER BY create_time desc
    </select>

    <select id="getSysUserListByRoleId" resultMap="BaseResultMap">
        SELECT
        work_sys_user.*
        FROM
        work_sys_user_role,
        work_sys_user
        <where>
            AND work_sys_user_role.role_id = #{roleId}
            AND work_sys_user_role.user_id = work_sys_user.user_id
            <include refid="filterRef"/>
        </where>
        <if test="orderBy != null and orderBy != ''">
            ORDER BY ${orderBy}
        </if>
    </select>

    <select id="getNotInSysUserListByRoleId" resultMap="BaseResultMap">
        SELECT * FROM work_sys_user
        <where>
            NOT EXISTS (SELECT * FROM work_sys_user_role
            WHERE work_sys_user_role.role_id = #{roleId} AND work_sys_user_role.user_id = work_sys_user.user_id)
            <include refid="filterRef"/>
        </where>
        <if test="orderBy != null and orderBy != ''">
            ORDER BY ${orderBy}
        </if>
    </select>

    <select id="getSysUserListByDataPermId" resultMap="BaseResultMap">
        SELECT
        work_sys_user.*
        FROM
        work_sys_data_perm_user,
        work_sys_user
        <where>
            AND work_sys_data_perm_user.data_perm_id = #{dataPermId}
            AND work_sys_data_perm_user.user_id = work_sys_user.user_id
            <include refid="filterRef"/>
        </where>
        <if test="orderBy != null and orderBy != ''">
            ORDER BY ${orderBy}
        </if>
    </select>

    <select id="getNotInSysUserListByDataPermId" resultMap="BaseResultMap">
        SELECT * FROM work_sys_user
        <where>
            NOT EXISTS (SELECT * FROM work_sys_data_perm_user
            WHERE work_sys_data_perm_user.data_perm_id = #{dataPermId} AND work_sys_data_perm_user.user_id =
            work_sys_user.user_id)
            <include refid="filterRef"/>
        </where>
        <if test="orderBy != null and orderBy != ''">
            ORDER BY ${orderBy}
        </if>
    </select>

    <select id="getSysUserListByDeptPostId" resultMap="BaseResultMap">
        SELECT
        work_sys_user.*
        FROM
        work_sys_user_post,
        work_sys_user
        <where>
            AND work_sys_user_post.dept_post_id = #{deptPostId}
            AND work_sys_user_post.user_id = work_sys_user.user_id
            <include refid="filterRef"/>
        </where>
        <if test="orderBy != null and orderBy != ''">
            ORDER BY ${orderBy}
        </if>
    </select>

    <select id="getNotInSysUserListByDeptPostId" resultMap="BaseResultMap">
        SELECT * FROM work_sys_user
        <where>
            NOT EXISTS (SELECT * FROM work_sys_user_post
            WHERE work_sys_user_post.dept_post_id = #{deptPostId} AND work_sys_user_post.user_id =
            work_sys_user.user_id)
            <include refid="filterRef"/>
        </where>
        <if test="orderBy != null and orderBy != ''">
            ORDER BY ${orderBy}
        </if>
    </select>

    <select id="getSysUserListByPostId" resultMap="BaseResultMap">
        SELECT
        work_sys_user.*
        FROM
        work_sys_user_post,
        work_sys_user
        <where>
            AND work_sys_user_post.post_id = #{postId}
            AND work_sys_user_post.user_id = work_sys_user.user_id
            <include refid="filterRef"/>
        </where>
        <if test="orderBy != null and orderBy != ''">
            ORDER BY ${orderBy}
        </if>
    </select>

    <!-- 以下查询仅用于权限分配的问题定位，由于关联表较多，可能会给系统运行带来性能影响 -->
    <select id="getSysPermListWithDetail" resultType="map">
        SELECT
        r.role_id roleId,
        r.role_name roleName,
        m.menu_id menuId,
        m.menu_name menuName,
        m.menu_type menuType,
        pc.perm_code_id permCodeId,
        pc.perm_code permCode,
        pc.perm_code_type permCodeType,
        p.url
        FROM
        work_sys_user_role ur,
        work_sys_role r,
        work_sys_role_menu rm,
        work_sys_menu m,
        work_sys_menu_perm_code mpc,
        work_sys_perm_code pc,
        work_sys_perm_code_perm pcp,
        work_sys_perm p
        <where>
            AND ur.user_id = #{userId}
            AND ur.role_id = r.role_id
            AND ur.role_id = rm.role_id
            AND rm.menu_id = m.menu_id
            AND rm.menu_id = mpc.menu_id
            AND mpc.perm_code_id = pc.perm_code_id
            AND mpc.perm_code_id = pcp.perm_code_id
            AND pcp.perm_id = p.perm_id
            <if test="url != null and url != ''">
                AND p.url = #{url}
            </if>
        </where>
        ORDER BY
        r.role_id, m.menu_id, pc.perm_code_id, p.url
    </select>

    <select id="getSysPermCodeListWithDetail" resultType="map">
        SELECT
        r.role_id roleId,
        r.role_name roleName,
        m.menu_id menuId,
        m.menu_name menuName,
        m.menu_type menuType,
        pc.perm_code_id permCodeId,
        pc.perm_code permCode,
        pc.perm_code_type permCodeType
        FROM
        work_sys_user_role ur,
        work_sys_role r,
        work_sys_role_menu rm,
        work_sys_menu m,
        work_sys_menu_perm_code mpc,
        work_sys_perm_code pc
        <where>
            AND ur.user_id = #{userId}
            AND ur.role_id = r.role_id
            AND ur.role_id = rm.role_id
            AND rm.menu_id = m.menu_id
            AND rm.menu_id = mpc.menu_id
            AND mpc.perm_code_id = pc.perm_code_id
            <if test="permCode != null and permCode != ''">
                AND pc.perm_code = #{permCode}
            </if>
        </where>
        ORDER BY
        r.role_id, m.menu_id, pc.perm_code_id
    </select>

    <select id="getSysMenuListWithDetail" resultType="map">
        SELECT
        r.role_id roleId,
        r.role_name roleName,
        m.menu_id menuId,
        m.menu_name menuName,
        m.menu_type menuType
        FROM
        work_sys_user_role ur,
        work_sys_role r,
        work_sys_role_menu rm,
        work_sys_menu m
        <where>
            AND ur.user_id = #{userId}
            AND ur.role_id = r.role_id
            AND ur.role_id = rm.role_id
            AND rm.menu_id = m.menu_id
            <if test="menuName != null and menuName != ''">
                AND m.menu_name = #{menuName}
            </if>
        </where>
        ORDER BY
        r.role_id, m.menu_id
    </select>


    <select id="getSysUsersByDeptId" resultMap="BaseResultMap">
        SELECT
        work_sys_user.*
        FROM
        work_sys_user
        <where>
            work_sys_user.dept_id = #{deptId}
        </where>
        and deleted_flag ='1'
        ORDER BY create_time desc
    </select>

    <select id="getSysUsersByMobile" resultMap="BaseResultMap">
        SELECT
        work_sys_user.*
        FROM
        work_sys_user
        <where>
            work_sys_user.mobile = #{mobile}
        </where>
        and deleted_flag ='1'
    </select>

    <select id="getSysUsersByMobileOfTheir" resultMap="BaseResultMap">
        SELECT
        work_sys_user.*
        FROM
        work_sys_user
        <where>
            work_sys_user.mobile = #{mobile}
        </where>
        and deleted_flag ='1' and user_id != #{userId}
    </select>

    <select id="getSysUsersByJobNumber" resultMap="BaseResultMap">
        SELECT
        work_sys_user.*
        FROM
        work_sys_user
        <where>
            work_sys_user.job_number = #{jobNumber}
        </where>
        and deleted_flag ='1'
    </select>

    <select id="getSysUsersByJobNumberOfTheir" resultMap="BaseResultMap">
        SELECT
        work_sys_user.*
        FROM
        work_sys_user
        <where>
            work_sys_user.job_number = #{jobNumber}
        </where>
        and deleted_flag ='1' and user_id != #{userId}
    </select>

    <select id="getSysUsersByEmail" resultMap="BaseResultMap">
        SELECT
        work_sys_user.*
        FROM
        work_sys_user
        <where>
            work_sys_user.email = #{email}
        </where>
        and deleted_flag ='1'
    </select>

    <select id="getSysUsersByEmailOfTheir" resultMap="BaseResultMap">
        SELECT
        work_sys_user.*
        FROM
        work_sys_user
        <where>
            work_sys_user.email = #{email}
        </where>
        and deleted_flag ='1' and user_id != #{userId}
    </select>


</mapper>
