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

    <resultMap type="TElRecord" id="TElRecordResult">
        <result property="id"    column="id"    />
        <result property="plantCode"    column="plant_code"    />
        <result property="name"    column="name"    />
        <result property="staffId"    column="staff_id"    />
        <result property="userId"    column="user_id"    />
        <result property="classes"    column="classes"    />
        <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="remarks"    column="remarks"    />
        <result property="deptId"    column="dept_id"    />
        <result property="quTotal"    column="qu_total"    />
        <result property="quRight"    column="qu_right"    />
        <result property="examTotal"    column="exam_total"    />
        <result property="examPassed"    column="exam_passed"    />
        <result property="deptName" column="dept_name" />
    </resultMap>

    <sql id="selectTElRecordVo">
        select d.id, d.plant_code, d.name, d.staff_id,d.user_id, d.classes, d.del_flag, d.creater_code, d.createdate, d.updater_code, d.updatedate, d.remarks, d.dept_id, d.qu_total, d.qu_right, d.exam_total, d.exam_passed from t_el_record d
    </sql>

    <select id="selectTElRecordList" parameterType="TElRecord" resultMap="TElRecordResult">
        <include refid="selectTElRecordVo"/>
        <where>
            <if test="plantCode != null  and plantCode != ''"> and plant_code = #{plantCode}</if>
            <if test="name != null  and name != ''"> and name like concat(concat('%', #{name}), '%')</if>
            <if test="staffId != null  and staffId != ''"> and staff_id = #{staffId}</if>
            <if test="classes != null  and classes != ''"> and classes = #{classes}</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="remarks != null  and remarks != ''"> and remarks = #{remarks}</if>
            <if test="deptId != null "> and dept_id = #{deptId}</if>
            <if test="quTotal != null "> and qu_total = #{quTotal}</if>
            <if test="quRight != null "> and qu_right = #{quRight}</if>
            <if test="examTotal != null "> and exam_total = #{examTotal}</if>
            <if test="examPassed != null "> and exam_passed = #{examPassed}</if>
            and d.del_flag = 0
        </where>
        <!-- 数据范围过滤 -->
        ${params.dataScope}
    </select>

    <select id="selectTElRecordById" parameterType="Long" resultMap="TElRecordResult">
        <include refid="selectTElRecordVo"/>
        where id = #{id}
    </select>

    <insert id="insertTElRecord" parameterType="TElRecord">
        <selectKey keyProperty="id" resultType="long" order="BEFORE">
            SELECT seq_t_el_record.NEXTVAL as id FROM DUAL
        </selectKey>
        insert into t_el_record
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">id,</if>
            <if test="plantCode != null">plant_code,</if>
            <if test="name != null">name,</if>
            <if test="staffId != null">staff_id,</if>
            <if test="userId != null">user_id,</if>
            <if test="classes != null">classes,</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="remarks != null">remarks,</if>
            <if test="deptId != null">dept_id,</if>
            <if test="quTotal != null">qu_total,</if>
            <if test="quRight != null">qu_right,</if>
            <if test="examTotal != null">exam_total,</if>
            <if test="examPassed != null">exam_passed,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id != null">#{id},</if>
            <if test="plantCode != null">#{plantCode},</if>
            <if test="name != null">#{name},</if>
            <if test="staffId != null">#{staffId},</if>
            <if test="userId != null">#{userId},</if>
            <if test="classes != null">#{classes},</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="remarks != null">#{remarks},</if>
            <if test="deptId != null">#{deptId},</if>
            <if test="quTotal != null">#{quTotal},</if>
            <if test="quRight != null">#{quRight},</if>
            <if test="examTotal != null">#{examTotal},</if>
            <if test="examPassed != null">#{examPassed},</if>
         </trim>
    </insert>

    <update id="updateTElRecord" parameterType="TElRecord">
        update t_el_record
        <trim prefix="SET" suffixOverrides=",">
            <if test="plantCode != null">plant_code = #{plantCode},</if>
            <if test="name != null">name = #{name},</if>
            <if test="staffId != null">staff_id = #{staffId},</if>
            <if test="userId != null">user_id = #{userId},</if>
            <if test="classes != null">classes = #{classes},</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="remarks != null">remarks = #{remarks},</if>
            <if test="deptId != null">dept_id = #{deptId},</if>
            <if test="quTotal != null">qu_total = #{quTotal},</if>
            <if test="quRight != null">qu_right = #{quRight},</if>
            <if test="examTotal != null">exam_total = #{examTotal},</if>
            <if test="examPassed != null">exam_passed = #{examPassed},</if>
        </trim>
        where id = #{id}
    </update>

    <update id="deleteTElRecordById" parameterType="Long">
        update t_el_record set del_flag = 2 where id = #{id}
    </update>


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

    <select id="queryNeedDeleteIds" resultType="String">
        SELECT b.staff_id from t_el_record b where b.del_flag = 0
            minus
        SELECT a.STAFFID FROM "T_STAFFMGR" a where DEPT_ID = 103 and a.del_flag = 0 and a.unit = 10
    </select>

    <select id="queryNeedInsertIds" resultType="String">
        SELECT a.STAFFID FROM "T_STAFFMGR" a where DEPT_ID = 103 and a.del_flag = 0 and a.unit = 10
            minus
        SELECT b.staff_id from t_el_record b where b.del_flag = 0
    </select>

    <update id="deleteTElRecordByStaffId" parameterType="String">
        update t_el_record set del_flag = 2 where staff_id = #{staffId}
    </update>

    <select id="queryQuData" parameterType="Long" resultMap="TElRecordResult">
        SELECT
            COUNT(*) AS qu_total,
            SUM(IS_RIGHT) AS qu_right
        FROM
            T_EL_USER_QU
        WHERE
            USER_ID = #{userId}
    </select>

    <select id="queryExamData" parameterType="Long" resultMap="TElRecordResult">
        SELECT
            COUNT(*) AS exam_total,
            SUM(CASE
                    WHEN p.USER_SCORE >= p.QUALIFY_SCORE THEN 1
                    ELSE 0
                END) AS exam_passed
        FROM
            T_EL_PAPER p
                JOIN
            T_EL_EXAM e
            ON
                e.exam_id = p.exam_id
        WHERE
            p.USER_ID = #{userId} and e.EXAM_TYPE = 1
    </select>



</mapper>
