<?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.CourseScheduleTeacherSalaryDao">
	
	<resultMap type="com.ym.mec.biz.dal.entity.CourseScheduleTeacherSalary" id="CourseScheduleTeacherSalary">
		<result column="id_" property="id" />
        <result column="group_type_" property="groupType" typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler"/>
        <result column="music_group_id_" property="musicGroupId"/>
		<result column="course_schedule_id_" property="courseScheduleId" />
		<result column="teacher_role_" property="teacherRole" typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler"/>
		<result column="user_id_" property="userId" />
		<result column="username_" property="userName"/>
		<result column="expect_salary_" property="expectSalary" />
		<result column="create_time_" property="createTime" />
		<result column="update_time_" property="updateTime" />
		<result column="subsidy_" property="subsidy" />
		<result column="actual_salary_" property="actualSalary" />
		<result column="teacher_actual_salary_" property="teacherActualSalary" />
		<result column="settlement_time_" property="settlementTime" />
		<result column="class_group_id_" property="classGroupId" />
		<result column="type_" property="courseSchedule.type" />
		<result column="teach_mode_" property="courseSchedule.teachMode" typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler"/>
		<result column="class_date_" property="courseSchedule.classDate" />
		<result column="start_class_time_" property="courseSchedule.startClassTime" />
		<result column="end_class_time_" property="courseSchedule.endClassTime" />
		<result column="schoole_id_" property="courseSchedule.schoolId" />
		<result column="reduce_salary" property="reduceSalary" />
		<result column="confirm_status_" property="confirmStatus" typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler"/>
		<result column="memo_" property="memo" />
		<result column="deduction_reason_" property="deductionReason"/>
		<result column="belong_to_daya_" property="belongToDaya" />
		<result column="enable_change_salary_" property="enableChangeSalary" />
	</resultMap>
	
	<resultMap type="com.ym.mec.biz.dal.dto.TeacherVipSalaryDto" id="teacherVipSalaryDto" extends="CourseScheduleTeacherSalary">
		<result column="vip_group_category_id_" property="vipGroupCategoryId" />
		<result column="vip_group_category_name_" property="vipGroupCategoryName" />
		<result column="job_nature_" property="teacherJobNature" typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler"/>
		<result column="teach_mode_" property="teachMode" typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler"/>
	</resultMap>
	
	<!-- 根据主键查询一条记录 -->
	<select id="get" resultMap="CourseScheduleTeacherSalary" >
		SELECT * FROM course_schedule_teacher_salary WHERE id_ = #{id} 
	</select>
	
	<!-- 全查询 -->
	<select id="findAll" resultMap="CourseScheduleTeacherSalary">
		SELECT * FROM course_schedule_teacher_salary ORDER BY id_
	</select>
	
	<!-- 向数据库增加一条记录 -->
	<insert id="insert" parameterType="com.ym.mec.biz.dal.entity.CourseScheduleTeacherSalary" useGeneratedKeys="true" keyColumn="id" keyProperty="id">
		INSERT INTO course_schedule_teacher_salary (id_,group_type_,music_group_id_,course_schedule_id_,teacher_role_,
		user_id_,expect_salary_,create_time_,update_time_,subsidy_,actual_salary_,settlement_time_,class_group_id_,
		reduce_salary,confirm_status_,memo_,enable_change_salary_)
		VALUES(#{id},#{groupType,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},#{musicGroupId},#{courseScheduleId},
		#{teacherRole},#{userId},#{expectSalary},now(),now(),#{subsidy},#{actualSalary},#{settlementTime},#{classGroupId},
		#{reduceSalary},#{confirmStatus,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},#{memo},#{enableChangeSalary})
	</insert>
    <insert id="batchInsert" parameterType="java.util.List" useGeneratedKeys="true" keyProperty="id_">
		INSERT INTO course_schedule_teacher_salary (id_,group_type_,music_group_id_,course_schedule_id_,teacher_role_,
		user_id_,expect_salary_,create_time_,update_time_,subsidy_,actual_salary_,settlement_time_,class_group_id_,
		reduce_salary,confirm_status_,memo_,enable_change_salary_)
		VALUES
		<foreach collection="list" item="data" separator=",">
			(#{data.id},#{data.groupType,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},#{data.musicGroupId},#{data.courseScheduleId},
			#{data.teacherRole},#{data.userId},#{data.expectSalary},now(),now(),#{data.subsidy},#{data.actualSalary},#{data.settlementTime},
			#{data.classGroupId},#{data.reduceSalary},#{data.confirmStatus,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},#{data.memo},
			#{data.enableChangeSalary})
		</foreach>
    </insert>

    <!-- 根据主键查询一条记录 -->
	<update id="update" parameterType="com.ym.mec.biz.dal.entity.CourseScheduleTeacherSalary">
		UPDATE course_schedule_teacher_salary 
		<set>
			<if test="userId != null">
				user_id_ = #{userId},
			</if>
            <if test="groupType != null">
                group_type_ = #{groupType,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},
            </if>
            <if test="musicGroupId != null">
                music_group_id_ = #{musicGroupId},
            </if>
			<if test="subsidy != null">
				subsidy_ = #{subsidy},
			</if>
			<if test="expectSalary != null">
				expect_salary_ = #{expectSalary},
			</if>
			<if test="courseScheduleId != null">
				course_schedule_id_ = #{courseScheduleId},
			</if>
			<if test="settlementTime != null">
				settlement_time_ = #{settlementTime},
			</if>
			<if test="teacherRole != null">
				teacher_role_ = #{teacherRole},
			</if>
			<if test="updateTime != null">
				update_time_ = now(),
			</if>
			<if test="actualSalary != null">
				actual_salary_ = #{actualSalary},
			</if>
			<if test="classGroupId != null">
				class_group_id_ = #{classGroupId},
			</if>
			<if test="reduceSalary != null">
				reduce_salary = #{reduceSalary},
			</if>
			<if test="confirmStatus != null">
				confirm_status_ = #{confirmStatus,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},
			</if>
			<if test="memo != null">
				memo_ = #{memo},
			</if>
			<if test="deductionReason != null">
				deduction_reason_ = #{deductionReason},
			</if>
			<if test="belongToDaya != null">
				belong_to_daya_ = #{belongToDaya},
			</if>
			<if test="enableChangeSalary != null">
				enable_change_salary_ = #{enableChangeSalary},
			</if>
		</set> WHERE id_ = #{id} 
	</update>

	<update id="batchUpdateTeacherExpectSalarys">
		<foreach collection="courseScheduleTeacherSalaries" item="courseScheduleTeacherSalary" separator=";">
			UPDATE course_schedule_teacher_salary
				SET expect_salary_ = #{courseScheduleTeacherSalary.expectSalary},update_time_ = NOW()
			WHERE
				id_ = #{courseScheduleTeacherSalary.id}
		</foreach>
	</update>

    <update id="batchUpdate">
        <foreach collection="courseScheduleTeacherSalaries" item="courseScheduleTeacherSalary" separator=";">
            UPDATE course_schedule_teacher_salary
            <set>
                <if test="courseScheduleTeacherSalary.expectSalary!=null">
                    expect_salary_ = #{courseScheduleTeacherSalary.expectSalary},
                </if>
                <if test="courseScheduleTeacherSalary.userId!=null">
                    user_id_ = #{courseScheduleTeacherSalary.userId},
                </if>
				<if test="courseScheduleTeacherSalary.belongToDaya!=null">
					belong_to_daya_ = #{courseScheduleTeacherSalary.belongToDaya},
				</if>
                update_time_ = NOW()
            </set>
            WHERE
            id_ = #{courseScheduleTeacherSalary.id}
        </foreach>
    </update>
	
	<!-- 根据主键删除一条记录 -->
	<delete id="delete" >
		DELETE FROM course_schedule_teacher_salary WHERE id_ = #{id} 
	</delete>

    <delete id="batchDeleteByCourseScheduleIds">
		DELETE FROM course_schedule_teacher_salary WHERE course_schedule_id_ IN
		<foreach collection="courseScheduleIds" item="courseScheduleId" open="(" close=")" separator=",">
			#{courseScheduleId}
		</foreach>
	</delete>

	<delete id="deleteWithCourseAndTeachRole">
		DELETE FROM course_schedule_teacher_salary
		WHERE course_schedule_id_ IN
			<foreach collection="courseIds" item="courseScheduleId" open="(" close=")" separator=",">
				#{courseScheduleId}
			</foreach>
			<if test="teachType!=null">
				AND teacher_role_ = #{teachType, typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler}
			</if>
	</delete>

	<sql id="courseScheduleTeacherSalaryQueryPage">
		<where>
			(cs.del_flag_ != 1 OR cs.del_flag_ IS NULL) AND cs.pre_course_flag_ = 0
			AND csts.settlement_time_ IS NOT NULL AND csts.teacher_role_ IS NOT NULL AND cs.type_ IS NOT NULL
			<if test="organId != null">
				AND su.organ_id_ = #{organId}
			</if>
			<if test="search != null">
				AND (su.username_ LIKE CONCAT('%',#{search},'%') OR su.phone_ LIKE CONCAT('%',#{search},'%'))
			</if>
			<if test="startTime != null">
				AND csts.settlement_time_ &gt;= DATE_FORMAT(#{startTime},'%Y-%m-%d')
			</if>
			<if test="endTime != null">
				AND csts.settlement_time_ &lt;= DATE_FORMAT(#{endTime},'%Y-%m-%d')
			</if>
			<if test="courseScheduleType != null">
				AND cs.type_ = #{courseScheduleType}
			</if>
			<if test="teacherId != null">
				AND csts.user_id_ = #{teacherId}
			</if>
		</where>
	</sql>

    <!-- 分页查询 -->
	<select id="queryPage" resultMap="CourseScheduleTeacherSalary" parameterType="map">
		SELECT * FROM course_schedule_teacher_salary
		<include refid="global.limit"/>
	</select>
	
	<!-- 查询当前表的总记录数 -->
	<select id="queryCount" resultType="int">
		SELECT COUNT(id_) FROM course_schedule_teacher_salary
	</select>

	<select id="countSalaries" resultType="java.lang.Integer">
		SELECT COUNT(csts.id_) FROM course_schedule_teacher_salary csts
		LEFT JOIN sys_user su ON su.id_ = csts.user_id_
		LEFT JOIN course_schedule cs ON csts.course_schedule_id_ = cs.id_
		<include refid="courseScheduleTeacherSalaryQueryPage"/>
	</select>

	<resultMap id="TeacherSalaryDtoMap" type="com.ym.mec.biz.dal.dto.TeacherSalaryDto">
		<result property="username" column="username"/>
		<result property="userId" column="user_id_"/>
		<result property="phone" column="phone_"/>
		<result property="type" column="type_" typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler"/>
		<result property="name" column="name_"/>
		<result property="actualSalary" column="actual_salary_"/>
		<result property="subsidy" column="subsidy_"/>
		<result property="settlementTime" column="settlement_time_"/>
		<result property="teacherRole" column="teacher_role_" typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler"/>
	</resultMap>

	<select id="querySalaries" resultMap="TeacherSalaryDtoMap">
		SELECT su.id_ user_id_,su.username_,su.phone_,cs.type_,cs.name_,csts.actual_salary_,csts.subsidy_,csts.settlement_time_,csts.teacher_role_
		FROM course_schedule_teacher_salary csts
		LEFT JOIN sys_user su ON su.id_ = csts.user_id_
		LEFT JOIN course_schedule cs ON csts.course_schedule_id_ = cs.id_
		<include refid="courseScheduleTeacherSalaryQueryPage"/>
		ORDER BY csts.settlement_time_ DESC
		<include refid="global.limit"/>
	</select>

	<resultMap id="VipGroupSalaryDto" type="com.ym.mec.biz.dal.dto.VipGroupSalaryDto" extends="CourseScheduleTeacherSalary">
		<result column="course_schedule_id_" property="courseScheduleId" />
		<result column="class_date_" property="classDate" />
		<result column="teach_mode_" property="teachMode" typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler" />
	</resultMap>

	<sql id="vipGroupSalaryQueryCondition">
		<where>
			vg.id_=#{vipGroupId}
			AND cs.group_type_='VIP'
			<if test="startTime!=null and endTime!=null">
				AND (cs.class_date_ BETWEEN #{startTime} AND #{endTime})
			</if>
		</where>
	</sql>

	<select id="findVipGroupSalarys" resultMap="VipGroupSalaryDto">
		SELECT

			cs.id_ course_schedule_id_,
			CONCAT(cs.class_date_," ",cs.start_class_time_) class_date_,
			cs.teach_mode_,
			csts.*
		FROM
			vip_group vg
			LEFT JOIN course_schedule cs ON vg.id_ = cs.music_group_id_
			LEFT JOIN course_schedule_teacher_salary csts ON cs.id_ = csts.course_schedule_id_
			<include refid="vipGroupSalaryQueryCondition"/>
		ORDER BY class_date_
		<include refid="global.limit"/>
	</select>
	<select id="countVipGroupSalarys" resultType="int">
		SELECT
			COUNT(*)
		FROM
			vip_group vg
			LEFT JOIN course_schedule cs ON vg.id_ = cs.music_group_id_
		<include refid="vipGroupSalaryQueryCondition"/>
	</select>
    <select id="findSomeDayAgoTeacherCourseSalaryNoSettlement" resultMap="CourseScheduleTeacherSalary">
		SELECT
			cs.class_date_,
			CONCAT(cs.class_date_	,' ',cs.start_class_time_) start_class_time_,
			CONCAT( cs.class_date_, ' ', cs.end_class_time_ ) end_class_time_,
			cs.teach_mode_,
		    cs.schoole_id_,
			csts.*
		FROM
			course_schedule_teacher_salary csts
			LEFT JOIN course_schedule cs ON cs.id_ = csts.course_schedule_id_
			LEFT JOIN vip_group vg ON vg.id_ = cs.music_group_id_
		WHERE 1=1
		<if test="startDate != null">
			AND cs.class_date_ BETWEEN #{startDate} AND #{endDate}
		</if>
		AND cs.group_type_ = 'VIP' AND vg.organ_id_ != 41
		AND csts.settlement_time_ IS NULL
		AND (cs.del_flag_ IS NULL OR cs.del_flag_=0)
    </select>
	<select id="findTemp" resultMap="CourseScheduleTeacherSalary">
		SELECT
		cs.class_date_,
		csts.*
		FROM
		course_schedule_teacher_salary csts
		LEFT JOIN course_schedule cs ON cs.id_ = csts.course_schedule_id_
		WHERE
		cs.id_ IN (961473)
		AND csts.settlement_time_ IS NULL
		AND (cs.del_flag_ IS NULL OR cs.del_flag_=0)
	</select>
    <select id="findTeacherCourseSalaryNoSettlement" resultMap="CourseScheduleTeacherSalary">
		SELECT
		cs.class_date_,
		CONCAT(cs.class_date_	,' ',cs.start_class_time_) start_class_time_,
		CONCAT( cs.class_date_, ' ', cs.end_class_time_ ) end_class_time_,
		cs.schoole_id_,
		csts.*
		FROM
			course_schedule_teacher_salary csts
			LEFT JOIN course_schedule cs ON cs.id_ = csts.course_schedule_id_
		WHERE 1=1
		<if test="startDate != null">
			AND cs.class_date_ BETWEEN #{startDate} AND #{endDate}
		</if>
		AND cs.type_ = 'PRACTICE'
		AND csts.settlement_time_ IS NULL
		AND (cs.del_flag_ IS NULL OR cs.del_flag_=0)
    </select>
    
    <select id="queryByCourseScheduleIdAndUserId" resultMap="CourseScheduleTeacherSalary" parameterType="map">
		SELECT * FROM course_schedule_teacher_salary where user_id_ = #{teacherId} and course_schedule_id_ = #{courseScheduleId}
    </select>

	<select id="findByCourseScheduleIdsAndTeacherRole" resultMap="CourseScheduleTeacherSalary">
		SELECT
			csts.*,
			su.real_name_ username_
		FROM
			course_schedule_teacher_salary csts
			LEFT JOIN sys_user su ON csts.user_id_=su.id_
		WHERE csts.course_schedule_id_ IN
		<foreach collection="courseScheduleIds" item="courseScheduleId" open="(" close=")" separator=",">
			#{courseScheduleId}
		</foreach>
		AND csts.teacher_role_=#{teacherRole}
	</select>

	<select id="findByCourseIdsAndTeacherRole" resultMap="CourseScheduleTeacherSalary">
		SELECT
		csts.*,
		su.real_name_ username_
		FROM
		course_schedule_teacher_salary csts
		LEFT JOIN sys_user su ON csts.user_id_=su.id_
		WHERE csts.course_schedule_id_ IN
		<foreach collection="courseScheduleIds" item="courseScheduleId" open="(" close=")" separator=",">
			#{courseScheduleId}
		</foreach>
		<if test="teacherRole!=null and teacherRole!=''">
			AND csts.teacher_role_=#{teacherRole}
		</if>
	</select>

	<select id="findMusicGroupSettlementTypeByCourse" resultType="map">
		SELECT
			cs.id_ AS 'key',
			mg.settlement_type_ AS 'value'
		FROM
			course_schedule cs
			LEFT JOIN class_group cg ON cs.class_group_id_=cg.id_
			LEFT JOIN music_group mg ON cg.music_group_id_=mg.id_
		WHERE cs.id_ IN
		<foreach collection="courseScheduleIds" item="courseScheduleId" open="(" close=")" separator=",">
			#{courseScheduleId}
		</foreach>
		AND mg.settlement_type_ IS NOT NULL
	</select>
	<select id="findMusicGroupChargeTypeByCourse" resultType="map">
		SELECT
			cs.id_ AS 'key',
			mg.charge_type_id_ AS 'value'
		FROM
			course_schedule cs
			LEFT JOIN class_group cg ON cs.class_group_id_=cg.id_
			LEFT JOIN music_group mg ON cg.music_group_id_=mg.id_
		WHERE cs.id_ IN
		<foreach collection="courseScheduleIds" item="courseScheduleId" open="(" close=")" separator=",">
			#{courseScheduleId}
		</foreach>
	</select>
	<select id="findCourseSubsidyByCourses" resultType="map">
		SELECT
			cs.id_ AS 'key',
			s.subsidy_ AS 'value'
		FROM
			course_schedule cs
			LEFT JOIN school s ON cs.schoole_id_=s.id_
		WHERE cs.id_ IN
		<foreach collection="courseScheduleIds" item="courseScheduleId" open="(" close=")" separator=",">
			#{courseScheduleId}
		</foreach>
	</select>

	<resultMap id="CourseScheduleTeachersDto" type="com.ym.mec.biz.dal.dto.CourseScheduleTeachersDto">
		<result property="teacherId" column="user_id_"/>
		<result property="teacherName" column="real_name_"/>
		<result property="teacherRole" column="teacher_role_"/>
		<result property="expectSalary" column="expect_salary_"/>
		<result property="subsidy" column="subsidy_"/>
	</resultMap>
    <select id="queryTeacherSalary" resultMap="CourseScheduleTeachersDto">
		SELECT csts.user_id_,su.real_name_,csts.teacher_role_,csts.expect_salary_,csts.subsidy_
		FROM course_schedule_teacher_salary csts
		LEFT JOIN sys_user su ON su.id_ = csts.user_id_
		WHERE csts.course_schedule_id_ = #{courseScheduleId}
			<if test="teacherId!=null">
				AND csts.user_id_ = #{teacherId}
			</if>
	</select>
	<select id="findByCourseSchedules" resultMap="CourseScheduleTeacherSalary">
		SELECT * FROM  course_schedule_teacher_salary WHERE course_schedule_id_ IN
		<foreach collection="courseScheduleIds" item="courseScheduleId" open="(" close=")" separator=",">
			#{courseScheduleId}
		</foreach>
	</select>

	<select id="findByCourseSchedule" resultMap="CourseScheduleTeacherSalary">
		SELECT * FROM  course_schedule_teacher_salary WHERE course_schedule_id_=#{courseScheduleId}
	</select>
	<select id="findByCourseSchedulesWithNoSettlement" resultMap="CourseScheduleTeacherSalary">
		SELECT csts.*,CASE WHEN cs.teach_mode_ = 'ONLINE' THEN csts.expect_salary_ * (CASE WHEN (ta.sign_in_status_ IS NULL OR ta.sign_in_status_ = 0) AND (ta.sign_out_status_ IS NULL OR ta.sign_out_status_ = 0) THEN 0
		WHEN ta.sign_in_status_ IS NULL OR ta.sign_in_status_ = 0 THEN 0.5
		WHEN ta.sign_out_status_ IS NULL OR ta.sign_out_status_ = 0 THEN 0.5 ELSE 1 END) ELSE csts.expect_salary_ END teacher_actual_salary_
		FROM course_schedule_teacher_salary csts
			LEFT JOIN teacher_attendance ta ON ta.course_schedule_id_ = csts.course_schedule_id_
			LEFT JOIN course_schedule cs ON cs.id_ = csts.course_schedule_id_
		WHERE csts.course_schedule_id_ IN
		<foreach collection="courseScheduleIds" item="courseScheduleId" open="(" close=")" separator=",">
			#{courseScheduleId}
		</foreach>
		AND csts.settlement_time_ IS NULL
	</select>

    <select id="countTeacherOnlineOfflineSalaryByClass" resultType="map">
		SELECT
			cs.teach_mode_ AS 'key',
			SUM( csts.actual_salary_ ) AS 'value'
		FROM
			course_schedule cs
			LEFT JOIN course_schedule_teacher_salary csts ON cs.id_ = csts.course_schedule_id_
		WHERE cs.class_group_id_=#{classGroupId} AND cs.id_ IS NOT NULL AND cs.status_='OVER' AND cs.pre_course_flag_ = 0
		GROUP BY cs.teach_mode_
    </select>

	<update id="batchUpdateWages" parameterType="map">
		UPDATE course_schedule_teacher_salary csts LEFT JOIN course_schedule cs ON csts.course_schedule_id_ = cs.id_
		SET
			csts.subsidy_ = #{subsidy},
		    csts.enable_change_salary_ = IF(teacher_role_='BISHOP', 0, 1),
		    <if test="salary!=null">
				csts.expect_salary_ = #{salary},
			</if>
			csts.update_time_ = now()
		WHERE csts.user_id_ = #{userId} AND csts.class_group_id_ = #{classGroupId} AND csts.settlement_time_ IS NULL
		  AND CONCAT(cs.class_date_,' ',cs.start_class_time_) &gt;= #{startDateTime} AND cs.pre_course_flag_ = 0
	</update>
	
    <update id="batchUpdateTeacherExpectSalary">
		UPDATE course_schedule_teacher_salary
		SET expect_salary_ = 0,update_time_ = NOW()
		WHERE
			course_schedule_id_ = #{courseScheduleId}
			AND user_id_ IN
			<foreach collection="userIds" item="userId" open="(" close=")" separator=",">
				#{userId}
			</foreach>
	</update>

	<update id="batchDeleteByCourseAndTeacher">
		DELETE FROM course_schedule_teacher_salary
		WHERE course_schedule_id_ = #{courseScheduleId}
		<if test="userIds != null">
		  AND user_id_ IN
		  <foreach collection="userIds" item="userId" open="(" close=")" separator=",">
			  #{userId}
		  </foreach>
		</if>
	</update>
    <update id="batchUpdateTeacher">
		UPDATE course_schedule_teacher_salary csts SET csts.user_id_ = #{teacherId},csts.update_time_ = NOW(),
		csts.expect_salary_ = (SELECT main_teacher_salary_ FROM teacher_default_practice_group_salary WHERE user_id_ = #{teacherId} LIMIT 1)
		WHERE csts.course_schedule_id_ IN
		<foreach collection="courseScheduleIds" item="courseScheduleId" open="(" close=")" separator=",">
			#{courseScheduleId}
		</foreach>
		AND csts.teacher_role_ = 'BISHOP'
	</update>

	<update id="updateTeacherSalaryConfirmStatus">
		UPDATE course_schedule_teacher_salary SET confirm_status_=#{confirmStatus, typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler}
		WHERE group_type_!='COMM'
		AND id_ IN
		<foreach collection="ids" item="id" open="(" close=")" separator=",">
			#{id}
		</foreach>
	</update>
	<update id="updateTeacherSalaryConfirmStatusToComplied">
		UPDATE course_schedule_teacher_salary SET confirm_status_=3
		WHERE confirm_status_!=2
        AND group_type_!='COMM'
		AND id_ IN
		<foreach collection="ids" item="id" open="(" close=")" separator=",">
			#{id}
		</foreach>
	</update>
    <update id="batchUpdateCourseSalarys">
		UPDATE course_schedule_teacher_salary SET expect_salary_ = #{salary} WHERE course_schedule_id_ IN
		<foreach collection="courseIds" item="courseId" separator="," open="(" close=")">
			#{courseId}
		</foreach>
	</update>

    <update id="updateEnableChangeSalaryByClassGroup">
		UPDATE course_schedule_teacher_salary SET enable_change_salary_ = #{enableChangeSalary} WHERE class_group_id_=#{classGroupId}
	</update>

    <select id="findCourseScheduleTeacherSalaryByMusicGroupId" resultMap="CourseScheduleTeacherSalary">
		SELECT * FROM course_schedule_teacher_salary WHERE music_group_id_=#{musicGroupId} AND group_type_ = #{groupType,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler}
	</select>
	<select id="countActualAmount" resultType="java.util.Map">
		SELECT
			course_schedule_id_ AS 'key',
			SUM( actual_price_ ) AS 'value'
		FROM
			course_schedule_student_payment
		WHERE course_schedule_id_ IN
			<foreach collection="courseScheduleIds" item="courseScheduleId" open="(" close=")" separator=",">
				#{courseScheduleId}
			</foreach>
		GROUP BY
			course_schedule_id_
	</select>
	<select id="checkCourseIsSettlement" resultType="int">
		SELECT
			COUNT(*)
		FROM
			course_schedule_teacher_salary
		WHERE
			course_schedule_id_ = #{courseScheduleId}
			AND settlement_time_ IS NOT NULL
	</select>
	<select id="checkCoursesIsSettlement" resultType="java.util.Map">
		SELECT
            course_schedule_id_ AS 'key',
			COUNT(*) AS 'value'
		FROM
			course_schedule_teacher_salary
		WHERE
			settlement_time_ IS NOT NULL
			AND course_schedule_id_ IN
			<foreach collection="courseScheduleIds" item="courseScheduleId" open="(" close=")" separator=",">
				#{courseScheduleId}
			</foreach>
		GROUP BY course_schedule_id_
	</select>
    <select id="findByGroup" resultMap="CourseScheduleTeacherSalary">
		SELECT
			*
		FROM
			course_schedule_teacher_salary
		WHERE
			group_type_ = #{groupType}
			AND music_group_id_ = #{groupId}
	</select>
	<select id="findByGroupWithNotStart" resultMap="CourseScheduleTeacherSalary">
		SELECT
			*
		FROM
			course_schedule_teacher_salary csts
			LEFT JOIN course_schedule cs ON csts.course_schedule_id_=cs.id_
		WHERE
			csts.group_type_ = #{groupType}
			AND csts.music_group_id_ = #{groupId}
			AND CONCAT(cs.class_date_, ' ', cs.start_class_time_)>NOW() AND cs.pre_course_flag_ = 0
	</select>

    <delete id="deleteByMusicGroupId" parameterType="map" >
		DELETE cssp FROM course_schedule_teacher_salary cssp
		LEFT JOIN course_schedule cs on cssp.course_schedule_id_ = cs.id_
		WHERE cssp.music_group_id_=#{musicGroupId} AND cssp.group_type_=#{groupType,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler} and cs.status_ = 'NOT_START'
	</delete>
	<delete id="deleteByGroup">
		DELETE FROM course_schedule_teacher_salary WHERE music_group_id_=#{groupId} AND group_type_=#{groupType,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler}
	</delete>
	<delete id="deletePreCourse">
		DELETE FROM course_schedule_teacher_salary WHERE course_schedule_id_ IN (
			SELECT id_ FROM course_schedule WHERE music_group_id_ = #{musicGroupId} AND group_type_ = 'MUSIC' AND pre_course_flag_ = 1)
	</delete>

    <select id="queryUserNoSettlementListByTypeAndStartClassDate" resultMap="CourseScheduleTeacherSalary">
		SELECT ts.*,cs.type_,cs.teach_mode_,cs.class_date_,cs.start_class_time_,cs.end_class_time_
		FROM course_schedule_teacher_salary ts
		LEFT JOIN course_schedule cs ON ts.course_schedule_id_ = cs.id_
		WHERE FIND_IN_SET(cs.type_, #{typeList}) AND ts.user_id_ = #{userId} AND ts.settlement_time_ IS NULL AND cs.class_date_ &gt;= #{startClassDate} AND cs.pre_course_flag_ = 0
	</select>
    <select id="findByTeacher" resultMap="CourseScheduleTeacherSalary">
		SELECT
			CONCAT(cs.class_date_,' ',cs.start_class_time_) start_class_time_,
			CONCAT(cs.class_date_,' ',cs.end_class_time_) end_class_time_,
			csts.*
		FROM
			course_schedule cs
			LEFT JOIN course_schedule_teacher_salary csts ON cs.id_ = csts.course_schedule_id_
		WHERE
			cs.music_group_id_ = #{groupId}
			AND cs.group_type_ = #{groupType,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler}
			AND cs.type_ = #{courseType,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler}
			AND csts.user_id_ IN
			<foreach collection="teacherIds" item="teacherId" open="(" close=")" separator=",">
				#{teacherId}
			</foreach>
		  ORDER BY csts.create_time_ DESC
    </select>
    <select id="findStudentRelateTeachers" resultMap="CourseScheduleTeacherSalary">
		SELECT
			csts.*
		FROM
			course_schedule_student_payment cssp
			LEFT JOIN course_schedule_teacher_salary csts ON cssp.course_schedule_id_=csts.course_schedule_id_
		WHERE cssp.user_id_=#{studentId} AND csts.user_id_ IN
		<foreach collection="teacherIds" item="teacherId" open="(" close=")" separator=",">
			#{teacherId}
		</foreach>
    </select>
	<select id="countTeacherCourses" resultType="java.util.Map">
		SELECT
			user_id_ AS 'key',
			COUNT(course_schedule_id_) AS 'value'
		FROM
			course_schedule_teacher_salary
		WHERE
			user_id_ IN
			<foreach collection="teacherIds" item="teacherId" open="(" close=")" separator=",">
				#{teacherIds}
			</foreach>
			AND group_type_ = #{groupType,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler}
		GROUP BY
			user_id_
	</select>
	<resultMap id="ExportTeacherSalaryDtoMap" type="com.ym.mec.biz.dal.dto.ExportTeacherSalaryDto">
		<result property="signOutTime" column="signOutTime"/>
		<result property="signInTime" column="signInTime"/>
		<result property="studentSignInTime" column="studentSignInTime"/>
		<result property="studentSignOutTime" column="studentSignOutTime"/>
		<result property="complaintsStatus" column="complaints_status_"/>
		<result property="disposeContent" column="dispose_content_"/>
		<result property="deductionReason" column="deduction_reason_"/>
		<result property="teacherName" column="teacherName"/>
		<result property="groupName" column="groupName"/>
		<result property="groupId" column="group_id_"/>
		<result property="studentNum" column="studentNum"/>
		<result property="realStudentNum" column="realStudentNum"/>
		<result property="teacherId" column="teacherId"/>
		<result property="activeName" column="activeName"/>
		<result property="vipActiveName" column="vip_active_name_"/>
		<result property="vipCategoryName" column="vip_category_name_"/>
		<result property="courseScheduleType" column="courseScheduleType"/>
		<result property="signOutStatus" column="signOutStatus"/>
		<result property="signInStatus" column="signInStatus"/>
		<result property="teacherRole" column="teacherRole"/>
		<result property="classDate" column="classDate"/>
		<result property="teachMode" column="teach_mode_"/>
		<result property="courseScheduleId" column="courseScheduleId"/>
		<result property="jobType" column="jobType"/>
		<result property="address" column="address"/>
		<result property="price" column="price"/>
		<result property="changeType" column="changeType"/>
		<result property="courseScheduleOrganName" column="courseScheduleOrganName"/>
		<result property="courseScheduleStartTime" column="courseScheduleStartTime"/>
		<result property="courseScheduleEndTime" column="courseScheduleEndTime"/>
		<result property="signCourseScheduleTime" column="signCourseScheduleTime"/>
		<result property="currentTime" column="currentTime"/>
		<result property="teacherOrganName" column="teacherOrganName"/>
		<result property="onlineClassesUnitPrice" column="online_classes_unit_price_"/>
		<result property="offlineClassesUnitPrice" column="offline_classes_unit_price_"/>
 	</resultMap>
    <select id="exportMusicTeacherSalary" resultMap="ExportTeacherSalaryDtoMap">
		SELECT cs.`id_` courseScheduleId,ts.user_id_ teacherId,mgo.name_ courseScheduleOrganName,mg.name_ groupName,
		ct.name_ changeType,u.real_name_ teacherName,o.name_ teacherOrganName,cs.music_group_id_ group_id_,
		CASE WHEN cs.teach_mode_ = 'OFFLINE' THEN '线下' WHEN cs.teach_mode_ = 'ONLINE' THEN '线上' END teach_mode_,
		CASE WHEN t.job_nature_ = 'PART_TIME' THEN '兼职' WHEN t.job_nature_ = 'FULL_TIME' THEN '全职' ELSE '零时工' END jobType,
		CASE WHEN cs.type_ = 'SINGLE' THEN '单技课' WHEN cs.type_ = 'MIX' THEN '合奏课' WHEN cs.type_ = 'HIGH' THEN '小班课'  WHEN cs.type_ = 'VIP' THEN 'vip课'
		WHEN cs.type_ = 'DEMO' THEN '试听课'  WHEN cs.type_ = 'COMPREHENSIVE' THEN '综合课'  WHEN cs.type_ = 'PRACTICE' THEN '练习课'  WHEN cs.type_ = 'ENLIGHTENMENT' THEN '启蒙课'
		WHEN cs.type_ = 'TRAINING_SINGLE' THEN '集训单技课'  WHEN cs.type_ = 'TRAINING_MIX' THEN '集训合奏课' WHEN cs.type_ = 'CLASSROOM' THEN '课堂课'
		WHEN cs.type_ = 'COMM' THEN '对外课程'  WHEN cs.type_ = 'HIGH_ONLINE' THEN '网络基础训练课'  ELSE '乐团网管课' END courseScheduleType,
		cs.class_date_ classDate,cs.start_class_time_ courseScheduleStartTime,cs.end_class_time_ courseScheduleEndTime,
		ROUND((UNIX_TIMESTAMP(cs.end_class_time_)-UNIX_TIMESTAMP(cs.start_class_time_))/60) signCourseScheduleTime,
		ts.actual_salary_ price,CASE WHEN s.name_ IS NULL THEN '网络教室' ELSE s.name_ END address,
		CASE WHEN ts.teacher_role_ = 'TEACHING' THEN '助教' ELSE '主教' END teacherRole,
		CASE WHEN ta.sign_in_status_ = 0 THEN '异常' WHEN ta.sign_in_status_ = 1 THEN '正常' END signInStatus,
		CASE WHEN ta.sign_out_status_=0 THEN '异常' WHEN ta.sign_out_status_ = 1 THEN '正常' END signOutStatus,
		CASE WHEN ta.complaints_status_=0 THEN '拒绝' WHEN ta.complaints_status_ = 1 THEN '通过'
		WHEN ta.complaints_status_=2 THEN '待处理' WHEN ta.complaints_status_ = 3 THEN '已撤销' END complaints_status_,ta.dispose_content_,
		ROUND((UNIX_TIMESTAMP(ta.sign_out_time_)-UNIX_TIMESTAMP(ta.sign_in_time_))/60) currentTime,
		ta.sign_in_time_ signInTime,ta.sign_out_time_ signOutTime,COUNT(DISTINCT sp.id_) studentNum,ts.deduction_reason_
		FROM course_schedule_teacher_salary ts
		LEFT JOIN course_schedule cs ON ts.course_schedule_id_ = cs.id_
		LEFT JOIN `teacher_attendance` ta ON ta.`teacher_id_` = ts.`user_id_` AND ta.`course_schedule_id_` = ts.`course_schedule_id_`
		LEFT JOIN class_group cg ON cs.class_group_id_ = cg.id_
		LEFT JOIN sys_user u ON ts.user_id_= u.id_
		LEFT JOIN teacher t ON t.id_ = ts.user_id_
		LEFT JOIN organization o ON o.id_ = t.organ_id_
		LEFT JOIN school s ON s.id_ = cs.schoole_id_
		LEFT JOIN course_schedule_student_payment sp ON sp.course_schedule_id_ = ts.course_schedule_id_
		LEFT JOIN music_group mg ON (mg.id_ = ts.music_group_id_ AND ts.group_type_ = 'MUSIC')
		LEFT JOIN organization mgo ON mgo.id_ = mg.organ_id_
		LEFT JOIN charge_type ct ON ct.id_ = mg.charge_type_id_
		WHERE cs.class_date_ BETWEEN #{firstDayOfMonth} AND #{lastDayOfMonth} AND cs.`del_flag_` != 1 AND cs.group_type_ = 'MUSIC' AND ts.settlement_time_ IS NOT NULL
		AND t.is_settlement_salary_ = 1
		<if test="courseTypeList != null">
			and cs.type_ in (#{courseTypeList})
		</if>
		GROUP BY ts.id_,ta.id_
	</select>
	<select id="exportVipTeacherSalary" resultMap="ExportTeacherSalaryDtoMap">
		SELECT cs.`id_` courseScheduleId,ts.user_id_ teacherId,vgo.name_ courseScheduleOrganName,vg.name_ groupName,
		u.real_name_ teacherName,o.name_ teacherOrganName,cs.music_group_id_ group_id_,
		CASE WHEN cs.teach_mode_ = 'OFFLINE' THEN '线下' WHEN cs.teach_mode_ = 'ONLINE' THEN '线上' END teach_mode_,
		CASE WHEN t.job_nature_ = 'PART_TIME' THEN '兼职' WHEN t.job_nature_ = 'FULL_TIME' THEN '全职' ELSE '零时工' END jobType,
		'vip课' courseScheduleType,
		cs.class_date_ classDate,cs.start_class_time_ courseScheduleStartTime,cs.end_class_time_ courseScheduleEndTime,
		ROUND((UNIX_TIMESTAMP(cs.end_class_time_)-UNIX_TIMESTAMP(cs.start_class_time_))/60) signCourseScheduleTime,
		ts.actual_salary_ price,vga.name_ vip_active_name_,vgc.name_ vip_category_name_,
		CASE WHEN s.name_ IS NULL THEN '网络教室' ELSE s.name_ END address,
		CASE WHEN ts.teacher_role_ = 'TEACHING' THEN '助教' ELSE '主教' END teacherRole,
		CASE WHEN ta.sign_in_status_ = 0 THEN '异常' WHEN ta.sign_in_status_ = 1 THEN '正常' END signInStatus,
		CASE WHEN ta.sign_out_status_=0 THEN '异常' WHEN ta.sign_out_status_ = 1 THEN '正常' END signOutStatus,
		CASE WHEN ta.complaints_status_=0 THEN '拒绝' WHEN ta.complaints_status_ = 1 THEN '通过'
		WHEN ta.complaints_status_=2 THEN '待处理' WHEN ta.complaints_status_ = 3 THEN '已撤销' END complaints_status_,ta.dispose_content_,
		ROUND((UNIX_TIMESTAMP(ta.sign_out_time_)-UNIX_TIMESTAMP(ta.sign_in_time_))/60) currentTime,
		ta.sign_in_time_ signInTime,ta.sign_out_time_ signOutTime,COUNT(DISTINCT sp.id_) studentNum,
		vg.online_classes_unit_price_,vg.offline_classes_unit_price_,
		GROUP_CONCAT(sa.sign_in_time_) studentSignInTime,GROUP_CONCAT(sa.sign_out_time_) studentSignOutTime,ts.deduction_reason_
		FROM course_schedule_teacher_salary ts
		LEFT JOIN course_schedule cs ON ts.course_schedule_id_ = cs.id_
		LEFT JOIN `teacher_attendance` ta ON ta.`teacher_id_` = ts.`user_id_` AND ta.`course_schedule_id_` = ts.`course_schedule_id_`
		LEFT JOIN class_group cg ON cs.class_group_id_ = cg.id_
		LEFT JOIN sys_user u ON ts.user_id_= u.id_
		LEFT JOIN teacher t ON t.id_ = ts.user_id_
		LEFT JOIN organization o ON o.id_ = t.organ_id_
		LEFT JOIN school s ON s.id_ = cs.schoole_id_
		LEFT JOIN course_schedule_student_payment sp ON sp.course_schedule_id_ = ts.course_schedule_id_
		LEFT JOIN `student_attendance` sa ON sa.`course_schedule_id_` = ts.`course_schedule_id_`
		LEFT JOIN vip_group vg ON (vg.id_ = ts.music_group_id_ AND ts.group_type_ = 'VIP')
		LEFT JOIN vip_group_activity vga ON vg.vip_group_activity_id_ = vga.id_
		LEFT JOIN organization vgo ON vgo.id_ = vg.organ_id_
		left join vip_group_category vgc on vgc.id_ = vg.vip_group_category_id_
		WHERE vg.organ_id_ != 41 AND cs.class_date_ BETWEEN #{firstDayOfMonth} AND #{lastDayOfMonth} AND cs.`del_flag_` != 1
		AND cs.group_type_ = 'VIP' AND ts.settlement_time_ IS NOT NULL
		AND t.is_settlement_salary_ = 1
		<if test="courseTypeList != null">
			and cs.type_ in (#{courseTypeList})
		</if>
		GROUP BY ts.id_,ta.id_
	</select>
	<select id="querySettlementScheduleId" resultType="java.lang.Long">
		SELECT DISTINCT csts.course_schedule_id_ FROM course_schedule_teacher_salary csts
		WHERE csts.settlement_time_ IS NULL AND FIND_IN_SET(csts.course_schedule_id_,#{courseScheduleIds})
	</select>
	<select id="exportPracticeTeacherSalary" resultMap="ExportTeacherSalaryDtoMap">
		SELECT cs.`id_` courseScheduleId,ts.user_id_ teacherId,pgo.name_ courseScheduleOrganName,pg.name_ groupName,
		u.real_name_ teacherName,o.name_ teacherOrganName,cs.music_group_id_ group_id_,
		CASE WHEN cs.teach_mode_ = 'OFFLINE' THEN '线下' WHEN cs.teach_mode_ = 'ONLINE' THEN '线上' END teach_mode_,
		CASE WHEN t.job_nature_ = 'PART_TIME' THEN '兼职' WHEN t.job_nature_ = 'FULL_TIME' THEN '全职' ELSE '零时工' END jobType,
		'网管课' courseScheduleType,
		cs.class_date_ classDate,cs.start_class_time_ courseScheduleStartTime,cs.end_class_time_ courseScheduleEndTime,
		ROUND((UNIX_TIMESTAMP(cs.end_class_time_)-UNIX_TIMESTAMP(cs.start_class_time_))/60) signCourseScheduleTime,
		ts.actual_salary_ price,CASE WHEN s.name_ IS NULL THEN '网络教室' ELSE s.name_ END address,
		CASE WHEN ts.teacher_role_ = 'TEACHING' THEN '助教' ELSE '主教' END teacherRole,
		CASE WHEN ta.sign_in_status_ = 0 THEN '异常' WHEN ta.sign_in_status_ = 1 THEN '正常' END signInStatus,
		CASE WHEN ta.sign_out_status_= 0 THEN '异常' WHEN ta.sign_out_status_ = 1 THEN '正常' END signOutStatus,
		CASE WHEN ta.complaints_status_=0 THEN '拒绝' WHEN ta.complaints_status_ = 1 THEN '通过'
		WHEN ta.complaints_status_=2 THEN '待处理' WHEN ta.complaints_status_ = 3 THEN '已撤销' END complaints_status_,ta.dispose_content_,
		ROUND((UNIX_TIMESTAMP(ta.sign_out_time_)-UNIX_TIMESTAMP(ta.sign_in_time_))/60) currentTime,
		ta.sign_in_time_ signInTime,ta.sign_out_time_ signOutTime,COUNT(DISTINCT sp.id_) studentNum,
		GROUP_CONCAT(sa.sign_in_time_) studentSignInTime,GROUP_CONCAT(sa.sign_out_time_) studentSignOutTime,ts.deduction_reason_
		FROM course_schedule_teacher_salary ts
		LEFT JOIN course_schedule cs ON ts.course_schedule_id_ = cs.id_
		LEFT JOIN `teacher_attendance` ta ON ta.`teacher_id_` = ts.`user_id_` AND ta.`course_schedule_id_` = ts.`course_schedule_id_`
		LEFT JOIN class_group cg ON cs.class_group_id_ = cg.id_
		LEFT JOIN sys_user u ON ts.user_id_= u.id_
		LEFT JOIN teacher t ON t.id_ = ts.user_id_
		LEFT JOIN organization o ON o.id_ = t.organ_id_
		LEFT JOIN school s ON s.id_ = cs.schoole_id_
		LEFT JOIN course_schedule_student_payment sp ON sp.course_schedule_id_ = ts.course_schedule_id_
		LEFT JOIN `student_attendance` sa ON sa.`course_schedule_id_` = ts.`course_schedule_id_`
		LEFT JOIN practice_group pg ON (pg.id_ = ts.music_group_id_ AND ts.group_type_ = 'PRACTICE')
		LEFT JOIN organization pgo ON pgo.id_ = pg.organ_id_
		WHERE cs.class_date_ BETWEEN #{firstDayOfMonth} AND #{lastDayOfMonth} AND cs.`del_flag_` != 1 AND cs.group_type_ = 'PRACTICE' AND ts.settlement_time_ IS NOT NULL
		AND t.is_settlement_salary_ = 1
		<if test="courseTypeList != null">
			and cs.type_ in (#{courseTypeList})
		</if>
		GROUP BY ts.id_,ta.id_
	</select>
	
	<select id="queryOnlineCourseByGroupType" resultMap="teacherVipSalaryDto" parameterType="map">
		SELECT csts.*,cs.teach_mode_,vgc.name_ vip_group_category_name_,vg.vip_group_category_id_, t.job_nature_
		FROM `course_schedule_teacher_salary` csts LEFT JOIN `course_schedule` cs on csts.`course_schedule_id_` = cs.`id_`
		left join vip_group vg on vg.id_ = csts.music_group_id_ and csts.group_type_ = 'VIP'
		left join vip_group_category vgc on vgc.id_ = vg.vip_group_category_id_
		left join teacher t on t.id_ = csts.user_id_ 
		WHERE cs.`group_type_` = 'VIP'
		  AND csts.settlement_time_ IS NULL
		  and cs.class_date_ between #{startDate} and #{endDate}
	</select>

    <select id="isTeacher" resultType="java.lang.Boolean">
		SELECT COUNT(DISTINCT csts.user_id_) FROM class_group_teacher_mapper csts
		WHERE csts.class_group_id_ = #{groupId} AND csts.user_id_ = #{userId}
	</select>
    <select id="countTeacherGiveLesson" resultType="java.lang.Integer">
		SELECT COUNT(DISTINCT csts.id_) FROM course_schedule_teacher_salary csts
		LEFT JOIN course_schedule cs ON cs.id_ = csts.course_schedule_id_
		WHERE NOW() >= CONCAT(cs.class_date_ ,' ',cs.end_class_time_) AND csts.user_id_ = #{userId} AND cs.pre_course_flag_ = 0
	</select>

	<resultMap id="TeacherCourseSalaryDetail4WebDto" type="com.ym.mec.biz.dal.dto.TeacherCourseSalaryDetail4WebDto">
		<result property="id" column="id_" />
		<result property="organId" column="organ_id_" />
		<result property="organName" column="organ_name_" />
		<result property="groupType" column="group_type_" typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler"/>
		<result property="courseScheduleId" column="course_schedule_id_" />
		<result property="startClassTime" column="classes_start_time_" />
		<result property="endClassTime" column="classes_end_time_" />
		<result property="courseName" column="course_name_" />
		<result property="teacherId" column="teacher_id_" />
		<result property="teacherName" column="teacher_name_" />
		<result property="signInTime" column="sign_in_time_" />
		<result property="signInStatus" column="sign_in_status_" typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler"/>
		<result property="signOutStatus" column="sign_out_status_" typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler"/>
		<result property="signOutTime" column="sign_out_time_" />
		<result property="expectSalary" column="expect_salary_" />
		<result property="actualSalary" column="actual_salary_" />
		<result property="reduceSalary" column="reduce_salary" />
		<result property="confirmStatus" column="confirm_status_" typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler"/>
		<result property="memo" column="memo_" />
		<result property="deductionReason" column="deduction_reason_" />
		<result property="belongDaya" column="belong_to_daya_" />
	</resultMap>

	<sql id="queryCourseSalaryCondition">
		<where>
			(cs.del_flag_ IS NULL OR cs.del_flag_=0)
            AND csts.group_type_!='COMM'
			AND csts.settlement_time_ IS NOT NULL
			AND t.is_settlement_salary_ = 1
			<if test="teacherId!=null">
				AND csts.user_id_=#{teacherId}
			</if>
			<if test="groupType!=null">
				AND cs.group_type_=#{groupType,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler}
			</if>
			<if test="courseScheduleId!=null">
				AND cs.id_=#{courseScheduleId}
			</if>
			<if test="signInStatus!=null and signInStatus==1">
				AND ta.sign_in_status_=#{signInStatus}
			</if>
            <if test="signInStatus!=null and signInStatus==0">
                AND (ta.sign_in_status_=#{signInStatus} OR ta.sign_in_status_ IS NULL)
            </if>
			<if test="signOutStatus!=null and signOutStatus==1">
				AND ta.sign_out_status_=#{signOutStatus}
			</if>
            <if test="signOutStatus!=null and signOutStatus==0">
                AND (ta.sign_out_status_=#{signOutStatus} OR ta.sign_out_status_ IS NULL)
            </if>
			<if test="confirmStatus!=null">
				AND csts.confirm_status_=#{confirmStatus}
			</if>
			<if test="groupName!=null">
				AND cs.name_ LIKE CONCAT('%', #{groupName}, '%')
			</if>
			<if test="month!=null">
				AND DATE_FORMAT(cs.class_date_, '%Y-%m') = #{month}
			</if>
			<if test="organIdList">
				AND FIND_IN_SET(t.organ_id_,#{organIdList})
			</if>
			<if test="search!=null">
				AND (csts.course_schedule_id_=#{search} OR cs.name_ LIKE CONCAT('%', #{search}, '%'))
			</if>
			<if test="belongDaya!=null">
				AND csts.belong_to_daya_ = #{belongDaya}
			</if>
		</where>
	</sql>

	<select id="findIsSettlementCourseSalarys" resultMap="TeacherCourseSalaryDetail4WebDto">
		SELECT
			csts.id_,
			cs.id_ course_schedule_id_,
			cs.group_type_,
			CONCAT(cs.class_date_, ' ', cs.start_class_time_) classes_start_time_,
			CONCAT(cs.class_date_, ' ', cs.end_class_time_) classes_end_time_,
			cs.name_ course_name_,
			csts.user_id_ teacher_id_,
			su.real_name_ teacher_name_,
			t.organ_id_,
			o.name_ organ_name_,
			ta.sign_in_time_,
			ta.sign_in_status_,
			ta.sign_out_time_,
			ta.sign_out_status_,
			csts.expect_salary_,
			csts.actual_salary_,
			csts.reduce_salary,
			csts.confirm_status_,
			csts.memo_,
			csts.deduction_reason_,
		    csts.belong_to_daya_
		FROM
			course_schedule_teacher_salary csts
			LEFT JOIN course_schedule cs ON csts.course_schedule_id_=cs.id_
			LEFT JOIN teacher_attendance ta ON csts.course_schedule_id_=ta.course_schedule_id_ AND csts.user_id_ = ta.teacher_id_
			LEFT JOIN teacher t ON csts.user_id_=t.id_
			LEFT JOIN sys_user su ON csts.user_id_=su.id_
			LEFT JOIN organization o ON t.organ_id_=o.id_
		<include refid="queryCourseSalaryCondition"/>
		ORDER BY classes_start_time_,cs.id_ DESC
		<include refid="global.limit"/>
	</select>

	<select id="countIsSettlementCourseSalarys" resultType="int">
		SELECT
			COUNT(csts.id_)
		FROM
			course_schedule_teacher_salary csts
		LEFT JOIN course_schedule cs ON csts.course_schedule_id_=cs.id_
		LEFT JOIN teacher_attendance ta ON csts.course_schedule_id_=ta.course_schedule_id_ AND csts.user_id_ = ta.teacher_id_
		LEFT JOIN teacher t ON csts.user_id_=t.id_
		LEFT JOIN sys_user su ON csts.user_id_=su.id_
		<include refid="queryCourseSalaryCondition"/>
		ORDER BY csts.id_ DESC
		<include refid="global.limit"/>
	</select>

	<resultMap id="CourseTeacherSalaryStatisticsDto" type="com.ym.mec.biz.dal.dto.CourseTeacherSalaryStatisticsDto">
		<result property="groupType" column="group_type_" typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler"/>
		<result property="statType" column="group_type_"/>
		<result property="month" column="month_"/>
		<result property="courseTimes" column="course_times_"/>
		<result property="confirmStatus" column="confirm_status_" typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler"/>
		<result property="totalActualSalary" column="total_actual_salary_"/>
		<result property="totalReduceSalary" column="total_reduce_salary_"/>
	</resultMap>

    <select id="getTeacherGroupTypeSalary" resultMap="CourseTeacherSalaryStatisticsDto">
		SELECT
			csts.group_type_,
			COUNT(DISTINCT csts.course_schedule_id_) course_times_,
			SUM(actual_salary_) total_actual_salary_,
			SUM(reduce_salary) total_reduce_salary_
		FROM
			course_schedule_teacher_salary csts
			LEFT JOIN course_schedule cs ON csts.course_schedule_id_ = cs.id_
		WHERE
			( cs.del_flag_ IS NULL OR cs.del_flag_ = 0 )
			AND csts.settlement_time_ IS NOT NULL
			AND csts.user_id_ = #{teacherId}
			AND DATE_FORMAT( cs.class_date_, '%Y-%m' ) = #{month}
			GROUP BY csts.group_type_
	</select>
	<select id="getTeacherMonthSalarys" resultMap="CourseTeacherSalaryStatisticsDto">
		SELECT
			DATE_FORMAT( cs.class_date_, '%Y-%m' ) month_,
			SUM(actual_salary_) total_actual_salary_,
			SUM(reduce_salary) total_reduce_salary_,MAX(csts.confirm_status_) confirm_status_
		FROM
			course_schedule_teacher_salary csts
			LEFT JOIN course_schedule cs ON csts.course_schedule_id_ = cs.id_
		WHERE
			( cs.del_flag_ IS NULL OR cs.del_flag_ = 0 )
			AND (cs.is_lock_=0 OR cs.is_lock_ IS NULL)
			AND (cs.new_course_id_ IS NULL OR cs.new_course_id_=cs.id_)
			AND csts.settlement_time_ IS NOT NULL
			AND csts.user_id_ = #{teacherId}
			AND DATE_FORMAT( cs.class_date_, '%Y' ) = #{year}
			AND csts.confirm_status_ IS NOT NULL
		GROUP BY DATE_FORMAT( cs.class_date_, '%Y-%m' )
	</select>
	<select id="findTeacherSalaryIdsWithMonth" resultType="java.lang.Long">
		SELECT
			csts.id_
		FROM
			course_schedule_teacher_salary csts
			LEFT JOIN course_schedule cs ON csts.course_schedule_id_ = cs.id_
		WHERE
			( cs.del_flag_ IS NULL OR cs.del_flag_ = 0 )
			AND csts.group_type_!='COMM'
			AND csts.settlement_time_ IS NOT NULL
			<if test="teacherId!=null">
				AND csts.user_id_ = #{teacherId}
			</if>
			AND DATE_FORMAT( cs.class_date_, '%Y-%m' ) = #{month}
	</select>
    <select id="findIsSettlementTeacherIdsWithMonth" resultType="int">
        SELECT
          DISTINCT csts.user_id_
        FROM
        course_schedule_teacher_salary csts
        LEFT JOIN course_schedule cs ON csts.course_schedule_id_ = cs.id_
        WHERE
        	( cs.del_flag_ IS NULL OR cs.del_flag_ = 0 )
			AND csts.group_type_!='COMM'
        	AND csts.settlement_time_ IS NOT NULL
        	AND DATE_FORMAT( cs.class_date_, '%Y-%m' ) = #{month}
    </select>

	<select id="countOpenConfirmSalarysWithMonth" resultType="int">
		SELECT
			COUNT(csts.id_)
		FROM
			course_schedule_teacher_salary csts
			LEFT JOIN course_schedule cs ON cs.id_ = csts.course_schedule_id_
		WHERE
			DATE_FORMAT( cs.class_date_, '%Y-%m' ) = #{month}
			AND csts.group_type_!='COMM'
			AND csts.settlement_time_ IS NOT NULL
			<if test="confirmStatus!=null">
				AND csts.confirm_status_=#{confirmStatus,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler}
			</if>
			<if test="confirmStatus==null">
				AND csts.confirm_status_ IS NULL
			</if>
	</select>

    <select id="getCoursesSettlementTime" resultType="java.util.Map">
		SELECT course_schedule_id_ 'key',DATE_FORMAT(MAX(settlement_time_), "%Y-%m-%d %H:%i:%s") 'value' FROM course_schedule_teacher_salary
		WHERE course_schedule_id_ IN
		<foreach collection="courseScheduleIds" item="courseScheduleId" open="(" close=")" separator=",">
			#{courseScheduleId}
		</foreach>
		GROUP BY course_schedule_id_;
	</select>
    <select id="getHasTeacherSalaryCount" resultType="int">
		SELECT COUNT(*) FROM course_schedule_teacher_salary
		WHERE course_schedule_id_ IN
		<foreach collection="courseScheduleIds" item="courseScheduleId" open="(" close=")" separator=",">
			#{courseScheduleId}
		</foreach>
	</select>
    <select id="queryTeacherByTeaching" resultType="java.lang.Integer">
		SELECT DISTINCT cgtm.user_id_ FROM class_group_teacher_mapper cgtm
		WHERE cgtm.class_group_id_ = #{classGroupId}
		<if test="teacherRole != null">
			AND cgtm.teacher_role_ = #{teacherRole}
		</if>
	</select>
    <select id="queryTeacherByBishop" resultType="java.lang.Integer">
		SELECT DISTINCT cgtm.user_id_ FROM class_group_teacher_mapper cgtm
		WHERE cgtm.class_group_id_ = #{classGroupId} AND cgtm.teacher_role_ = 'BISHOP';
	</select>
	<select id="queryTeacherIdMap" resultType="java.util.Map">
		SELECT course_schedule_id_ 'key',GROUP_CONCAT(DISTINCT user_id_) 'value' FROM course_schedule_teacher_salary
		WHERE course_schedule_id_ IN
		<foreach collection="courseScheduleIds" open="(" close=")" separator="," item="item">
			#{item}
		</foreach>
		<if test="teacherRole != null">
			AND teacher_role_ = #{teacherRole}
		</if>
		GROUP BY course_schedule_id_
	</select>
	<select id="queryTeacherNameMap" resultType="java.util.Map">
		SELECT course_schedule_id_ 'key',GROUP_CONCAT(DISTINCT su.real_name_) 'value'
		FROM course_schedule_teacher_salary csts
		LEFT JOIN sys_user su ON su.id_ = csts.user_id_
		WHERE course_schedule_id_ IN
		<foreach collection="courseScheduleIds" open="(" close=")" separator="," item="item">
			#{item}
		</foreach>
		<if test="teacherRole != null">
			AND teacher_role_ = #{teacherRole}
		</if>
		GROUP BY course_schedule_id_
	</select>

	<select id="getIsSalaryWithDate" resultMap="CourseScheduleTeacherSalary">
		SELECT
			csts.*,
			CONCAT(cs.class_date_,' ',cs.start_class_time_) start_class_time_
		FROM
			course_schedule_teacher_salary csts
			LEFT JOIN course_schedule cs ON csts.course_schedule_id_ = cs.id_
		WHERE
			cs.class_date_ BETWEEN #{startDate} AND #{endDate}
		  AND ( cs.del_flag_ IS NULL OR cs.del_flag_ = 0 )
		  AND csts.settlement_time_ IS NOT NULL
	</select>

    <select id="getTeacherSalaryOverview" resultType="com.ym.mec.biz.dal.dto.TeacherIncomeReviewDto">
		SELECT
			SUM(csts.actual_salary_) totalIncome,
			SUM((CASE WHEN 1=1
					<if test="startClassDay!=null and startClassDay!=''">
			    		AND cs.class_date_ &gt;= #{startDay}
					</if>
					<if test="endDay!=null and endDay!=''">
						AND cs.class_date_ &lt;= #{endDay}
					</if>
			    	<if test="teachIncomeType!=null and teachIncomeType=='SUBSIDY'">
						AND IF(csts.settlement_time_ IS NULL, s.subsidy_ IS NOT NULL AND s.subsidy_>0, csts.subsidy_ IS NOT NULL AND csts.subsidy_>0)
					</if>
			    THEN csts.expect_salary_ + IF(s.subsidy_ IS NULL, 0, s.subsidy_) ELSE 0 END)) thisMonthExpectIncome
		FROM
			course_schedule_teacher_salary csts
			LEFT JOIN course_schedule cs ON csts.course_schedule_id_ = cs.id_
			LEFT JOIN school s ON cs.schoole_id_=s.id_
		WHERE
			( cs.del_flag_ IS NULL OR cs.del_flag_ = 0 )
			AND (cs.is_lock_=0 OR cs.is_lock_ IS NULL)
			AND (cs.new_course_id_ IS NULL OR cs.new_course_id_=cs.id_)
			AND csts.user_id_ = #{teacherId} AND cs.pre_course_flag_ = 0
			<if test="startClassDay!=null and startClassDay!=''">
				AND cs.class_date_ &gt;= #{startClassDay}
			</if>
			<if test="courseStatus!=null">
				AND cs.status_ = #{courseStatus, typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler}
			</if>
	</select>

	<select id="teacherIncomeStat" resultType="com.ym.mec.biz.dal.dto.LocalDateBigDecimalMapDto">
		SELECT
			<if test="month==null">
				CONCAT(DATE_FORMAT( cs.class_date_, '%Y-%m' ), '-01') date,
				CONCAT(DATE_FORMAT( cs.class_date_, '%Y-%m' )) dateStr,
			</if>
			<if test="month!=null">
				DATE_FORMAT( cs.class_date_, '%Y-%m-%d' ) date,
				DATE_FORMAT( cs.class_date_, '%Y-%m-%d' ) dateStr,
			</if>
		    SUM(csts.subsidy_) subsidy,
			SUM( csts.actual_salary_) amount
		FROM
			course_schedule_teacher_salary csts
				LEFT JOIN course_schedule cs ON csts.course_schedule_id_ = cs.id_
		WHERE
			( cs.del_flag_ IS NULL OR cs.del_flag_ = 0 )
			AND ( cs.is_lock_ = 0 OR cs.is_lock_ IS NULL )
			AND ( cs.new_course_id_ IS NULL OR cs.new_course_id_ = cs.id_ )
			AND csts.user_id_ = #{teacherId}
			<if test="year!=null">
				AND YEAR ( cs.class_date_ ) = #{year}
			</if>
		  	AND csts.actual_salary_ &gt; 0
			<if test="month!=null">
				AND MONTH(cs.class_date_) = #{month}
			</if>
		  	AND cs.class_date_ &gt;= '2021-03-01'
		  AND csts.settlement_time_ IS NOT NULL
		GROUP BY
			<if test="month==null">
				MONTH(cs.class_date_)
			</if>
			<if test="month!=null">
				DATE_FORMAT( cs.class_date_, '%Y-%m-%d' )
			</if>
	</select>

    <select id="getTodayHasCourseTeacherIds" resultType="int">
		SELECT DISTINCT csts.user_id_
		FROM course_schedule_teacher_salary csts
			LEFT JOIN course_schedule cs ON cs.id_=csts.course_schedule_id_
		WHERE cs.class_date_ = CURDATE()
			AND ( cs.del_flag_ IS NULL OR cs.del_flag_ = 0 )
			AND ( cs.is_lock_ = 0 OR cs.is_lock_ IS NULL )
			AND ( cs.new_course_id_ IS NULL OR cs.new_course_id_ = cs.id_ ) AND cs.pre_course_flag_ = 0
	</select>
</mapper>
