<?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.course.mapper.CourseMapper">

    <resultMap type="com.zhongzheng.modules.course.domain.Course" id="CourseResult">
        <result property="courseId" column="course_id"/>
        <result property="prefixName" column="prefix_name"/>
        <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="educationTypeId" column="education_type_id"/>
        <result property="businessId" column="business_id"/>
        <result property="schoolId" column="school_id"/>
        <result property="courseName" column="course_name"/>
        <result property="publishStatus" column="publish_status"/>
        <result property="sort" column="sort"/>
        <result property="suitableObject" column="suitable_object"/>
        <result property="pcDetailHtml" column="pc_detail_html"/>
        <result property="mobileDetailHtml" column="mobile_detail_html"/>
        <result property="majorId" column="major_id"/>
        <result property="code" column="code"/>
        <result property="subjectId" column="subject_id"/>
        <result property="projectId" column="project_id"/>
    </resultMap>

    <resultMap type="com.zhongzheng.modules.course.vo.CourseVo" id="CourseResultVo">
        <result property="courseId" column="course_id"/>
        <result property="prefixName" column="prefix_name"/>
        <result property="coverUrl" column="cover_url"/>
        <result property="introduction" column="introduction"/>
        <result property="status" column="status"/>
        <result property="educationTypeId" column="education_type_id"/>
        <result property="businessId" column="business_id"/>
        <result property="schoolId" column="school_id"/>
        <result property="courseName" column="course_name"/>
        <result property="publishStatus" column="publish_status"/>
        <result property="sort" column="sort"/>
        <result property="suitableObject" column="suitable_object"/>
        <result property="pcDetailHtml" column="pc_detail_html"/>
        <result property="mobileDetailHtml" column="mobile_detail_html"/>
        <result property="majorId" column="major_id"/>
        <result property="code" column="code"/>
        <result property="subjectId" column="subject_id"/>
        <result property="projectId" column="project_id"/>
        <result property="createTime" column="create_time"/>
        <result property="updateTime" column="update_time"/>
        <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="categoryName" column="category_name"/>
        <result property="subjectName" column="subject_name"/>
        <collection property="goodsList" column="course_id" select="findGoodsList"/>
    </resultMap>

    <resultMap type="com.zhongzheng.modules.course.vo.CourseUserVo" id="CourseUserVo">
        <result property="courseId" column="course_id"/>
        <result property="prefixName" column="prefix_name"/>
        <result property="coverUrl" column="cover_url"/>
        <result property="gradeId" column="grade_id"/>
        <result property="introduction" column="introduction"/>
        <result property="status" column="status"/>
        <result property="educationTypeId" column="education_type_id"/>
        <result property="businessId" column="business_id"/>
        <result property="schoolId" column="school_id"/>
        <result property="courseName" column="course_name"/>
        <result property="publishStatus" column="publish_status"/>
        <result property="sort" column="sort"/>
        <result property="suitableObject" column="suitable_object"/>
        <result property="pcDetailHtml" column="pc_detail_html"/>
        <result property="mobileDetailHtml" column="mobile_detail_html"/>
        <result property="majorId" column="major_id"/>
        <result property="code" column="code"/>
        <result property="subjectId" column="subject_id"/>
        <result property="projectId" column="project_id"/>
        <result property="createTime" column="create_time"/>
        <result property="updateTime" column="update_time"/>
        <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="categoryName" column="category_name"/>
        <result property="subjectName" column="subject_name"/>
    </resultMap>
    <resultMap type="com.zhongzheng.modules.goods.vo.GoodsPeriodVo" id="GoodsPeriodVo">
        <result property="goodsId" column="goods_id"/>
        <result property="year" column="year"/>
        <result property="goodsName" column="goods_name"/>
        <result property="periodStatus" column="period_status"/>
        <result property="rebuild" column="rebuild"/>
        <result property="gradeId" column="grade_id"/>
        <result property="examNum" column="exam_num"/>
        <result property="recordNum" column="record_num"/>
        <result property="applyStatus" column="apply_status"/>
        <result property="beforeStatus" column="before_status"/>
        <result property="applyName" column="apply_name"/>
        <result property="beforeName" column="before_name"/>
        <result property="rebuildNum" column="rebuildNum"/>
        <result property="rebuildStatus" column="rebuild_status"/>
    </resultMap>

    <resultMap type="com.zhongzheng.modules.goods.vo.GoodsPeriodStatusVo" id="GoodsPeriodStatusVo">
        <result property="id" column="id"/>
        <result property="type" column="type"/>
        <result property="name" column="name"/>
        <result property="periodId" column="period_id"/>
        <result property="sectionType" column="section_type"/>
    </resultMap>

    <resultMap type="com.zhongzheng.modules.goods.vo.GoodsUserVo" id="GoodsUserVo">
        <result property="goodsId" column="goods_id"/>
        <result property="year" column="year"/>
        <result property="supplyId" column="supply_id"/>
        <result property="userId" column="user_id"/>
        <result property="goodsType" column="goods_type"/>
        <result property="educationTypeId" column="education_type_id"/>
        <result property="businessId" column="business_id"/>
        <result property="schoolId" column="school_id"/>
        <result property="majorId" column="major_id"/>
        <result property="goodsName" column="goods_name"/>
        <result property="standPrice" column="stand_price"/>
        <result property="lowestPrice" column="lowest_price"/>
        <result property="createTime" column="create_time"/>
        <result property="updateTime" column="update_time"/>
        <result property="status" column="status"/>
        <result property="validityStartTime" column="validity_start_time"/>
        <result property="validityEndTime" column="validity_end_time"/>
        <result property="studyStartTime" column="study_start_time"/>
        <result property="studyEndTime" column="study_end_time"/>
        <result property="certificateIds" column="certificate_ids"/>
        <result property="introduce" column="introduce"/>
        <result property="suitableObject" column="suitable_object"/>
        <result property="buyNote" column="buy_note"/>
        <result property="pcDetailHtml" column="pc_detail_html"/>
        <result property="mobileDetailHtml" column="mobile_detail_html"/>
        <result property="goodsStatus" column="goods_status"/>
        <result property="coverUrl" column="cover_url"/>
        <result property="classHours" column="class_hours"/>
        <result property="standPriceJson" column="stand_price_json"/>
        <result property="code" column="code"/>
        <result property="projectId" column="project_id"/>
        <result property="goodsAuditionConfig" column="goods_audition_config"/>
        <result property="goodsPhotographConfig" column="goods_photograph_config"/>
        <result property="goodsPlayConfig" column="goods_play_config"/>
        <result property="goodsExamConfig" column="goods_exam_config"/>
        <result property="gradeId" column="grade_id"/>
        <result property="rebuildStatus" column="rebuild_status"/>

        <result property="supplyName" column="supply_name"/>
        <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="categoryName" column="category_name"/>
        <result property="handoutsId" column="handouts_id"/>
        <result property="templateType" column="template_type"/>
        <result property="period" column="period"/>
        <result property="makeStartTime" column="make_start_time"/>
        <result property="makeEndTime" column="make_end_time"/>
        <result property="studyCount" column="study_count"/>
        <result property="makeGoodsName" column="make_goods_name"/>
        <result property="makeGoodsCode" column="make_goods_code"/>
        <result property="applyStatus" column="apply_status"/>
        <result property="beforeStatus" column="before_status"/>
        <result property="applyName" column="apply_name"/>
        <result property="beforeName" column="before_name"/>
        <result property="courseNum" column="course_num"/>
    </resultMap>


    <select id="findGoodsList" resultMap="CourseGoodsListResult">
        SELECT
            g.goods_name,
            g.goods_id
        FROM
            goods_course gc
             LEFT JOIN goods g ON gc.goods_id = g.goods_id
        WHERE
            gc.course_id =#{course_id}
    </select>

    <resultMap type="com.zhongzheng.modules.course.vo.CourseGoodsVo" id="CourseGoodsListResult">
        <result property="goodsName" column="goods_name"/>
        <result property="goodsId" column="goods_id"/>
    </resultMap>

    <select id="selectCourseList" parameterType="com.zhongzheng.modules.course.bo.CourseQueryBo" resultMap="CourseResultVo">
        SELECT
        c.*,
        cet.education_name,
        cpt.project_name,
        cb.business_name,
        s.school_name,
        m.category_name,
        cs.subject_name
        FROM
        course c
        LEFT JOIN course_education_type cet ON c.education_type_id = cet.id AND cet.status =1
        LEFT JOIN course_project_type cpt ON c.project_id = cpt.id AND cpt.status =1
        LEFT JOIN course_business cb ON c.business_id = cb.id AND cb.status =1
        LEFT JOIN school s ON s.id = c.school_id AND s.status =1
        LEFT JOIN major m ON c.major_id = m.id AND m.status =1
        LEFT JOIN course_subject cs ON cs.id = c.subject_id AND cs.status =1
        WHERE
        1 = 1 AND c.status !=-1
        <if test="status != null and status.size()!=0 ">
            AND c.status in
            <foreach collection="status" item="item" index="index" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
        <if test="educationTypeId != null and educationTypeId != ''">
            AND c.education_type_id = #{educationTypeId}
        </if>
        <if test="subjectId != null and subjectId != ''">
            AND c.subject_id = #{subjectId}
        </if>
        <if test="businessId != null and businessId != ''">
            AND c.business_id = #{businessId}
        </if>
        <if test="prefixName != null and prefixName != ''">
            AND c.prefix_name like concat('%', #{prefixName}, '%')
        </if>
        <if test="courseName != null and courseName != ''">
            AND c.course_name like concat('%', #{courseName}, '%')
        </if>
        <if test="publishStatus != null ">
            AND c.publish_status = #{publishStatus}
        </if>
        <if test="schoolId != null and schoolId != ''">
            AND c.school_id = #{schoolId}
        </if>
        <if test="majorId != null and majorId != ''">
            AND c.major_id = #{majorId}
        </if>
        <if test="key != null and key != ''">
            AND (c.course_name like concat('%', #{key}, '%') or c.code = #{key})
        </if>
        GROUP BY
        c.course_id
        ORDER BY c.create_time DESC
    </select>

    <select id="selectCourseList_COUNT" resultType="Long">
        SELECT
            count(distinct c.course_id )
        FROM
            course c
        WHERE
        1 = 1
        <if test="status != null and status.size()!=0 ">
            AND c.status in
            <foreach collection="status" item="item" index="index" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
        <if test="educationTypeId != null and educationTypeId != ''">
            AND c.education_type_id = #{educationTypeId}
        </if>
        <if test="subjectId != null and subjectId != ''">
            AND c.subject_id = #{subjectId}
        </if>
        <if test="businessId != null and businessId != ''">
            AND c.business_id = #{businessId}
        </if>
        <if test="prefixName != null and prefixName != ''">
            AND c.prefix_name like concat('%', #{prefixName}, '%')
        </if>
        <if test="courseName != null and courseName != ''">
            AND c.course_name like concat('%', #{courseName}, '%')
        </if>
        <if test="publishStatus != null ">
            AND c.publish_status = #{publishStatus}
        </if>
        <if test="schoolId != null and schoolId != ''">
            AND c.school_id = #{schoolId}
        </if>
        <if test="majorId != null and majorId != ''">
            AND c.major_id = #{majorId}
        </if>


    </select>

    <select id="selectDetailById" parameterType="Long"  resultMap="CourseResultVo">
        SELECT
            c.*,
            cet.education_name,
            cpt.project_name,
            cb.business_name,
            s.school_name,
            m.category_name,
            cs.subject_name
        FROM
            course c
                LEFT JOIN course_education_type cet ON c.education_type_id = cet.id
                LEFT JOIN course_project_type cpt ON c.project_id = cpt.id
                LEFT JOIN course_business cb ON c.business_id = cb.id
                LEFT JOIN school s ON s.id = c.school_id
                LEFT JOIN major m ON c.major_id = m.id
                LEFT JOIN course_subject cs ON cs.id = c.subject_id
        WHERE
            course_id = #{id}
    </select>

    <select id="getCourseUserVoInfo" parameterType="com.zhongzheng.modules.course.bo.CourseMenuQueryBo"  resultMap="CourseUserVo">
        SELECT
            c.*,
            cet.education_name,
            cpt.project_name,
            cb.business_name,
            s.school_name,
            m.category_name,
            cs.subject_name
        FROM
            course c
                LEFT JOIN course_education_type cet ON c.education_type_id = cet.id
                LEFT JOIN course_project_type cpt ON c.project_id = cpt.id
                LEFT JOIN course_business cb ON c.business_id = cb.id
                LEFT JOIN school s ON s.id = c.school_id
                LEFT JOIN major m ON c.major_id = m.id
                LEFT JOIN course_subject cs ON cs.id = c.subject_id
        WHERE
            course_id = #{id}
    </select>

    <select id="listGoodsPeriodVo" parameterType="com.zhongzheng.modules.user.bo.UserPlanQueryBo"  resultMap="GoodsPeriodVo">
        SELECT
            g.goods_id,
            g.goods_name,
            g.`year`,
            (SELECT COUNT(1) FROM user_period up LEFT JOIN user_period_status ups on up.id = ups.period_id where up.goods_id = g.goods_id AND ups.period_status =1  and (ups.`status` =3 or ups.`status` =0) and up.grade_id = cgu.grade_id) as rebuild,
            (case WHEN (SELECT COUNT(1) FROM user_period up LEFT JOIN user_period_status ups on up.id = ups.period_id where up.goods_id = g.goods_id AND ups.period_status =1 and ups.`status` =0) >0 then 0 ELSE 1 end) as rebuild_status,
            cgu.grade_id,
            (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.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.user_id =cgu.user_id and ubr.grade_id = cg.grade_id) 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=g.goods_id and up.user_id = cgu.user_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` =3 and ups.period_status = 1 and up.goods_id=g.goods_id and up.user_id = cgu.user_id and up.type = 1 and up.grade_id = cgu.grade_id) as rebuildNum,
            (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=g.goods_id and up.user_id = cgu.user_id 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=g.goods_id and up.user_id = cgu.user_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=g.goods_id and up.user_id = cgu.user_id and up.type = 2) as exam_cheat,
            (case WHEN (SELECT COUNT(1) FROM exam_apply_goods eag LEFT JOIN exam_apply ea on eag.apply_id = ea.apply_id where eag.goods_id = cgg.goods_id and ea.`status` = 1 and unix_timestamp(now()) BETWEEN ea.apply_start_time and ea.apply_end_time) >0 then 1 ELSE 0 end) as apply_status,
                (case WHEN (SELECT COUNT(1) FROM exam_before_goods ebg LEFT JOIN exam_before eb on ebg.before_id = eb.before_id where ebg.goods_id = cgg.goods_id and eb.`status` = 1 and unix_timestamp(now()) BETWEEN eb.before_start_time and eb.before_end_time) >0 then 1 ELSE 0 end) as before_status,
            (SELECT ea.apply_name FROM exam_apply_goods eag LEFT JOIN exam_apply ea on eag.apply_id = ea.apply_id where eag.goods_id = cgg.goods_id and ea.`status` = 1 and unix_timestamp(now()) BETWEEN ea.apply_start_time and ea.apply_end_time) as apply_name,
            (SELECT eb.before_name FROM exam_before_goods ebg LEFT JOIN exam_before eb on ebg.before_id = eb.before_id where ebg.goods_id = cgg.goods_id and eb.`status` = 1 and unix_timestamp(now()) BETWEEN eb.before_start_time and eb.before_end_time) as before_name
        FROM
            class_grade_user cgu
                LEFT JOIN class_grade_goods cgg ON cgu.grade_id = cgg.grade_id
                LEFT JOIN class_grade cg on cg.grade_id = cgu.grade_id
                LEFT JOIN goods g on g.goods_id=cgg.goods_id
        WHERE
            cgu.user_id =#{userId}
        AND cg.status=1
          AND  (unix_timestamp(now()) &lt; cg.class_end_time or cg.class_start_time is null)
    </select>

    <select id="listSection" parameterType="com.zhongzheng.modules.grade.bo.ClassGradeUserQueryBo"  resultMap="GoodsPeriodStatusVo">
        SELECT
            ups.id,
            ups.period_id,
            cs.`name`,
            1 as type,
               cs.section_type as section_type
        FROM
            user_period up
                LEFT JOIN user_period_status ups ON up.id = ups.period_id
                LEFT JOIN course_section cs ON cs.section_id = up.section_id
        WHERE
            1 = 1
          AND user_id = #{userId}
          AND ups.period_status = 1
          AND ups.`status` = 0
          AND up.exam_id = 0
          and up.grade_id = #{gradeId}
        UNION
        SELECT
            ups.id,
            ups.period_id,
            exam_name as name,
            2 as type,
               0 as  section_type
        FROM
            user_period up
                LEFT JOIN user_period_status ups ON up.id = ups.period_id
                LEFT JOIN exam e ON e.exam_id = up.exam_id
        WHERE
            1 = 1
          AND user_id = #{userId}
          AND ups.period_status = 1
          AND ups.`status` = 0
          AND up.section_id = 0
          and up.grade_id = #{gradeId}
    </select>

    <select id="goodsList" parameterType="com.zhongzheng.modules.course.bo.CourseQueryBo"  resultMap="GoodsUserVo">
        SELECT
        g.*,
        o.user_id,
        (SELECT cet.education_name FROM  course_education_type cet  where cet.id = g.education_type_id) as education_name,
        (SELECT cet.project_name FROM  course_project_type cet where cet.id = g.project_id) as project_name,
        (SELECT cet.business_name FROM  course_business cet  where cet.id = g.business_id) as business_name,
        (SELECT COUNT(1) FROM goods_course gc where gc.goods_id = g.goods_id) as course_num,
        (case WHEN (SELECT COUNT(1) FROM user_period up LEFT JOIN user_period_status ups on up.id = ups.period_id where up.goods_id = g.goods_id AND ups.period_status =1 and ups.`status` =0) >0 then 0 ELSE 1 end) as rebuild_status,
        (case WHEN (SELECT COUNT(1) FROM exam_apply_goods eag LEFT JOIN exam_apply ea on eag.apply_id = ea.apply_id where eag.goods_id = g.goods_id and ea.`status` = 1 and unix_timestamp(now()) BETWEEN ea.apply_start_time and ea.apply_end_time) >0 then 1 ELSE 0 end) as apply_status,
        (case WHEN (SELECT COUNT(1) FROM exam_before_goods ebg LEFT JOIN exam_before eb on ebg.before_id = eb.before_id where ebg.goods_id = g.goods_id and eb.`status` = 1 and unix_timestamp(now()) BETWEEN eb.before_start_time and eb.before_end_time) >0 then 1 ELSE 0 end) as before_status,
        (SELECT ea.apply_name FROM exam_apply_goods eag LEFT JOIN exam_apply ea on eag.apply_id = ea.apply_id where eag.goods_id = g.goods_id and ea.`status` = 1 and unix_timestamp(now()) BETWEEN ea.apply_start_time and ea.apply_end_time) as apply_name,
        (SELECT eb.before_name FROM exam_before_goods ebg LEFT JOIN exam_before eb on ebg.before_id = eb.before_id where ebg.goods_id = g.goods_id and eb.`status` = 1 and unix_timestamp(now()) BETWEEN eb.before_start_time and eb.before_end_time) as before_name
        FROM
        `order` o
        LEFT JOIN order_goods og ON og.order_sn = o.order_sn
        LEFT JOIN goods g on og.goods_id = g.goods_id
        where 1=1
        AND og.`status` = 1
        AND og.refund_status in (0,1,3)
        AND og.pay_status in (2,3)
        AND g.goods_type =1
        <if test="userId != null and userId != ''">
            and o.user_id = #{userId}
        </if>
        order by o.create_time desc
    </select>
    <select id="courseList" parameterType="com.zhongzheng.modules.course.bo.CourseQueryBo"  resultMap="CourseUserVo">
        SELECT
            c.*,
                #{gradeId} as grade_id
        FROM
            course c
                LEFT JOIN goods_course gc ON gc.course_id = c.course_id
        where 1=1
          and gc.goods_id =#{goodsId}

    </select>

    <select id="rebuildNext" parameterType="Long"  resultType="Long">
        SELECT
            COUNT( 1 )
        FROM
            user_period up
                LEFT JOIN user_period_status ups ON up.id = ups.period_id
        WHERE
            up.goods_id = #{goodsId}
          AND up.grade_id = #{gradeId}
          AND ups.`status` = 0
          AND ups.period_status = 1
    </select>
    <select id="gradeIdSelect" parameterType="map"  resultType="Long">
        SELECT
            cgu.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
        WHERE
            cgu.user_id=#{userId}

          and cg.`status` =1
          and cgg.goods_id = #{goodsId}
        order by cg.create_time desc LIMIT 1
    </select>
</mapper>
