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

    <resultMap type="Subject" id="SubjectResult">
        <result property="subjectId"    column="subjectId"    />
        <result property="subjectName"    column="subjectName"    />
        <result property="pinyin"    column="pinyin"    />
        <result property="sort"    column="sort"    />
        <result property="location"    column="location"    />
        <result property="examType"    column="examType"    />
        <result property="updateTime"    column="update_time"    />
    </resultMap>

    <sql id="selectSubjectVo">
        select subjectId, subjectName, pinyin, sort, location, examType from subject
    </sql>

    <select id="selectSubjectList" parameterType="Subject" resultMap="SubjectResult">
        <include refid="selectSubjectVo"/>
        <where>
            <if test="subjectName != null  and subjectName != ''"> and subjectName like concat('%', #{subjectName}, '%')</if>
            <if test="location != null  and location != ''"> and (`location` IS NULL OR FIND_IN_SET(#{location}, `location`) &gt; 0)</if>
            <if test="examType != null  and examType != ''"> and (`examType` IS NULL OR FIND_IN_SET(#{examType}, `examType`) &gt; 0)</if>
            <if test="subjectId != null "> and (subjectId &lt;= 3 or subjectId = #{subjectId})</if>
        </where>
        order by sort asc
    </select>

    <select id="selectSubjectListBack" parameterType="Subject" resultMap="SubjectResult">
        <include refid="selectSubjectVo"/>
        <where>
            <if test="subjectName != null  and subjectName != ''"> and subjectName like concat('%', #{subjectName}, '%')</if>
            <if test="subjectId != null"> subjectId = #{subjectId}</if>
        </where>
        order by sort asc
    </select>

    <select id="selectSubjectById" parameterType="Long" resultMap="SubjectResult">
        <include refid="selectSubjectVo"/>
        where subjectId = #{subjectId}
    </select>

    <insert id="insertSubject" parameterType="Subject">
        insert into subject
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="subjectId != null">subjectId,</if>
            <if test="subjectName != null">subjectName,</if>
            <if test="pinyin != null">pinyin,</if>
            <if test="location != null">location,</if>
            <if test="examType != null">examType,</if>
            <if test="updateTime != null">update_time,</if>

         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="subjectId != null">#{subjectId},</if>
            <if test="subjectName != null">#{subjectName},</if>
            <if test="pinyin != null">#{pinyin},</if>
            <if test="location != null">#{location},</if>
            <if test="examType != null">#{examType},</if>
            <if test="updateTime != null">#{updateTime},</if>

         </trim>
    </insert>

    <update id="updateSubject" parameterType="Subject">
        update subject
        <trim prefix="SET" suffixOverrides=",">
            <if test="subjectName != null">subjectName = #{subjectName},</if>
            <if test="pinyin != null">pinyin = #{pinyin},</if>
            <if test="location != null">location = #{location},</if>
            <if test="examType != null">examType = #{examType},</if>
            <if test="updateTime != null">update_time=#{updateTime},</if>

        </trim>
        where subjectId = #{subjectId}
    </update>

    <delete id="deleteSubjectById" parameterType="Long">
        delete from subject where subjectId = #{subjectId}
    </delete>

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

    <select id="getQuestionsSubject" parameterType="Integer" resultMap="SubjectResult">
     select t1.subjectId,ANY_VALUE(t1.subjectName) subjectName from `subject` t1
    left join chapter t2 on t2.subjectId=t1.subjectId
    where t2.pharseId=#{phaseId}
    GROUP BY t1.subjectId
    </select>

</mapper>
