<?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.ModuleMapper">

    <!-- 获取所有应用 -->
    <select id="listModules" resultType="com.xhkjedu.sapi.model.system.TModule">
        SELECT m.moduleid,m.modulename,m.moduleurl,m.moduleico,m.modulepid,m.morder,m.minout,m.comm,
        m.mclass,m.level,m.createid,m.createtime,m.schoolid,u.username AS createname,m.mabbrevname
        ,m.defaultid,m.mdefault,m.belong,m.belongcode,m.display
        FROM t_module m LEFT JOIN t_user u ON m.createid=u.userid
        WHERE m.schoolid=#{module.schoolid} and m.belong=#{module.belong}
        <if test="module.belongcode!=null and module.belongcode==1">and m.belongcode in (1,3)</if>
        <if test="module.belongcode!=null and module.belongcode==2">and m.belongcode in (2,3)</if>
        <if test="module.display!=null">and m.display=#{module.display}</if>
        ORDER BY m.morder,m.moduleid
    </select>

    <!-- 获取供第一层模块 -->
    <select id="listLevelModule" resultType="com.xhkjedu.sapi.vo.system.ModuleSelVo">
        SELECT moduleid,modulename,LEVEL FROM t_module
        WHERE schoolid=#{schoolid} and LEVEL=1 and display=1 and belong=#{belong}
        order by morder,moduleid
    </select>

    <update id="updateOrder">
        update t_module set morder=morder+1
        where modulepid=#{module.modulepid} and morder>=#{module.morder} and moduleid!=#{module.moduleid}
    </update>
    <!--模块上移-->
    <update id="moveUp">
        UPDATE t_module
        <trim prefix="set" suffixOverrides=",">
            <trim prefix="morder = CASE " suffix="END,">
                <foreach collection="list" index="index" item="item">
                    when moduleid=#{item.moduleid} then #{item.morder}
                </foreach>
            </trim>
        </trim>
        where
        <foreach collection="list" item="item" index="index" separator="or">
            moduleid=#{item.moduleid}
        </foreach>
    </update>

    <!--模块选择列表-->
    <select id="listForSel2" resultType="java.util.Map">
        SELECT m.moduleid,m.modulename,m.mabbrevname,m.belong,m.belongcode
        FROM t_module m WHERE m.schoolid=#{schoolid} and m.level!=1 and m.belong=#{belong}
        <if test="belongcode!=null and belongcode==1">and m.belongcode in (1,3)</if>
        <if test="belongcode!=null and belongcode==2">and m.belongcode in (2,3)</if>
        and m.display=1 ORDER BY m.morder,m.moduleid
    </select>
    <!--删除默认模块关联的学校模块-->
    <delete id="deleteByDefaultId">
        delete from t_module where defaultid=#{defaultid}
    </delete>
    <!--批量保存-->
    <insert id="batchSave">
        INSERT INTO t_module (moduleid,modulename,mabbrevname,moduleurl,moduleico,modulepid,belong,belongcode,morder,comm
        ,minout,mclass,level,mdefault,display,defaultid,createid,createtime,schoolid ) VALUES
        <foreach collection="modules" item="m" index="index" separator=",">
            (#{m.moduleid},#{m.modulename},#{m.mabbrevname},#{m.moduleurl},#{m.moduleico},#{m.modulepid},#{m.belong},#{m.belongcode},#{m.morder},#{m.comm}
            ,#{m.minout},#{m.mclass},#{m.level},#{m.mdefault},#{m.display},#{m.defaultid},#{m.createid},#{m.createtime},#{m.schoolid})
        </foreach>
    </insert>

    <!--修改模块状态-->
    <update id="updateModuleDisplay">
        update t_module set display=#{display} where moduleid=#{moduleid}
    </update>
    <!--根据学校id获取管理员角色id-->
    <select id="getRoleIdBySchoolId" resultType="java.lang.Integer">
        select roleid from t_role where schoolid=#{schoolid} and roletype=0 limit 1
    </select>

    <!--获取指定模块对应的模块码-->
    <select id="listModulesByModuleIds" resultType="com.xhkjedu.sapi.model.system.TModule">
        select moduleid,belongcode from t_module where moduleid in
        <foreach collection="list" index="index" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>
    </select>
</mapper>
