<?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.user.mapper.UserPlanMapper">

    <resultMap type="com.zhongzheng.modules.user.domain.UserPlan" id="UserPlanResult">
        <result property="userId" column="user_id"/>
        <result property="schedule" column="schedule"/>
        <result property="examDate" column="exam_date"/>
        <result property="reminderTime" column="reminder_time"/>
        <result property="studyCount" column="study_count"/>
        <result property="studyDay" column="study_day"/>
        <result property="endTime" column="end_time"/>
        <result property="startTime" column="start_time"/>
        <result property="createTime" column="create_time"/>
        <result property="updateTime" column="update_time"/>
        <result property="status" column="status"/>
        <result property="pitchNum" column="pitch_num"/>
        <result property="studyNum" column="study_num"/>
        <result property="planId" column="plan_id"/>
    </resultMap>

    <resultMap type="com.zhongzheng.modules.user.vo.CoursePlanVo" id="CourseResult">
        <result property="courseId" column="course_id"/>
        <result property="categoryId" column="category_id"/>
        <result property="price" column="price"/>
        <result property="duration" column="duration"/>
        <result property="startTime" column="start_time"/>
        <result property="endTime" column="end_time"/>
        <result property="coverUrl" column="cover_url"/>
        <result property="introduction" column="introduction"/>
        <result property="status" column="status"/>
        <result property="teacherIds" column="teacher_ids"/>
        <result property="courseName" column="course_name"/>
        <result property="chapterNum" column="chapter_num"/>
        <result property="sectionNum" column="section_num"/>
        <result property="categoryName" column="category_name"/>
    </resultMap>


    <resultMap type="com.zhongzheng.modules.exam.vo.ExamArrangementTimeVo" id="ExamArrangementTimeResult">
        <result property="arrangementId" column="arrangement_id"/>
        <result property="startTime" column="start_time"/>
        <result property="endTime" column="end_time"/>
    </resultMap>

    <select id="selectByCourse" parameterType="com.zhongzheng.modules.user.bo.UserPlanQueryBo"  resultMap="CourseResult">
        SELECT
        rs.course_id,
        rs.category_id,
        rs.price,
        rs.duration,
        rs.start_time,
        rs.end_time,
        rs.cover_url,
        rs.introduction,
        rs.create_time,
        rs.update_time,
        rs.STATUS,
        rs.teacher_ids,
        rs.course_name,
        sum( rs.section_num ) AS section_num,
        count( rs.chapter_id ) AS chapter_num,
        mc.category_name,
        mc.category_id,
        sum( rs.total_time ) AS duration
        FROM
        (
        SELECT
        c.*,
        count( cs.section_id ) AS section_num,
        cs.chapter_id,
        IFNULL( sum( cs.video_time ), 0 ) AS total_time
        FROM
        course c
        LEFT JOIN course_chapter cc ON c.course_id = cc.course_id
        LEFT JOIN course_section cs ON cc.chapter_id = cs.chapter_id
        WHERE
        1 = 1
        <if test="courseId != null and courseId !=''">
            and c.course_id in
            <foreach collection="courseId" open="(" separator="," close=")" item="courseId">
                #{courseId}
            </foreach>
        </if>
        GROUP BY
        cs.chapter_id,
        c.course_id
        ) rs
        LEFT JOIN major_category mc ON rs.category_id = mc.category_id
        GROUP BY
        course_id
        LIMIT 1
    </select>

    <select id="selectByExam" parameterType="Long"  resultMap="ExamArrangementTimeResult">
        SELECT
            e.*
        FROM
            exam_arrangement s
                LEFT JOIN exam_arrangement_time e ON e.arrangement_id = s.id
        WHERE 1=1
          AND s.status != 0 AND
        <if test="categoryId != null and categoryId !='' ">
          s.exam_type_id = #{categoryId}
        </if>
        ORDER BY e.start_time LIMIT 1
    </select>


    <select id="selectByListCourse" parameterType="com.zhongzheng.modules.user.bo.UserPlanQueryBo"  resultMap="CourseResult">
        SELECT
        rs.course_id,
        rs.price,
        rs.start_time,
        rs.end_time,
        rs.cover_url,
        rs.create_time,
        rs.update_time,
        rs.STATUS,
        rs.teacher_ids,
        rs.course_name,
        sum( section_num ) AS section_num,
        count( chapter_id ) AS chapter_num,
        mc.category_name,
        mc.category_id,
        sum( total_time ) AS duration
        FROM
        (
        SELECT
        c.*,
        count( cs.section_id ) AS section_num,
        cs.chapter_id,
        IFNULL( sum( cs.video_time ), 0 ) AS total_time
        FROM
        course c
        LEFT JOIN course_chapter cc ON c.course_id = cc.course_id
        LEFT JOIN course_section cs ON cc.chapter_id = cs.chapter_id
        WHERE
        1 = 1
        <if test="courseId != null and courseId !=''">
            and c.course_id in
            <foreach collection="courseId" open="(" separator="," close=")" item="courseId">
                #{courseId}
            </foreach>
        </if>
        GROUP BY cs.chapter_id,c.course_id)rs LEFT JOIN major_category mc on rs.category_id = mc.category_id
        GROUP BY course_id
    </select>

    <select id="selectByUserPlan" parameterType="com.zhongzheng.modules.user.bo.UserPlanQueryBo"  resultMap="UserPlanResult">
        SELECT * FROM user_plan where 1=1 AND `status` =1
                                  AND user_id = #{userId}
        <if test="planId != null and planId !=''">
            and plan_id = #{planId}
        </if>
        order by exam_date
    </select>

    <insert id="insertSource" parameterType="map" useGeneratedKeys="true">
        insert into user_plan_cource (
        plan_id,
        source_id
        )values

            (#{planId},
            #{courseId})

    </insert>
    <resultMap type="com.zhongzheng.modules.user.domain.UserPlanCource" id="UserPlanCourceResult">
        <result property="planId" column="plan_id"/>
        <result property="sourceId" column="source_id"/>
    </resultMap>
    <select id="selectCourse" parameterType="Long"  resultMap="UserPlanCourceResult">
        SELECT * FROM user_plan_cource where 1=1 AND plan_id=#{planId}
    </select>


    <select id="selectCountCourse" parameterType="com.zhongzheng.modules.user.bo.UserPlanQueryBo"  resultType="int">
        SELECT COUNT(c.source_id)
        FROM user_plan_cource c LEFT JOIN user_plan u ON u.plan_id = c.plan_id
        WHERE 1=1 and u.status=1
        <if test="courseId != null and courseId !=''">
            and c.source_id in
            <foreach collection="courseId" open="(" separator="," close=")" item="courseId">
                #{courseId}
            </foreach>
        </if>
        <if test="userId != null and userId !=''">
            and u.user_id = #{userId}
        </if>
    </select>


    <select id="selectByCourseId" parameterType="map"  resultType="long">
        SELECT
        s.section_id
        FROM
        course_section s
        LEFT JOIN course_chapter r ON  s.chapter_id = r.chapter_id
        LEFT JOIN course c ON c.course_id = r.course_id
        WHERE
        1 = 1
        <if test="courseId != null and courseId !=''">
            AND c.course_id =#{courseId}
        </if>
        ORDER BY r.sort,s.sort ASC
    </select>
</mapper>