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

	<resultMap type="com.ym.mec.biz.dal.entity.TeacherSalaryComplaints" id="TeacherSalaryComplaints">
		<result column="id_" property="id" />
		<result column="teacher_id_" property="teacherId" />
		<result column="salary_settlement_month_" property="salarySettlementMonth" />
		<result column="reason_" property="reason" />
		<result column="status_" property="statusEnum" typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler" />
		<result column="operator_id_" property="operatorId"/>
		<result column="handing_suggestion_" property="handingSuggestion" />
		<result column="organ_name_" property="organName" />
		<result column="real_name_" property="realName" />
		<result column="operator_real_name_" property="operatorRealName" />
		<result column="handing_suggestion_" property="handingSuggestion" />
		<result column="create_time_" property="createTime" />
		<result column="update_time_" property="updateTime" />
	</resultMap>
	<!-- 向数据库增加一条记录 -->
	<insert id="insert" parameterType="com.ym.mec.biz.dal.entity.TeacherSalaryComplaints"
			useGeneratedKeys="true" keyColumn="id" keyProperty="id">
		INSERT INTO teacher_salary_complaints
		(teacher_id_,salary_settlement_month_,reason_,status_,operator_id_,handing_suggestion_,create_time_,update_time_)
		VALUES(#{teacherId},#{salarySettlementMonth},#{reason},#{statusEnum, typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},
		#{operatorId},#{handingSuggestion},NOW(),NOW())
	</insert>

	<!-- 根据主键查询一条记录 -->
	<update id="update" parameterType="com.ym.mec.biz.dal.entity.TeacherSalaryComplaints">
		UPDATE teacher_salary_complaints
		<set>
			<if test="statusEnum != null">
				status_ = #{statusEnum, typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},
			</if>
			<if test="teacherId != null">
				teacher_id_ = #{teacherId},
			</if>
			<if test="salarySettlementMonth != null">
				salary_settlement_month_ = #{salarySettlementMonth},
			</if>
			<if test="reason != null">
				reason_ = #{reason},
			</if>
			<if test="operatorId != null">
				operator_id_ = #{operatorId},
			</if>
			<if test="handingSuggestion != null">
				handing_suggestion_ = #{handingSuggestion},
			</if>
			<if test="updateTime != null">
				update_time_ = NOW()
			</if>
		</set>
		WHERE id_ = #{id}
	</update>
	<update id="repealComplaints">
		UPDATE teacher_salary_complaints tsc SET tsc.status_ = 'WITHDRAWN'
		WHERE tsc.teacher_id_ = #{teacherId} AND tsc.salary_settlement_month_ = #{salarySettlementMonth}
		AND tsc.status_ = 'PENDING'
	</update>

    <update id="updateComplaintStatusTuDoneWithMonth">
		UPDATE teacher_salary_complaints tsc SET tsc.status_ = 'DONE'
		WHERE tsc.salary_settlement_month_ = #{month}
		AND tsc.status_ = 'PENDING'
	</update>

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

	<!-- 全查询 -->
	<select id="findAll" resultMap="TeacherSalaryComplaints">
		SELECT * FROM
		teacher_salary_complaints ORDER BY id_
	</select>

	<!-- 根据主键删除一条记录 -->
	<delete id="delete">
		DELETE FROM teacher_salary_complaints WHERE id_ =
		#{id}
	</delete>

	<!-- 分页查询 -->
	<select id="queryPage" resultMap="TeacherSalaryComplaints" parameterType="map">
		SELECT o.name_ organ_name_,su.real_name_,suo.real_name_ operator_real_name_,tsc.*
		FROM teacher_salary_complaints tsc
		LEFT JOIN teacher t ON t.id_ = tsc.teacher_id_
		LEFT JOIN sys_user su ON su.id_ = t.id_
		LEFT JOIN sys_user suo ON suo.id_ = tsc.operator_id_
		LEFT JOIN organization o ON o.id_ = t.organ_id_
		<include refid="queryPageSql"/>
		ORDER BY tsc.create_time_ DESC
		<include refid="global.limit" />
	</select>

	<sql id="queryPageSql">
		<where>
			<if test="organId != null and organId != ''">
				FIND_IN_SET(t.organ_id_,#{organId})
			</if>
			<if test="salarySettlementMonth != null and salarySettlementMonth != ''">
				AND tsc.salary_settlement_month_ = #{salarySettlementMonth}
			</if>
			<if test="teacherId != null">
				AND tsc.teacher_id_ = #{teacherId}
			</if>
			<if test="statusEnum != null">
				AND tsc.status_ = #{statusEnum,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler}
			</if>
		</where>
	</sql>

	<!-- 查询当前表的总记录数 -->
	<select id="queryCount" resultType="int">
		SELECT COUNT(tsc.id_)
		FROM teacher_salary_complaints tsc
		LEFT JOIN teacher t ON t.id_ = tsc.teacher_id_
		<include refid="queryPageSql"/>
	</select>
	<select id="findByMonthAndTeacherId" resultMap="TeacherSalaryComplaints">
		SELECT * FROM teacher_salary_complaints tsc
		WHERE tsc.teacher_id_ = #{teacherId} AND tsc.salary_settlement_month_ = #{settlementMonth}
		AND tsc.status_ = #{statusEnum,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler} LIMIT 1
	</select>

	<select id="findTeacherSalaryComplaintsWithMonth" resultMap="TeacherSalaryComplaints">
		SELECT * FROM teacher_salary_complaints tsc
		WHERE tsc.teacher_id_ = #{teacherId} AND tsc.salary_settlement_month_ = #{month}
		<if test="status!=null">
			AND tsc.status_ = #{status,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler}
		</if>
	</select>

</mapper>
