<?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.slive.mapper.liveplay.CsMapper">
    <!--课程列表-->
    <select id="listAll" resultType="com.xhkjedu.slive.vo.liveplay.CourseVo">
        select
        c.courseid,c.coursename,c.gradeid,c.begindate,c.enddate,c.coursenum,c.begintime,c.endtime,c.duration,c.limitstamp
        ,c.limitstu,c.stunum,c.coursestate,c.courseselected,c.coursetype,c.comm,c.teacherid,c.classid,c.starttime,c.createid
        ,c.subjectid,s.subjectname,ct.typename,c.ctypeid
        ,count(distinct case when cs.sectionstate=3 then cs.sectionid else null end)sectionnum
        ,count(distinct case when cs.sectionstate=2 then cs.sectionid else null end)insectionnum
        ,concat('[',group_concat(distinct json_object('teachername',u.username,'headpic',u.headpic)),']')teachername
        ,group_concat( date_format(cs.sectiondate ,'%m月%d日') separator '、')sectiondate
        from l_course c left join l_course_section cs on cs.courseid=c.courseid
        left join t_user u on cs.teacherid=u.userid
        left join t_subject s on c.subjectid = s.subjectid
        left join l_course_type ct on c.ctypeid = ct.ctypeid
        <if test="course.classid!=null and course.classid!=0">
            left join l_course_class cc on cc.courseid=c.courseid
        </if>
        where c.createid=#{course.createid} and c.deleted=1 and c.courseschool=1
        <if test="course.subjectid!=null and course.subjectid!='0'.toString() and course.subjectid!=''">
            and c.subjectid=#{course.subjectid}
        </if>
        <if test="course.coursename!=null and course.coursename!=''">
            and c.coursename like '%${course.coursename}%'
        </if>
        <if test="course.coursestate!=null">
            and c.coursestate=#{course.coursestate}
        </if>
        <if test="course.today!=null and course.today==1">
            and cs.sectiondate=date_format(now(),'%Y-%m-%d')
        </if>
        <if test="course.coursetype!=null">
            and c.coursetype=#{course.coursetype}
        </if>
        <if test="course.ctypeid!=null and course.ctypeid!=0">
            and ct.ctypeid=#{course.ctypeid}
        </if>
        <if test="course.createid!=null and course.createid!=0">
            and c.createid=#{course.createid}
        </if>
        <if test="course.classid!=null and course.classid!=0">
            and cc.classid=#{course.classid}
        </if>
        <if test="course.teachername!=null and course.teachername!=''">
            and u.username like '%${course.teachername}%'
        </if>
        group by c.courseid order by c.courseid desc
    </select>
    <!--课程班级-->
    <resultMap id="classResult" type="java.util.Map">
        <result property="schoolid" column="schoolid"></result>
        <result property="schoolname" column="schoolname"></result>
        <collection property="classes" ofType="java.util.Map" javaType="java.util.List">
            <result property="classid" column="classid"></result>
            <result property="gradeid" column="gradeid"></result>
            <result property="classname" column="classname"></result>
        </collection>
    </resultMap>
    <select id="listClass" resultMap="classResult">
        select lcc.classid,tc.gradeid,tc.classname,ts.schoolid,ts.schoolname
        from l_course_class lcc left join t_class tc on lcc.classid = tc.classid
        left join t_school ts on tc.schoolid = ts.schoolid
        where lcc.courseid=#{courseid}
    </select>
    <!--我的月份下日期课节数量-->
    <select id="listMySectionDate" resultType="java.util.Map">
        select cs.sectiondate as timestr,count(distinct cs.sectionid)as num
        ,count(distinct case when cs.sectionstate=0 then cs.sectionid else null end)wksnum
        ,count(distinct case when cs.sectionstate=2 then cs.sectionid else null end)jxznum
        ,count(distinct case when cs.sectionstate=3 then cs.sectionid else null end)yjsnum
        ,count(distinct case when cs.sectionstate=4 then cs.sectionid else null end)ygqnum
        from l_course_section cs left join l_course c on cs.courseid = c.courseid
        where cs.teacherid=#{section.teacherid} and c.deleted=1 and c.courseschool=1 and DATE_FORMAT(cs.sectiondate, '%Y-%m')=#{section.timestr}
        <if test="section.cstate!=null and section.cstate!=-1">
            and cs.sectionstate=#{section.cstate}
        </if>
        <if test="section.sectionname!=null and section.sectionname!=''">
            and cs.sectionname like '%${section.sectionname}%'
        </if>
        group by cs.sectiondate order by cs.sectiondate asc
    </select>
    <!--我的课程列表-->
    <select id="listMySection" resultType="java.util.Map">
        select c.courseid,c.gradeid,c.stunum,c.comm,c.createid,c.classid
        ,(select t.typename from l_course_type t where t.ctypeid=c.ctypeid)typename
        ,cs.sectionid,cs.sectionname,cs.sectiondate,cs.teacherid,cs.stunum cynum
        ,cs.planbegintime,cs.planendtime,cs.begintime,cs.endtime,cs.sectionstate
        ,(UNIX_TIMESTAMP(CONCAT(cs.sectiondate,' ',cs.planendtime))-UNIX_TIMESTAMP(CONCAT(cs.sectiondate,' ',cs.planbegintime))) totaltime
        ,(select count(*) from l_course_section_playback p where p.sectionid=cs.sectionid and p.pb is not null)playnum
        from l_course_section cs left join l_course c on cs.courseid = c.courseid
        where cs.teacherid=#{section.teacherid} and c.deleted=1 and c.courseschool=1 and cs.sectiondate=#{section.timestr}
        <if test="section.cstate!=null and section.cstate!=-1">
            and cs.sectionstate=#{section.cstate}
        </if>
        <if test="section.sectionname!=null and section.sectionname!=''">
            and cs.sectionname like '%${section.sectionname}%'
        </if>
        order by cs.sectionstate,abs(UNIX_TIMESTAMP(concat(cs.sectiondate,' ',cs.planbegintime))-UNIX_TIMESTAMP(now())),cs.sectionid
    </select>
</mapper>
