<?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.ym.mec.biz.dal.dao.CourseScheduleComplaintsDao">

	<resultMap type="com.ym.mec.biz.dal.entity.CourseScheduleComplaints" id="CourseScheduleComplaints">
		<result column="id_" property="id" />
		<result column="user_id_" property="userId" />
		<result column="course_schedule_id_" property="courseScheduleId" />
		<result column="reason_" property="reason" />
		<result column="status_" property="status" typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler"/>
		<result column="wf_process_id_" property="wfProcessId" />
		<result column="wf_order_id_" property="wfOrderId" />
		<result column="teacher_liability_ratio_" property="teacherLiabilityRatio" />
		<result column="student_liability_ratio_" property="studentLiabilityRatio" />
		<result column="create_time_" property="createTime" />
		<result column="update_time_" property="updateTime" />
		<result column="score_" property="score" />
		<result column="tenant_id_" property="tenantId" />
	</resultMap>

	<!-- 根据主键查询一条记录 -->
	<select id="get" resultMap="CourseScheduleComplaints" >
		SELECT * FROM course_schedule_complaints WHERE id_ = #{id}
	</select>

	<!-- 全查询 -->
	<select id="findAll" resultMap="CourseScheduleComplaints">
		SELECT * FROM course_schedule_complaints where tenant_id_ = #{tenantId} ORDER BY id_
	</select>

	<!-- 向数据库增加一条记录 -->
	<insert id="insert" parameterType="com.ym.mec.biz.dal.entity.CourseScheduleComplaints" useGeneratedKeys="true" keyColumn="id" keyProperty="id">
		<!--
		<selectKey resultClass="int" keyProperty="id" >
		SELECT SEQ_WSDEFINITION_ID.nextval AS ID FROM DUAL
		</selectKey>
		-->
		INSERT INTO course_schedule_complaints (id_,user_id_,course_schedule_id_,reason_,status_,wf_process_id_,wf_order_id_,create_time_,update_time_,teacher_liability_ratio_,student_liability_ratio_,score_,tenant_id_) VALUES(#{id},#{userId},#{courseScheduleId},#{reason},#{status, typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},#{wfProcessId},#{wfOrderId},now(),now(),#{teacherLiabilityRatio},#{studentLiabilityRatio},#{score},#{tenantId})
	</insert>

	<!-- 根据主键查询一条记录 -->
	<update id="update" parameterType="com.ym.mec.biz.dal.entity.CourseScheduleComplaints">
		UPDATE course_schedule_complaints
		<set>
			<if test="status != null">
			status_ = #{status, typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},
			</if>
			<if test="wfOrderId != null">
			wf_order_id_ = #{wfOrderId},
			</if>
			<if test="userId != null">
			user_id_ = #{userId},
			</if>
			<if test="wfProcessId != null">
			wf_process_id_ = #{wfProcessId},
			</if>
			<if test="id != null">
			id_ = #{id},
			</if>
			<if test="courseScheduleId != null">
			course_schedule_id_ = #{courseScheduleId},
			</if>
			<if test="reason != null">
			reason_ = #{reason},
			</if>
			<if test="updateTime != null">
			update_time_ = now(),
			</if>
			<if test="teacherLiabilityRatio != null">
			teacher_liability_ratio_ = #{teacherLiabilityRatio},
			</if>
			<if test="studentLiabilityRatio != null">
			student_liability_ratio_ = #{studentLiabilityRatio},
			</if>
			<if test="score != null">
				score_ = #{score},
			</if>
		</set> WHERE id_ = #{id} and tenant_id_ = #{tenantId}
	</update>

	<!-- 根据主键删除一条记录 -->
	<delete id="delete" >
		DELETE FROM course_schedule_complaints WHERE id_ = #{id}
	</delete>
    <delete id="delByCourseScheduleId">
		DELETE FROM course_schedule_complaints WHERE course_schedule_id_ = #{courseScheduleId}
	</delete>
	<delete id="delByCourseScheduleIds">
		DELETE FROM course_schedule_complaints WHERE course_schedule_id_ IN
		<foreach collection="courseScheduleIds" item="id" open="(" close=")" separator=",">
			#{id}
		</foreach>
	</delete>

	<!-- 分页查询 -->
	<select id="queryPage" resultMap="CourseScheduleComplaints" parameterType="map">
		SELECT * FROM course_schedule_complaints WHERE score_ &lt;= 2 and tenant_id_ = #{tenantId} ORDER BY id_ <include refid="global.limit"/>
	</select>

	<!-- 查询当前表的总记录数 -->
	<select id="queryCount" resultType="int">
		SELECT COUNT(*) FROM course_schedule_complaints WHERE score_ &lt;= 2 and tenant_id_ = #{tenantId}
	</select>
    <select id="findByUserAndCourse" resultMap="CourseScheduleComplaints">
		SELECT * FROM course_schedule_complaints WHERE user_id_=#{userId} AND course_schedule_id_=#{courseScheduleId}
    </select>

    <select id="findByCourseScheduleIds" resultMap="CourseScheduleComplaints">
		SELECT * FROM course_schedule_complaints WHERE course_schedule_id_ IN
		<foreach collection="ids" item="id" open="(" close=")" separator=",">
			#{id}
		</foreach>
		AND status_!='REJECT' AND score_ &lt;= 2
	</select>
	<resultMap id="CourseScheduleComplaintsDtoMap" type="com.ym.mec.biz.dal.dto.CourseScheduleComplaintsDto">
		<result property="username" column="username_"/>
		<result property="reason" column="reason_"/>
		<result property="score" column="score_"/>
		<result property="avatar" column="avatar_"/>
		<result property="userId" column="user_id_"/>
	</resultMap>
	<select id="findByCourseScheduleId" resultMap="CourseScheduleComplaintsDtoMap">
		SELECT su.username_,csc.reason_,csc.score_,su.avatar_,su.id_ user_id_
		FROM course_schedule_complaints csc
		LEFT JOIN sys_user su ON su.id_ = csc.user_id_
		WHERE csc.course_schedule_id_ = #{courseScheduleId}
	</select>

	<select id="findByUserIdAndCourseId" resultMap="CourseScheduleComplaintsDtoMap">
		SELECT su.username_,csc.reason_,csc.score_,su.avatar_ FROM course_schedule_complaints csc
		LEFT JOIN sys_user su on csc.user_id_ = su.id_
		<where>
			<if test="userId != null">
				user_id_=#{userId}
			</if>
			<if test="courseScheduleId != null">
				AND course_schedule_id_=#{courseScheduleId}
			</if>
		</where>
		LIMIT 1
	</select>

	<select id="findByCourseIdAndUserId" resultMap="CourseScheduleComplaints">
		SELECT * FROM course_schedule_complaints WHERE course_schedule_id_ IN
		<foreach collection="courseIds" item="courseId" open="(" close=")" separator=",">
			#{courseId}
		</foreach>
		AND user_id_ = #{userId}
	</select>
	<select id="findComplaintsByCourseScheduleIds" resultMap="CourseScheduleComplaints">
		SELECT * FROM course_schedule_complaints WHERE course_schedule_id_ IN
		<foreach collection="ids" item="id" open="(" close=")" separator=",">
			#{id}
		</foreach>
	</select>
	<select id="findCourseIdComplaints" resultMap="CourseScheduleComplaintsDtoMap">
		SELECT su.username_,csc.reason_,csc.score_,su.avatar_ FROM course_schedule_complaints csc
		LEFT JOIN sys_user su on csc.user_id_ = su.id_ WHERE
		course_schedule_id_=#{courseScheduleId}
	</select>
</mapper>
