<?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.jeeplus.modules.test.manytomany.mapper.StudentCourseMapper">
    
	<sql id="studentCourseColumns">
		a.id AS "id",
		a.student_id AS "student.id",
		a.course_id AS "course.id",
		a.score AS "score",
		a.create_by AS "createBy.id",
		a.create_date AS "createDate",
		a.update_by AS "updateBy.id",
		a.update_date AS "updateDate",
		a.remarks AS "remarks",
		a.del_flag AS "delFlag",
				
		student.name AS "student.name",
				
		course.name AS "course.name"
	</sql>
	
	<sql id="studentCourseJoins">
		
		LEFT JOIN test_student student ON student.id = a.student_id
		LEFT JOIN test_course course ON course.id = a.course_id
	</sql>
	
    
	<select id="get" resultType="StudentCourse" >
		SELECT 
			<include refid="studentCourseColumns"/>
		FROM test_student_course a
		<include refid="studentCourseJoins"/>
		WHERE a.id = #{id}
	</select>
	
	<select id="findList" resultType="StudentCourse" >
		SELECT 
			<include refid="studentCourseColumns"/>
		FROM test_student_course a
		<include refid="studentCourseJoins"/>
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL}
			${dataScope}
			<if test="student != null and student.id != null and student.id != ''">
				AND a.student_id = #{student.id}
			</if>
			<if test="course != null and course.id != null and course.id != ''">
				AND a.course_id = #{course.id}
			</if>
		</where>
		<choose>
			<when test="page !=null and page.orderBy != null and page.orderBy != ''">
				ORDER BY ${page.orderBy}
			</when>
			<otherwise>
				ORDER BY a.update_date DESC
			</otherwise>
		</choose>
	</select>
	
	<select id="findAllList" resultType="StudentCourse" >
		SELECT 
			<include refid="studentCourseColumns"/>
		FROM test_student_course a
		<include refid="studentCourseJoins"/>
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL}
			${dataScope}
		</where>		
		<choose>
			<when test="page !=null and page.orderBy != null and page.orderBy != ''">
				ORDER BY ${page.orderBy}
			</when>
			<otherwise>
				ORDER BY a.update_date DESC
			</otherwise>
		</choose>
	</select>
	
	<insert id="insert">
		INSERT INTO test_student_course(
			id,
			student_id,
			course_id,
			score,
			create_by,
			create_date,
			update_by,
			update_date,
			remarks,
			del_flag
		) VALUES (
			#{id},
			#{student.id},
			#{course.id},
			#{score},
			#{createBy.id},
			#{createDate},
			#{updateBy.id},
			#{updateDate},
			#{remarks},
			#{delFlag}
		)
	</insert>
	
	<update id="update">
		UPDATE test_student_course SET 	
			student_id = #{student.id},
			course_id = #{course.id},
			score = #{score},
			update_by = #{updateBy.id},
			update_date = #{updateDate},
			remarks = #{remarks}
		WHERE id = #{id}
	</update>
	
	
	<!--物理删除-->
	<update id="delete">
		DELETE FROM test_student_course
		WHERE id = #{id}
	</update>
	
	<!--逻辑删除-->
	<update id="deleteByLogic">
		UPDATE test_student_course SET 
			del_flag = #{DEL_FLAG_DELETE}
		WHERE id = #{id}
	</update>
	
	
	<!-- 根据实体名称和字段名称和字段值获取唯一记录 -->
	<select id="findUniqueByProperty" resultType="StudentCourse" statementType="STATEMENT">
		select * FROM test_student_course  where ${propertyName} = '${value}'
	</select>
	
</mapper>