<?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.xhkjedu.sapi.mapper.system.UserModuleMapper">
    <!-- 用户常用模块列表 -->
    <select id="listUserModules" resultType="com.xhkjedu.sapi.vo.system.UserModuleVo">
        SELECT um.umid, um.moduleid,m.modulename,m.moduleurl,m.moduleico,m.minout,m.mabbrevname
        FROM t_user_module um LEFT JOIN t_module m ON um.moduleid=m.moduleid
        WHERE um.userid=#{userid} and m.display=1
        <if test="belongcode!=null and belongcode==1">and um.belongcode in (1,3)</if>
        <if test="belongcode!=null and belongcode==2">and um.belongcode in (2,3)</if>
        ORDER BY um.umorder
    </select>

    <!-- 批量保存用户常用模块 -->
    <insert id="saveBathUserModule">
        INSERT INTO t_user_module (userid,moduleid,umorder,belongcode,createtime,schoolid) VALUES
        <foreach collection ="list" item="um" index= "index" separator =",">
            (#{um.userid},#{um.moduleid},#{um.umorder},#{um.belongcode},#{um.createtime},#{um.schoolid})
        </foreach>
    </insert>

    <!--根据用户id删除用户常用应用-->
    <delete id="deleteByUserid">
        delete from t_user_module where userid=#{userid}
    </delete>
    <!--获取有常用模块的用户id集合-->
    <select id="listUserId" resultType="java.lang.Integer">
        SELECT um.userid FROM t_user_module um LEFT JOIN t_role_module rm ON um.moduleid=rm.moduleid
        WHERE rm.roleid=#{roleid} GROUP BY um.userid
    </select>
    <!--根据用户id删除没有权限的常用模块-->
    <delete id="deleteNoRoleByUserId">
        DELETE FROM t_user_module WHERE userid=#{userid} AND moduleid
        NOT IN(SELECT rm.moduleid FROM t_user u LEFT JOIN t_user_role ur ON u.userid=ur.userid
        LEFT JOIN t_role_module rm ON ur.roleid=rm.roleid
        WHERE u.userid=#{userid} GROUP BY rm.moduleid)
    </delete>
</mapper>
