<?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="expect_music_course_salary_" jdbcType="DECIMAL" property="expectMusicCourseSalary"/>
        <result column="expect_vip_course_salary_" jdbcType="DECIMAL" property="expectVipCourseSalary"/>
        <result column="expect_total_salary_" jdbcType="DECIMAL" property="expectTotalSalary"/>
        <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"/>
        <collection property="subjectList" ofType="com.ym.mec.biz.dal.entity.Subject">
            <result column="subject_id" property="id"/>
            <result column="subject_name" property="name"/>
        </collection>
    </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_,
        expect_music_course_salary_, expect_vip_course_salary_,
        expect_total_salary_, average_class_minutes_,
        statistics_date_time_)
        VALUES (#{id,jdbcType=BIGINT}, #{userId,jdbcType=INTEGER}, #{organId,jdbcType=INTEGER}, #{month,jdbcType=DATE},
        #{subjectIdList,jdbcType=VARCHAR}, #{vipCourseNum,jdbcType=INTEGER}, #{musicCourseNum,jdbcType=INTEGER},
        #{expectMusicCourseSalary,jdbcType=DECIMAL}, #{expectVipCourseSalary,jdbcType=DECIMAL},
        #{expectTotalSalary,jdbcType=DECIMAL}, #{averageClassMinutes,jdbcType=INTEGER},NOW())
    </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_,
        expect_music_course_salary_, expect_vip_course_salary_,
        expect_total_salary_, average_class_minutes_,statistics_date_time_)
        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.expectMusicCourseSalary,jdbcType=DECIMAL}, #{list.expectVipCourseSalary,jdbcType=DECIMAL},
        #{list.expectTotalSalary,jdbcType=DECIMAL}, #{list.averageClassMinutes,jdbcType=INTEGER},NOW())
        </foreach>
    </insert>

    <!-- 查找下月排课小于基准课酬的老师-->
    <select id="findUserByWarringSalary" resultMap="TeacherCourseStatistics"><![CDATA[
        SELECT SUM(csts.expect_salary_) expect_total_salary_, csts.user_id_,t.organ_id_,t.subject_id_ subject_id_list_,
        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_
        LEFT JOIN teacher t on t.id_ = csts.user_id_
        WHERE cs.class_date_ >= #{startDate} AND cs.class_date_ <= #{endDate} AND csts.group_type_ IN ('VIP','MUSIC') AND cs.del_flag_ != 1
        GROUP BY csts.user_id_ HAVING expect_total_salary_ <= #{salary}
        ]]>
  </select>
    <select id="findUserCourseSalaryByUserIds" resultMap="TeacherCourseStatistics">
        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') AND cs.del_flag_ != 1
        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_,
        s.id_ subject_id,s.name_ subject_name FROM teacher_course_statistics tcs
        left join sys_user u on tcs.user_id_ = u.id_
        left join organization o on tcs.organ_id_ = o.id_
        LEFT JOIN subject s ON FIND_IN_SET(s.id_,tcs.subject_id_list_)
        <include refid="queryParams"/>
        <include refid="global.limit"/>
    </select>

    <!-- 查询当前表的总记录数 -->
    <select id="queryCount" resultType="int" parameterType="map">
        SELECT COUNT(*) FROM teacher_course_statistics tcs
        <include refid="queryParams"/>
    </select>

    <sql id="queryParams">
        <where>
            <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>
        </where>
    </sql>
</mapper>