<?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.TeacherCourseStatisticsDao">
    <resultMap id="TeacherCourseStatistics" type="com.ym.mec.biz.dal.entity.TeacherCourseStatistics">
        <result column="id_" jdbcType="BIGINT" property="id"/>
        <result column="user_id_" jdbcType="INTEGER" property="userId"/>
        <result column="organ_id_" jdbcType="INTEGER" property="organId"/>
        <result column="month_" jdbcType="DATE" property="month"/>
        <result column="subject_id_list_" jdbcType="VARCHAR" property="subjectIdList"/>
        <result column="vip_course_num_" jdbcType="INTEGER" property="vipCourseNum"/>
        <result column="music_course_num_" jdbcType="INTEGER" property="musicCourseNum"/>
        <result column="practice_course_num_" jdbcType="INTEGER" property="practiceCourseNum"/>
        <result column="expect_music_course_salary_" jdbcType="DECIMAL" property="expectMusicCourseSalary"/>
        <result column="expect_vip_course_salary_" jdbcType="DECIMAL" property="expectVipCourseSalary"/>
        <result column="expect_practice_course_salary_" jdbcType="DECIMAL" property="expectPracticeCourseSalary"/>
        <result column="expect_total_salary_" jdbcType="DECIMAL" property="expectTotalSalary"/>
        <result column="low_salary" jdbcType="DECIMAL" property="lowSalary"/>
        <result column="average_class_minutes_" jdbcType="INTEGER" property="averageClassMinutes"/>
        <result column="statistics_date_time_" jdbcType="DATE" property="statisticsDateTime"/>
        <result column="group_type_" jdbcType="VARCHAR" property="groupType"/>
        <result column="real_name_" jdbcType="VARCHAR" property="realName"/>
        <result column="phone_" jdbcType="VARCHAR" property="phone"/>
        <result column="organ_name_" jdbcType="VARCHAR" property="organName"/>
        <result column="month_str_" jdbcType="VARCHAR" property="monthStr"/>
        <result column="tenant_id_" property="tenantId"/>
        <association property="subjectList"
                     select="com.ym.mec.biz.dal.dao.SubjectDao.findBySubjectByIdList"
                        column="{subjectIdList = tcs.subject_id_list_}"/>
    </resultMap>

    <resultMap id="TeacherCourseStatistics1" type="com.ym.mec.biz.dal.entity.TeacherCourseStatistics">
        <result column="id_" jdbcType="BIGINT" property="id"/>
        <result column="user_id_" jdbcType="INTEGER" property="userId"/>
        <result column="organ_id_" jdbcType="INTEGER" property="organId"/>
        <result column="month_" jdbcType="DATE" property="month"/>
        <result column="subject_id_list_" jdbcType="VARCHAR" property="subjectIdList"/>
        <result column="vip_course_num_" jdbcType="INTEGER" property="vipCourseNum"/>
        <result column="music_course_num_" jdbcType="INTEGER" property="musicCourseNum"/>
        <result column="practice_course_num_" jdbcType="INTEGER" property="practiceCourseNum"/>
        <result column="expect_music_course_salary_" jdbcType="DECIMAL" property="expectMusicCourseSalary"/>
        <result column="expect_vip_course_salary_" jdbcType="DECIMAL" property="expectVipCourseSalary"/>
        <result column="expect_practice_course_salary_" jdbcType="DECIMAL" property="expectPracticeCourseSalary"/>
        <result column="expect_total_salary_" jdbcType="DECIMAL" property="expectTotalSalary"/>
        <result column="low_salary" jdbcType="DECIMAL" property="lowSalary"/>
        <result column="average_class_minutes_" jdbcType="INTEGER" property="averageClassMinutes"/>
        <result column="statistics_date_time_" jdbcType="DATE" property="statisticsDateTime"/>
        <result column="group_type_" jdbcType="VARCHAR" property="groupType"/>
        <result column="real_name_" jdbcType="VARCHAR" property="realName"/>
        <result column="phone_" jdbcType="VARCHAR" property="phone"/>
        <result column="organ_name_" jdbcType="VARCHAR" property="organName"/>
        <result column="month_str_" jdbcType="VARCHAR" property="monthStr"/>
        <result column="tenant_id_" jdbcType="INTEGER" property="tenantId"/>
    </resultMap>
    <insert id="insert" parameterType="com.ym.mec.biz.dal.entity.TeacherCourseStatistics" useGeneratedKeys="true"
            keyColumn="id" keyProperty="id">
        INSERT INTO teacher_course_statistics (id_, user_id_, organ_id_, month_,
        subject_id_list_, vip_course_num_, music_course_num_, practice_course_num_,
        expect_music_course_salary_, expect_vip_course_salary_, expect_practice_course_salary_,
        expect_total_salary_, average_class_minutes_,
        statistics_date_time_,low_salary,tenant_id_)
        VALUES (#{id,jdbcType=BIGINT}, #{userId,jdbcType=INTEGER}, #{organId,jdbcType=INTEGER}, #{month,jdbcType=DATE},
        #{subjectIdList,jdbcType=VARCHAR}, #{vipCourseNum,jdbcType=INTEGER}, #{musicCourseNum,jdbcType=INTEGER}, #{practiceCourseNum},
        #{expectMusicCourseSalary,jdbcType=DECIMAL}, #{expectVipCourseSalary,jdbcType=DECIMAL}, #{expectPracticeCourseSalary},
        #{expectTotalSalary,jdbcType=DECIMAL}, #{averageClassMinutes,jdbcType=INTEGER},NOW(),#{lowSalary},#{tenantId})
    </insert>

    <insert id="batchAdd" parameterType="java.util.List" useGeneratedKeys="true" keyColumn="id" keyProperty="id">
        INSERT INTO teacher_course_statistics (id_, user_id_, organ_id_, month_,
        subject_id_list_, vip_course_num_, music_course_num_, practice_course_num_,
        expect_music_course_salary_, expect_vip_course_salary_, expect_practice_course_salary_,
        expect_total_salary_, average_class_minutes_,statistics_date_time_,low_salary,tenant_id_)
        VALUE
        <foreach collection="lists" item="list" separator=",">
        (#{list.id,jdbcType=BIGINT}, #{list.userId,jdbcType=INTEGER}, #{list.organId,jdbcType=INTEGER}, #{list.month,jdbcType=DATE},
        #{list.subjectIdList,jdbcType=VARCHAR}, #{list.vipCourseNum,jdbcType=INTEGER}, #{list.musicCourseNum,jdbcType=INTEGER},
            #{list.practiceCourseNum},
        #{list.expectMusicCourseSalary,jdbcType=DECIMAL}, #{list.expectVipCourseSalary,jdbcType=DECIMAL},
            #{list.expectPracticeCourseSalary},
        #{list.expectTotalSalary,jdbcType=DECIMAL}, #{list.averageClassMinutes,jdbcType=INTEGER},NOW(),#{list.lowSalary},#{list.tenantId})
        </foreach>
    </insert>

    <delete id="deleteByMonth">
        DELETE FROM teacher_course_statistics WHERE month_=#{date}
    </delete>

    <!-- 查找下月排课小于基准课酬的老师-->
    <select id="findUserByWarringSalary" resultMap="TeacherCourseStatistics1"><![CDATA[
        SELECT ctt.expect_total_salary_, t.id_ user_id_,t.organ_id_,t.subject_id_ subject_id_list_,
               ctt.average_class_minutes_,t.tenant_id_
        FROM teacher t
         LEFT JOIN sys_user su ON t.id_=su.id_
         LEFT JOIN (
            SELECT
                csts.user_id_,
                SUM( csts.expect_salary_ ) expect_total_salary_,
                ROUND( AVG( TIMESTAMPDIFF( MINUTE, start_class_time_, end_class_time_ )), 0 ) average_class_minutes_
            FROM course_schedule_teacher_salary csts
                     LEFT JOIN course_schedule cs ON cs.id_=csts.course_schedule_id_
            WHERE cs.del_flag_ != 1 AND cs.pre_course_flag_ = 0
              AND cs.class_date_ >= #{startDate} AND cs.class_date_ <= #{endDate}
              AND csts.group_type_ IN ( 'VIP', 'MUSIC', 'PRACTICE' )
            GROUP BY
                csts.user_id_
        ) ctt ON ctt.user_id_=t.id_
        WHERE su.del_flag_ != 1 AND su.lock_flag_!=1
          AND t.demission_date_ IS NULL
          AND t.organ_id_ IS NOT NULL
        ]]>
  </select>
    <select id="findUserCourseSalaryByUserIds" resultMap="TeacherCourseStatistics1">
        SELECT SUM(csts.expect_salary_) expect_total_salary_, COUNT(csts.id_) music_course_num_, csts.user_id_, csts.group_type_
        FROM course_schedule_teacher_salary csts LEFT JOIN course_schedule cs ON cs.id_ = csts.course_schedule_id_
        WHERE csts.user_id_ IN
        <foreach collection="userIds" item="userId" open="(" separator="," close=")">
            #{userId}
        </foreach>
        <![CDATA[AND cs.class_date_ >= #{startDate} AND cs.class_date_ <= #{endDate}
        AND csts.group_type_ IN ('VIP', 'MUSIC', 'PRACTICE') AND cs.del_flag_ != 1 AND cs.pre_course_flag_ = 0
        GROUP BY csts.user_id_, csts.group_type_;
  ]]></select>

    <select id="queryPage" resultMap="TeacherCourseStatistics" parameterType="map">
        SELECT tcs.*,u.real_name_,u.phone_,o.name_ organ_name_,DATE_FORMAT(tcs.month_,'%Y年%m月') month_str_
        FROM teacher_course_statistics tcs
        LEFT JOIN sys_user u ON tcs.user_id_ = u.id_
        LEFT JOIN teacher t ON t.id_ = tcs.user_id_
        LEFT JOIN organization o ON tcs.organ_id_ = o.id_
        <include refid="queryParams"/>
        <include refid="global.limit"/>
    </select>

    <!-- 查询当前表的总记录数 -->
    <select id="queryCount" resultType="int" parameterType="map">
        SELECT COUNT(tcs.id_) FROM teacher_course_statistics tcs
        LEFT JOIN sys_user u ON tcs.user_id_ = u.id_
        LEFT JOIN teacher t ON t.id_ = tcs.user_id_
        <include refid="queryParams"/>
    </select>

    <sql id="queryParams">
        <where>
            u.lock_flag_ = 0 AND u.del_flag_ = 0 AND t.job_nature_ = 'FULL_TIME'
            AND t.demission_date_ IS NULL and tcs.tenant_id_ = #{tenantId} AND t.is_settlement_salary_ = 1
            <if test="organId != null">
                AND FIND_IN_SET(tcs.organ_id_,#{organId})
            </if>
            <if test="monthStr != null">
                AND DATE_FORMAT(tcs.month_,'%Y-%m') = #{monthStr}
            </if>
            <if test="userId != null">
                AND tcs.user_id_ = #{userId}
            </if>
            <if test="lowSalary != null">
                AND tcs.low_salary = #{lowSalary}
            </if>
            <if test="minSalary != null">
                AND tcs.expect_total_salary_ &gt; #{minSalary}
            </if>
            <if test="maxSalary != null">
                AND tcs.expect_total_salary_ &lt; #{maxSalary}
            </if>
        </where>
    </sql>
</mapper>
