<?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.project.training.elearn.mapper.TElRepoMapper">

    <resultMap type="TElRepo" id="TElRepoResult">
        <result property="repoId"    column="repo_id"    />
        <result property="code"    column="code"    />
        <result property="title"    column="title"    />
        <result property="delFlag"    column="del_flag"    />
        <result property="createrCode"    column="creater_code"    />
        <result property="createdate"    column="createdate"    />
        <result property="updaterCode"    column="updater_code"    />
        <result property="updatedate"    column="updatedate"    />
        <result property="deptId"    column="dept_id"    />
        <result property="remarks"    column="remarks"    />
        <result property="deptName" column="dept_name" />
        <result property="radioCount" column="radio_count" />
        <result property="multiCount" column="multi_count" />
        <result property="judgeCount"  column="judge_count"/>
        <result property="examType"    column="exam_type"    />
        <result property="isStudy"    column="is_study"    />
    </resultMap>

    <sql id="selectTElRepoVo">
        select d.repo_id,d.exam_type,d.is_study, d.code, d.title, d.del_flag, d.creater_code, d.createdate, d.updater_code, d.updatedate, d.dept_id, d.remarks ,
            (SELECT COUNT(0) FROM t_el_qu_repo WHERE repo_id=d.repo_id AND qu_type=1) AS radio_count,
               (SELECT COUNT(0) FROM t_el_qu_repo WHERE repo_id=d.repo_id AND qu_type=2) AS multi_count,
               (SELECT COUNT(0) FROM t_el_qu_repo WHERE repo_id=d.repo_id AND qu_type=3) AS judge_count
        from t_el_repo d
    </sql>

    <select id="selectTElRepoList" parameterType="TElRepo" resultMap="TElRepoResult">
        <include refid="selectTElRepoVo"/>
        <where>
            <if test="examType != null "> and exam_type = #{examType}</if>
            <if test="isStudy != null "> and is_study = #{isStudy}</if>
            <if test="code != null  and code != ''"> and code = #{code}</if>
            <if test="title != null  and title != ''"> and title like concat(concat('%', #{title}), '%')</if>
            <if test="createrCode != null  and createrCode != ''"> and creater_code = #{createrCode}</if>
            <if test="createdate != null "> and createdate = #{createdate}</if>
            <if test="updaterCode != null  and updaterCode != ''"> and updater_code = #{updaterCode}</if>
            <if test="updatedate != null "> and updatedate = #{updatedate}</if>
            <if test="deptId != null "> and dept_id = #{deptId}</if>
            <if test="remarks != null  and remarks != ''"> and remarks = #{remarks}</if>
            and d.del_flag = 0
        </where>
        <!-- 数据范围过滤 -->
        ${params.dataScope}
    </select>

    <select id="selectTElRepoById" parameterType="Long" resultMap="TElRepoResult">
        <include refid="selectTElRepoVo"/>
        where repo_id = #{repoId}
    </select>

    <insert id="insertTElRepo" parameterType="TElRepo">
        <selectKey keyProperty="repoId" resultType="long" order="BEFORE">
            SELECT seq_t_el_repo.NEXTVAL as repoId FROM DUAL
        </selectKey>
        insert into t_el_repo
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="repoId != null">repo_id,</if>
            <if test="code != null">code,</if>
            <if test="title != null">title,</if>
            <if test="delFlag != null">del_flag,</if>
            <if test="createrCode != null">creater_code,</if>
            <if test="createdate != null">createdate,</if>
            <if test="updaterCode != null">updater_code,</if>
            <if test="updatedate != null">updatedate,</if>
            <if test="deptId != null">dept_id,</if>
            <if test="remarks != null">remarks,</if>
            <if test="examType != null">exam_type,</if>
            <if test="isStudy != null">is_study,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="repoId != null">#{repoId},</if>
            <if test="code != null">#{code},</if>
            <if test="title != null">#{title},</if>
            <if test="delFlag != null">#{delFlag},</if>
            <if test="createrCode != null">#{createrCode},</if>
            <if test="createdate != null">#{createdate},</if>
            <if test="updaterCode != null">#{updaterCode},</if>
            <if test="updatedate != null">#{updatedate},</if>
            <if test="deptId != null">#{deptId},</if>
            <if test="remarks != null">#{remarks},</if>
            <if test="examType != null">#{examType},</if>
            <if test="isStudy != null">#{isStudy},</if>
         </trim>
    </insert>

    <update id="updateTElRepo" parameterType="TElRepo">
        update t_el_repo
        <trim prefix="SET" suffixOverrides=",">
            <if test="code != null">code = #{code},</if>
            <if test="title != null">title = #{title},</if>
            <if test="delFlag != null">del_flag = #{delFlag},</if>
            <if test="createrCode != null">creater_code = #{createrCode},</if>
            <if test="createdate != null">createdate = #{createdate},</if>
            <if test="updaterCode != null">updater_code = #{updaterCode},</if>
            <if test="updatedate != null">updatedate = #{updatedate},</if>
            <if test="deptId != null">dept_id = #{deptId},</if>
            <if test="remarks != null">remarks = #{remarks},</if>
            <if test="examType != null">exam_type = #{examType},</if>
            <if test="isStudy != null">is_study = #{isStudy},</if>
        </trim>
        where repo_id = #{repoId}
    </update>

    <update id="deleteTElRepoById" parameterType="Long">
        update t_el_repo set del_flag = 2 where repo_id = #{repoId}
    </update>

    <update id="deleteTElRepoByIds" parameterType="String">
        update t_el_repo set del_flag = 2 where repo_id in
        <foreach item="repoId" collection="array" open="(" separator="," close=")">
            #{repoId}
        </foreach>
    </update>

</mapper>
