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

    <resultMap type="com.zhongzheng.modules.course.domain.CourseSubject" id="CourseSubjectResult">
        <result property="id" column="id"/>
        <result property="encoder" column="encoder"/>
        <result property="remark" column="remark"/>
        <result property="status" column="status"/>
        <result property="createTime" column="create_time"/>
        <result property="updateTime" column="update_time"/>
        <result property="subjectName" column="subject_name"/>
    </resultMap>

    <resultMap type="com.zhongzheng.modules.course.vo.CourseProjectTypeVo" id="CourseProjectTypeVoResult">
        <result property="id" column="id"/>
        <result property="projectName" column="project_name"/>
        <result property="encoder" column="encoder"/>
        <result property="remark" column="remark"/>
        <result property="educationId" column="education_id"/>
        <result property="educationName" column="education_name"/>
        <result property="status" column="status"/>
    </resultMap>

    <resultMap type="com.zhongzheng.modules.course.vo.CourseSubjectVo" id="CourseSubjectVoResult">
        <result property="id" column="id"/>
        <result property="encoder" column="encoder"/>
        <result property="remark" column="remark"/>
        <result property="status" column="status"/>
        <result property="subjectName" column="subject_name"/>
    </resultMap>

    <resultMap type="com.zhongzheng.modules.course.vo.CourseProjectTypeVo" id="CourseProjectTypeVoTwoResult">
        <result property="id" column="pid"/>
        <result property="projectName" column="pproject_name"/>
        <result property="encoder" column="pencoder"/>
        <result property="remark" column="premark"/>
        <result property="educationId" column="peducation_id"/>
        <result property="educationName" column="peducation_name"/>
        <result property="status" column="pstatus"/>
    </resultMap>

    <select id="selectProject" parameterType="com.zhongzheng.modules.course.bo.CourseProjectTypeQueryBo"  resultMap="CourseProjectTypeVoResult">
        SELECT
        p.*,
        e.education_name
        FROM
        course_subject_project j
        LEFT JOIN course_project_type p on p.id=j.project_id
        LEFT JOIN course_education_type e ON p.education_id = e.id
        where 1=1
        <if test="status != null and status.size()!=0 ">
            AND p.status in
            <foreach collection="status" item="item" index="index" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
        <if test="id != null and id!=0 ">
            AND j.subject_id = #{id}
        </if>
    </select>

    <select id="queryList" parameterType="com.zhongzheng.modules.course.bo.CourseSubjectQueryBo"  resultMap="CourseSubjectVoResult">
        SELECT
        s.*
        FROM
        course_subject s
        WHERE
        1 =1
        <if test="status != null and status.size()!=0 ">
            AND s.status in
            <foreach collection="status" item="item" index="index" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
        <if test="educationId != null and educationId!=0 ">
            and (SELECT COUNT(p.id) FROM course_subject_project t LEFT JOIN course_project_type p on t.project_id = p.id where p.education_id = #{educationId} ) > 0
        </if>
        <if test="projectId != null and projectId!=0 ">
            and (SELECT COUNT(t.id) FROM course_subject_project t  where t.project_id = #{projectId}) > 0
        </if>
        <if test="subjectName != null and subjectName!='' ">
            AND s.subject_name = #{subjectName}
        </if>
        <if test="wrong != null and wrong!=0 ">
              and (select COUNT(v.id) FROM course_subject_project v where s.id=v.subject_id) = 0
        </if>
        <if test="id != null and id!='' ">
            and s.id = #{id}
        </if>
        order by s.update_time desc
    </select>

    <select id="queryProject" parameterType="map"  resultMap="CourseProjectTypeVoResult">
        SELECT
            p.*,
            (SELECT e.education_name FROM course_education_type e where p.education_id = e.id) as education_name,
            (SELECT e.id FROM course_education_type e where p.education_id = e.id) as education_id
        FROM
            course_project_type p
                LEFT JOIN course_subject_project j ON p.id = j.project_id
        WHERE
            j.subject_id=#{id}
    </select>
</mapper>
