<?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.ruoyi.project.business.mapper.flow.FlowMapper">

    <sql id="FlowColumn">
        f
        .
        id
        , f.flow_config_id, f.audit_flow, f.business_id, f.status, f.business_desc, f.promoter,
        f.del_flag, f.create_by, f.create_time, f.update_by, f.update_time
    </sql>

    <resultMap type="Flow" id="FlowResult">
        <result property="id" column="id"/>
        <result property="flowConfigId" column="flow_config_id"/>
        <result property="auditFlow" column="audit_flow"/>
        <result property="businessId" column="business_id"/>
        <result property="businessData" column="business_data"/>
        <result property="businessDesc" column="business_desc"/>
        <result property="status" column="status"/>
        <result property="promoter" column="promoter"/>
        <result property="promoterName" column="promoter_name"/>
        <result property="businessData" column="business_data"/>
        <result property="delFlag" column="del_flag"/>
        <result property="createBy" column="create_by"/>
        <result property="createTime" column="create_time"/>
        <result property="updateBy" column="update_by"/>
        <result property="updateTime" column="update_time"/>
    </resultMap>

    <insert id="save" parameterType="Flow" useGeneratedKeys="true" keyProperty="id">
        INSERT INTO flow (flow_config_id, audit_flow, business_id, business_desc, STATUS, promoter, business_data,
                          del_flag, create_by, create_time, update_by, update_time)
        VALUES (#{flowConfigId}, #{auditFlow}, #{businessId}, #{businessDesc}, #{status}, #{promoter}, #{businessData},
                '0',
                #{createBy}, #{createTime}, #{updateBy}, #{updateTime})
    </insert>

    <select id="list" resultMap="FlowResult">
        select
        <include refid="FlowColumn"/>
        from flow f
        left join (
        select distinct flow_id,ftu.audit_user
        from flow_task ft
        left join flow_task_user ftu on ft.id = ftu.task_id
        <!--            <if test="!param.currentUser.isAdmin">-->
        <!--                where ftu.audit_user = #{param.currentUser.userId}-->
        <!--            </if>-->
        ) perm on f.id = perm.flow_id
        <where>
            f.del_flag = 0
            -- and perm.flow_id is not null
            <!-- 基础筛选条件 -->
            <if test="param.auditFlow != null">
                and f.audit_flow = #{param.auditFlow}
            </if>
            <if test="param.userId != null ">
                and f.promoter = #{param.userId}
            </if>
            <if test="!param.currentUser.isAdmin and param.promoters != null and param.promoters.size() > 0">
                and ((f.promoter in
                <foreach collection="param.promoters" item="id" open="(" close=")" separator=",">
                    #{id}
                </foreach>)
                or (perm.audit_user in
                <foreach collection="param.promoters" item="id" open="(" close=")" separator=",">
                    #{id}
                </foreach>))
            </if>
            <if test="param.status != null">
                and f.status = #{param.status}
            </if>
            <if test="param.businessDesc != null and param.businessDesc != ''">
                and f.business_desc like concat('%', #{param.businessDesc}, '%')
            </if>
        </where>
        order by f.status ASC, f.id DESC
    </select>


    <update id="remove">
        update flow set del_flag = 1 where audit_flow = #{auditFlow.value} and del_flag = 0 and business_id in
        <foreach collection="businessIds" open="(" close=")" separator="," item="businessId">
            #{businessId}
        </foreach>
    </update>

    <update id="abort">
        update flow set current_status = 5 where audit_flow = #{auditFlow.value} and del_flag = 0 and business_id in
        <foreach collection="businessIds" open="(" close=")" separator="," item="businessId">
            #{businessId}
        </foreach>
    </update>

    <update id="updateStatus">
        update flow
        set status      = #{param.status},
            update_by   = #{param.updateBy},
            update_time = #{param.updateTime}
        where id = #{param.id}
    </update>

    <select id="getById" resultMap="FlowResult">
        select<include refid="FlowColumn"/>, business_data from flow f where f.id = #{id}
    </select>

    <select id="getBusinessFlow" resultMap="FlowResult">
        select<include refid="FlowColumn"/>, business_data from flow f where f.audit_flow = #{auditFlow} and
        f.business_id = #{businessId} and f.del_flag = '0'
    </select>

    <select id="getFlowList" resultMap="FlowResult">
        select
        <include refid="FlowColumn"/>, ft.id as currentTaskId, ft.node_name as nodeName
        from flow f
        left join flow_task ft on f.id = ft.flow_id and ft.status = #{status}
        where f.del_flag = 0
        and f.business_id in
        <foreach collection="ids" item="id" open="(" separator="," close=")">
            #{id}
        </foreach>
        and f.audit_flow in
        <foreach collection="auditFlows" item="auditFlow" open="(" separator="," close=")">
            #{auditFlow.value}
        </foreach>
    </select>

    <select id="getHistoryFlowList" resultMap="FlowResult">
        select
        <include refid="FlowColumn"/>
        from flow f where f.audit_flow = #{auditFlow} and f.business_id = #{businessId}
        order by f.create_time asc
    </select>

    <select id="getAuditHistoryFlowList" resultMap="FlowResult">
        select<include refid="FlowColumn"/>, u.user_name as promoter_name
        from flow f
        left join sys_user u on f.promoter = u.user_id
        where f.business_id = #{businessId}
        and f.audit_flow in
        <foreach collection="auditFlows" item="auditFlow" open="(" separator="," close=")">
            #{auditFlow.value}
        </foreach>
        order by f.create_time asc
    </select>

    <resultMap id="FlowVOResult" type="com.ruoyi.project.business.domain.flow.FlowVO" extends="FlowResult">

    </resultMap>
    <select id="getTodoList" resultMap="FlowVOResult">
        select f.id, f.audit_flow, f.business_id, f.business_desc, f.promoter, f.status, ftu.audit_user, ftu.create_time as startTime
        from flow_task_user ftu
        left join flow_task ft on ftu.task_id = ft.id
        left join flow f on ft.flow_id = f.id
        where f.del_flag = '0'
        and ftu.status = 100
        <if test="params.auditFlow != null">
            and f.audit_flow = #{params.auditFlow}
        </if>
        <if test="params.businessDesc != null and params.businessDesc != ''">
            and f.business_desc like  concat('%', #{params.businessDesc}, '%')
        </if>
        <if test="params.status != null">
            and f.status = #{params.status}
        </if>
        <if test="params.userId != null">
            and ftu.audit_user = #{params.userId}
        </if>
        <if test="params.beginTime != null and params.beginTime != ''"><!-- 开始时间检索 -->
            and ftu.create_time &gt;= #{params.beginTime}
        </if>
        <if test="params.endTime != null and params.endTime != ''"><!-- 结束时间检索 -->
            and ftu.create_time &lt; #{params.endTime}
        </if>
        order by f.create_time desc
        <if test="params.indexQuery != null and params.indexQuery">
            limit 3
        </if>
    </select>

    <!-- 获取本人有权限的审批流程 -->
    <sql id="selectPermFlowList">
        (select id, audit_flow, business_id
         from flow f

                  left join (select distinct flow_id
                             from flow_task ft
                                      left join (select distinct task_id
                                                 from flow_task_user
                                                 where del_flag = 0
                                                   and audit_user = #{createBy}
                                                   and status != 0) perm_task on ft.id = perm_task.task_id
                             where ft.del_flag = 0
                               and perm_task.task_id is not null) perm_flow on f.id = perm_flow.flow_id
         where f.del_flag = 0
           and perm_flow.flow_id is not null)
    </sql>

</mapper>
