<?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.exam.mapper.ExamKnowledgeMapper">

    <resultMap type="com.zhongzheng.modules.exam.domain.ExamKnowledge" id="ExamKnowledgeResult">
        <result property="knowledgeId" column="knowledge_id"/>
        <result property="knowledgeName" column="knowledge_name"/>
        <result property="status" column="status"/>
        <result property="createTime" column="create_time"/>
        <result property="updateTime" column="update_time"/>
        <result property="encoder" column="encoder"/>
    </resultMap>

    <resultMap type="com.zhongzheng.modules.exam.vo.ExamKnowledgeVo" id="ExamKnowledgeVoResult">
        <result property="knowledgeId" column="knowledge_id"/>
        <result property="knowledgeName" column="knowledge_name"/>
        <result property="status" column="status"/>
        <result property="encoder" column="encoder"/>
        <association property="courseBusinessList" javaType="java.util.List" resultMap="CourseBusinessVoResult" />
    </resultMap>

    <resultMap type="com.zhongzheng.modules.course.vo.CourseBusinessVo" id="CourseBusinessVoResult">
        <result property="businessName" column="business_name"/>
        <result property="businessId" column="business_id"/>
        <result property="subjectId" column="subject_id"/>
        <result property="remark" column="remark"/>
        <result property="projectId" column="project_id"/>
        <result property="schoolYear" column="school_year"/>
        <result property="status" column="status"/>
        <result property="educationName" column="education_name"/>
        <result property="projectName" column="project_name"/>
        <result property="educationId" column="education_id"/>
        <result property="subjectName" column="subject_name"/>
    </resultMap>


    <select id="queryList" parameterType="com.zhongzheng.modules.exam.bo.ExamKnowledgeQueryBo" resultMap="ExamKnowledgeVoResult">
        SELECT
        k.*
        FROM
        exam_knowledge k where 1=1
        <if test="status != null and status.size()!=0 ">
            AND k.status in
            <foreach collection="status" item="item" index="index" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>

        <if test="educationId != null and educationId !=0 ">
            AND #{educationId} in (
            SELECT
            e.id AS education_id
            FROM
            exam_knowledge_business b
            LEFT JOIN course_business s ON s.id = b.business_id
            LEFT JOIN course_subject j ON j.id = b.subject_id
            LEFT JOIN course_project_type p ON s.project_id = p.id
            LEFT JOIN course_education_type e ON p.education_id = e.id
            WHERE
            b.knowledge_id = k.knowledge_id
            )
        </if>

        <if test="subjectId != null and subjectId !=0 ">
            AND #{subjectId} in (
            SELECT
            b.subject_id
            FROM
            exam_knowledge_business b
            LEFT JOIN course_business s ON s.id = b.business_id
            LEFT JOIN course_subject j ON j.id = b.subject_id
            LEFT JOIN course_project_type p ON s.project_id = p.id
            LEFT JOIN course_education_type e ON p.education_id = e.id
            WHERE
            b.knowledge_id = k.knowledge_id
            )
        </if>

        <if test="businessId != null and businessId !=0 ">
            AND #{businessId} in (
            SELECT
            b.business_id
            FROM
            exam_knowledge_business b
            LEFT JOIN course_business s ON s.id = b.business_id
            LEFT JOIN course_subject j ON j.id = b.subject_id
            LEFT JOIN course_project_type p ON s.project_id = p.id
            LEFT JOIN course_education_type e ON p.education_id = e.id
            WHERE
            b.knowledge_id = k.knowledge_id
            )
        </if>

        <if test="knowledgeName != null ">
            AND k.knowledge_name like concat('%', #{knowledgeName}, '%')
        </if>
        order by k.create_time desc
    </select>

    <select id="queryCourseBusinessList" resultType="long" resultMap="CourseBusinessVoResult">
        SELECT
            b.knowledge_id as k_id,
            b.business_id,
            b.subject_id,
            s.business_name,
            j.subject_name,
            e.education_name,
            e.id AS education_id,
            p.project_name,
            p.id as project_id
        FROM
            exam_knowledge_business b
                LEFT JOIN course_business s ON s.id = b.business_id
                LEFT JOIN course_subject j ON j.id = b.subject_id
                LEFT JOIN course_project_type p ON s.project_id = p.id
                LEFT JOIN course_education_type e ON p.education_id = e.id
        WHERE b.knowledge_id = #{knowledgeId}
        and s.status=1
    </select>
</mapper>
