<?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.hzs.system.sys.mapper.SysUserMenuMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.hzs.common.domain.system.sys.SysUserMenu">
        <result property="userId" column="USER_ID"/>
        <result property="menuId" column="MENU_ID"/>
        <result property="delFlag" column="DEL_FLAG"/>
        <result property="pkCreator" column="PK_CREATOR"/>
        <result property="creationTime" column="CREATION_TIME"/>
        <result property="pkModified" column="PK_MODIFIED"/>
        <result property="modifiedTime" column="MODIFIED_TIME"/>
    </resultMap>

    <!-- 查询列表 -->
    <select id="queryList" parameterType="com.hzs.system.sys.param.SysUserMenuQueryParam"
            resultType="com.hzs.system.sys.vo.SysUserMenuQueryVO">
        select u.user_id,
        u.nick_name,
        u.creation_time,
        to_char(wm_concat(distinct ur.role_id)) role_ids,
        to_char(wm_concat(distinct um.menu_id)) menu_ids
        from (select u.user_id, u.nick_name, max(um.creation_time) creation_time
        from sys_user u
        left join sys_user_menu um
        on u.user_id = um.user_id
        and um.del_flag = 0
        <if test="roleId != null">
            left join sys_user_role ur
            on ur.user_id = u.user_id
        </if>
        where u.del_flag = 0
        and um.user_id is not null
        <if test="roleId != null">
            and ur.role_id = #{roleId}
        </if>
        <if test="nickName != null and nickName != ''">
            and u.nick_name like #{nickName} || '%'
        </if>
        <if test="menuId != null">
            and um.menu_id = #{menuId}
        </if>
        <if test="creationTime != null">
            and um.creation_time >= #{creationTime[0]}
            and #{creationTime[1]} >= um.creation_time
        </if>
        group by u.user_id, u.nick_name) u
        left join sys_user_menu um
        on u.user_id = um.user_id
        and um.del_flag = 0
        inner join sys_menu m
        on m.menu_id = um.menu_id
        and m.del_flag = 0
        and m.menu_type in ('M', 'C')
        left join sys_user_role ur
        on ur.user_id = u.user_id
        group by u.user_id, u.nick_name, u.creation_time
        order by u.creation_time desc
    </select>

    <!-- 查询详情 -->
    <select id="queryDetail" resultType="com.hzs.system.sys.vo.SysUserMenuDetailVO">
        select u.user_id,
               to_char(wm_concat(distinct ur.role_id)) role_ids,
               to_char(wm_concat(distinct um.menu_id)) menu_ids
        from sys_user u
                 left join sys_user_role ur
                           on ur.user_id = u.user_id
                 left join SYS_USER_MENU um
                           on um.user_id = u.user_id
                               and um.del_flag = 0
        where u.del_flag = 0
          and u.user_id = #{userId}
        group by u.user_id
    </select>

    <!-- 批量新增用户菜单配置 -->
    <insert id="insertUserMenuBatch">
        MERGE INTO sys_user_menu sum
        using (
        <foreach collection="list" item="item" separator=" union ">
            select
            #{item.userId} user_id,
            #{item.menuId} menu_id,
            #{item.pkCreator} pk_creator
            from dual
        </foreach>
        ) tmp
        on (1 = 0)
        WHEN NOT MATCHED THEN
        insert (user_id, menu_id, pk_creator)
        values (tmp.user_id, tmp.menu_id, tmp.pk_creator)
    </insert>

</mapper>
