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

    <select id="findMaxVersion" resultType="com.qmth.distributed.print.business.entity.TFCustomFlow">
        select * from t_f_custom_flow tfcf
        <where>
            1 = 1 and tfcf.version =
            (select max(t.version) from t_f_custom_flow t
            <where>
                <if test="schoolId != null and schoolId != ''">
                    and t.school_id = #{schoolId}
                </if>
                <if test="orgId != null and orgId != ''">
                    and t.org_id = #{orgId}
                </if>
                <if test="type != null and type != ''">
                    and t.type = #{type}
                </if>
                and t.enable = 1
                and t.publish = 1
            </where>
            )
            <if test="schoolId != null and schoolId != ''">
                and tfcf.school_id = #{schoolId}
            </if>
            <if test="orgId != null and orgId != ''">
                and tfcf.org_id = #{orgId}
            </if>
            <if test="type != null and type != ''">
                and tfcf.type = #{type}
            </if>
            and tfcf.enable = 1
            and tfcf.publish = 1
        </where>
    </select>

    <select id="list" resultType="com.qmth.distributed.print.business.entity.TFCustomFlow">
        select tfcf.id,tfcf.school_id as schoolId,tfcf.org_id as orgId,tfcf.name,tfcf.type,tfcf.model_type as modelType,tfcf.version,tfcf.enable,tfcf.publish from t_f_custom_flow tfcf
        <where>
            <if test="schoolId != null and schoolId != ''">
                and tfcf.school_id = #{schoolId}
            </if>
            <if test="name != null and name != ''">
                and tfcf.name like concat('%',#{name},'%')
            </if>
            <if test="orgId != null and orgId != ''">
                and tfcf.org_id = #{orgId}
            </if>
            and tfcf.enable = 1
        </where>
    </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>

    <select id="findByFlowId" resultType="com.qmth.distributed.print.business.bean.flow.dto.StandardFlowData">
        select
            tfcf.*,
            tfcfe.flow_id as flowId,
            if(tbu2.login_name is not null,tbu2.login_name,tbu1.login_name) as updateName
        from
            t_f_custom_flow tfcf
                join t_f_custom_flow_entity tfcfe on tfcfe.t_f_custom_flow_id = tfcf.id
                join sys_user tbu1 on tfcf.create_id = tbu1.id
                join sys_user tbu2 on tfcf.update_id = tbu2.id
        where tfcfe.flow_id = #{flowId}
    </select>
</mapper>
