<?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.dayou.mapper.WorkFlowNodeInstanceMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.dayou.entity.WorkFlowNodeInstance">
        <result column="id" property="id" />
        <result column="deleted" property="deleted" />
        <result column="created" property="created" />
        <result column="modified" property="modified" />
        <result column="flow_id" property="flowId" />
        <result column="node_id" property="nodeId" />
        <result column="business_id" property="businessId" />
        <result column="business_type" property="businessType" />
        <result column="sequence" property="sequence" />
        <result column="state" property="state" />
        <result column="comment" property="comment" />

    </resultMap>

    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        id,
        deleted,
        created,
        modified,
        flow_id, node_id, business_id, business_type, sequence, state, comment
    </sql>

    <select id="getById" parameterType="java.lang.Long" resultType="com.dayou.entity.WorkFlowNodeInstance">
        SELECT
            wfni.id,
            wfni.flow_id,
            wfni.node_id,
            wfni.business_id,
            wfni.business_sub_id,
            wfni.business_min_id,
            wfni.business_type,
            wfni.sequence,
            wfni.state,
            wn.verify,
            wfni.created,
            wn.code as nodeCode
        FROM
            work_flow_node_instance wfni
                LEFT JOIN work_node wn ON wn.id = wfni.node_id
        WHERE
            wfni.deleted = 0
          AND wn.deleted = 0
          AND wfni.id = #{instanceNodeId}
    </select>

    <select id="getInstanceNodeFlow" resultType="com.dayou.vo.WorkNodeInstanceVO">
        SELECT
            wf.id AS flowId,
            wn.id AS nodeId,
            wn.privious_node_id as pNodeId,
            wf.CODE AS flowCode,
            wn.NAME AS nodeName,
            wn.CODE AS nodeCode,
            ifnull( wfni.state, 'WAITING' ) AS state,
            wfni.id as instanceId
        FROM
            ( SELECT id, CODE FROM work_flow WHERE CODE = #{mainBusiness} AND deleted = 0 AND state = 0 ) wf
                LEFT JOIN ( SELECT id, privious_node_id, flow_id, NAME, CODE FROM work_node WHERE deleted = 0 ) wn ON wf.id = wn.flow_id
                LEFT JOIN ( SELECT id, flow_id, node_id, state FROM work_flow_node_instance WHERE deleted = 0 AND business_id = #{businessId}
                    <if test="businessSubId!=null and businessSubId!='' and businessMinId!=null and businessMinId!=''">
                        AND (business_min_id = #{businessMinId} or business_min_id IS NULL)
                        AND (business_sub_id = #{businessSubId} or business_sub_id IS NULL)
                    </if>
                    <if test="(businessMinId==null or businessMinId=='') and businessSubId!=null and businessSubId!=''">
                        AND business_min_id IS NULL
                        AND (business_sub_id = #{businessSubId} or business_sub_id IS NULL)
                    </if>
                    <if test="(businessMinId!=null and businessMinId!='') and (businessSubId==null or businessSubId=='')">
                        AND business_sub_id IS NULL
                        AND (business_min_id = #{businessMinId} or business_min_id IS NULL)
                    </if>
                    <if test="(businessMinId==null or businessMinId=='') and (businessSubId==null or businessSubId=='')">
                        AND business_sub_id IS NULL
                        AND business_min_id IS NULL
                    </if>
                      ) wfni ON ( wfni.flow_id = wf.id AND wfni.node_id = wn.id )
    </select>

    <resultMap id="currentInstanceNodeMap" type="com.dayou.vo.WorkNodeInstanceVO">
        <result column="flowId" property="flowId" />
        <result column="nodeId" property="nodeId" />
        <result column="pNodeId" property="pNodeId" />
        <result column="flowCode" property="flowCode" />
        <result column="nodeName" property="nodeName" />
        <result column="nodeCode" property="nodeCode" />
        <result column="state" property="state" />
        <result column="instanceId" property="instanceId" />
        <result column="sequence" property="sequence" />
        <result column="reversible" property="reversible" />
        <result column="restartable" property="restartable" />
        <result column="terminable" property="terminable" />
        <result column="skippable" property="skippable" />
        <collection property="tasks" ofType="com.dayou.entity.WorkNodeTask" select="selectNodeTasks" column="{nodeId=nodeId,instanceId=instanceId}"/>
    </resultMap>
    <select id="instanceCurrent" resultMap="currentInstanceNodeMap">
        SELECT
            wf.id AS flowId,
            wn.id AS nodeId,
            wn.privious_node_id as pNodeId,
            wf.CODE AS flowCode,
            wn.NAME AS nodeName,
            wn.CODE AS nodeCode,
            wfni.state,
            wfni.id as instanceId,
            wfni.sequence,
            wn.reversible,
            wn.restartable,
            wn.terminable,
            wn.skippable
        FROM
            ( SELECT id, flow_id, node_id, state,sequence FROM work_flow_node_instance WHERE deleted = 0 AND business_id =#{businessId} AND state='PENDING' AND business_type = #{mainBusiness}
        <if test="businessSubId!=null and businessSubId!='' and businessMinId!=null and businessMinId!=''">
            AND business_sub_id = #{businessSubId}
            AND business_min_id = #{businessMinId}
        </if>
        <if test="businessSubId!=null and businessSubId!='' and (businessMinId==null or businessMinId=='') ">
            AND business_sub_id = #{businessSubId}
            AND business_min_id IS NULL
        </if>
        <if test="(businessSubId==null or businessSubId=='') and (businessMinId!=null and businessMinId!='') ">
            AND business_min_id = #{businessMinId}
            AND business_sub_id IS NULL
        </if>
        <if test="(businessSubId==null or businessSubId=='') and (businessMinId==null or businessMinId=='') ">
            AND business_sub_id IS NULL
            AND business_min_id IS NULL
        </if>
            ) wfni left join
            ( SELECT id, CODE FROM work_flow WHERE CODE = #{mainBusiness} AND deleted = 0 AND state = 0 ) wf   ON  wfni.flow_id = wf.id
                   LEFT JOIN ( SELECT id, privious_node_id, flow_id, NAME, CODE,reversible,restartable,terminable,skippable FROM work_node WHERE deleted = 0 ) wn ON wfni.node_id = wn.id
    </select>

    <select id="selectNodeTasks" resultType="com.dayou.entity.WorkNodeTask">
        SELECT
            wnt.id,
            wnt.description,
            wnt.info,
            wtr.id AS recordId,
            wnt.task_type
        FROM
            work_node_task wnt
                LEFT JOIN work_task_record wtr ON wnt.id = wtr.task_id
                AND wtr.instance_id = #{instanceId}
        WHERE
            wnt.node_id = #{nodeId}
          AND wnt.deleted = 0 and wtr.deleted = 0
    </select>

    <select id="getInstanceNodeInfoById" parameterType="java.lang.Long" resultType="com.dayou.entity.WorkFlowNodeInstance">
            select wfni.*,wn.code as nodeCode from work_flow_node_instance wfni left join work_node wn on wn.id = wfni.node_id
                where wfni.deleted = 0 and wn.deleted = 0 and wfni.id = #{instanceNodeId}
    </select>

    <update id="batchCommitNode" parameterType="java.util.List">
        update work_flow_node_instance set state = 'FINISHED' WHERE id in (
            <foreach collection="currentNodeIds" index="index" item="item" separator=",">
                #{item}
            </foreach>
            )
    </update>

    <select id="instanceNodeCountByCode" parameterType="java.lang.String" resultType="com.dayou.vo.NightingaleEchartsVO">
        SELECT
            wn.name,
            t.num AS
        `value`

        FROM
            (
            SELECT
            node_id,
            count(*) AS num
            FROM
            work_flow_node_instance
            WHERE
            deleted = 0
            AND business_type = #{businessType}
            AND flow_id = ( SELECT id FROM work_flow WHERE CODE =#{businessType} AND state = 0 AND deleted = 0 )
            GROUP BY
            node_id
            ) t
            LEFT JOIN work_node wn ON wn.id = t.node_id
        ORDER BY
            t.num ASC
    </select>

    <select id="checkIfReverse" resultType="java.lang.String">
        SELECT
            distinct wn.CODE
        FROM
            work_flow_node_instance wfi
                LEFT JOIN work_node wn ON wn.id = wfi.node_id
        WHERE
            wfi.deleted = 0
          AND wn.deleted = 0
        <if test="productionType!=null and productionType!='' and productionType=='STATEMENT'">
            AND wfi.business_sub_id like concat ('%',#{productionNo},'%')
        </if>
        <if test="productionType!=null and productionType!='' and productionType!='STATEMENT'">
            AND wfi.business_min_id like concat ('%',#{productionNo},'%')
        </if>
    </select>

    <select id="getInOutInstanceNode" parameterType="java.lang.String" resultType="map">
        SELECT
            wtr.id,
            wfni.*
        FROM
            work_task_record wtr
                LEFT JOIN work_node_task wnt ON wnt.id = wtr.task_id
                inner JOIN (
                SELECT
                    wn.code,
                    wfni.id as instanceId,
                    wfni.business_id as businessId,
                    wfni.business_sub_id as businessSubId,
                    wfni.business_min_id as businessMinId,
                    wfni.business_type as businessType
                FROM
                    work_flow_node_instance wfni
                        LEFT JOIN work_node wn ON wn.id = wfni.node_id
                WHERE
                    wfni.state = 'PENDING'
                  AND wfni.deleted = 0
                  AND wn.deleted = 0
            ) wfni ON wtr.instance_id = wfni.instanceId

        WHERE
            wnt.deleted = 0
          AND wtr.deleted = 0 and wfni.code in ('STATEMENT_IN','STATEMENT_OUT','REPORT_IN','REPORT_OUT','LETTER_IN','LETTER_OUT')
        and (wfni.businessSubId = #{productionNo} or wfni.businessMinId = #{productionNo})
    </select>

    <select id="getNodeFinishedTime" resultType="java.util.Date">
        SELECT
            max(created)
        FROM
            work_flow_log
        WHERE
            instance_id = (
                SELECT
                    id
                FROM
                    work_flow_node_instance
                WHERE
                    flow_id = #{flowId}
                  AND node_id = #{nodeId}
                  AND business_id = #{businessId}
                  AND business_sub_id = #{businessSubId}
                  AND business_type = #{businessType}
                  <if test="businessMinId!=null and businessMinId!=''">
                      and business_min_id = #{businessMinId}
                  </if>
                  AND state = 'FINISHED'
                  AND deleted = 0
                     order by created desc limit 1
            )
          AND deleted =0 and state = 'PASS'
    </select>
</mapper>
