<?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.zhongzheng.modules.grade.mapper.ClassGradeUserMapper">

    <resultMap type="com.zhongzheng.modules.grade.domain.ClassGradeUser" id="ClassGradeUserResult">
        <result property="id" column="id"/>
        <result property="userId" column="user_id"/>
        <result property="gradeId" column="grade_id"/>
        <result property="status" column="status"/>
        <result property="createTime" column="create_time"/>
        <result property="updateTime" column="update_time"/>
        <result property="fromType" column="from_type"/>
        <result property="orderGoodsId" column="order_goods_id"/>
        <result property="periodWaitTime" column="period_wait_time"/>
        <result property="changeGrade" column="change_grade"/>
        <result property="periodTime" column="period_time"/>
    </resultMap>

    <resultMap type="com.zhongzheng.modules.grade.vo.ClassGradeUserVo" id="ClassGradeUserVoResult">
        <result property="id" column="id"/>
        <result property="userId" column="user_id"/>
        <result property="gradeId" column="grade_id"/>
        <result property="className" column="class_name"/>
        <result property="status" column="status"/>
        <result property="code" column="user_account"/>
        <result property="realName" column="realname"/>
        <result property="idCard" column="id_card"/>
        <result property="createBy" column="create_by"/>
        <result property="telPhone" column="telphone"/>
        <result property="changeGrade" column="change_grade"/>
    </resultMap>


    <resultMap type="com.zhongzheng.modules.grade.vo.ClassGradeUserGoodsVo" id="ClassGradeUserGoodsVoResult">
        <result property="id" column="id"/>
        <result property="userId" column="user_id"/>
        <result property="gradeId" column="grade_id"/>
        <result property="status" column="status"/>
        <result property="goodsCode" column="goods_code"/>
        <result property="goodsName" column="goods_name"/>
        <result property="gradeStatus" column="grade_status"/>
        <result property="gradeCode" column="grade_code"/>
        <result property="gradeName" column="grade_name"/>
        <result property="year" column="year"/>
        <result property="educationName" column="education_name"/>
        <result property="projectName" column="project_name"/>
        <result property="businessName" column="business_name"/>
        <result property="schoolName" column="school_name"/>
        <result property="createTime" column="create_time"/>
        <result property="createBy" column="create_by"/>
        <result property="goodsId" column="goods_id"/>
        <result property="classStartTime" column="class_start_time"/>
        <result property="classEndTime" column="class_end_time"/>
    </resultMap>

    <resultMap type="com.zhongzheng.modules.grade.vo.ClassPeriodStudentVo" id="ClassPeriodStudentVo">
        <result property="userId" column="user_id"/>
        <result property="gradeId" column="grade_id"/>
        <result property="studentCode" column="user_account"/>
        <result property="realName" column="realname"/>
        <result property="idCard" column="id_card"/>
        <result property="telPhone" column="telphone"/>
        <result property="status" column="status"/>
        <result property="classHours" column="class_hours"/>
        <result property="periodStatus" column="period_status"/>
        <result property="secAllNum" column="sec_all_num"/>
        <result property="examNum" column="exam_num"/>
        <result property="recordNum" column="record_num"/>
        <result property="periodPlush" column="period_plush"/>
        <result property="startTime" column="start_time"/>
        <result property="endTime" column="end_time"/>
        <result property="studyStartTime" column="study_start_time"/>
        <result property="studyEndTime" column="study_end_time"/>
        <result property="rebuildNum" column="rebuild_num"/>
        <result property="profileStatus" column="profile_status"/>
        <result property="className" column="class_name"/>
        <result property="goodsName" column="goods_name"/>
        <result property="standPrice" column="stand_price"/>
        <result property="goodsCode" column="goods_code"/>
        <result property="keyValue" column="key_value"/>
        <result property="classStartTime" column="class_start_time"/>
        <result property="classEndTime" column="class_end_time"/>
    </resultMap>

    <resultMap type="com.zhongzheng.modules.grade.vo.ClassPeriodUserVo" id="ClassPeriodUserVo">
        <result property="userId" column="user_id"/>
        <result property="studentCode" column="user_account"/>
        <result property="realName" column="realname"/>
        <result property="oneInchPhotos" column="one_inch_photos"/>
        <result property="idCardImg1" column="id_card_img1"/>
        <result property="idCardImg2" column="id_card_img2"/>
        <result property="idCard" column="id_card"/>
        <result property="telPhone" column="telphone"/>
        <result property="secAllNum" column="sec_all_num"/>
        <result property="examNum" column="exam_num"/>
        <result property="recordNum" column="record_num"/>
        <result property="pass" column="pass"/>
        <result property="cheat" column="cheat"/>
        <result property="pending" column="pending"/>
        <result property="examPass" column="exam_pass"/>
        <result property="examPending" column="exam_pending"/>
        <result property="examCheat" column="exam_cheat"/>
        <result property="keyValue" column="key_value"/>
    </resultMap>

    <resultMap type="com.zhongzheng.modules.grade.vo.ClassPeriodVo" id="ClassPeriodVo">
        <result property="userId" column="user_id"/>
        <result property="id" column="id"/>
        <result property="typeName" column="type_name"/>
        <result property="realName" column="realname"/>
        <result property="classHours" column="class_hours"/>
        <result property="studyStartTime" column="study_start_time"/>
        <result property="status" column="status"/>
        <result property="auditTime" column="audit_time"/>
        <result property="type" column="type"/>
        <result property="courseId" column="course_id"/>
        <result property="studyEndTime" column="study_end_time"/>
        <result property="durationTime" column="duration_time"/>
    </resultMap>

    <resultMap type="com.zhongzheng.modules.grade.vo.ClassPeriodChapterVo" id="ClassPeriodChapterVo">
        <result property="userId" column="user_id"/>
        <result property="id" column="id"/>
        <result property="courseId" column="course_id"/>
        <result property="typeName" column="type_name"/>
        <result property="realName" column="realname"/>
        <result property="classHours" column="class_hours"/>
        <result property="studyStartTime" column="study_start_time"/>
        <result property="status" column="status"/>
        <result property="auditTime" column="audit_time"/>
        <result property="type" column="type"/>
        <result property="studyEndTime" column="study_end_time"/>
        <result property="performance" column="performance"/>
    </resultMap>

    <resultMap type="com.zhongzheng.modules.grade.vo.ClassPeriodSectionVo" id="ClassPeriodSectionVo">
        <result property="userId" column="user_id"/>
        <result property="id" column="id"/>
        <result property="typeName" column="type_name"/>
        <result property="realName" column="realname"/>
        <result property="classHours" column="class_hours"/>
        <result property="durationTime" column="duration_time"/>
        <result property="studyStartTime" column="study_start_time"/>
        <result property="status" column="status"/>
        <result property="auditTime" column="audit_time"/>
        <result property="type" column="type"/>
        <result property="studyEndTime" column="study_end_time"/>
        <result property="auditNum" column="audit_num"/>
    </resultMap>

    <resultMap type="com.zhongzheng.modules.course.vo.CourseChapterVo" id="CourseChapterResultVo">
        <result property="chapterId" column="chapter_id"/>
        <result property="name" column="name"/>
    </resultMap>

    <select id="selectUser" parameterType="com.zhongzheng.modules.grade.bo.ClassGradeUserQueryBo"
            resultMap="ClassGradeUserVoResult">
        SELECT
        g.*,
        u.user_account,
        u.realname,
        u.id_card,
        u.telphone,
        cg.class_name
        FROM
        class_grade_user g
        LEFT JOIN `user` u ON g.user_id = u.user_id
        LEFT JOIN class_grade cg ON cg.grade_id = g.grade_id
        WHERE
        1 = 1

        AND g.grade_id = #{gradeId}
        AND g.user_id = #{userId}

    </select>

    <select id="select" parameterType="com.zhongzheng.modules.grade.bo.ClassGradeUserQueryBo"
            resultMap="ClassGradeUserVoResult">
        SELECT
        g.*,
        u.user_account,
        u.realname,
        u.id_card,
        u.telphone
        FROM
        class_grade_user g
        LEFT JOIN `user` u ON g.user_id = u.user_id
        WHERE
        1 = 1
        <if test="status != null and status.size()!=0 ">
            AND g.status in
            <foreach collection="status" item="item" index="index" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
        <if test="gradeId != null and gradeId != ''">
            AND g.grade_id = #{gradeId}
        </if>
        <if test="userId != null and userId != ''">
            AND g.user_id = #{userId}
        </if>
        <if test="classStartTime != null or classEndTime != null ">
            AND #{classStartTime} BETWEEN g.class_start_time
        </if>
        order by g.update_time desc
    </select>

    <select id="listUser" parameterType="com.zhongzheng.modules.grade.bo.ClassGradeUserQueryBo"
            resultMap="ClassGradeUserGoodsVoResult">
        SELECT
        * ,
        (select `code` FROM goods g LEFT JOIN class_grade_goods c on g.goods_id = c.goods_id where c.grade_id =
        u.grade_id) as goods_code,
        (select `year` FROM goods g LEFT JOIN class_grade_goods c on g.goods_id = c.goods_id where c.grade_id =
        u.grade_id) as year,
        (select goods_name FROM goods g LEFT JOIN class_grade_goods c on g.goods_id = c.goods_id where c.grade_id =
        u.grade_id) as goods_name,
        (select b.business_name FROM course_business b LEFT JOIN goods g on b.id = g.business_id LEFT JOIN
        class_grade_goods c on g.goods_id = c.goods_id where c.grade_id = u.grade_id) as business_name,
        (select b.project_name FROM course_project_type b LEFT JOIN goods g on b.id = g.project_id LEFT JOIN
        class_grade_goods c on g.goods_id = c.goods_id where c.grade_id = u.grade_id) as project_name,
        (select b.school_name FROM school b LEFT JOIN goods g on b.id = g.school_id LEFT JOIN class_grade_goods c on
        g.goods_id = c.goods_id where c.grade_id = u.grade_id) as school_name,
        (select b.education_name FROM course_education_type b LEFT JOIN goods g on b.id = g.education_type_id LEFT JOIN
        class_grade_goods c on g.goods_id = c.goods_id where c.grade_id = u.grade_id) as education_name,
        (SELECT g.grade_code FROM class_grade g where u.grade_id = g.grade_id) as grade_code,
        (SELECT g.class_name FROM class_grade g where u.grade_id = g.grade_id) as grade_name,
        (SELECT g.status FROM class_grade g where u.grade_id = g.grade_id) as grade_status
        FROM
        class_grade_user u
        where 1=1
        <if test="status != null and status.size()!=0 ">
            AND u.status in
            <foreach collection="status" item="item" index="index" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
        <if test="userId != null and userId != ''">
            and u.user_id= #{userId}
        </if>
        order by u.update_time desc
    </select>


    <select id="listUserPeriod" parameterType="com.zhongzheng.modules.grade.bo.ClassGradeUserQueryBo"
            resultMap="ClassPeriodStudentVo">
        SELECT
        u.user_account,
        u.user_id,
        cgu.`status`,
        u.realname,
        cgg.goods_id,
        u.id_card,
        cgu.grade_id,
        u.telphone,
        up.`status` as profile_status,
        g.class_hours as class_hours,
        g.study_start_time as study_start_time,
        g.study_end_time as study_end_time,
        g.goods_name,
        g.code as goods_code,
        g.stand_price,
        cgu.period_status,
        cg.class_start_time,
        cg.class_end_time,
        cg.class_name,
        cgu.period_plush,
        IFNULL(ge.exam_num,0) as exam_num,
        <if test="userPhoto != null and userPhoto == 1">
        up.key_value,
        </if>
        cgu.period_time as end_time,
        (SELECT COUNT(DISTINCT ubr.module_id,ubr.chapter_id,ubr.exam_id) FROM user_bank_record ubr  where ubr.`status`=1 and ubr.report_status=1 and ubr.grade_id = cgu.grade_id and ubr.user_id = cgu.user_id and ubr.current_status = 1) as record_num
        <if test="userPhoto == null">
      ,(select COUNT(up.id) from user_period up LEFT JOIN user_period_status ups on up.id=ups.period_id where up.goods_id = (SELECT cgg.goods_id FROM class_grade_goods cgg where cg.grade_id=cgg.grade_id) and up.grade_id = cgu.grade_id and up.user_id = u.user_id
        and ups.period_status=0 and ups.`status`=0 and (SELECT COUNT(upss.id) from user_period_status upss where upss.id = ups.id and upss.period_status = 1 and upss.`status` = 2) > 0 ) as rebuild_num
        </if>
        FROM
        class_grade_user cgu
        LEFT JOIN class_grade cg ON cgu.grade_id = cg.grade_id
        LEFT JOIN class_grade_goods cgg on cg.grade_id=cgg.grade_id
        LEFT JOIN (SELECT
        COUNT( m.id ) AS exam_num,
        c.goods_id
        FROM
        course_menu_exam m
        LEFT JOIN goods_course c ON m.course_id = c.course_id
        where
        m.type = 1
        GROUP BY c.goods_id ) ge on cgg.goods_id = ge.goods_id
        LEFT JOIN `user` u ON u.user_id = cgu.user_id
        LEFT JOIN user_profile up on u.user_id =up.user_id and up.goods_id = cgg.goods_id and up.type_status=1 and up.current_status = 1
        LEFT JOIN goods g on cgg.goods_id = g.goods_id
        where 1=1
        and cgu.`status` =1
        <if test="changeGrade != null and changeGrade != ''">
            AND cgu.change_grade = #{changeGrade}
        </if>
        <if test="profileStatus != null and profileStatus != ''">
            AND up.status = #{profileStatus}
        </if>
        <if test="gradeId != null and gradeId !='' ">
            and cgu.grade_id = #{gradeId}
        </if>
        <if test="businessId != null and businessId != ''">
            AND g.business_id = #{businessId}
        </if>
        <if test="schoolId != null and schoolId != ''">
            AND g.school_id = #{schoolId}
        </if>
        <if test="majorId != null and majorId != ''">
            AND g.major_id = #{majorId}
        </if>
        <if test="periodStatus != null ">
            and cgu.period_status = #{periodStatus}
        </if>
        <if test="classStartTime != null and classStartTime != '' ">
            AND cg.class_start_time > #{classStartTime}
        </if>
        <if test="classEndTime != null and classEndTime != '' ">
            AND #{classEndTime} >  cg.class_end_time
        </if>
        <if test="periodStatus == 2 ">
            order by cgu.period_wait_time,cgu.update_time desc
        </if>
        <if test="studyStatus != null and studyStatus == 1 ">
            and cgu.period_status = -1
        </if>
        <if test="studyStatus != null and studyStatus == 2 ">
            and cgu.period_status != -1
        </if>
        <if test="periodStatus != 2 ">
            order by cgu.update_time desc
        </if>
    </select>

    <select id="getUserPeriodStatus" parameterType="com.zhongzheng.modules.grade.bo.ClassGradeUserQueryBo"
            resultMap="ClassPeriodStudentVo">
        SELECT
        IFNULL(ge.exam_num,0) as exam_num,
        cg.class_start_time,
        cg.class_end_time,
        g.study_start_time,
        g.study_end_time,
        (SELECT COUNT(DISTINCT ubr.module_id,ubr.chapter_id,ubr.exam_id) FROM user_bank_record ubr LEFT JOIN class_grade_goods cgg on cgg.goods_id
        = ubr.goods_id where ubr.`status`=1 and ubr.report_status=1 and ubr.grade_id = cg.grade_id and ubr.user_id = cgu.user_id and ubr.current_status = 1) as record_num,
        cgu.user_id,
        cgg.goods_id
        FROM
        class_grade_user cgu
        LEFT JOIN class_grade cg ON cgu.grade_id = cg.grade_id
        LEFT JOIN class_grade_goods cgg on cg.grade_id=cgg.grade_id
        LEFT JOIN goods g on g.goods_id=cgg.goods_id
        LEFT JOIN (SELECT
        COUNT( m.id ) AS exam_num,
        c.goods_id
        FROM
        course_menu_exam m
        LEFT JOIN goods_course c ON m.course_id = c.course_id
        where
        m.type = 1
        GROUP BY c.goods_id ) ge on cgg.goods_id = ge.goods_id
        where 1=1
        and cgu.`status` =1

        <if test="gradeId != null and gradeId !='' ">
            and cgu.grade_id = #{gradeId}
        </if>

        <if test="userId != null ">
            and cgu.user_id = #{userId}
        </if>
        LIMIT 1
    </select>

    <select id="listPeriod" parameterType="com.zhongzheng.modules.grade.bo.ClassGradeUserQueryBo"
            resultMap="ClassPeriodUserVo">
        SELECT
        u.user_account,
        u.realname,
        u.id_card,
        u.user_id,
        u.telphone,
        (SELECT cgg.goods_id FROM class_grade_goods cgg where cg.grade_id=cgg.grade_id) as goods_id,
        u.one_inch_photos,
        u.id_card_img1,
        u.id_card_img2,
        (SELECT COUNT(m.id) FROM course_menu_exam m LEFT JOIN goods_course c on m.course_id=c.course_id LEFT JOIN
        class_grade_goods cgg on cgg.goods_id = c.goods_id where cg.grade_id=cgg.grade_id and m.type = 1 ) as exam_num,
        (SELECT COUNT(DISTINCT ubr.module_id,ubr.chapter_id, ubr.exam_id) FROM user_bank_record ubr  where ubr.`status`=1 and ubr.report_status =1 and ubr.user_id=#{userId} and ubr.grade_id=cgu.grade_id and ubr.current_status=1) as record_num,
        (SELECT COUNT(up.id) FROM user_period up LEFT JOIN user_period_status ups on up.id = ups.period_id where
        ups.`status` =1 and ups.period_status = 1 and up.goods_id=#{goodsId} and up.user_id = #{userId} and up.grade_id = cgu.grade_id and up.type = 1) as pass,
        (SELECT COUNT(up.id) FROM user_period up LEFT JOIN user_period_status ups on up.id = ups.period_id where
        ups.`status` =2 and up.grade_id = cg.grade_id  and ups.period_status = 1 and up.goods_id=#{goodsId} and up.grade_id = cg.grade_id and up.user_id = #{userId} and up.type = 1) as pending,
       (SELECT
        COUNT(up.id) FROM user_period up LEFT JOIN user_period_status ups on up.id = ups.period_id where ups.`status`
        =0 and ups.period_status = 1 and up.goods_id=#{goodsId} and up.grade_id = cg.grade_id and  up.user_id = #{userId} and up.type = 1) as cheat,
        (SELECT COUNT(up.id) FROM user_period up LEFT JOIN user_period_status ups on up.id = ups.period_id where
        ups.`status` =1 and ups.period_status = 1 and up.goods_id=#{goodsId} and up.user_id = #{userId} and up.grade_id = cg.grade_id and up.type = 2) as exam_pass,
        (SELECT
        COUNT(up.id) FROM user_period up LEFT JOIN user_period_status ups on up.id = ups.period_id where ups.`status`
        =0 and ups.period_status = 1 and up.goods_id=#{goodsId} and up.grade_id = cg.grade_id and up.user_id = #{userId} and up.type = 2) as exam_cheat,
        (SELECT
        COUNT(up.id) FROM user_period up LEFT JOIN user_period_status ups on up.id = ups.period_id where ups.`status`
        =2 and ups.period_status = 1 and up.goods_id=#{goodsId} and up.grade_id = cg.grade_id and up.user_id = #{userId} and up.type = 2) as exam_pending,
        (SELECT up.key_value FROM user_profile up where up.goods_id = #{goodsId} and up.user_id =#{userId} and up.current_status = 1 and up.type_status =1 LIMIT 1) as key_value
        FROM
        class_grade_user cgu
        LEFT JOIN class_grade cg ON cgu.grade_id = cg.grade_id
        LEFT JOIN `user` u ON u.user_id = cgu.user_id
        where 1=1
        <if test="userId != null and userId !='' ">
            and u.user_id = #{userId}
        </if>
        <if test="gradeId != null and gradeId !='' ">
            and cgu.grade_id = #{gradeId}
        </if>
        <if test="periodStatus != null and periodStatus !='' ">
            and cgu.period_status = #{periodStatus}
        </if>
        LIMIT 1
    </select>

    <select id="listPeriodAudit" parameterType="com.zhongzheng.modules.grade.bo.ClassGradeUserQueryBo"
            resultMap="ClassPeriodVo">
        SELECT
        cm.menu_id as id,
        cm.course_id,
        (SELECT u.realname FROM `user` u where 1=1      <if test="userId != null and userId !='' ">
        and u.user_id = #{userId}
    </if>) as realname,

        (SELECT u.user_id FROM `user` u where 1=1     <if test="userId != null and userId !='' ">
        and u.user_id = #{userId}
    </if>) as user_id,
        (SELECT cm.module_name FROM course_module cm where cm.menu_id = cm.module_id) as type_name,
        (SELECT COUNT(ccs.section_id) FROM course_chapter_section ccs LEFT JOIN course_module_chapter cmc on
        cmc.chapter_id=ccs.chapter_id where cmc.module_id=cm.menu_id) as class_hours,
        1 as type,
        NULL as study_start_time,
        NULL as study_end_time
        FROM
        course_menu cm
        LEFT JOIN goods_course gc on gc.course_id = cm.course_id
        where 1=1
        <if test="goodsId != null and goodsId !='' ">
            and gc.goods_id=#{goodsId}
        </if>
        AND cm.type=1
        UNION
        SELECT
        cm.menu_id as id,
        cm.course_id,
        (SELECT u.realname FROM `user` u where 1=1  <if test="userId != null and userId !='' ">
        and u.user_id = #{userId}
    </if>) as realname,
        (SELECT u.user_id FROM `user` u where 1=1  <if test="userId != null and userId !='' ">
        and u.user_id = #{userId}
    </if>) as user_id,
        (SELECT cc.`name` FROM course_chapter cc where cm.menu_id = cc.chapter_id) as type_name,
        (SELECT COUNT(ccs.section_id) FROM course_chapter_section ccs where ccs.chapter_id=cm.menu_id) as class_hours,
        2 as type,
        (SELECT usr.create_time FROM user_study_record usr LEFT JOIN course_chapter_section ccs on usr.section_id =
        ccs.section_id and ccs.chapter_id = usr.chapter_id where ccs.chapter_id = cm.menu_id AND usr.current_status = 1 AND usr.user_id=#{userId} and usr.course_id = cm.course_id and usr.goods_id = gc.goods_id and usr.grade_id = #{gradeId} ORDER BY usr.create_time ASC
        limit 1) as study_start_time,
        (SELECT usr.update_time FROM user_study_record usr LEFT JOIN course_chapter_section ccs on usr.section_id =
        ccs.section_id and ccs.chapter_id = usr.chapter_id where ccs.chapter_id = cm.menu_id AND usr.current_status = 1 AND usr.user_id=#{userId} and usr.course_id = cm.course_id and usr.goods_id = gc.goods_id and usr.grade_id = #{gradeId} ORDER BY usr.create_time DESC
        limit 1) as study_end_time
        FROM
        course_menu cm
        LEFT JOIN goods_course gc on gc.course_id = cm.course_id
        where 1=1   <if test="goodsId != null and goodsId !='' ">
        and gc.goods_id=#{goodsId}
    </if>  AND cm.type=2
    </select>

    <select id="listPeriodAuditSection" parameterType="com.zhongzheng.modules.grade.bo.ClassGradeUserQueryBo"
            resultMap="ClassPeriodVo">
        SELECT
        cm.menu_id as id,
        cm.course_id,
        (SELECT u.realname FROM `user` u where 1=1
        <if test="userId != null and userId !='' ">
            and u.user_id = #{userId}
        </if>
        ) as realname,
        (SELECT u.user_id FROM `user` u where 1=1
        <if test="userId != null and userId !='' ">
            and u.user_id = #{userId}
        </if>
        ) as user_id,
        cs.`name` as type_name,
        cs.duration_time,
        null as class_hours,
        3 as type
        FROM
        course_menu cm
        LEFT JOIN course_section cs on cm.menu_id = cs.section_id
        LEFT JOIN goods_course gc on gc.course_id = cm.course_id
        where 1=1
        <if test="goodsId != null and goodsId !='' ">
            and gc.goods_id = #{goodsId}
        </if>
        AND cm.type=3
    </select>

    <select id="listPeriodAuditExam"  parameterType="map" resultMap="ClassPeriodChapterVo">
        SELECT
        cme.exam_id as id,
        cme.course_id,
        (SELECT u.realname FROM `user` u where 1=1
        <if test="userId != null and userId !='' ">
            and u.user_id = #{userId}
        </if>
        ) as realname,
        (SELECT u.user_id FROM `user` u where 1=1
        <if test="userId != null and userId !='' ">
            and u.user_id = #{userId}
        </if>
        ) as user_id,
        (SELECT e.exam_name FROM exam e where cme.exam_id = e.exam_id) as type_name,
        null as class_hours,
        4 as type,
        (
        SELECT
        ups.record_start_time
        FROM
        user_period_status ups
        LEFT JOIN user_period up ON up.id = ups.period_id
        WHERE
        period_status = 1
        AND up.course_id = gc.course_id
        <if test="goodsId != null and goodsId !='' ">
            AND up.goods_id = #{goodsId}
        </if>
        and cme.section_id =up.section_id
        AND up.exam_id = cme.exam_id

        ) AS study_start_time,
        (
        SELECT
        ups.record_end_time
        FROM
        user_period_status ups
        LEFT JOIN user_period up ON up.id = ups.period_id
        WHERE
        period_status = 1

        AND up.course_id = gc.course_id

        <if test="goodsId != null and goodsId !='' ">
            AND up.goods_id = #{goodsId}
        </if>
        AND up.exam_id = cme.exam_id
        ) AS study_end_time,
        (
        SELECT
        ups.`status`
        FROM
        user_period_status ups
        LEFT JOIN user_period up ON up.id = ups.period_id
        WHERE
        period_status = 1
        AND up.course_id = gc.course_id
        <if test="goodsId != null and goodsId !='' ">
            AND up.goods_id = #{goodsId}
        </if>
        and cme.section_id =up.section_id
        AND up.exam_id = cme.exam_id
        ) AS STATUS,
        (
        SELECT
        ups.create_time
        FROM
        user_period_status ups
        LEFT JOIN user_period up ON up.id = ups.period_id
        WHERE
        period_status = 1
        AND up.course_id = gc.course_id
        <if test="goodsId != null and goodsId !='' ">
            AND up.goods_id = #{goodsId}
        </if>
        and cme.section_id =up.section_id
        AND up.exam_id = cme.exam_id
        ) AS audit_time,
        (
        SELECT
        COUNT( ups.id )
        FROM
        user_period_status ups
        LEFT JOIN user_period up ON up.id = ups.period_id
        WHERE
        period_status = 1
        AND up.course_id = gc.course_id
        <if test="goodsId != null and goodsId !='' ">
            AND up.goods_id = #{goodsId}
        </if>
        and cme.section_id =up.section_id
        AND up.exam_id = cme.exam_id
        ) AS audit_num
        FROM
        course_menu_exam cme
        LEFT JOIN goods_course gc on gc.course_id = cme.course_id
        LEFT JOIN course_menu cm on cme.section_id = cm.menu_id
        where 1=1
        <if test="goodsId != null and goodsId !='' ">
            and gc.goods_id= #{goodsId}
        </if>
        AND cm.type=3 and cme.chapter_id=0 and cme.module_id=0 and cme.type=1 and cm.type=3
    </select>

    <select id="listperiodChapter" parameterType="map" resultMap="ClassPeriodChapterVo">
        SELECT
        cc.chapter_id as id,
        cm.course_id,
        ( SELECT u.realname FROM `user` u WHERE 1=1 <if test="userId != null and userId !='' ">
        and u.user_id = #{userId}
    </if>  LIMIT 1) AS realname,
        ( SELECT u.user_id FROM `user` u WHERE 1=1 <if test="userId != null and userId !='' ">
        and u.user_id = #{userId}
    </if> LIMIT 1) AS user_id,
        cc.`name` as type_name,
        ( SELECT COUNT( ccs.section_id ) FROM course_chapter_section ccs WHERE ccs.chapter_id = cc.chapter_id ) AS
        class_hours,
        2 AS type,
        (
        SELECT
        usr.create_time
        FROM
        user_study_record usr
        LEFT JOIN course_chapter_section ccs ON usr.section_id = ccs.section_id and ccs.chapter_id = usr.chapter_id
        WHERE
        ccs.chapter_id = cc.chapter_id
        AND usr.current_status = 1
        <if test="courseId != null and courseId !='' ">
            AND usr.course_id=#{courseId}
        </if>
        <if test="moduleId != null and moduleId !='' ">
            AND usr.module_id=#{moduleId}
        </if>
        <if test="goodsId != null and goodsId !='' ">
            AND usr.goods_id=#{goodsId}
        </if>
        <if test="gradeId != null and gradeId !='' ">
            AND usr.grade_id=#{gradeId}
        </if>
        <if test="userId != null and userId !='' ">
            AND usr.user_id=#{userId}
        </if>
        ORDER BY
        usr.create_time ASC
        LIMIT 1
        ) AS study_start_time,
        (
        SELECT
        usr.update_time
        FROM
        user_study_record usr
        LEFT JOIN course_chapter_section ccs ON usr.section_id = ccs.section_id and ccs.chapter_id = usr.chapter_id
        WHERE
        ccs.chapter_id = cc.chapter_id
        AND usr.current_status = 1
        <if test="courseId != null and courseId !='' ">
            AND usr.course_id=#{courseId}
        </if>
        <if test="moduleId != null and moduleId !='' ">
            AND usr.module_id=#{moduleId}
        </if>
        <if test="goodsId != null and goodsId !='' ">
            AND usr.goods_id=#{goodsId}
        </if>
        <if test="gradeId != null and gradeId !='' ">
            AND usr.grade_id=#{gradeId}
        </if>
        <if test="userId != null and userId !='' ">
            AND usr.user_id=#{userId}
        </if>
        ORDER BY
        usr.create_time DESC
        LIMIT 1
        ) AS study_end_time,
        (
        SELECT
        ubr.performance
        FROM
        user_bank_record ubr
        LEFT JOIN course_menu_exam cme ON cme.exam_id = ubr.exam_id and cme.chapter_id  = ubr.chapter_id
        WHERE 1=1
        AND ubr.current_status = 1
        <if test="moduleId != null and moduleId !='' ">
            AND ubr.module_id=#{moduleId}
        </if>
        <if test="goodsId != null and goodsId !='' ">
            AND ubr.goods_id=#{goodsId}
        </if>
        <if test="moduleId != null and moduleId !='' ">
            AND cme.module_id=#{moduleId}
        </if>
        <if test="courseId != null and courseId !='' ">
            AND cme.course_id =#{courseId}
        </if>
        <if test="userId != null and userId !='' ">
            AND ubr.user_id=#{userId}
        </if>
        AND cme.chapter_id = cmc.chapter_id
        LIMIT 1 ) AS performance
        FROM
        course_menu cm
        LEFT JOIN course_module_chapter cmc ON cm.menu_id = cmc.module_id
        LEFT JOIN course_chapter cc ON cmc.chapter_id = cc.chapter_id
        WHERE
        1 = 1
        <if test="moduleId != null and moduleId !='' ">
            AND cmc.module_id=#{moduleId}
        </if>
        <if test="courseId != null and courseId !='' ">
            AND cm.course_id =#{courseId}
        </if>
    </select>

    <select id="listperiodExam" parameterType="map" resultMap="ClassPeriodSectionVo">
        SELECT
        cme.exam_id as id,
        cme.course_id,
        (SELECT u.realname FROM `user` u where 1=1
        <if test="userId != null and userId !='' ">
            and u.user_id = #{userId}
        </if>
        ) as realname,
        (SELECT u.user_id FROM `user` u where 1=1
        <if test="userId != null and userId !='' ">
            and u.user_id = #{userId}
        </if>
        ) as user_id,
        (SELECT e.exam_name FROM exam e where cme.exam_id = e.exam_id) as type_name,
        null as class_hours,
        4 as type
        FROM
        course_menu_exam cme
        LEFT JOIN exam e ON cme.exam_id = e.exam_id
        WHERE 1=1
        <if test="courseId != null and courseId !='' ">
            AND  cme.course_id=#{courseId}
        </if>
        <if test="chapterId != null and chapterId !='' ">
            AND cme.chapter_id =#{chapterId}
        </if>
        <if test="moduleId != null and moduleId !='' ">
            AND cme.module_id =#{moduleId}
        </if>
    </select>


    <select id="listPeriodSection" parameterType="map" resultMap="ClassPeriodSectionVo">
        SELECT
        cs.section_id AS id,
        #{courseId} AS course_id,
        ( SELECT u.realname FROM `user` u WHERE 1 = 1 <if test="userId != null and userId !='' ">
        and u.user_id = #{userId}
    </if>  ) AS realname,
        ( SELECT u.user_id FROM `user` u WHERE 1 = 1 <if test="userId != null and userId !='' ">
        and u.user_id = #{userId}
    </if>  ) AS user_id,
        cs.NAME AS type_name,
        cs.duration_time,
        NULL AS class_hours,
        3 AS type
        FROM
        course_chapter_section ccs
        LEFT JOIN course_section cs ON ccs.section_id = cs.section_id
        WHERE
        1 = 1
        <if test="chapterId != null and chapterId !='' ">
            AND ccs.chapter_id = #{chapterId}
        </if>
    </select>


    <select id="listPeriodSectionExam" parameterType="map" resultMap="ClassPeriodSectionVo">
        SELECT
        cme.exam_id as id,
        cme.course_id,
        (SELECT u.realname FROM `user` u where 1=1
        <if test="userId != null and userId !='' ">
            and u.user_id = #{userId}
        </if>
        ) as realname,
        (SELECT u.user_id FROM `user` u where 1=1
        <if test="userId != null and userId !='' ">
            and u.user_id = #{userId}
        </if>
        ) as user_id,
        (SELECT e.exam_name FROM exam e where cme.exam_id = e.exam_id) as type_name,
        null as class_hours,
        4 as type
        FROM
        course_menu_exam cme
        LEFT JOIN exam e ON cme.exam_id = e.exam_id
        WHERE 1=1
        and cme.module_id =0
        <if test="courseId != null and courseId !='' ">
            AND  cme.course_id=#{courseId}
        </if>
        <if test="chapterId != null and chapterId !='' ">
            AND cme.chapter_id = #{chapterId}
        </if>
    </select>

    <select id="selectStart" parameterType="map" resultType="long">
        SELECT
            usr.create_time
        FROM
            user_study_record usr
        WHERE
            usr.goods_id = #{goodsId}
          AND usr.grade_id = #{gradeId}
          AND usr.user_id = #{userId} UNION
        SELECT
            ubr.create_time
        FROM
            user_bank_record ubr
        WHERE
            ubr.goods_id =#{goodsId}
          AND ubr.grade_id = #{gradeId}
          AND ubr.user_id = #{userId}
        ORDER BY create_time ASC
    </select>

    <select id="sendClassGradeUser"  resultMap="ClassGradeUserGoodsVoResult">
        SELECT
        cgu.user_id,
        cg.class_start_time,
        cg.class_end_time,
        cgg.goods_id,
        cg.grade_id
        FROM
        class_grade_user cgu
        LEFT JOIN class_grade cg on  cgu.grade_id = cg.grade_id
        LEFT JOIN class_grade_goods cgg on cg.grade_id = cgg.grade_id
        LEFT JOIN goods g on cgg.goods_id = g.goods_id
        LEFT JOIN inform_remind_business irb on  irb.business_id = g.business_id
        where 1=1
        and irb.remind_id =5
          and cgu.status = 1
        AND unix_timestamp(now()) BETWEEN cg.class_end_time-1728000 and cg.class_end_time-1641600
        AND (SELECT COUNT(1) FROM inform_user iu where 1=1 and iu.remind_id = 5 and cgu.user_id = iu.user_id and cgu.grade_id = iu.grade_id and iu.system_status=3) &lt; 1
    </select>


    <select id="sendTenClassGradeUser"  resultMap="ClassGradeUserGoodsVoResult">

        SELECT
        cgu.user_id,
        cg.class_start_time,
        cg.class_end_time,
        cgg.goods_id,
        cg.grade_id
        FROM
        class_grade_user cgu
        LEFT JOIN class_grade cg on  cgu.grade_id = cg.grade_id
        LEFT JOIN class_grade_goods cgg on cg.grade_id = cgg.grade_id
        LEFT JOIN goods g on cgg.goods_id = g.goods_id
        LEFT JOIN inform_remind_business irb on  irb.business_id = g.business_id
        where 1=1
        and irb.remind_id =6
          and cgu.status = 1
        AND unix_timestamp(now()) BETWEEN cg.class_end_time-864000 and cg.class_end_time-777600
        AND (SELECT COUNT(1) FROM inform_user iu where 1=1 and iu.remind_id = 6 and cgu.user_id = iu.user_id and cgu.grade_id = iu.grade_id and iu.system_status=3) &lt; 1
    </select>


    <select id="sendFiveClassGradeUser"  resultMap="ClassGradeUserGoodsVoResult">

        SELECT
        cgu.user_id,
        cg.class_start_time,
        cg.class_end_time,
        cgg.goods_id,
        cg.grade_id
        FROM
        class_grade_user cgu
        LEFT JOIN class_grade cg on  cgu.grade_id = cg.grade_id
        LEFT JOIN class_grade_goods cgg on cg.grade_id = cgg.grade_id
        LEFT JOIN goods g on cgg.goods_id = g.goods_id
        LEFT JOIN inform_remind_business irb on  irb.business_id = g.business_id
        where 1=1
        and irb.remind_id =7
          and cgu.status = 1
        AND unix_timestamp(now()) BETWEEN cg.class_end_time-432000 and cg.class_end_time-345600
        AND (SELECT COUNT(1) FROM inform_user iu where 1=1 and iu.remind_id = 7 and cgu.user_id = iu.user_id and cgu.grade_id = iu.grade_id and iu.system_status=3) &lt; 1
    </select>
    <select id="selectUserCount" parameterType="com.zhongzheng.modules.user.domain.User" resultType="long">
        SELECT
            COUNT( 1 )
        FROM
            `user`
        WHERE
            id_card = #{idCard} and user_id != #{userId} and `status` = 1
    </select>

    <select id="selectUserCertificate" parameterType="com.zhongzheng.modules.grade.bo.ClassGradeUserQueryBo"
            resultMap="ClassGradeUserVoResult">

    </select>

    <select id="findChapterList" parameterType="com.zhongzheng.modules.grade.bo.ClassGradeUserQueryBo" resultMap="CourseChapterResultVo">
        SELECT
            cc.chapter_id,
            cc.`name`
        FROM
            course_module_chapter p
                LEFT JOIN course_chapter cc ON p.chapter_id = cc.chapter_id
                LEFT JOIN course_menu m ON m.menu_id = p.module_id
                LEFT JOIN goods_course gc ON gc.course_id = m.course_id
        WHERE
            gc.goods_id = #{goodsId}
          AND m.type IN ( 1 ) UNION
        SELECT
            cc.chapter_id,
            cc.`name`
        FROM
            course_menu m
                LEFT JOIN course_chapter cc ON m.menu_id = cc.chapter_id
                LEFT JOIN goods_course gc ON gc.course_id = m.course_id
        WHERE
            gc.goods_id = #{goodsId}
          AND m.type IN ( 2 )
    </select>
</mapper>
