<?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.ruoyi.learn.mapper.LearnPaperMapper">

    <resultMap type="LearnPaper" id="LearnPaperResult">
        <result property="id"    column="id"    />
        <result property="subjectId"    column="subjectId"    />
        <result property="paperName"    column="paperName"    />
        <result property="year"    column="year"    />
        <result property="paperType"    column="paperType"    />
        <result property="status"    column="status"    />
        <result property="number"    column="number"    />
        <result property="fenshu"    column="fenshu"    />
        <result property="paperSource"    column="paperSource"    />
        <result property="directKey"    column="direct_key"    />
        <result property="paperInfo"    column="paper_info"    />
        <result property="tiid"    column="tiid"    />
        <result property="osspath"    column="osspath"    />
        <result property="filename"    column="filename"    />
        <result property="relateId"    column="relate_id"    />
        <result property="locations"    column="locations"    />
        <result property="examineeTypes"    column="examineeTypes"    />
        <result property="createTime"    column="createTime"    />
    </resultMap>

    <sql id="selectLearnPaperVo">
        select id, subjectId, paperName, year, paperType, number, fenshu, paperSource, direct_key, paper_info, tiid, osspath, filename, relate_id, locations, examineeTypes, createTime from learn_paper
    </sql>

    <select id="selectLearnPaperList" parameterType="LearnPaper" resultMap="LearnPaperResult">
        <include refid="selectLearnPaperVo"/>
        <where>
            <if test="subjectId != null "> and subjectId = #{subjectId}</if>
            <if test="paperName != null  and paperName != ''"> and paperName like concat('%', #{paperName}, '%')</if>
            <if test="year != null "> and year = #{year}</if>
            <if test="paperType != null  and paperType != ''"> and paperType = #{paperType}</if>
            <if test="status != null"> and status = #{status}</if>
            <if test="number != null  and number != ''"> and number = #{number}</if>
            <if test="fenshu != null "> and fenshu = #{fenshu}</if>
            <if test="paperSource != null "> and paperSource = #{paperSource}</if>
            <if test="directKey != null  and directKey != ''"> and direct_key like concat(#{directKey}, '%')</if>
            <if test="paperInfo != null  and paperInfo != ''"> and paper_info = #{paperInfo}</if>
            <if test="tiid != null  and tiid != ''"> and tiid = #{tiid}</if>
            <if test="osspath != null  and osspath != ''"> and osspath = #{osspath}</if>
            <if test="filename != null  and filename != ''"> and filename like concat('%', #{filename}, '%')</if>
            <if test="relateId != null "> and relate_id = #{relateId}</if>
            <if test="locations != null  and locations != ''"> and locations = #{locations}</if>
            <if test="examineeTypes != null  and examineeTypes != ''"> and examineeTypes = #{examineeTypes}</if>
            <if test="createTime != null "> and createTime = #{createTime}</if>
        </where>
    </select>

    <select id="selectLearnPaperForStudent" resultMap="LearnPaperResult">
        SELECT p.*
          FROM `learn_test_student` ts JOIN `learn_paper` p on ts.paper_id = p.id
        <where> ts.student_id = #{studentId}
            <if test="status != null "> and ts.status = #{status}</if>
        </where>

    </select>

    <select id="selectLearnPaperById" parameterType="Long" resultMap="LearnPaperResult">
        <include refid="selectLearnPaperVo"/>
        where id = #{id}
    </select>

    <insert id="insertLearnPaper" parameterType="LearnPaper" useGeneratedKeys="true" keyProperty="id">
        insert into learn_paper
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="subjectId != null">subjectId,</if>
            <if test="paperName != null">paperName,</if>
            <if test="year != null">year,</if>
            <if test="paperType != null">paperType,</if>
            <if test="status != null">status,</if>
            <if test="number != null">number,</if>
            <if test="fenshu != null">fenshu,</if>
            <if test="paperSource != null">paperSource,</if>
            <if test="directKey != null and directKey != ''">direct_key,</if>
            <if test="paperInfo != null and paperInfo != ''">paper_info,</if>
            <if test="tiid != null">tiid,</if>
            <if test="osspath != null">osspath,</if>
            <if test="filename != null">filename,</if>
            <if test="relateId != null">relate_id,</if>
            <if test="locations != null">locations,</if>
            <if test="examineeTypes != null">examineeTypes,</if>
            <if test="createTime != null">createTime,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="subjectId != null">#{subjectId},</if>
            <if test="paperName != null">#{paperName},</if>
            <if test="year != null">#{year},</if>
            <if test="paperType != null">#{paperType},</if>
            <if test="status != null">#{status},</if>
            <if test="number != null">#{number},</if>
            <if test="fenshu != null">#{fenshu},</if>
            <if test="paperSource != null">#{paperSource},</if>
            <if test="directKey != null and directKey != ''">#{directKey},</if>
            <if test="paperInfo != null and paperInfo != ''">#{paperInfo},</if>
            <if test="tiid != null">#{tiid},</if>
            <if test="osspath != null">#{osspath},</if>
            <if test="filename != null">#{filename},</if>
            <if test="relateId != null">#{relateId},</if>
            <if test="locations != null">#{locations},</if>
            <if test="examineeTypes != null">#{examineeTypes},</if>
            <if test="createTime != null">#{createTime},</if>
         </trim>
    </insert>

    <update id="updateLearnPaper" parameterType="LearnPaper">
        update learn_paper
        <trim prefix="SET" suffixOverrides=",">
            <if test="subjectId != null">subjectId = #{subjectId},</if>
            <if test="paperName != null">paperName = #{paperName},</if>
            <if test="year != null">year = #{year},</if>
            <if test="paperType != null">paperType = #{paperType},</if>
            <if test="status != null">status = #{status},</if>
            <if test="number != null">number = #{number},</if>
            <if test="fenshu != null">fenshu = #{fenshu},</if>
            <if test="paperSource != null">paperSource = #{paperSource},</if>
            <if test="directKey != null and directKey != ''">direct_key = #{directKey},</if>
            <if test="paperInfo != null and paperInfo != ''">paper_info = #{paperInfo},</if>
            <if test="tiid != null">tiid = #{tiid},</if>
            <if test="osspath != null">osspath = #{osspath},</if>
            <if test="filename != null">filename = #{filename},</if>
            <if test="relateId != null">relate_id = #{relateId},</if>
            <if test="locations != null">locations = #{locations},</if>
            <if test="examineeTypes != null">examineeTypes = #{examineeTypes},</if>
            <if test="createTime != null">createTime = #{createTime},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteLearnPaperById" parameterType="Long">
        delete from learn_paper where id = #{id}
    </delete>

    <delete id="deleteLearnPaperByIds" parameterType="String">
        delete from learn_paper where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>

    <select id="selectPapersListFromFavorites" parameterType="LearnPaper" resultMap="LearnPaperResult">
        select p.*,0 readNum from b_customer_favorites f
        left join learn_paper p on f.refId = p.id
        <where> f.type = 'paper' and f.customercode = #{examineeId}
            <if test="subjectId != null and subjectId > 0"> and f.course = #{subjectId} </if>
             <if test="collect "> and f.status = 1</if>
        </where>
    </select>


</mapper>
