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

    <resultMap type="com.ym.mec.biz.dal.entity.TempBuyFreeLiveTheoryCourse" id="TempBuyFreeLiveTheoryCourse">
        <result column="id_" property="id"/>
        <result column="user_id_" property="userId"/>
        <result column="live_id_" property="liveId"/>
        <result column="live_goods_id_" property="liveGoodsId"/>
        <result column="recommend_level_" property="recommendLevel"/>
        <result column="create_time_" property="createTime"/>
        <result column="update_time_" property="updateTime"/>

        <result column="student_name_" property="studentName"/>
    </resultMap>

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

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

    <!-- 向数据库增加一条记录 -->
    <insert id="insert" parameterType="com.ym.mec.biz.dal.entity.TempBuyFreeLiveTheoryCourse" useGeneratedKeys="true"
            keyColumn="id" keyProperty="id">
        INSERT INTO temp_buy_free_live_theory_course (user_id_, live_id_, live_goods_id_,recommend_level_, create_time_, update_time_)
        VALUES (#{userId}, #{liveId}, #{liveGoodsId}, #{recommendLevel}, NOW(), NOW())
    </insert>

    <!-- 根据主键查询一条记录 -->
    <update id="update" parameterType="com.ym.mec.biz.dal.entity.TempBuyFreeLiveTheoryCourse">
        UPDATE temp_buy_free_live_theory_course
        <set>
            <if test="userId != null">
                user_id_ = #{userId},
            </if>
            <if test="liveGoodsId != null">
                live_goods_id_ = #{liveGoodsId},
            </if>
            <if test="liveId != null">
                live_id_ = #{liveId},
            </if>
            <if test="recommendLevel != null and recommendLevel != ''">
                recommend_level_ = #{recommendLevel},
            </if>
            <if test="updateTime != null">
                update_time_ = #{updateTime},
            </if>
        </set>
        WHERE id_ = #{id}
    </update>

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

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

    <!-- 查询当前表的总记录数 -->
    <select id="queryCount" resultType="int">
        SELECT COUNT(*)
        FROM temp_buy_free_live_theory_course
    </select>
    <select id="countByUserId" resultType="java.lang.Integer">
        SELECT COUNT(user_id_)
        FROM temp_buy_free_live_theory_course
        WHERE user_id_ = #{userId}
    </select>

    <select id="list" resultMap="TempBuyFreeLiveTheoryCourse">
        select
               t.*,IFNULL(real_name_,username_) as student_name_
        from temp_buy_free_live_theory_course t
        left join sys_user u on t.user_id_ = u.id_
        <where>
            <if test="param.liveId != null and param.liveId != ''">
                and t.live_id_ = #{param.liveId}
            </if>
            <if test="param.liveGoodsId != null">
                and t.live_goods_id_ = #{param.liveGoodsId}
            </if>
        </where>
    </select>
    <select id="findByGoodsIdAndUserId" resultMap="TempBuyFreeLiveTheoryCourse">
        SELECT * FROM temp_buy_free_live_theory_course
        WHERE user_id_ = #{userId} AND live_goods_id_ = #{liveGoodsId} LIMIT 1
    </select>
</mapper>
