<?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.OrderPossessUserMapper">

    <resultMap type="com.zhongzheng.modules.user.domain.OrderPossessUser" id="OrderPossessUserResult">
        <result property="userId" column="user_id"/>
        <result property="sourceId" column="source_id"/>
        <result property="typeId" column="type_id"/>
        <result property="possessId" column="possess_id"/>
        <result property="createTime" column="create_time"/>
        <result property="updateTime" column="update_time"/>
    </resultMap>






    <resultMap type="com.zhongzheng.modules.course.domain.Course" 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="createTime" column="create_time"/>
        <result property="updateTime" column="update_time"/>
        <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>


    <select id="selectByCource" parameterType="com.zhongzheng.modules.user.bo.OrderPossessUserQueryBo"  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
        GROUP BY
        cs.chapter_id,
        c.course_id
        ) rs
        LEFT JOIN major_category mc ON rs.category_id = mc.category_id
        LEFT JOIN order_possess_user o ON o.possess_id = rs.course_id
        WHERE
        1 = 1
        AND o.type_id = 1
        <if test="userId != null and userId != ''">
            AND o.user_id = #{userId}
        </if>
        GROUP BY
        rs.course_id
    </select>

    <resultMap type="com.zhongzheng.modules.exam.domain.ExamNote" id="ExamNoteResult">
        <result property="fileId" column="file_id"/>
        <result property="categoryId" column="category_id"/>
        <result property="name" column="name"/>
        <result property="teacherIds" column="teacher_ids"/>
        <result property="price" column="price"/>
        <result property="coverUrl" column="cover_url"/>
        <result property="introduction" column="introduction"/>
        <result property="createTime" column="create_time"/>
        <result property="updateTime" column="update_time"/>
        <result property="status" column="status"/>
        <result property="fileUrls" column="file_urls"/>
        <result property="sort" column="sort"/>
        <result property="categoryName" column="category_name"/>
    </resultMap>

    <select id="selectByEmphasis" parameterType="com.zhongzheng.modules.user.bo.OrderPossessUserQueryBo" resultMap="ExamNoteResult">
        SELECT
            en.*,
            mc.category_name
        FROM
            exam_note en
                LEFT JOIN major_category mc ON en.category_id = mc.category_id
            LEFT JOIN order_possess_user o ON o.possess_id =en.file_id
        where 1=1  and o.type_id =3
        <if test="userId != null and userId != ''">
            AND o.user_id = #{userId}
        </if>
    </select>
</mapper>
