<?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="cn.iocoder.yudao.module.knowledge.mapper.TTestPaperAnswerSheetMapper">

    <resultMap type="cn.iocoder.yudao.module.knowledge.entity.TTestPaperAnswerSheet"
               id="tTestPaperAnswerSheetMap">
        <result property="id" column="id"/>
        <result property="testPaperInfoId" column="test_paper_info_id"/>
        <result property="score" column="score"/>
        <result property="submitAnswer" column="submit_answer"
                typeHandler="com.baomidou.mybatisplus.extension.handlers.JacksonTypeHandler"/>
        <result property="createTime" column="create_time"/>
        <result property="updateTime" column="update_time"/>
        <result property="deptId" column="dept_id"/>
        <result property="identityId" column="identity_id"/>
        <result property="deleted" column="deleted"/>
        <result property="isHand" column="is_hand"/>
        <result property="examinationNumber" column="examination_number"/>
        <result property="startDate" column="create_time"/>
        <result property="paperName" column="name"/>
    </resultMap>

    <!-- 用于select查询公用抽取的列 -->
    <sql id="columns">
        id
        ,test_paper_info_id,score,submit_answer,create_time,update_time,dept_id,identity_id,
            deleted,examination_number,examination_name
    </sql>
    <update id="delByPaperInfoIds">
        <foreach collection="paperInfoIds" item="item" separator=";">
            update t_test_paper_answer_sheet set deleted = 1 where test_paper_info_id = #{item}
            and deleted = 0
        </foreach>
    </update>
    <select id="findList" resultMap="tTestPaperAnswerSheetMap">
        SELECT
        ttpa.id,
        ttpa.examination_number,
        ttpa.examination_name,
        ttpa.dept_id,
        ttpa.score,
        ttpa.test_paper_info_id,
        ttpa.pass,
        ttpa.is_hand,
        ttpa.create_time
        FROM
        t_test_paper_answer_sheet ttpa
        LEFT JOIN t_test_paper_info ttpi ON ttpi.id = ttpa.test_paper_info_id
        <where>
            <if test="params.id != null">
                and ttpa.id = #{id}
            </if>
            <if test="params.testPaperInfoId != null">
                and ttpa.test_paper_info_id = #{params.testPaperInfoId}
            </if>
            <if test="params.submitAnswer != null and params.submitAnswer != ''">
                and ttpa.submit_answer = #{params.submitAnswer}
            </if>
            <if test="params.deptId != null and params.deptId != ''">
                and ttpa.dept_id = #{params.deptId}
            </if>

            <if test="params.examinationNumber != null and params.examinationNumber != ''">
                and ttpa.examination_number = #{params.examinationNumber}
            </if>
            <if test="params.examinationName != null and params.examinationName != ''">
                and ttpa.examination_name like concat('%',#{params.examinationName},'%')
            </if>
            <if test="params.identityIdList != null and params.identityIdList.size > 0">
                and user_id in
                <foreach collection="params.identityIdList" item="item" index="index" open="(" close=")" separator=",">
                    #{item}
                </foreach>
            </if>
        </where>
        order by ttpa.create_time desc
    </select>


    <select id="findOwnList" resultMap="tTestPaperAnswerSheetMap">
        SELECT
        ttpa.id,
        ttpa.examination_number,
        ttpa.examination_name,
        ttpa.dept_id,
        ttpa.score,
        ttpa.test_paper_info_id,
        ttpa.pass,
        ttpa.is_hand,
        ttpa.create_time,
        ttpi.name
        FROM
        t_test_paper_answer_sheet ttpa
        LEFT JOIN t_test_paper_info ttpi ON ttpi.id = ttpa.test_paper_info_id
        <where>
            <if test="params.id != null">
                and ttpa.id = #{id}
            </if>
            <if test="params.testPaperInfoId != null">
                and ttpa.test_paper_info_id = #{params.testPaperInfoId}
            </if>
            <if test="params.submitAnswer != null and params.submitAnswer != ''">
                and ttpa.submit_answer = #{params.submitAnswer}
            </if>
            <if test="params.deptId != null and params.deptId != ''">
                and ttpa.dept_id = #{params.deptId}
            </if>

            <if test="params.examinationNumber != null and params.examinationNumber != ''">
                and ttpa.examination_number = #{params.examinationNumber}
            </if>
            <if test="params.examinationName != null and params.examinationName != ''">
                and ttpa.examination_name like concat('%',#{params.examinationName},'%')
            </if>
            <if test="params.userId != null and params.userId != ''">
                and ttpa.user_id = #{params.userId}
            </if>
        </where>
        order by ttpa.create_time desc
    </select>


    <select id="selectByPaperInfoId" resultMap="tTestPaperAnswerSheetMap">
        select
        *
        from t_test_paper_answer_sheet
        <where>
            <if test="params.testPaperInfoId != null">
                and test_paper_info_id = #{params.testPaperInfoId}
            </if>
            <if test="params.userId != null and params.userId != ''">
                and user_id = #{params.userId}
            </if>
        </where>
    </select>

    <select id="selfDetail" resultMap="tTestPaperAnswerSheetMap">
        select *
        from t_test_paper_answer_sheet
        where
          test_paper_info_id = #{testPaperInfoId}
          and user_id = #{userId}
          and deleted = 0
        order by create_time desc limit 1
    </select>
    <select id="testCount" resultType="java.lang.Integer">
        select count(*)
        from t_test_paper_answer_sheet
        where user_id = #{userId}
          and test_paper_info_id = #{testPaperInfoId}
          and deleted = 0
          and `type` = 1
    </select>
    <select id="retake" resultType="java.lang.Integer">
        select count(*)
        from t_test_paper_answer_sheet
        where user_id = #{userId}
          and deleted = 0
          and test_paper_info_id =
              (select test_paper_info_id from t_test_paper_answer_sheet where id = #{id} and deleted = 0 limit 1)
    </select>

    <select id="retakes" resultType="java.lang.Integer">
        select count(*)
        from t_test_paper_answer_sheet
        where user_id = #{userId}
          and deleted = 0
          and test_paper_info_id = #{paperId}
    </select>
    <select id="count" resultType="java.lang.Integer">
        select count(*) from t_test_paper_answer_sheet where user_id = #{userId} and deleted = 0
        and test_paper_info_id = #{testPaperInfoId}
    </select>

</mapper>
