<?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.distributed.print.business.mapper.TFFlowApproveMapper">

    <select id="findByFlowId" resultType="com.qmth.distributed.print.business.entity.TFFlowApprove">
        select * from t_f_flow_approve tffa where tffa.flow_id = #{flowId}
    </select>

    <select id="findApproveList" resultType="com.qmth.distributed.print.business.bean.result.FlowApproveListResult">
        select temp.*
        from
        (
        select
        t.*,
        tffl.pend_approve_id as pendApproveId,
        tffl.object_id as objectId,
        tffl.object_table as objectTable,
        (
        select
        group_concat(su.real_name,'(',so.name,')')
        from
        sys_user su join sys_org so on so.id = su.org_id
        where
        FIND_IN_SET(su.id,replace(tffl.pend_approve_id,', ',','))) as pendApproveUserName
        from
        (
        select
        tffa.id,
        et.user_id as teacherId,
        (
        select
        CONCAT(su.real_name,'(',so.name,')')
        from
        sys_user su join sys_org so on so.id = su.org_id
        where
        su.id = et.user_id) as teacherUserName,
        tffa.create_time as createTime,
        tffa.status,
        tffa.setup,
        tfcfe.id as flowEntityId,
        tffa.flow_id as flowId,
        (
        select
        tffl.id
        from
        t_f_flow_log tffl
        where
        tffl.flow_id = tffa.flow_id
        order by
        tffl.create_time desc
        limit 1) as flowLogId
        from
        t_f_flow_approve tffa
        join t_f_custom_flow_entity tfcfe on
        tfcfe.flow_id = tffa.flow_id
        join exam_task et on
        et.flow_id = tffa.flow_id
        <where>
            <if test="schoolId != null and schoolId != ''">
                and tffa.school_id = #{schoolId}
            </if>
            <if test="orgId != null and orgId != ''">
                and tffa.org_id = #{orgId}
            </if>
            <if test="teachingRoomId != null and teachingRoomId != ''">
                and et.teaching_room_id = #{teachingRoomId}
            </if>
            <choose>
                <when test="status != null and status != ''">
                    <choose>
                        <when test="status == 'START'">
                            and tffa.status = #{status}
                        </when>
                        <otherwise>
                            and (tffa.status <![CDATA[ <> ]]> 'START' and tffa.status = #{status})
                        </otherwise>
                    </choose>
                </when>
                <otherwise>
                    and tffa.status <![CDATA[ <> ]]> 'START'
                </otherwise>
            </choose>
            <if test="startTime != null and startTime != ''">
                and tffa.create_time <![CDATA[ >= ]]> #{startTime}
            </if>
            <if test="endTime != null and endTime != ''">
                and tffa.create_time <![CDATA[ < ]]> #{endTime}
            </if>
            and tffa.enable = 1
            and et.enable = 1
        </where>
        ) t
        join t_f_flow_log tffl on
        tffl.id = t.flowLogId
        <where>
            <if test="teacherUserName != null and teacherUserName != ''">
                and t.teacherUserName like concat('%',#{teacherUserName},'%')
            </if>
        </where>
         ) temp
        <where>
            <if test="pendApproveUserName != null and pendApproveUserName != ''">
                and temp.pendApproveUserName like concat('%',#{pendApproveUserName},'%')
            </if>
        </where>
        order by temp.createTime desc
    </select>
</mapper>
