<?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.ie.mapper.PapersMapper">

    <resultMap type="Papers" id="PapersResult">
        <result property="id"    column="id"    />
        <result property="paperName"    column="paperName"    />
        <result property="subjectName"    column="subjectName"    />
        <result property="parseName"    column="parseName"    />
        <result property="number"    column="number"    />
        <result property="tiid"    column="tiid"    />
        <result property="year"    column="year"    />
        <result property="area"    column="area"    />
        <result property="paperType"    column="paperType"    />
        <result property="gradeName"    column="gradeName"    />
        <result property="mark"    column="mark"    />
        <result property="isExist"    column="isExist"    />
        <result property="fenshu"    column="fenshu"    />
        <result property="paperSource"    column="paperSource"    />
        <result property="attachments"    column="attachments"    />
        <result property="osspath"    column="osspath"    />
        <result property="filename"    column="filename"    />
        <result property="subjectId"    column="subjectId"    />
        <result property="paperTypeId"    column="paperTypeId"    />
        <result property="examineeId"    column="examineeId"    />
        <result property="accuracy"    column="accuracy"    />
        <result property="location"    column="location"    />
        <result property="examineeType"    column="examineeType"    />
        <result property="createTime"    column="createTime"    />
        <result property="collect"    column="collect"    />
        <collection  property="questions"   javaType="java.util.List"  resultMap="QuestionsQueryResult" />
    </resultMap>

    <resultMap type="com.ruoyi.ie.domain.Questions" id="QuestionsQueryResult">
        <result property="id"    column="id2"    />
        <result property="title"    column="title2"    />
        <result property="optionA"    column="option_a2"    />
        <result property="optionB"    column="option_b2"    />
        <result property="optionC"    column="option_c2"    />
        <result property="optionD"    column="option_d2"    />
        <result property="optionE"    column="option_e2"    />
        <result property="qtpye"    column="qtpye2"    />
        <result property="diff"    column="diff2"    />
        <result property="subjectId"    column="subjectId2"    />
        <result property="gradeId"    column="gradeId2"    />
        <result property="knowledges"    column="knowledges2"    />
        <result property="area"    column="area2"    />
        <result property="year"    column="year2"    />
        <result property="paperTpye"    column="paperTpye2"    />
        <result property="source"    column="source2"    />
        <result property="fromSite"    column="fromSite2"    />
        <result property="isSub"    column="isSub2"    />
        <result property="isNormal"    column="isNormal2"    />
        <result property="isKonw"    column="isKonw2"    />
        <result property="similarity"    column="Similarity2"    />
        <result property="isunique"    column="isunique2"    />
    </resultMap>


    <sql id="selectPapersVo">
        select pap.id, pap.paperName, pap.subjectId, pap.subjectName, pap.parseName, pap.number, pap.tiid, pap.year, pap.area,
        pap.paperType, pap.gradeName, pap.mark, pap.isExist, pap.fenshu, pap.paperSource,pap.osspath,pap.attachments,pap.filename,pap.paperTypeId
        from papers pap
    </sql>

    <select id="selectMyPapersList" parameterType="Map" resultMap="PapersResult">
        select pap.id, pap.paperName, pap.subjectId, pap.subjectName, pap.parseName, pap.number, pap.tiid, pap.year, pap.area,
               pap.paperType, pap.gradeName, pap.mark, pap.isExist, pap.fenshu, pap.paperSource,pap.osspath,pap.attachments,pap.filename,pap.paperTypeId,
               pe.`examinee_id` examineeId, pe.begin_time createTime, IF(pe.`state` = 6, round(pe.`score_rate` * 100), null) accuracy, f.refId > 0 collect
        from `mxjb_paper_examinee` pe join papers pap on pe.paper_id = pap.id
        left join b_customer_favorites f on f.type = 'paper' and f.customercode = #{creatorId} and f.status = 1 and f.refId = pap.id
        <where> pe.customer_code = #{creatorId} and pe.state &gt; 4
        <if test="subjectId != null"> and pap.subjectId = #{subjectId}</if>
        <choose>
            <when test="paperType == '所有'">and pap.paperType != '历年真题' </when>
            <when test="paperType == '模拟试卷'">and pap.paperType in ('历年真题', '模拟试卷') </when>
            <when test="paperType != null and paperType != ''"> and pap.paperType like concat('%', #{paperType},'%')</when>
        </choose>
        </where>
       order by pe.`end_time` DESC, pe.begin_time desc
    </select>

    <select id="selectPapersList" parameterType="Papers" resultMap="PapersResult">
        <include refid="selectPapersVo"/>
        <where>
            <if test="paperName != null  and paperName != ''"> and pap.paperName like concat('%', #{paperName}, '%')</if>
            <if test="subjectId != null "> and pap.subjectId = #{subjectId}</if>
            <if test="subjectName != null  and subjectName != ''"> and pap.subjectName like concat('%', #{subjectName}, '%')</if>
            <if test="parseName != null  and parseName != ''"> and pap.parseName like concat('%', #{parseName}, '%')</if>
            <if test="number != null  and number != ''"> and pap.number = #{number}</if>
            <if test="tiid != null  and tiid != ''"> and pap.tiid = #{tiid}</if>
            <if test="year != null "> and pap.year = #{year}</if>
            <if test="area != null  and area != ''"> and pap.area like concat('%', #{area},'%')</if>
            <if test="location != null  and location != ''"> and (`location` IS NULL OR FIND_IN_SET(#{location}, `location`) &gt; 0)</if>
            <if test="examineeType != null  and examineeType != ''"> and (`examineeType` IS NULL OR FIND_IN_SET(#{examineeType}, `examineeType`) &gt; 0)</if>
            <choose>
              <when test="paperType == '所有'">and pap.paperType != '历年真题' </when>
              <when test="paperType == '模拟试卷'">and pap.paperType in ('历年真题', '模拟试卷') </when>
              <when test="paperType != null and paperType != ''"> and pap.paperType like concat('%', #{paperType},'%')</when>
            </choose>
            <if test="gradeName != null  and gradeName != ''"> and pap.gradeName like concat('%', #{gradeName}, '%')</if>
            <if test="mark != null "> and pap.mark = #{mark}</if>
            <if test="isExist != null "> and pap.isExist = #{isExist}</if>
            <if test="fenshu != null "> and pap.fenshu = #{fenshu}</if>
        </where>
        order by pap.id desc
    </select>

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

    <select id="selectPapersById" parameterType="Long" resultMap="PapersResult">
        <include refid="selectPapersVo"/>
        where pap.id = #{id}
    </select>

    <select id="getPaperInfo" parameterType="Long" resultMap="PapersResult">
        <include refid="selectPapersVo"/>
        where pap.id = #{paperId}
    </select>

    <insert id="insertPapers" parameterType="Papers" useGeneratedKeys="true" keyProperty="id">
        insert into papers
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="paperName != null">paperName,</if>
            <if test="subjectId != null">subjectId,</if>
            <if test="subjectName != null">subjectName,</if>
            <if test="parseName != null">parseName,</if>
            <if test="number != null">number,</if>
            <if test="tiid != null">tiid,</if>
            <if test="year != null">year,</if>
            <if test="area != null">area,</if>
            <if test="paperType != null">paperType,</if>
            <if test="gradeName != null">gradeName,</if>
            <if test="mark != null">mark,</if>
            <if test="isExist != null">isExist,</if>
            <if test="fenshu != null">fenshu,</if>
            <if test="paperSource != null">paperSource,</if>
            <if test="osspath != null">osspath,</if>
            <if test="attachments != null">attachments,</if>
            <if test="filename != null">filename,</if>
            <if test="location != null">location,</if>
            <if test="examineeType != null">examineeType,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="paperName != null">#{paperName},</if>
            <if test="subjectId != null">#{subjectId},</if>
            <if test="subjectName != null">#{subjectName},</if>
            <if test="parseName != null">#{parseName},</if>
            <if test="number != null">#{number},</if>
            <if test="tiid != null">#{tiid},</if>
            <if test="year != null">#{year},</if>
            <if test="area != null">#{area},</if>
            <if test="paperType != null">#{paperType},</if>
            <if test="gradeName != null">#{gradeName},</if>
            <if test="mark != null">#{mark},</if>
            <if test="isExist != null">#{isExist},</if>
            <if test="fenshu != null">#{fenshu},</if>
            <if test="paperSource != null">#{paperSource},</if>
            <if test="osspath != null">#{osspath},</if>
            <if test="attachments != null">#{attachments},</if>
            <if test="filename != null">#{filename},</if>
            <if test="location != null">#{location},</if>
            <if test="examineeType != null">#{examineeType},</if>
         </trim>
    </insert>

    <insert id="insertMxjbPapers" parameterType="Papers" useGeneratedKeys="true" keyProperty="id">
        insert into mxjb_papers
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="paperName != null">paperName,</if>
            <if test="subjectId != null">subjectId,</if>
            <if test="subjectName != null">subjectName,</if>
            <if test="parseName != null">parseName,</if>
            <if test="number != null">number,</if>
            <if test="tiid != null">tiid,</if>
            <if test="year != null">year,</if>
            <if test="area != null">area,</if>
            <if test="paperType != null">paperType,</if>
            <if test="gradeName != null">gradeName,</if>
            <if test="mark != null">mark,</if>
            <if test="isExist != null">isExist,</if>
            <if test="fenshu != null">fenshu,</if>
            <if test="paperSource != null">paperSource,</if>
            <if test="osspath != null">osspath,</if>
            <if test="attachments != null">attachments,</if>
            <if test="filename != null">filename,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="paperName != null">#{paperName},</if>
            <if test="subjectId != null">#{subjectId},</if>
            <if test="subjectName != null">#{subjectName},</if>
            <if test="parseName != null">#{parseName},</if>
            <if test="number != null">#{number},</if>
            <if test="tiid != null">#{tiid},</if>
            <if test="year != null">#{year},</if>
            <if test="area != null">#{area},</if>
            <if test="paperType != null">#{paperType},</if>
            <if test="gradeName != null">#{gradeName},</if>
            <if test="mark != null">#{mark},</if>
            <if test="isExist != null">#{isExist},</if>
            <if test="fenshu != null">#{fenshu},</if>
            <if test="paperSource != null">#{paperSource},</if>
            <if test="osspath != null">#{osspath},</if>
            <if test="attachments != null">#{attachments},</if>
            <if test="filename != null">#{filename},</if>
        </trim>
    </insert>

    <update id="updatePapers" parameterType="Papers">
        update papers
        <trim prefix="SET" suffixOverrides=",">
            <if test="paperName != null">paperName = #{paperName},</if>
            <if test="subjectName != null">subjectName = #{subjectName},</if>
            <if test="parseName != null">parseName = #{parseName},</if>
            <if test="number != null">number = #{number},</if>
            <if test="tiid != null">tiid = #{tiid},</if>
            <if test="year != null">year = #{year},</if>
            <if test="area != null">area = #{area},</if>
            <if test="paperType != null">paperType = #{paperType},</if>
            <if test="gradeName != null">gradeName = #{gradeName},</if>
            <if test="mark != null">mark = #{mark},</if>
            <if test="isExist != null">isExist = #{isExist},</if>
            <if test="fenshu != null">fenshu = #{fenshu},</if>
            <if test="paperSource != null">paperSource = #{paperSource},</if>
            <if test="osspath != null">osspath = #{osspath},</if>
            <if test="attachments != null">attachments = #{attachments},</if>
            <if test="filename != null">filename = #{filename},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deletePapersById" parameterType="Long">
        delete from papers where id = #{id}
    </delete>

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

  <select id="filterList" parameterType="com.ruoyi.ie.domain.Papers" resultType="String">
    select distinct ${column} from papers pap
    <where>
      <if test="subjectName != null  and subjectName != ''"> and pap.subjectName = #{subjectName} </if>
      <if test="parseName != null  and parseName != ''"> and pap.parseName = #{parseName} </if>
      <if test="number != null  and number != ''"> and pap.number = #{number}</if>
      <if test="tiid != null  and tiid != ''"> and pap.tiid = #{tiid}</if>
      <if test="year != null "> and pap.year = #{year}</if>
      <if test="area != null  and area != ''"> and pap.area = #{area}</if>
      <choose>
        <when test="paperType != null and paperType != ''"> and pap.paperType = #{paperType}</when>
        <otherwise>and pap.paperType != '历年真题' </otherwise>
      </choose>
      <if test="gradeName != null  and gradeName != ''"> and pap.gradeName = #{gradeName} </if>
      <if test="mark != null "> and pap.mark = #{mark}</if>
      <if test="isExist != null "> and pap.isExist = #{isExist}</if>
      <if test="fenshu != null "> and pap.fenshu = #{fenshu}</if>
      <choose>
        <when test="column=='year'"> and year &lt; 2200 and year &gt; 2014</when>
        <otherwise>and pap.${column} != '' </otherwise>
      </choose>
    </where>
    <choose>
    	<when test="column=='paperType'">
    		order by FIELD(paperType, '单元测试', '月考试卷', '期中考试', '期末考试', '专题练习', '同步练习', '开学考试')
    	</when>
      <when test="column=='subjectName'">
        order by FIELD(subjectName, '语文','数学','英语','物理','化学','生物','历史','地理','政治')
      </when>
      <when test="column=='gradeName'">
        order by LOCATE(SUBSTRING(REPLACE(gradeName, '高', ''), 1, 1), '一1二2三3四4五5六6七7八8九9')
      </when>
      <when test="column=='year'">
        order by year desc
      </when>
      <when test="column=='area'">
        order by field(area,'全国','湖南','湖北','安徽','江西','河南','广东','广西','河北','山东','北京','四川','贵州','江苏','福建','浙江','山西','吉林','陕西','海南','内蒙古','黑龙江','重庆','甘肃','上海','云南','辽宁','青海','新疆','西藏','宁夏','天津')
      </when>
      <otherwise>
        order by ${column}
      </otherwise>
    </choose>
  </select>

  <select id="selectCollectInfo" parameterType="java.util.HashMap" resultMap="PapersResult">
  	SELECT p.id from b_customer_favorites f
	left join papers p on f.refId = p.id
	where f.type = 'paper'
	and f.status = 1
	and f.customerCode = #{customerCode}
	and p.id in
	<foreach item="id" collection="ids" open="(" separator="," close=")">
		        #{id}
	</foreach>
  </select>

  <select id="selectGrades" parameterType="Papers" resultMap="PapersResult">
  	SELECT distinct gradeName
	from papers p
	where 1=1

	<if test="subjectName != null  and subjectName != ''"> and p.subjectName = #{subjectName} </if>
    <if test="parseName != null  and parseName != ''">
    	and p.parseName = #{parseName}
    </if>



    <if test="parseName != null  and parseName != ''">
    	<if test="parseName == '高中'">
    		order by FIELD(p.gradeName, '高一','高二','高三')
    	</if>
    	<if test="parseName == '初中'">
    		order by FIELD(p.gradeName, '七年级','八年级','九年级')
    	</if>
    </if>

  </select>

</mapper>
