<?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.qmth.themis.business.dao.TEAudioMapper">

    <select id="query" resultType="com.qmth.themis.business.entity.TEAudio">
        select
        t.id,
        t.orgId,
        t.activityId,
        t.content,
        t.`type`,
        IFNULL(t.enable, false) as enable,
        t.audioDefault,
        t.attachmentId,
        if(t.updateName is not null,
        t.updateName,
        t.createName) as updateName,
        if(t.updateTime is not null,
        t.updateTime,
        t.createTime) as updateTime,
        t.playTime
        from
        (
        select
        tea.id,
        tea.org_id as orgId,
        tea.activity_id as activityId,
        tea.content,
        tea.`type`,
        tea.enable,
        tea.audio_default as audioDefault,
        tea.attachment_id as attachmentId,
        tea.play_time as playTime,
        (
        select
        t.name
        from
        t_b_user t
        where
        t.id = tea.create_id) as createName,
        tea.create_time as createTime,
        (
        select
        t.name
        from
        t_b_user t
        where
        t.id = tea.update_id) as updateName,
        tea.update_time as updateTime
        from
        t_e_audio tea
        <where>1 = 1
            <if test="activityId != null and activityId != ''">
                and tea.activity_id = #{activityId}
            </if>
        </where>
            order by tea.create_time
        ) t
    </select>

    <select id="queryResult" resultType="com.qmth.themis.business.bean.result.TEAudioResult">
        select
            tea.id,
            tea.org_id as orgId,
            tea.activity_id as activityId,
            tea.attachment_id as attachmentId,
            tea.content,
            tea.enable,
            tea.audio_default as audioDefault,
            tea.`type`,
            (case
             when tea.`type` = 'BEFORE'
                 then teea.start_time - (tea.play_time * 1000)
             else teea.finish_time - (tea.play_time * 1000)
            end) as execTime,
        <!--(case
        when tea.`type` = 'BEFORE'
        then unix_timestamp(current_timestamp()) * 1000 + 1000 * 60 * 2
        else unix_timestamp(current_timestamp()) * 1000 + 1000 * 60 * 3
        end) as execTime,-->
        teea.start_time as startTime,
        teea.finish_time as finishTime
    from
        t_e_audio tea
            join t_e_exam_activity teea on
            teea.id = tea.activity_id
    <where> 1 = 1
        <if test="activityId != null and activityId != ''">
            and tea.activity_id = #{activityId}
        </if>
        and teea.enable = 1
        and tea.enable = 1
    </where>
    order by tea.create_time
</select>
</mapper>
