<?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.tofly.workflow.process.mapper.TfProcessLMapper">

    <resultMap id="tfProcessLMap" type="com.tofly.workflow.process.entity.TfProcessL">
        <id property="id" column="ID"/>
        <result property="processNumber" column="PROCESS_NUMBER"/>
        <result property="relationId" column="RELATION_ID"/>
        <result property="relationNumber" column="RELATION_NUMBER"/>
        <result property="category" column="CATEGORY"/>
        <result property="processType" column="PROCESS_TYPE"/>
        <result property="configId" column="CONFIG_ID"/>
        <result property="ywType" column="YW_TYPE"/>
        <result property="edition" column="EDITION"/>
        <result property="name" column="NAME"/>
        <result property="processState" column="PROCESS_STATE"/>
        <result property="registerPost" column="REGISTER_POST"/>
        <result property="registerDept" column="REGISTER_DEPT"/>
        <result property="registerStaff" column="REGISTER_STAFF"/>
        <result property="registerTime" column="REGISTER_TIME"/>
        <result property="passingNode" column="PASSING_NODE"/>
        <result property="tenantId" column="TENANT_ID"/>
        <result property="isDel" column="IS_DEL"/>
        <result property="remark" column="REMARK"/>
        <result property="createUser" column="CREATE_USER"/>
        <result property="createTime" column="CREATE_TIME"/>
        <association property="typeName" column="YW_TYPE"
                     select="com.tofly.common.mapper.SysCommonMapper.getDictName"></association>
        <association property="createUserName" column="CREATE_USER"
                     select="com.tofly.common.mapper.SysCommonMapper.getRealName"></association>
        <association property="processStateName" column="PROCESS_STATE" select="getProcessStateName"></association>
        <association property="isDelName" column="IS_DEL" select="getIsDelName"></association>
        <association property="tenantIdName" column="TENANT_ID" javaType="java.lang.String"
                     select="com.tofly.common.mapper.SysCommonMapper.getTenantName"></association>
    </resultMap>
    
    <sql id="processsql" >
        ID,PROCESS_NUMBER,RELATION_ID,RELATION_NUMBER,CATEGORY,PROCESS_TYPE,CONFIG_ID,YW_TYPE,EDITION,NAME,PROCESS_STATE,REGISTER_POST,REGISTER_DEPT
        ,REGISTER_STAFF,REGISTER_TIME,PASSING_NODE,TENANT_ID,REMARK,CREATE_USER,CREATE_TIME 
    </sql>

    <select id="getProcessStateName" resultType="string">
        SELECT decode(#{processState}, '1', '草稿', '2', '处理', '3', '终止', '4', '处理完成') as processStateName
        FROM dual
    </select>

    <select id="getIsDelName" resultType="string">
        SELECT decode(#{isDel}, '0', '未删除', '1', '已删除') as isDelName
        FROM dual
    </select>
    <select id="getProcessNumber" resultType="java.lang.String">
        SELECT PROCESS_NUMBER FROM(SELECT ID,PROCESS_NUMBER FROM TF_PROCESS_L WHERE CREATE_TIME> #{dateTime} AND PROCESS_TYPE = #{processType} order by ID DESC )where rownum=1
    </select>

    <resultMap id="ProcessByIdMap" type="com.tofly.workflow.process.entity.vo.ProcessVo" extends="tfProcessLMap">
        <association property="nodes" column="ID" select="com.tofly.workflow.process.mapper.TfProcessNodeLMapper.getProcessNodes" ></association>
        <association property="nowHandleLS" column="ID" select="com.tofly.workflow.process.mapper.TfNowHandleLMapper.getNowHandleLS" ></association>

    </resultMap>

    <select id="getProcessById" resultMap="ProcessByIdMap">
        SELECT <include refid="processsql"></include> FROM TF_PROCESS_L WHERE ID = #{id}
    </select>

<!--    <select id="getProcessList" resultMap="ProcessByIdMap">-->
<!--        SELECT <include refid="processsql"></include>-->
<!--        FROM TF_PROCESS_L-->
<!--        WHERE-->
<!--            1=1-->
<!--        <if test="processIdList != null and processIdList.size() > 0">-->
<!--            AND ID IN-->
<!--            <foreach collection="processIdList" item="id" open="(" close=")" separator=",">-->
<!--                #{id}-->
<!--            </foreach>-->
<!--        </if>-->
<!--    </select>-->
    <select id="getProcessNodeList" resultType="com.tofly.workflow.process.entity.vo.ProcessNodeVo">
        SELECT t1.ID, (SELECT REAL_NAME FROM SYS_USER WHERE ID = t1.HANDLE_USER) HANDLE_USER_NAME, t1.HANDLE_TIME, t1.PROCESS_ID, t1.PROCESS_STEP, t1.STEP_NODE, t1.PAGE_CODE, t2.CONFIG_ID, t2.EDITION, t2.TENANT_ID
        FROM TF_PROCESS_NODE_L t1
            LEFT JOIN TF_PROCESS_L t2 ON t1.PROCESS_ID = t2.ID
        WHERE
            1=1 AND t1.PAGE_CODE is not null AND t1.IS_VALID = 1
        <if test="processIdList != null and processIdList.size() > 0">
            AND t1.PROCESS_ID IN
            <foreach collection="processIdList" item="id" open="(" close=")" separator=",">
                #{id}
            </foreach>
        </if>
    </select>

    <select id="getFsProcess" resultMap="ProcessByIdMap">
        SELECT <include refid="processsql"></include> FROM TF_PROCESS_L
        <where>
            <if test="dto.id!=null"> AND ID = #{dto.id}</if>
            <if test="dto.relationId!=null"> AND RELATION_ID =#{dto.relationId}</if>
            <if test="dto.processType!=null and dto.processType.size()>0">
                AND PROCESS_TYPE IN
                <foreach collection="dto.processType" item="id" open="(" close=")" separator=",">
                    #{id}
                </foreach>
            </if>
            <if test="dto.configId!=null and dto.configId.size()>0">
                AND CONFIG_ID IN
                <foreach collection="dto.configId" item="id" open="(" close=")" separator=",">
                    #{id}
                </foreach>
            </if>
            <if test="dto.ywType!=null and dto.ywType.size()>0">
                AND YW_TYPE IN
                <foreach collection="dto.ywType" item="id" open="(" close=")" separator=",">
                    #{id}
                </foreach>
            </if>

        </where>
    </select>

    <select id="getProcessNodeType" resultType="Integer">
        SELECT * FROM (select ttp.NODE_TYPE from TF_NOW_HANDLE_L h
        left join TF_TASK_P ttp on h.PROCESS_ID = ttp.PROCESS_ID AND h.PROCESS_STEP = ttp.PROCESS_STEP AND h.STEP_NODE = ttp.STEP_NODE AND ttp.STATUS = 0
        where h.PROCESS_ID = #{id} ORDER BY ttp.ID DESC) WHERE ROWNUM = 1
    </select>

    <select id="getMaterialNodeById" resultType="com.tofly.workflow.process.entity.vo.ProcessMaterialVo">
        SELECT p.ID PROCESS_ID,p.RELATION_ID,p.TENANT_ID,b.b.PROCESS_NUMBER,	b.addre addr ,	hdu.job_number handle_user ,	sd.code  deptCode,b.PROJECT_NAME,b.USER_TYPE,b.DUTY_USER,du.REAL_NAME DUTY_USER_NAME,n.ID NODE_ID,n.OUTCOME ACCESS_ID, a.YY_WZ_ID WAREHOUSE,ao.YY_WZ_ID OUT_WAREHOUSE,n.STAFF_ID,u.REAL_NAME STAFF_NAME,n.data_time
        FROM TF_PROCESS_L p LEFT JOIN TF_PROCESS_NODE_L n ON (p.ID = n.PROCESS_ID)
                            LEFT JOIN TF_PROJECT_BASE_W b ON (p.RELATION_ID = b.PROCESS_ID)
                            LEFT JOIN TF_MATTER_ACCESS_P a ON (n.OUTCOME = a.ID)
                            LEFT JOIN TF_MATTER_ACCESS_P ao ON (n.ONE_ID = ao.ID)
                            LEFT JOIN SYS_USER u ON (n.STAFF_ID = u.ID)
                            LEFT JOIN SYS_USER hdu ON (n.handle_user = hdu.ID )
                            LEFT JOIN SYS_USER du ON (b.DUTY_USER = du.ID)
                            left join ZMRQ.SYS_USER_DEPT dept  on n.handle_user = dept.user_id
                            left join sys_dept sd on dept.dept_id = sd.id
        WHERE p.IS_DEL = 0 AND p.PROCESS_STATE = 4 AND n.IS_VALID = 1 AND n.IS_DEL = 0 AND n.PROCESS_STEP = 1 AND n.STEP_NODE = 1
        AND p.ID = #{id}
    </select>
    <select id="getProcessIdWithOutPush" resultType="java.lang.Long">

        select *from tf_process_l where process_number like 'clly%'
                                    and id not in (select process_id from tf_material_out_in_w )



    </select>
    <select id="findProcessLastCheckTime" resultType="java.util.Date">

        SELECT  max(node.HANDLE_TIME)
        from TF_PROCESS_NODE_L node left join TF_PROCESS_L p on node.process_id = p.id
                                    LEFT JOIN TF_NOW_HANDLE_L n  on (node.PROCESS_ID = n.PROCESS_ID and  node.LABEL = n.LABEL)
        where node.LABEL = 1571328373371297794  and node.PROCESS_ID =#{processId} and n.id is null



    </select>
    <select id="findCompleteTime" resultType="java.util.Date">

        SELECT max(HANDLE_TIME) from TF_PROCESS_NODE_L where process_id = #{processId}


    </select>


</mapper>