<?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.StudentVisitDao">
    <resultMap id="StudentVisit" type="com.ym.mec.biz.dal.dto.StudentVisitDto">
        <id column="id_" property="id"/>
        <result column="music_group_id_" property="musicGroupId"/>
        <result column="musicGroupName" property="musicGroupName"/>
        <result column="organ_id_" property="organId"/>
        <result column="organName" property="organName"/>
        <result column="student_id_" property="studentId"/>
        <result column="studentName" property="studentName"/>
        <result column="teacher_id_" property="teacherId"/>
        <result column="teacherName" property="teacherName"/>
        <result column="visiter_type_" property="visiterType" typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler"/>
        <result column="type_" property="type"/>
        <result column="purpose_" property="purpose"/>
        <result column="overview_" property="overview"/>
        <result column="feedback_" property="feedback"/>
        <result column="visit_time_" property="visitTime"/>
        <result column="create_time_" property="createTime"/>
        <result column="object_id_" property="objectId"/>
        <result column="tenant_id_" property="tenantId"/>
        <result column="attachments_" property="attachments"/>
        <result column="feedback_type_" property="feedbackType" typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler"/>
        <result column="feedback_type_desc_" property="feedbackTypeDesc" typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler"/>
    </resultMap>
    <sql id="Base_Column_List">
        <!--@mbg.generated-->
        id_, music_group_id_, organ_id_, student_id_, teacher_id_, visiter_type_, type_, purpose_, overview_, feedback_,
        visit_time_, create_time_,tenant_id_,feedback_type_,feedback_type_desc_,attachments_
    </sql>
    <select id="get" parameterType="java.lang.Integer" resultMap="StudentVisit">
        select
        <include refid="Base_Column_List"/>
        from student_visit
        where id_ = #{id,jdbcType=INTEGER}
    </select>
    <delete id="delete" parameterType="java.lang.Integer">
        <!--@mbg.generated-->
        delete from student_visit
        where id_ = #{id,jdbcType=INTEGER}
    </delete>
    <insert id="insert" keyColumn="id_" keyProperty="id" parameterType="com.ym.mec.biz.dal.entity.StudentVisit"
            useGeneratedKeys="true">
        <!--@mbg.generated-->
        insert into student_visit (music_group_id_, organ_id_, student_id_, teacher_id_, visiter_type_, feedback_type_,feedback_type_desc_,
        type_, purpose_, overview_,
        feedback_, visit_time_, create_time_,object_id_,tenant_id_,attachments_)
        values (#{musicGroupId,jdbcType=VARCHAR}, #{organId,jdbcType=INTEGER}, #{studentId,jdbcType=INTEGER},
        #{teacherId,jdbcType=INTEGER},#{visiterType,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},
                #{feedbackType,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},
                #{feedbackTypeDesc,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},
        #{type,jdbcType=VARCHAR}, #{purpose,jdbcType=VARCHAR}, #{overview,jdbcType=VARCHAR},
        #{feedback,jdbcType=VARCHAR}, #{visitTime}, #{createTime},#{objectId},#{tenantId},#{attachments})
    </insert>
    <update id="update" parameterType="com.ym.mec.biz.dal.entity.StudentVisit">
        <!--@mbg.generated-->
        update student_visit
        <set>
            <if test="attachments != null and attachments != ''">
                attachments_ = #{attachments},
            </if>
            <if test="musicGroupId != null">
                music_group_id_ = #{musicGroupId,jdbcType=VARCHAR},
            </if>
            <if test="studentId != null">
                student_id_ = #{studentId,jdbcType=INTEGER},
            </if>
            <if test="teacherId != null">
                teacher_id_ = #{teacherId,jdbcType=INTEGER},
            </if>
            <if test="type != null">
                type_ = #{type,jdbcType=VARCHAR},
            </if>
            <if test="purpose != null">
                purpose_ = #{purpose,jdbcType=VARCHAR},
            </if>
            <if test="overview != null">
                overview_ = #{overview,jdbcType=VARCHAR},
            </if>
            <if test="feedback != null">
                feedback_ = #{feedback,jdbcType=VARCHAR},
            </if>
            <if test="visitTime != null">
                visit_time_ = #{visitTime},
            </if>
            <if test="objectId != null">
                object_id_ = #{objectId},
            </if>
        </set>
        WHERE id_ = #{id,jdbcType=INTEGER} AND tenant_id_ = #{tenantId}
    </update>

    <select id="queryPage" resultMap="StudentVisit">
        SELECT * FROM student_visit sv
        <include refid="queryPageSql"/>
        ORDER BY sv.id_ DESC
        <include refid="global.limit"/>
    </select>

    <select id="getPageList" resultMap="StudentVisit">
        SELECT sv.*,t.real_name_ teacherName,o.name_ organName,s.username_ studentName,mg.name_ musicGroupName FROM
        student_visit sv
        LEFT JOIN sys_user t ON sv.teacher_id_ = t.id_
        LEFT JOIN organization o ON sv.organ_id_ = o.id_
        LEFT JOIN sys_user s ON s.id_ = sv.student_id_
        LEFT JOIN music_group mg ON sv.music_group_id_ = mg.id_
        <include refid="queryPageSql"/>
        ORDER BY sv.visit_time_ DESC
        <include refid="global.limit"/>
    </select>
    <sql id="queryPageSql">
        <where>
        	sv.tenant_id_ = #{tenantId}
            <if test="search != null and search != ''">
                AND (sv.object_id_ = #{search} OR t.id_ = #{search} OR s.id_ = #{search} OR t.real_name_ LIKE CONCAT('%',#{search},'%') OR s.username_ LIKE CONCAT('%',#{search},'%'))
            </if>
            <if test="teacher != null and teacher != ''">
                AND (t.id_ = #{teacher} OR t.real_name_ LIKE CONCAT('%',#{teacher},'%'))
            </if>
            <if test="student != null and student != ''">
                AND (s.id_ = #{student} OR s.username_ LIKE CONCAT('%',#{student},'%'))
            </if>
            <if test="teacherId !=null">
                AND sv.teacher_id_ = #{teacherId}
            </if>
            <if test="studentId !=null">
                AND sv.student_id_ = #{studentId}
            </if>
            <if test="organId != null and organId != ''">
                AND FIND_IN_SET(sv.organ_id_,#{organId})
            </if>
            <if test="visiterType != null">
                AND sv.visiter_type_ = #{visiterType,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler}
            </if>
            <if test="type != null">
                AND sv.type_ = #{type}
            </if>
            <if test="feedbackType != null and feedbackType != ''">
                AND sv.feedback_type_ = #{feedbackType}
            </if>
            <if test="feedbackTypeDesc != null and feedbackTypeDesc != ''">
                AND sv.feedback_type_desc_ = #{feedbackTypeDesc}
            </if>
            <if test="purpose != null">
                AND sv.purpose_ = #{purpose}
            </if>
            <if test="startTime != null">
                AND sv.visit_time_ >= #{startTime}
            </if>
            <if test="endTime != null">
                AND sv.visit_time_ &lt;= #{endTime}
            </if>
        </where>
    </sql>
    <select id="queryCount" resultType="int">
        SELECT COUNT(*) FROM student_visit sv
        LEFT JOIN sys_user t ON sv.teacher_id_ = t.id_
        LEFT JOIN sys_user s ON s.id_ = sv.student_id_
        <include refid="queryPageSql"/>
    </select>

    <select id="getInfo" resultMap="StudentVisit">
        SELECT sv.*, t.real_name_ teacherName, o.name_ organName, s.username_ studentName, mg.name_ musicGroupName
        FROM student_visit sv
                 LEFT JOIN sys_user t ON sv.teacher_id_ = t.id_
                 LEFT JOIN organization o ON sv.organ_id_ = o.id_
                 LEFT JOIN sys_user s ON s.id_ = sv.student_id_
                 LEFT JOIN music_group mg ON sv.music_group_id_ = mg.id_
        WHERE sv.id_ = #{id}
    </select>


    <select id="getStudents" resultMap="com.ym.mec.biz.dal.dao.MusicGroupDao.BasicUserDto">
        SELECT s.id_ user_id_,s.username_,s.avatar_ head_url_,s.gender_,s.phone_ FROM course_schedule cs
        LEFT JOIN course_schedule_student_payment cssp ON cssp.course_schedule_id_ = cs.id_
        LEFT JOIN sys_user s ON s.id_ = cssp.user_id_
        WHERE cs.actual_teacher_id_ = #{teacherId}
        <if test="student != null and student != ''">
            AND (s.phone_ LIKE CONCAT('%',#{student},'%') OR s.username_ LIKE CONCAT('%',#{student},'%'))
        </if>
        AND s.del_flag_ = 0
        GROUP BY s.id_
    </select>

    <select id="getStudentsCount" resultType="int">
        SELECT COUNT(DISTINCT s.id_) FROM course_schedule cs
        LEFT JOIN course_schedule_student_payment cssp ON cssp.course_schedule_id_ = cs.id_
        LEFT JOIN sys_user s ON s.id_ = cssp.user_id_
        WHERE cs.actual_teacher_id_ = #{teacherId}
        <if test="student != null and student != ''">
            AND (s.phone_ LIKE CONCAT('%',#{student},'%') OR s.username_ LIKE CONCAT('%',#{student},'%'))
        </if>
        AND s.del_flag_ = 0
    </select>
    <select id="getMusicGroupVisitStudent" resultType="java.lang.Integer">
        SELECT DISTINCT student_id_ FROM student_visit WHERE music_group_id_=#{musicGroupId}
        AND student_id_ IN
        <foreach collection="studentIds" item="studentId" open="(" close=")" separator=",">
            #{studentId}
        </foreach>
    </select>
    <select id="getTeacherVisitCount" resultType="com.ym.mec.biz.dal.dto.TeacherVisitDto">
        SELECT teacher_id_ teacherId,date_format(visit_time_, '%Y-%m') month, COUNT(*) num
        FROM student_visit
        WHERE teacher_id_ IN
        <foreach collection="teacherIds" item="teacherId" open="(" close=")" separator=",">
            #{teacherId}
        </foreach>
        AND visit_time_ >= #{startTime}
        <![CDATA[ AND visit_time_ <= #{endTime} ]]>
        GROUP BY teacher_id_,date_format(visit_time_, '%Y-%m')
    </select>
    <select id="queryRecordVisitStudentIds" resultType="java.lang.Integer">
        SELECT DISTINCT st.user_id_ FROM student st
        LEFT JOIN student_visit sv ON st.user_id_ = sv.student_id_
        WHERE sv.type_ = '云教练' AND st.teacher_id_ = #{teacherId}
        AND sv.visit_time_ BETWEEN DATE_FORMAT(#{addDays1}, '%Y%m%d') AND DATE_FORMAT(#{addDays3}, '%Y%m%d')
    </select>
</mapper>
