<?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.TempLiveTeacherCardDao">

    <resultMap type="com.ym.mec.biz.dal.entity.TempLiveTeacherCard" id="TempLiveTeacherCard">
        <result column="id_" property="id"/>
        <result column="user_id_" property="userId"/>
        <result column="username_" property="username"/>
        <result column="avatar_" property="avatar"/>
        <result column="introduction_" property="introduction"/>
        <result column="music_content_" property="musicContent"/>
        <result column="subject_id_" property="subjectId"/>
        <result column="level_" property="level"/>
        <result column="im_group_id_" property="imGroupId"/>
        <result column="live_goods_id_" property="liveGoodsId"/>
        <result column="create_time_" property="createTime"/>
        <result column="update_time_" property="updateTime"/>

        <result column="recommend_level_flag_" property="recommendLevelFlag"/>
        <result column="join_flag_" property="joinFlag"/>
        <result column="subject_name_" property="subjectName"/>
        <result column="student_id_" property="studentId"/>
    </resultMap>

    <!-- 根据主键查询一条记录 -->
    <select id="get" resultMap="TempLiveTeacherCard">
        SELECT *
        FROM temp_live_teacher_card
        WHERE id_ = #{id}
    </select>

    <!-- 全查询 -->
    <select id="findAll" resultMap="TempLiveTeacherCard">
        SELECT *
        FROM temp_live_teacher_card
        ORDER BY id_
    </select>

    <!-- 向数据库增加一条记录 -->
    <insert id="insert" parameterType="com.ym.mec.biz.dal.entity.TempLiveTeacherCard" useGeneratedKeys="true"
            keyColumn="id" keyProperty="id">
        INSERT INTO temp_live_teacher_card
        (id_,user_id_,username_,avatar_,introduction_,music_content_,subject_id_,level_,im_group_id_,live_goods_id_,create_time_,update_time_)
        VALUES(#{id},#{username},#{userId},#{avatar},#{introduction},#{musicContent},#{subjectId},#{level},#{imGroupId},#{liveGoodsId},#{createTime},#{updateTime})
    </insert>

    <!-- 根据主键查询一条记录 -->
    <update id="update" parameterType="com.ym.mec.biz.dal.entity.TempLiveTeacherCard">
        UPDATE temp_live_teacher_card
        <set>
            <if test="subjectId != null">
                subject_id_ = #{subjectId},
            </if>
            <if test="userId != null">
                user_id_ = #{userId},
            </if>
            <if test="username != null">
                username_ = #{username},
            </if>
            <if test="avatar != null">
                avatar_ = #{avatar},
            </if>
            <if test="introduction != null">
                introduction_ = #{introduction},
            </if>
            <if test="id != null">
                id_ = #{id},
            </if>
            <if test="musicContent != null">
                music_content_ = #{musicContent},
            </if>
            <if test="imGroupId != null">
                im_group_id_ = #{imGroupId},
            </if>
            <if test="liveGoodsId != null">
                live_goods_id_ = #{liveGoodsId},
            </if>
            <if test="updateTime != null">
                update_time_ = #{updateTime},
            </if>
            <if test="level != null">
                level_ = #{level},
            </if>
            <if test="createTime != null">
                create_time_ = #{createTime},
            </if>
        </set>
        WHERE id_ = #{id}
    </update>

    <!-- 根据主键删除一条记录 -->
    <delete id="delete">
        DELETE
        FROM temp_live_teacher_card
        WHERE id_ = #{id}
    </delete>

    <!-- 分页查询 -->
    <select id="queryPage" resultMap="TempLiveTeacherCard" parameterType="map">
        SELECT * FROM temp_live_teacher_card ORDER BY id_
        <include refid="global.limit"/>
    </select>

    <!-- 查询当前表的总记录数 -->
    <select id="queryCount" resultType="int">
        SELECT COUNT(*)
        FROM temp_live_teacher_card
    </select>

    <select id="list" resultMap="TempLiveTeacherCard">
        select t.*,
               s.name_ as subject_name_,
               #{studentId} as student_id_,
               IF(a.live_goods_id_ is null,0,1) as recommend_level_flag_,
               (select count(1) from im_group_member m where m.im_group_id_ = t.im_group_id_ and m.user_id_ = #{studentId})>0 AS join_flag_
        from temp_live_teacher_card t
        left join `subject` s on t.subject_id_ = s.id_
        left join (
            select
                live_goods_id_,recommend_level_
            from temp_buy_free_live_theory_course
            where user_id_ = #{studentId} and recommend_level_ is not null
            group by live_goods_id_,recommend_level_
        ) a on t.live_goods_id_ = a.live_goods_id_ and t.level_ = a.recommend_level_
        where FIND_IN_SET(subject_id_,#{subjectId})
        order by field(t.level_,'one','two','three')
    </select>

    <select id="getBySubjectIdAndLevel" resultMap="TempLiveTeacherCard">
        SELECT *
        FROM temp_live_teacher_card
        WHERE subject_id_ = #{subjectId} and level_ = #{level}
    </select>


    <select id="querySubjectOption" resultType="com.ym.mec.biz.dal.entity.Subject">
        SELECT
            t.id_ as id,t.name_ as name
        FROM subject t
        where exists(
            select 1 from temp_live_teacher_card cs
            where t.id_ = cs.subject_id_
            and cs.username_ is not null
            and cs.music_content_ is not null
        )
        order by t.id_
    </select>

    <select id="getJoinStstus" resultType="java.lang.Integer">
        select case when
            (
                select count(1) from temp_buy_free_live_theory_course
                where user_id_ = #{userId} and live_goods_id_ = #{goodsId}
            ) = 0 then 0
            when (
                select count(1) from temp_live_teacher_card a
                left join im_group_member b on a.im_group_id_ = b.im_group_id_
                where a.live_goods_id_ = #{goodsId} and b.user_id_ = #{userId}
            ) = 0 then 1 else 2 end
        from dual
    </select>
    <select id="getJoinSubject" resultType="java.lang.String">
        select subject_id_ from temp_live_teacher_card a
        left join im_group_member b on a.im_group_id_ = b.im_group_id_
        where a.live_goods_id_ = #{goodsId} and b.user_id_ = #{userId}
        limit 1;
    </select>
</mapper>
