<?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.StudentRepairDao">
    <resultMap id="StudentRepair" type="com.ym.mec.biz.dal.entity.StudentRepair">
        <result column="id_" jdbcType="INTEGER" property="id"/>
        <result column="music_group_id_" jdbcType="VARCHAR" property="musicGroupId"/>
        <result column="student_instrument_id_" property="studentInstrumentId"/>
        <result column="maintenance_status_" property="maintenanceStatus"/>
        <result column="repair_name_" property="repairName"/>
        <result column="repair_img_" property="repairImg"/>
        <result column="exemption_amount_" property="exemptionAmount"/>
        <result column="trans_no_" jdbcType="VARCHAR" property="transNo"/>
        <result column="trans_no_" jdbcType="VARCHAR" property="transNo"/>
        <result column="organ_id_" jdbcType="INTEGER" property="organId"/>
        <result column="name_" jdbcType="INTEGER" property="organName"/>
        <result column="student_id_" jdbcType="INTEGER" property="studentId"/>
        <result column="student_name_" jdbcType="VARCHAR" property="studentName"/>
        <result column="student_school_" jdbcType="VARCHAR" property="studentSchool"/>
        <result column="employee_id_" jdbcType="INTEGER" property="employeeId"/>
        <result column="employee_name_" jdbcType="VARCHAR" property="employeeName"/>
        <result column="employee_phone_" jdbcType="VARCHAR" property="employeePhone"/>
        <result column="employee_address_" jdbcType="VARCHAR" property="employeeAddress"/>
        <result column="subject_id_" jdbcType="INTEGER" property="subjectId"/>
        <result column="subject_name_" jdbcType="VARCHAR" property="subjectName"/>
        <result column="type_" jdbcType="INTEGER" property="type"/>
        <result column="instrument_no_" jdbcType="VARCHAR" property="instrumentNo"/>
        <result column="fee_list_" jdbcType="VARCHAR" property="feeList"/>
        <result column="description_" jdbcType="VARCHAR" property="description"/>
        <result column="amount_" jdbcType="DECIMAL" property="amount"/>
        <result column="finish_time_" jdbcType="TIMESTAMP" property="finishTime"/>
        <result column="send_type_" jdbcType="INTEGER" property="sendType"/>
        <result column="contact_name_" jdbcType="VARCHAR" property="contactName"/>
        <result column="contact_mobile_" jdbcType="VARCHAR" property="contactMobile"/>
        <result column="address_" jdbcType="VARCHAR" property="address"/>
        <result column="pay_status_" jdbcType="INTEGER" property="payStatus"/>
        <result column="repair_status_" jdbcType="INTEGER" property="repairStatus"/>
        <result column="create_time_" jdbcType="TIMESTAMP" property="createTime"/>
        <result column="update_time_" jdbcType="TIMESTAMP" property="updateTime"/>
        <result column="username_" jdbcType="VARCHAR" property="studentName"/>
        <result column="goods_json_" jdbcType="VARCHAR" property="goodsJson"/>
        <result column="coupon_ids_" jdbcType="VARCHAR" property="couponIds"/>
        <result column="tenant_id_" property="tenantId"/>
    </resultMap>
    <!-- 根据主键查询一条记录 -->
    <select id="get" resultMap="StudentRepair">
        SELECT *
        FROM student_repair
        WHERE id_ = #{id}
    </select>

    <insert id="insert" parameterType="com.ym.mec.biz.dal.entity.StudentRepair" useGeneratedKeys="true" keyColumn="id"
            keyProperty="id">
        <!--@mbg.generated-->
        insert into student_repair (id_,music_group_id_, trans_no_,student_instrument_id_,maintenance_status_,repair_name_,
        repair_img_,exemption_amount_,organ_id_,student_id_, student_name_,student_school_,employee_id_, employee_name_,
        subject_id_,subject_name_, type_, instrument_no_,fee_list_,description_, amount_, finish_time_,send_type_,
        contact_name_, contact_mobile_,address_,pay_status_, create_time_,update_time_,repair_status_,goods_json_,coupon_ids_,tenant_id_)
        values (#{id,jdbcType=INTEGER},#{musicGroupId}, #{transNo,jdbcType=VARCHAR}, #{studentInstrumentId},
        #{maintenanceStatus},#{repairName},#{repairImg},#{exemptionAmount},#{organId,jdbcType=INTEGER},
        #{studentId,jdbcType=INTEGER}, #{studentName,jdbcType=VARCHAR}, #{studentSchool,jdbcType=VARCHAR},
        #{employeeId,jdbcType=INTEGER}, #{employeeName,jdbcType=VARCHAR}, #{subjectId,jdbcType=INTEGER},
        #{subjectName,jdbcType=VARCHAR}, #{type,jdbcType=INTEGER}, #{instrumentNo,jdbcType=VARCHAR},
        #{feeList,jdbcType=VARCHAR},#{description,jdbcType=VARCHAR}, #{amount,jdbcType=DECIMAL},
        #{finishTime,jdbcType=TIMESTAMP},#{sendType,jdbcType=INTEGER}, #{contactName,jdbcType=VARCHAR},
        #{contactMobile,jdbcType=VARCHAR},#{address,jdbcType=VARCHAR}, #{payStatus,jdbcType=INTEGER},
        #{createTime,jdbcType=TIMESTAMP},#{updateTime,jdbcType=TIMESTAMP},#{repairStatus},#{goodsJson},#{couponIds},#{tenantId})
    </insert>
    <update id="update" parameterType="com.ym.mec.biz.dal.entity.StudentRepair">
        UPDATE student_repair
        <set>
            <if test="couponIds != null">
                coupon_ids_ = #{couponIds},
            </if>
            <if test="musicGroupId != null">
                music_group_id_ = #{musicGroupId},
            </if>
            <if test="goodsJson != null">
                goods_json_ = #{goodsJson},
            </if>
            <if test="transNo != null">
                trans_no_ = #{transNo},
            </if>
            <if test="repairStatus != null">
                repair_status_ = #{repairStatus},
            </if>
            <if test="organId != null">
                organ_id_ = #{organId},
            </if>
            <if test="studentId != null">
                student_id_ = #{studentId},
            </if>
            <if test="studentName != null">
                student_name_ = #{studentName},
            </if>
            <if test="studentSchool != null">
                student_school_ = #{studentSchool},
            </if>
            <if test="employeeId != null">
                employee_id_ = #{employeeId},
            </if>
            <if test="employeeName != null">
                employee_name_ = #{employeeName},
            </if>
            <if test="subjectId != null">
                subject_id_ = #{subjectId},
            </if>
            <if test="subjectName != null">
                subject_name_ = #{subjectName},
            </if>
            <if test="type != null">
                type_ = #{type},
            </if>
            <if test="instrumentNo != null">
                instrument_no_ = #{instrumentNo},
            </if>
            <if test="feeList != null">
                fee_list_ = #{feeList},
            </if>
            <if test="description != null">
                description_ = #{description},
            </if>
            <if test="amount != null">
                amount_ = #{amount},
            </if>
            <if test="finishTime != null">
                finish_time_ = #{finishTime},
            </if>
            <if test="sendType != null">
                send_type_ = #{sendType},
            </if>
            <if test="contactName != null">
                contact_name_ = #{contactName},
            </if>
            <if test="contactMobile != null">
                contact_mobile_ = #{contactMobile},
            </if>
            <if test="address != null">
                address_ = #{address},
            </if>
            <if test="payStatus != null">
                pay_status_ = #{payStatus},
            </if>
            <if test="updateTime != null">
                update_time_ = #{updateTime},
            </if>
        </set>
        WHERE id_ = #{id} and tenant_id_ = #{tenantId}
    </update>

    <select id="getStudents" resultMap="com.ym.mec.biz.dal.dao.MusicGroupDao.BasicUserDto">
        SELECT su.username_ ,su.id_ user_id_,su.avatar_ head_url_,su.gender_,su.phone_,s.care_package_,s.come_on_package_,su.organ_id_
        FROM sys_user su
        LEFT JOIN student s ON su.id_=s.user_id_
        WHERE FIND_IN_SET(organ_id_,#{organIdList})
        AND user_type_ LIKE '%STUDENT%'
        <if test="search">
            AND (username_ LIKE CONCAT(#{search},'%') OR phone_ LIKE CONCAT(#{search},'%'))
        </if>
        AND del_flag_ = 0
        <include refid="global.limit"/>
    </select>

    <select id="getStudentsCount" resultType="java.lang.Integer">
        SELECT COUNT(*) FROM sys_user
        WHERE FIND_IN_SET(organ_id_,#{organIdList})
        AND user_type_ LIKE '%STUDENT%'
        <if test="search">
            AND (username_ LIKE CONCAT(#{search},'%') OR phone_ LIKE CONCAT(#{search},'%'))
        </if>
        AND del_flag_ = 0
    </select>

    <select id="queryPage" resultMap="StudentRepair">
        SELECT sr.*,o.name_,su.username_ FROM student_repair sr
        LEFT JOIN organization o ON o.id_ = sr.organ_id_
        LEFT JOIN sys_user su on sr.student_id_ = su.id_
        <include refid="queryPageSql"/>
        ORDER BY id_ DESC
        <include refid="global.limit"/>
    </select>
    <sql id="queryPageSql">
        <where>
        	sr.tenant_id_ = #{tenantId}
            <if test="search != null and search != ''">
                AND (sr.trans_no_ LIKE CONCAT('%',#{search},'%') OR sr.student_id_ = #{search} OR su.username_ LIKE
                CONCAT('%',#{search},'%'))
            </if>
            <if test="employeeId != null">
                AND sr.employee_id_ = #{employeeId}
            </if>
            <if test="studentId != null">
                AND sr.student_id_ = #{studentId}
            </if>
            <if test="organIdList != null and organIdList != ''">
                AND FIND_IN_SET(sr.organ_id_,#{organIdList})
            </if>
            <if test="subjectId != null">
                AND sr.subject_id_ = #{subjectId}
            </if>
            <if test="type != null">
                AND sr.type_ = #{type}
            </if>
            <if test="repairStatus != null">
                AND sr.repair_status_ = #{repairStatus}
            </if>
            <if test="payStatus != null">
                AND sr.pay_status_ = #{payStatus}
            </if>
            <if test="startTime != null">
                AND sr.create_time_ >= #{startTime}
            </if>
            <if test="endTime != null">
                AND sr.create_time_ &lt;= #{endTime}
            </if>
            <if test="studentInstrumentId != null">
                AND sr.student_instrument_id_ = #{studentInstrumentId}
            </if>
            <if test="hasExemptionAmount != null and hasExemptionAmount ==1">
                AND sr.exemption_amount_ > 0
            </if>
            <if test="hasExemptionAmount != null and hasExemptionAmount ==0">
                AND sr.exemption_amount_ = 0
            </if>
        </where>
    </sql>
    <select id="queryCount" resultType="int">
        SELECT COUNT(*) FROM student_repair sr
        LEFT JOIN sys_user su on sr.student_id_ = su.id_
        <include refid="queryPageSql"/>
    </select>

    <select id="getUserMusicGroup" resultMap="com.ym.mec.biz.dal.dao.MusicGroupDao.MusicGroup">
        SELECT mg.id_,mg.name_,sr.user_id_ repair_user_id_ FROM music_group mg
        LEFT JOIN student_registration sr on sr.music_group_id_ = mg.id_
        WHERE sr.user_id_ IN
        <foreach collection="userIds" item="userId" open="(" close=")" separator=",">
            #{userId}
        </foreach>
        AND mg.status_='PROGRESS'
        ORDER BY id_ DESC
    </select>
    <select id="getStudentInfo" resultMap="com.ym.mec.biz.dal.dao.MusicGroupDao.BasicUserDto">
        SELECT su.username_,
               su.id_     user_id_,
               su.avatar_ head_url_,
               su.gender_,
               mg.id_     music_group_id_,
               mg.name_   music_group_name_,
               sr.actual_subject_id_ subject_id_
        FROM sys_user su
                 LEFT JOIN student_registration sr ON sr.user_id_ = su.id_
                 LEFT JOIN music_group mg ON mg.id_ = sr.music_group_id_
        WHERE su.id_ = #{studentId}
        ORDER BY mg.id_ DESC
        LIMIT 1
    </select>

    <select id="getRepairInfo" resultMap="StudentRepair">
        SELECT sr.*, su.phone_ employee_phone_, e.contact_address_ employee_address_
        FROM student_repair sr
                 LEFT JOIN sys_user su ON su.id_ = sr.employee_id_
                 LEFT JOIN employee e ON sr.employee_id_ = e.user_id_
        WHERE sr.id_ = #{id}
    </select>

    <select id="getStudentMusicGroupRepairer" resultMap="com.ym.mec.biz.dal.dao.MusicGroupDao.BasicUserDto">
        SELECT repairer.real_name_ username_, repairer.id_ user_id_, repairer.avatar_ head_url_
        FROM music_group mg
                 LEFT JOIN student_registration sr on sr.music_group_id_ = mg.id_
                 LEFT JOIN sys_user repairer ON repairer.id_ = mg.repair_user_id_
        WHERE sr.user_id_ = #{studentId}
          AND mg.status_ = 'PROGRESS'
          AND mg.repair_user_id_ IS NOT NULL
        ORDER BY mg.id_ DESC
        LIMIT 1
    </select>

    <select id="getStudentOrganRepairer" resultMap="com.ym.mec.biz.dal.dao.MusicGroupDao.BasicUserDto">
        SELECT su.real_name_ username_, su.id_ user_id_, su.avatar_ head_url_
        FROM employee e
                 LEFT JOIN sys_user su ON su.id_ = e.user_id_
                 LEFT JOIN sys_user_role sur ON sur.user_id_ = su.id_
        WHERE FIND_IN_SET(#{organId}, e.organ_id_list_)
          AND sur.role_id_ = 37
        ORDER BY RAND()
        LIMIT 1
    </select>
    <select id="countEduStudents" resultType="java.lang.Integer">
        SELECT COUNT(DISTINCT su.id_) FROM (
        SELECT sr.user_id_ FROM music_group mg
        LEFT JOIN student_registration sr ON mg.id_ = sr.music_group_id_
        WHERE mg.status_ != 'CANCELED' AND sr.music_group_status_ != 'QUIT' AND mg.educational_teacher_id_ = #{teacherId}
        UNION
        SELECT cgsm.user_id_ FROM vip_group vg
        LEFT JOIN class_group_student_mapper cgsm ON vg.id_ = cgsm.music_group_id_
        WHERE cgsm.group_type_ = 'VIP' AND vg.educational_teacher_id_ = #{teacherId} AND cgsm.status_ != 'QUIT'
        UNION
        SELECT pg.student_id_ user_id_ FROM practice_group pg
        WHERE pg.educational_teacher_id_ = #{teacherId}) sid
        LEFT JOIN sys_user su ON su.id_ = sid.user_id_
        <if test="search != null and search != ''">
            WHERE su.id_ = #{search} OR su.username_ LIKE CONCAT('%',#{search},'%') OR su.phone_ LIKE CONCAT('%',#{search},'%')
        </if>
    </select>
    <select id="queryEduStudents" resultMap="com.ym.mec.biz.dal.dao.MusicGroupDao.BasicUserDto">
        SELECT su.username_, su.id_ user_id_, su.avatar_ head_url_,su.phone_ FROM (
        SELECT sr.user_id_ FROM music_group mg
        LEFT JOIN student_registration sr ON mg.id_ = sr.music_group_id_
        WHERE mg.status_ != 'CANCELED' AND sr.music_group_status_ != 'QUIT' AND mg.educational_teacher_id_ = #{teacherId}
        UNION
        SELECT cgsm.user_id_ FROM vip_group vg
        LEFT JOIN class_group_student_mapper cgsm ON vg.id_ = cgsm.music_group_id_
        WHERE cgsm.group_type_ = 'VIP' AND vg.educational_teacher_id_ = #{teacherId} AND cgsm.status_ != 'QUIT'
        UNION
        SELECT pg.student_id_ user_id_ FROM practice_group pg
        WHERE pg.educational_teacher_id_ = #{teacherId}) sid
        LEFT JOIN sys_user su ON su.id_ = sid.user_id_
        <if test="search != null and search != ''">
            WHERE su.id_ = #{search} OR su.username_ LIKE CONCAT('%',#{search},'%') OR su.phone_ LIKE CONCAT('%',#{search},'%')
        </if>
    </select>
    <select id="countStuEducation" resultType="java.lang.Integer">
        SELECT DISTINCT sid.educational_teacher_id_ FROM (
        SELECT mg.educational_teacher_id_ FROM music_group mg
        LEFT JOIN student_registration sr ON mg.id_ = sr.music_group_id_
        WHERE mg.status_ != 'CANCELED' AND sr.music_group_status_ != 'QUIT' AND sr.user_id_ = #{studentId}
        UNION
        SELECT vg.educational_teacher_id_ FROM vip_group vg
        LEFT JOIN course_schedule_student_payment cssp ON vg.id_ = cssp.music_group_id_
        WHERE cssp.group_type_ = 'VIP' AND cssp.user_id_ = #{studentId}
        UNION
        SELECT pg.educational_teacher_id_ FROM practice_group pg
        WHERE pg.student_id_ = #{studentId}) sid
    </select>
</mapper>
