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

    <select id="list" resultType="com.qmth.distributed.print.business.entity.TFFlow">
        select tff.id,tff.school_id as schoolId,tff.org_id as orgId,tff.name,tff.enable,tff.publish from t_f_flow tff
        <where>
            and tff.enable = 1
            <if test="name != null and name != ''">
                and tff.name like concat('%',#{name},'%')
            </if>
            <if test="schoolId != null and schoolId != ''">
                and tff.school_id = #{schoolId}
            </if>
<!--            <if test="orgId != null and orgId != ''">-->
<!--                and tff.org_id = #{orgId}-->
<!--            </if>-->
        </where>
    </select>

    <select id="flowApproveList" resultType="com.qmth.distributed.print.business.bean.result.FlowApproveResult">
        select
            tffal.flow_id as flowId,
            tffal.task_id as taskId,
            tffal.start_id as startId,
            su.real_name as startName,
            tffal.create_time as createTime,
            tffal.primary_approve_id as primaryApproveId,
            (
            select
                t.real_name
            from
                sys_user t
            where
                t.id = tffal.primary_approve_id) as primaryApproveName,
            tffal.primary_approve_remark as primaryApproveRemark,
            tffal.second_approve_id as secondApproveId,
            (
            select
                t.real_name
            from
                sys_user t
            where
                t.id = tffal.second_approve_id) as secondApproveName,
            tffal.second_approve_remark as secondApproveRemark,
            tffa.status
        from
            t_f_flow_approve_log tffal
        left join t_f_flow_approve tffa on tffa.flow_id = tffal.flow_id
        left join sys_user su on su.id = tffal.start_id
        <where>
            <if test="startName != null and startName != ''">
                and su.real_name like concat('%',#{startName},'%')
            </if>
            <if test="schoolId != null and schoolId != ''">
                and tffal.school_id = #{schoolId}
            </if>
<!--            <if test="orgId != null and orgId != ''">-->
<!--                and tffal.org_id = #{orgId}-->
<!--            </if>-->
            <if test="flowId != null and flowId != ''">
                and tffal.flow_id = #{flowId}
            </if>
            and tffal.enable = 1
        </where>
        order by
            tffal.create_time desc
    </select>

    <select id="findActIdByFlowKey" resultType="java.lang.String">
        select max(arp.ID_) as actFlowId from act_re_procdef arp where arp.KEY_ = #{flowKey} order by arp.VERSION_ desc LIMIT 1
    </select>
</mapper>
