<?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.srs.routine.mapper.RtClassMettingThemeMapper">
    
    <resultMap type="RtClassMettingTheme" id="RtClassMettingThemeResult">
        <result property="cmId"    column="cm_id"    />
        <result property="cmNo"    column="cm_no"    />
        <result property="cmTheme"    column="cm_theme"    />
        <result property="cmTextTheme"    column="cm_text_theme"    />
        <result property="cmTitle"    column="cm_title"    />
        <result property="cmIsupload"    column="cm_isupload"    />
        <result property="cmProp"    column="cm_prop"    />
        <result property="cmStart"    column="cm_start"    />
        <result property="cmEnd"    column="cm_end"    />
        <result property="status"    column="status"    />
        <result property="delFlag"    column="del_flag"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />
        <result property="isComplate" column="is_complate"/>
        <result property="remark"    column="remark"    />
    </resultMap>

    <sql id="selectRtClassMettingThemeVo">
        select cm_id, cm_no, cm_theme, cm_text_theme,cm_title, cm_isupload, cm_prop, cm_start, cm_end, status,
               del_flag, create_by, create_time, update_by, update_time, remark
        from rt_class_metting_theme
    </sql>

    <select id="selectRtClassMettingThemeList" parameterType="RtClassMettingTheme" resultMap="RtClassMettingThemeResult">
        <include refid="selectRtClassMettingThemeVo"/>
        <where>  
            <if test="cmNo != null  and cmNo != ''"> and cm_no = #{cmNo}</if>
            <if test="cmTheme != null  and cmTheme != ''"> and cm_theme = #{cmTheme}</if>
            <if test="cmTextTheme != null  and cmTextTheme != ''"> and cm_text_theme like concat('%',#{cmTextTheme},'%') </if>
            <if test="cmTitle != null  and cmTitle != ''"> and cm_title = #{cmTitle}</if>
            <if test="cmIsupload != null  and cmIsupload != ''"> and cm_isupload = #{cmIsupload}</if>
            <if test="cmProp != null  and cmProp != ''"> and cm_prop = #{cmProp}</if>
            <if test="cmStart != null "> and cm_start = #{cmStart}</if>
            <if test="cmEnd != null "> and cm_end = #{cmEnd}</if>
            <if test="status != null  and status != ''"> and status = #{status}</if>
        </where>
    </select>

    <select id="completeStuList" parameterType="RtClassMettingTheme" resultMap="RtClassMettingThemeResult">
        select distinct cm_id, cm_no, cm_theme, cm_text_theme,cm_title, cm_isupload, cm_prop, cm_start, cm_end, status,
        del_flag, create_by, create_time, update_by, update_time, remark,is_complate
        from (
        select cm.*,mc.cr_teacher_no,case when mc.cr_no is NULL then '0' else '1' end as is_complate
        from rt_class_metting_theme cm
        left join rt_class_metting_content mc on cm.cm_no=mc.cr_cm_no and mc.cr_class_no = #{classCode} ) temp
        <where>
            <!--<if test="userName != null  and userName != ''"> temp.rc_teacher_no= #{userName}</if>-->
            <if test="isComplate != null  and isComplate != ''"> temp.is_complate=#{isComplate}</if>
            <if test="cmNo != null  and cmNo != ''"> and temp.cm_no = #{cmNo}</if>
            <if test="cmTextTheme != null  and cmTextTheme != ''"> and temp.cm_text_theme like concat('%',#{cmTextTheme},'%')</if>
            <if test="cmTheme != null  and cmTheme != ''"> and temp.cm_theme like concat('%',#{cmTheme},'%')</if>
            <if test="cmTitle != null  and cmTitle != ''"> and temp.cm_title like concat('%',#{cmTitle},'%') </if>
            <if test="cmIsupload != null  and cmIsupload != ''"> and temp.cm_isupload = #{cmIsupload}</if>
            <if test="cmProp != null  and cmProp != ''"> and temp.cm_prop = #{cmProp}</if>
            <if test="cmStart != null "> and temp.cm_start = #{cmStart}</if>
            <if test="cmEnd != null "> and temp.cm_end = #{cmEnd}</if>
            <if test="status != null  and status != ''"> and temp.status = #{status}</if>
        </where>
        order by temp.create_time desc
    </select>

    <select id="selectCurrentThemeComplateList" parameterType="RtClassMettingTheme" resultMap="RtClassMettingThemeResult">
        select distinct cm_id, cm_no, cm_theme, cm_text_theme,cm_title, cm_isupload, cm_prop, cm_start, cm_end, status,
        del_flag, create_by, create_time, update_by, update_time, remark,is_complate
        from (
        select cm.*,mc.cr_teacher_no,case when mc.cr_no is NULL then '0' else '1' end as is_complate
        from rt_class_metting_theme cm
        left join rt_class_metting_content mc on cm.cm_no=mc.cr_cm_no and mc.cr_class_no in (
          select class_id from srs_class sc 
          left join cph_teacher ct on sc.teacher_id = ct.teacher_id 
          where ct.employee_id = #{userName}
        ) ) temp
        <where>
            <!--<if test="userName != null  and userName != ''"> temp.rc_teacher_no= #{userName}</if>-->
            <if test="isComplate != null  and isComplate != ''"> temp.is_complate=#{isComplate}</if>
            <if test="cmNo != null  and cmNo != ''"> and temp.cm_no = #{cmNo}</if>
            <if test="cmTextTheme != null  and cmTextTheme != ''"> and temp.cm_text_theme like concat('%',#{cmTextTheme},'%')</if>
            <if test="cmTheme != null  and cmTheme != ''"> and temp.cm_theme like concat('%',#{cmTheme},'%')</if>
            <if test="cmTitle != null  and cmTitle != ''"> and temp.cm_title like concat('%',#{cmTitle},'%') </if>
            <if test="cmIsupload != null  and cmIsupload != ''"> and temp.cm_isupload = #{cmIsupload}</if>
            <if test="cmProp != null  and cmProp != ''"> and temp.cm_prop = #{cmProp}</if>
            <if test="cmStart != null "> and temp.cm_start = #{cmStart}</if>
            <if test="cmEnd != null "> and temp.cm_end = #{cmEnd}</if>
            <if test="status != null  and status != ''"> and temp.status = #{status}</if>
        </where>
        order by temp.create_time desc
    </select>


    <select id="selectSubmit" resultType="ClassMettingThemeSumitListVo" parameterType="RtClassMeetingClass">
        select
            (
                select t1.cm_title
                from rt_class_metting_theme as t1 where t1.cm_no = #{cmNo}
            ) as cm_theme,g.dept_name as college_name,f.major_name,a.class_name,e.`name` as teacher_name,e.employee_id as teacher_no,g.dept_id,
            case
                when cr_id is null then "未提交"
                else "已提交"
                end as is_complate
        from rt_class_meeting_class as t1
                 left join srs_class as a on a.class_id = t1.class_id and t1.cmt_id = #{cmtId}
                 left join srs_grade as b on a.grade_id = b.grade_id
                 left join rt_class_metting_content as c on a.class_id = c.cr_class_no and c.cr_cm_no = #{cmNo}
                 left join cph_teacher as e on a.teacher_id = e.teacher_id
                 left join srs_majors as f on a.major_id = f.major_id
                 left join sys_dept as g on f.college_id = g.dept_id
        where b.grade_status in (3,1)
        order by college_name,class_name
    </select>


    <select id="selectRtClassMettingThemeByCmId" parameterType="Long" resultMap="RtClassMettingThemeResult">
        <include refid="selectRtClassMettingThemeVo"/>
        where cm_id = #{cmId}
    </select>
        
    <insert id="insertRtClassMettingTheme" parameterType="RtClassMettingTheme" useGeneratedKeys="true" keyProperty="cmId">
        insert into rt_class_metting_theme
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="cmNo != null">cm_no,</if>
            <if test="cmTheme != null">cm_theme,</if>
            <if test="cmTextTheme != null">cm_text_theme,</if>
            <if test="cmTitle != null">cm_title,</if>
            <if test="cmIsupload != null">cm_isupload,</if>
            <if test="cmProp != null">cm_prop,</if>
            <if test="cmStart != null">cm_start,</if>
            <if test="cmEnd != null">cm_end,</if>
            <if test="status != null">status,</if>
            <if test="delFlag != null">del_flag,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="remark != null">remark,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="cmNo != null">#{cmNo},</if>
            <if test="cmTheme != null">#{cmTheme},</if>
            <if test="cmTextTheme != null">#{cmTextTheme},</if>
            <if test="cmTitle != null">#{cmTitle},</if>
            <if test="cmIsupload != null">#{cmIsupload},</if>
            <if test="cmProp != null">#{cmProp},</if>
            <if test="cmStart != null">#{cmStart},</if>
            <if test="cmEnd != null">#{cmEnd},</if>
            <if test="status != null">#{status},</if>
            <if test="delFlag != null">#{delFlag},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="remark != null">#{remark},</if>
         </trim>
    </insert>

    <update id="updateRtClassMettingTheme" parameterType="RtClassMettingTheme">
        update rt_class_metting_theme
        <trim prefix="SET" suffixOverrides=",">
            <if test="cmNo != null">cm_no = #{cmNo},</if>
            <if test="cmTheme != null">cm_theme = #{cmTheme},</if>
            <if test="cmTextTheme != null">cm_text_theme = #{cmTextTheme},</if>
            <if test="cmTitle != null">cm_title = #{cmTitle},</if>
            <if test="cmIsupload != null">cm_isupload = #{cmIsupload},</if>
            <if test="cmProp != null">cm_prop = #{cmProp},</if>
            <if test="cmStart != null">cm_start = #{cmStart},</if>
            <if test="cmEnd != null">cm_end = #{cmEnd},</if>
            <if test="status != null">status = #{status},</if>
            <if test="delFlag != null">del_flag = #{delFlag},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="remark != null">remark = #{remark},</if>
        </trim>
        where cm_id = #{cmId}
    </update>

    <delete id="deleteRtClassMettingThemeByCmId" parameterType="Long">
        delete from rt_class_metting_theme where cm_id = #{cmId}
    </delete>

    <delete id="deleteRtClassMettingThemeByCmIds" parameterType="String">
        delete from rt_class_metting_theme where cm_id in 
        <foreach item="cmId" collection="array" open="(" separator="," close=")">
            #{cmId}
        </foreach>
    </delete>


    <select id="selectClassFile" parameterType="String" resultType="map">
        select c.class_name,b.res_path,e.dept_id
        from rt_class_metting_content as a
                 left join srs_attach_resource as b on a.cr_no = b.res_main_no
                 left join srs_class as c on a.cr_class_no = c.class_id
        left join srs_majors as d on c.major_id = d.major_id
        left join sys_dept as e on d.college_id = e.dept_id
        where a.cr_cm_no = #{cmNo}
    </select>

    <select id="listClassByMajorId" parameterType="RtClassMettingTheme" resultType="map">
        select a.*,cr_id,e.`name` as teacher_name
        from rt_class_meeting_class as t1
                 left join srs_class as a on a.class_id = t1.class_id and t1.cmt_id = #{cmtId}
                 left join srs_grade as b on a.grade_id = b.grade_id
                 left join rt_class_metting_content as c on a.class_id = c.cr_class_no and c.cr_cm_no = #{cmNo}
                 left join cph_teacher as e on a.teacher_id = e.teacher_id
        where a.major_id = #{majorId} and b.grade_status in (3,1)
    </select>

    <select id="listClassCmNo" parameterType="RtClassMeetingClass" resultType="map">
        select a.*,cr_id,e.`name` as teacher_name,f.major_name,f.college_id,g.dept_name
        from rt_class_meeting_class as t1
                 left join srs_class as a on a.class_id = t1.class_id and t1.cmt_id = #{cmtId}
                 left join srs_grade as b on a.grade_id = b.grade_id
                 left join rt_class_metting_content as c on a.class_id = c.cr_class_no and c.cr_cm_no = #{cmNo}
                 left join cph_teacher as e on a.teacher_id = e.teacher_id
                 left join srs_majors as f on a.major_id = f.major_id
                 left join sys_dept as g on f.college_id = g.dept_id
        where b.grade_status in (3,1)
        order by class_name
    </select>

</mapper>