<?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.sstudy.mapper.subjectbook.DirectorMapper">

    <!-- 根据科目获取章节-->
    <select id="listDirectors" resultType="com.xhkjedu.sstudy.model.subjectbook.TDirector">
       SELECT d.directorid,d.directorname,d.directorpid,d.directororder,d.leaf,d.lsbid,d.belong,
        d.directorlevel,d.createid,d.createtime,d.directorall,u.username AS createname
        FROM t_director d LEFT JOIN t_user u ON d.createid=u.userid
        WHERE d.lsbid=#{director.lsbid} and d.directorstate=1 and d.disabled=0
        <if test="director.belong!=null and director.belong==1">
            and d.belong=1
        </if>
        <if test="director.belong!=null and director.belong==3">
            and (d.belong=1 or (d.belong=3 and d.schoolid=#{director.schoolid}))
        </if>
        <if test="director.belong!=null and director.belong==4">
            and d.belong=4 and d.createid=#{director.createid}
        </if>
        ORDER BY d.directororder ASC,d.createtime asc
    </select>
    <!--教师收藏教材下的章节数量-->
    <select id="countByTeacherIdLsbId" resultType="java.lang.Integer">
        select count(*) from t_director where createid=#{teacherid} and lsbid=#{lsbid} and belong=4 and directorstate=1 and disabled=0
    </select>
    <!--查找使用章节的模块-->
    <select id="findUseModule" resultType="java.lang.String">
        SELECT CONCAT(
        (select (case when count(*)>0 then '子章节' else '' end) from t_director where directorpid=#{directorid} and directorstate=1 and disabled=0)
        )
    </select>
    <!--获取章节层级名称和章节层级-->
    <select id="getNameAndLevelById" resultType="com.xhkjedu.sstudy.vo.subjectbook.DirectorVo">
        select directorall directorname,directorlevel level from t_director where directorid=#{directorid}
    </select>
    <!--修改层级章节-->
    <update id="updateDirectorAll">
        UPDATE t_director d2
        <if test="3>level">
            LEFT JOIN t_director d3 ON d3.directorpid=d2.directorid
        </if>
        <if test="2>level">
            LEFT JOIN t_director d4 ON d4.directorpid=d3.directorid
        </if>
        SET d2.directorall=CONCAT(#{directorname},';',d2.directorname)
        <if test="3>level">
            ,d3.directorall=CONCAT(#{directorname},';',d2.directorname,';',d3.directorname)
        </if>
        <if test="2>level">
            ,d4.directorall=CONCAT(#{directorname},';',d2.directorname,';',d3.directorname,';',d4.directorname)
        </if>
        WHERE d2.directorpid=#{directorid}
    </update>
    <!--删除-->
    <update id="deleteById">
        update t_director set directorstate=10 where directorid=#{directorid}
    </update>
</mapper>
