<?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.TeacherSubjectbookMapper">
    <!--列表-->
    <select id="findAll" resultType="com.xhkjedu.sstudy.vo.subjectbook.TeacherSubjectbookVo">
        SELECT ts.tbid,ts.lsbid,ts.selected,sb.subjectid,s.subjectname,sb.lsbname,sb.coverpath,v.versionname
        FROM t_teacher_subjectbook ts
        left join t_subject_book sb on sb.lsbid=ts.lsbid
        LEFT JOIN t_subject s ON sb.subjectid=s.subjectid
        LEFT JOIN t_version v ON sb.versionid=v.versionid
        WHERE ts.teacherid=#{book.teacherid} and (sb.belong=1 or sb.schoolid=ts.schoolid)
        <if test="book.subjectid!=null and book.subjectid!='0'.toString() and book.subjectid!=''">
            and sb.subjectid=#{book.subjectid}
        </if>
        ORDER BY sb.level ASC,s.subjectorder ASC,v.versionorder,sb.lsborder
    </select>
    <!--学生教材列表-->
    <select id="listStuBook" resultType="com.xhkjedu.sstudy.vo.subjectbook.TeacherSubjectbookVo">
        select sb.lsbid,sb.subjectid,s.subjectname,sb.lsbname,sb.coverpath,v.versionname
        from t_class_student cs left join t_class c on cs.classid=c.classid
        left join t_class_teacher ct on ct.classid=cs.classid
        left join t_teacher_subjectbook ts on ts.teacherid=ct.teacherid and ts.schoolid=c.schoolid
        left join t_subject_book sb on sb.lsbid=ts.lsbid
        left join t_subject s on sb.subjectid=s.subjectid
        left join t_version v on sb.versionid=v.versionid
        where c.year=#{year} and c.classstate=1 and cs.studentid=#{studentid}
        and (sb.belong=1 or sb.schoolid=cs.schoolid)
        <if test="subjectid!=null and subjectid!='0'.toString() and subjectid!=''">
            and sb.subjectid=#{subjectid}
        </if>
        group by sb.lsbid order by sb.level,s.subjectorder,v.versionorder,sb.lsborder
    </select>
    <!--获取教师年级下班级列表-->
    <resultMap id="bookClassMap" type="com.xhkjedu.sstudy.vo.subjectbook.TeacherSClassVo">
        <result property="subjectid" column="subjectid"></result>
        <result property="gradeid" column="gradeid"></result>
        <collection property="classes" column="{teacherid=teacherid,year=year,gradeid=gradeid
        ,classtype=classtype,schoolid=schoolid}" select="listClass"></collection>
    </resultMap>
    <select id="listClass" resultType="com.xhkjedu.sstudy.vo.subjectbook.TeacherClassVo">
        select c.classid,c.classname,c.classtype
        from t_class_teacher ct left join t_class c on ct.classid=c.classid
        where ct.teacherid=#{teacherid} and c.year=#{year} and c.classstate=1 and c.gradeid=#{gradeid}
        and c.schoolid=#{schoolid}
        <if test="classtype!=null and classtype!=0">
            <if test="classtype!=11">and c.classtype=#{classtype}</if>
            <if test="classtype==11">and c.classtype in(1,2)</if>
        </if>
        <if test="classtype==null or classtype==0">
            and c.classtype in(1,2)
        </if>
        order by c.gradeid,c.classtype,c.classorder
    </select>
    <select id="listBookClass" resultMap="bookClassMap">
        select tp.subjectid,#{year} year,tp.teacherid,#{classtype} classtype,tp.schoolid
        ,(select c.gradeid from t_class_teacher ct left join t_class c on ct.classid = c.classid
        where ct.teacherid=#{teacherid} and c.classtype in(1,2) and c.classstate=1 and c.year=#{year} limit 1)gradeid
        from t_teacher_powersb tp
        where tp.teacherid=#{teacherid}
        group by tp.subjectid limit 1
    </select>
    <!--更改教师教材关系是否选中-->
    <update id="updateSelected">
        update t_teacher_subjectbook set selected=1 where tbid=#{tbid}
    </update>
    <update id="updateNoSelected">
        update t_teacher_subjectbook set selected=0 where teacherid=#{teacherid}
    </update>

    <!-- 根据用户id删除用户教材 -->
    <delete id="deleteByUserid">
        delete from t_teacher_subjectbook where teacherid=#{userid}
    </delete>
    <!-- 用户id获取教材集合 -->
    <select id="listLsbsByUserid" resultType="java.lang.String">
        SELECT lsbid FROM t_teacher_subjectbook WHERE teacherid=#{userid}
    </select>
    <!--根据教材id教师id获取教师教材关系信息-->
    <select id="getObjByLsbIdTeacherId" resultType="com.xhkjedu.sstudy.model.subjectbook.TTeacherSubjectbook">
        SELECT * FROM t_teacher_subjectbook WHERE lsbid=#{lsbid} and teacherid=#{teacherid} limit 1
    </select>
    <!--删除教师收藏教材-->
    <delete id="deleteByLsbIdTeacherId">
        delete from t_teacher_subjectbook
        where teacherid=#{teacherid} and (
        <foreach collection="lsbids" item="lsbid" separator="or">
            lsbid=#{lsbid}
        </foreach>
        )
    </delete>
    <!--获取教师权限教材科目下所有教材列表-->
    <select id="listSubjectLsb" resultType="com.xhkjedu.sstudy.vo.subjectbook.TeacherSubjectbookVo">
        SELECT sb.lsbid,sb.subjectid,s.subjectname,sb.lsbname,sb.coverpath,sb.versionid,v.versionname
        FROM t_teacher_powersb ts
        left join t_subject_book sb on sb.subjectid=ts.subjectid
        LEFT JOIN t_subject s ON sb.subjectid=s.subjectid
        LEFT JOIN t_version v ON sb.versionid=v.versionid
        WHERE ts.teacherid=#{teacherid} and (sb.belong=1 or sb.schoolid=ts.schoolid)
        group by sb.lsbid
        ORDER BY sb.level ASC,s.subjectorder ASC,v.versionorder,sb.lsborder
    </select>

    <!--根据教材id获取科目id-->
    <select id="getSubjectidByLsbid" resultType="java.lang.String">
        select subjectid from t_subject_book where lsbid=#{lsbid}
    </select>

    <!--根据教师id获取教师权限科目-->
    <select id="listSubjectidByTeacherid" resultType="java.lang.String">
        select DISTINCT subjectid from t_teacher_powersb where teacherid=#{teacherid} limit 1
    </select>
</mapper>
