<?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.TeacherCourseRewardDao">
    <resultMap id="teacherCourseReward" type="com.ym.mec.biz.dal.entity.TeacherCourseReward">
        <id column="id_" jdbcType="BIGINT" property="id"/>
        <result column="music_group_id_" jdbcType="VARCHAR" property="musicGroupId"/>
        <result column="group_type_" jdbcType="VARCHAR" property="groupType"/>
        <result column="course_schedule_id_list_" jdbcType="VARCHAR" property="courseScheduleIdList"/>
        <result column="course_group_name_" jdbcType="VARCHAR" property="courseGroupName"/>
        <result column="organ_id_" jdbcType="INTEGER" property="organId"/>
        <result column="organ_name_" jdbcType="VARCHAR" property="organName"/>
        <result column="teacher_id_" jdbcType="INTEGER" property="teacherId"/>
        <result column="teacher_name_" jdbcType="VARCHAR" property="teacherName"/>
        <result column="expect_reward_amount_" jdbcType="DECIMAL" property="expectRewardAmount"/>
        <result column="actual_reward_amount_" jdbcType="DECIMAL" property="actualRewardAmount"/>
        <result column="memo_" jdbcType="VARCHAR" property="memo"/>
        <result column="settlement_status_" property="settlementStatus" typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler"/>
        <result column="settlement_date_" property="settlementDate"/>
        <result column="confirm_status_" property="confirmStatus" typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler"/>
        <result column="create_time_" jdbcType="TIMESTAMP" property="createTime"/>
        <result column="update_time_" jdbcType="TIMESTAMP" property="updateTime"/>
    </resultMap>
    <sql id="Base_Column_List">
        <!--@mbg.generated-->
        id_, music_group_id_, group_type_, course_schedule_id_list_, course_group_name_,
        organ_id_, teacher_id_, expect_reward_amount_, actual_reward_amount_, memo_, create_time_,
        update_time_,settlement_status_,settlement_date_
    </sql>
    <select id="get" parameterType="java.lang.Long" resultMap="teacherCourseReward">
        select *
        from teacher_course_reward
        where id_ = #{id,jdbcType=BIGINT}
    </select>
    <delete id="delete" parameterType="java.lang.Long">
        delete
        from teacher_course_reward
        where id_ = #{id,jdbcType=BIGINT}
    </delete>
    <insert id="insert" keyColumn="id_" keyProperty="id" parameterType="com.ym.mec.biz.dal.entity.TeacherCourseReward"
            useGeneratedKeys="true">
        insert into teacher_course_reward (music_group_id_, group_type_, course_schedule_id_list_,
                                           course_group_name_, organ_id_, teacher_id_,
                                           expect_reward_amount_, actual_reward_amount_,
                                           memo_, create_time_, update_time_)
        values (#{musicGroupId,jdbcType=VARCHAR}, #{groupType,jdbcType=VARCHAR},
                #{courseScheduleIdList,jdbcType=VARCHAR},
                #{courseGroupName,jdbcType=VARCHAR}, #{organId,jdbcType=INTEGER}, #{teacherId,jdbcType=INTEGER},
                #{expectRewardAmount,jdbcType=DECIMAL}, #{actualRewardAmount,jdbcType=DECIMAL},
                #{memo,jdbcType=VARCHAR}, #{createTime,jdbcType=TIMESTAMP}, #{updateTime,jdbcType=TIMESTAMP})
    </insert>
    <update id="update" parameterType="com.ym.mec.biz.dal.entity.TeacherCourseReward">
        update teacher_course_reward
        <set>
            <if test="musicGroupId != null">
                music_group_id_ = #{musicGroupId,jdbcType=VARCHAR},
            </if>
            <if test="groupType != null">
                group_type_ = #{groupType,jdbcType=VARCHAR},
            </if>
            <if test="courseScheduleIdList != null">
                course_schedule_id_list_ = #{courseScheduleIdList,jdbcType=VARCHAR},
            </if>
            <if test="courseGroupName != null">
                course_group_name_ = #{courseGroupName,jdbcType=VARCHAR},
            </if>
            <if test="organId != null">
                organ_id_ = #{organId,jdbcType=INTEGER},
            </if>
            <if test="teacherId != null">
                teacher_id_ = #{teacherId,jdbcType=INTEGER},
            </if>
            <if test="expectRewardAmount != null">
                expect_reward_amount_ = #{expectRewardAmount,jdbcType=DECIMAL},
            </if>
            <if test="actualRewardAmount != null">
                actual_reward_amount_ = #{actualRewardAmount,jdbcType=DECIMAL},
            </if>
            <if test="memo != null">
                memo_ = #{memo,jdbcType=VARCHAR},
            </if>
            <if test="createTime != null">
                create_time_ = #{createTime,jdbcType=TIMESTAMP},
            </if>
            <if test="updateTime != null">
                update_time_ = #{updateTime,jdbcType=TIMESTAMP},
            </if>
            <if test="settlementDate != null">
                settlement_date_ = #{settlementDate},
            </if>
            <if test="settlementStatus != null">
                settlement_status_ = #{settlementStatus,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},
            </if>
            <if test="confirmStatus != null">
                confirm_status_ = #{confirmStatus,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler}
            </if>
        </set>
        where id_ = #{id,jdbcType=BIGINT}
    </update>
    <update id="batchUpdate">
        <foreach collection="teacherCourseRewards" item="item" index="index" open="" close="" separator=";">
            update teacher_course_reward
            <set>
                <if test="item.actualRewardAmount != null">
                    actual_reward_amount_ = #{item.actualRewardAmount,jdbcType=DECIMAL},
                </if>
                <if test="item.memo != null">
                    memo_ = #{item.memo,jdbcType=VARCHAR},
                </if>
                <if test="item.updateTime != null">
                    update_time_ = #{item.updateTime,jdbcType=TIMESTAMP},
                </if>
                <if test="item.updateTime == null">
                    update_time_ = NOW(),
                </if>
                <if test="item.settlementDate != null">
                    settlement_date_ = #{item.settlementDate},
                </if>
                <if test="item.settlementStatus != null">
                    settlement_status_ = #{item.settlementStatus,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler}
                </if>
            </set>
            where id_ = #{item.id,jdbcType=BIGINT}
        </foreach>
    </update>
    <update id="updateTeacherRewardConfirmStatusWithMonth">
        UPDATE teacher_course_reward SET confirm_status_ = #{confirmStatus,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler}
        WHERE DATE_FORMAT( settlement_date_, '%Y-%m' ) = #{month}
        <if test="teacherId!=null">
            AND teacher_id_=#{teacherId}
        </if>
        <if test="confirmStatus==@com.ym.mec.biz.dal.enums.TeacherSalaryConfirmStatus@COMPLETED">
            AND confirm_status_=1
        </if>
    </update>

    <sql id="queryCondition">
        <where>
            <if test="teacherId != null">
                AND tcr.teacher_id_=#{teacherId}
            </if>
            <if test="search != null and search != ''">
                AND (tcr.course_group_name_ LIKE CONCAT('%', #{search}, '%') OR tcr.music_group_id_ LIKE CONCAT(#{search}, '%'))
            </if>
            <if test="organId != null and organId != ''">
                AND FIND_IN_SET(tcr.organ_id_ ,#{organId})
            </if>
            <if test="settlementStatus != null and settlementStatus != ''">
                AND tcr.settlement_status_ = #{settlementStatus}
            </if>
            <if test="startTime != null">
                AND tcr.settlement_date_ >= #{startTime}
            </if>
            <if test="endTime != null">
                AND tcr.settlement_date_ &lt; #{endTime}
            </if>
        </where>
    </sql>

    <!-- 分页查询 -->
    <select id="queryPage" resultMap="teacherCourseReward" parameterType="map">
        SELECT tcr.*,su.real_name_ teacher_name_,o.name_ organ_name_ FROM teacher_course_reward tcr
        LEFT JOIN sys_user su ON su.id_ = tcr.teacher_id_
        LEFT JOIN organization o ON o.id_ = tcr.organ_id_
        <include refid="queryCondition"/>
        ORDER BY tcr.id_ DESC
        <include refid="global.limit"/>
    </select>

    <!-- 查询当前表的总记录数 -->
    <select id="queryCount" resultType="int" parameterType="map">
        SELECT count(*) FROM teacher_course_reward tcr
        <include refid="queryCondition"/>
    </select>
    <select id="findBySettlementStatus" resultMap="teacherCourseReward">
        SELECT * FROM teacher_course_reward tcr WHERE tcr.settlement_status_ = #{settlementStatus}
    </select>

    <select id="teacherCourseRewardStatWithMonth" resultMap="com.ym.mec.biz.dal.dao.CourseScheduleTeacherSalaryDao.CourseTeacherSalaryStatisticsDto">
        SELECT
            COUNT( music_group_id_ ) course_times_,
            SUM( actual_reward_amount_ ) total_actual_salary_,
            SUM( expect_reward_amount_ ) - SUM( actual_reward_amount_ ) total_reduce_salary_
        FROM
            teacher_course_reward
        WHERE
            teacher_id_ = #{teacherId}
            AND DATE_FORMAT( settlement_date_, '%Y-%m' ) = #{month}
        GROUP BY
            teacher_id_
    </select>

    <select id="findTeacherCourseRewards" resultMap="teacherCourseReward">
        SELECT
            *
        FROM
            teacher_course_reward
        WHERE
            teacher_id_ = #{teacherId}
            AND DATE_FORMAT( settlement_date_, '%Y-%m' ) = #{month}
        ORDER BY id_ DESC
        <include refid="global.limit"/>
    </select>
    <select id="countTeacherCourseRewards" resultType="int">
        SELECT
            COUNT(id_)
        FROM
            teacher_course_reward
        WHERE
            teacher_id_ = #{teacherId}
            AND DATE_FORMAT( settlement_date_, '%Y-%m' ) = #{month}
    </select>

    <select id="findTeacherCourseRewardWithGroup" resultMap="teacherCourseReward">
        SELECT
            *
        FROM
            teacher_course_reward
        WHERE
            teacher_id_ = #{teacherId}
            AND music_group_id_=#{groupId}
            AND group_type_=#{groupType, typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler}
    </select>

</mapper>
