<?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">
<!--mybatis-3-mapper.dtd:约束文件的名称，限制和检查在当前文件中出现的标签和属性符合mybatis的要求-->
<!--namespace：命名空间，要有唯一的值，要求使用dao接口的权限定名称（一个dao接口对应一个mapper，namespace指明对应哪个dao接口）-->
<mapper namespace="com.ruoyi.test.mapper.AnswerMapper">
    <!-- 所有的数据库操作都要写在mapper标签中，可以使用特定的标签表示数据库中的特定操作 -->

    <!-- 批量插入Answer对象 -->
    <insert id="answer" parameterType="Answer">
        INSERT INTO answer (
                            sender,
                            testid,
                            idanswer,
                            finishtime,
                            time
        )VALUES(
                    #{answer.sender},
                    #{answer.testid},
                    #{answer.idanswer},
                    Now(),
                    #{answer.time}
              )
    </insert>

    <select id="selectBySender" resultType="answer">
        SELECT t.*
        FROM (
                 SELECT a.testid,
                        a.idanswer,
                        a.finishtime,
                        ABS(TIMESTAMPDIFF(SECOND, STR_TO_DATE(a.finishtime, '%Y-%m-%d %H:%i:%s'), NOW())) AS time_diff,
                        a.sender
                 FROM answer a
                          INNER JOIN (
                     SELECT testid,
                            MIN(ABS(TIMESTAMPDIFF(SECOND, STR_TO_DATE(finishtime, '%Y-%m-%d %H:%i:%s'), NOW()))) AS min_time_diff
                     FROM answer
                     WHERE sender = #{sender}
                     GROUP BY testid
                 ) b ON a.testid = b.testid AND ABS(TIMESTAMPDIFF(SECOND, STR_TO_DATE(a.finishtime, '%Y-%m-%d %H:%i:%s'), NOW())) = b.min_time_diff
             ) AS t;
    </select>


</mapper>
