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

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.dayou.entity.WorkFlowLog">
        <result column="id" property="id" />
        <result column="created" property="created" />
        <result column="deleted" property="deleted" />
        <result column="modified" property="modified" />
        <result column="business_id" property="businessId" />
        <result column="business_type" property="businessType" />
        <result column="instance_id" property="instanceId" />
        <result column="state" property="state" />
        <result column="handler_id" property="handlerId" />
        <result column="handing_time" property="handingTime" />
        <result column="comments" property="comments" />
    </resultMap>

    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        id,
        created,
        deleted,
        modified,
        business_id, business_type, instance_id, state, handler_id, handing_time,comments
    </sql>

    <select id="getList"  resultType="com.dayou.vo.WorkflowLogVO">
        SELECT
            wfl.id,
            ( CASE wfl.state WHEN 'PASS' THEN '通过' WHEN 'REVERSE' THEN '退回' WHEN 'RESTART' THEN '重置' WHEN 'SKIP' THEN '跳过' WHEN 'TERMINATE' THEN '终止' END ) AS state,
            wn.NAME AS nodeName,
            wfl.handing_time,
            ifnull(wfl.comments,'-') as comments,
            wfl.created,
            u.name AS handlerName
        FROM
            work_flow_log wfl
                LEFT JOIN work_flow_node_instance wfni ON wfni.id = wfl.instance_id
                LEFT JOIN work_node wn ON wfni.node_id = wn.id
                LEFT JOIN user u ON u.id = wfl.handler_id
        WHERE
            wfl.business_id = #{businessId}
        <if test="businessSubId!=null and businessSubId!='' and businessMinId!=null and businessMinId!=''">
            AND (wfl.business_min_id = #{businessMinId} or wfl.business_min_id IS NULL)
            AND (wfl.business_sub_id = #{businessSubId} or wfl.business_sub_id IS NULL)
        </if>
        <if test="(businessMinId==null or businessMinId=='') and businessSubId!=null and businessSubId!=''">
            AND wfl.business_min_id IS NULL
            AND (wfl.business_sub_id = #{businessSubId} or wfl.business_sub_id IS NULL)
        </if>
        <if test="(businessMinId!=null and businessMinId!='') and (businessSubId==null or businessSubId=='')">
            AND wfl.business_sub_id IS NULL
            AND (wfl.business_min_id = #{businessMinId} or wfl.business_min_id IS NULL)
        </if>
        <if test="(businessMinId==null or businessMinId=='') and (businessSubId==null or businessSubId=='')">
            AND wfl.business_sub_id IS NULL
            AND wfl.business_min_id IS NULL
        </if>
          AND wfl.business_type = #{businessType}
        ORDER BY
            wfl.created DESC
    </select>

</mapper>
