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

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.dayou.entity.WorkTaskRecord">
        <result column="id" property="id" />
        <result column="created" property="created" />
        <result column="modified" property="modified" />
        <result column="deleted" property="deleted" />
        <result column="instance_id" property="instanceId" />
        <result column="task_id" property="taskId" />
        <result column="handler_id" property="handlerId" />
        <result column="finished" property="finished" />
        <result column="finish_time" property="finishTime" />
    </resultMap>

    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        id,
        created,
        modified,
        deleted,
        instance_id, task_id, handler_id, finished, finish_time
    </sql>

    <update id="finishOpening">
        UPDATE work_task_record
        SET finished = 1,
            finish_time = now()
        WHERE
            instance_id = #{id}
          AND task_id = (
            SELECT
                wnt.id
            FROM
                `work_node_task` wnt
                    LEFT JOIN work_node wn ON wn.id = wnt.node_id
            WHERE
                task_type = "OPENING"
              AND wnt.version = "LATEST"
              AND wn.flow_id = #{flowId}
              AND wnt.deleted = 0
            )
          AND deleted = 0
    </update>

    <select id="instanceNodeTasks" parameterType="java.lang.Long" resultType="com.dayou.dto.WorkNodeInstanceTaskDTO">
        SELECT
            wtr.id AS recordId,
            wtr.finished,
            wtr.handler_id,
            wnt.task_type,
            wnt.handler_permission,
            wnt.business_handle
        FROM
            work_task_record wtr
                LEFT JOIN work_node_task wnt ON wnt.id = wtr.task_id
        WHERE
            wtr.instance_id = #{instanceNodeId}
          AND wtr.deleted = 0
    </select>

    <select id="getNotBusinessHandleTaskId" parameterType="java.lang.Long" resultType="java.lang.Long">
        SELECT
            wtr.id
        FROM
            work_task_record wtr
                LEFT JOIN work_flow_node_instance wfni ON wtr.instance_id = wfni.id
                LEFT JOIN work_node_task wnt ON wtr.task_id = wnt.id
        WHERE
            wfni.id = #{instanceNodeId}
          AND wnt.deleted = 0
          AND wfni.deleted = 0
          AND wtr.deleted = 0
          AND wtr.finished = 0
          AND wnt.business_handle = 0
    </select>

    <select id="getInstanceTask" parameterType="java.lang.Long" resultType="com.dayou.vo.WorkTaskRecordVO">
        SELECT
            wtr.id,
            wfni.id AS instanceId,
            t.id AS taskId,
            u.name as handler,
            (case wtr.finished when 1 then '已完成' else '未完成' end) as finished,
            wtr.finish_time,
            t.description AS taskName,
            wfni.state AS nodeState
        FROM
            work_task_record wtr
                left join user u on u.id = wtr.handler_id
                LEFT JOIN work_node_task t ON t.id = wtr.task_id
                LEFT JOIN work_flow_node_instance wfni ON wfni.id = wtr.instance_id
        where wtr.instance_id = #{instanceId} and wtr.deleted = 0 and t.deleted = 0 and wfni.deleted = 0
    </select>

    <select id="taskTodoPage" parameterType="com.dayou.vo.TaskTodoVO" resultType="com.dayou.vo.TaskTodoVO">
        SELECT
        wfni.id,
        wtr.id as taskId,
        wfni.id AS currentNodeId,
        wfni.business_sub_id,
        wfni.business_sub_id as statementNo,
        wfni.business_min_id as reportNo,
        wfni.business_id,
        m.name as orderName,
        m.order_id,
        m.financial,
        m.allot_type,
        u1.name as clientManager,
        u2.name as principal,
        d.name as departmentName,
        m.business_object_type,
        wfni.name AS nodeName,
        wfni.code as nodeCode,
        u.name AS handler,
        wnt.description AS taskName,
        wtr.handler_id,
        wtr.finished as taskFinished,
        m.created,
        m.id as majorId,
        (select name from major_production where deleted = 0 and major_id = m.id and  report_no= ifnull(wfni.business_min_id,wfni.business_sub_id) limit 1) as productionName,
        cc1.name AS cClienteleName,
        cc2.name AS cClienteleSubName,
        cl1.name AS cClienteleContactName,
        cl1.mobile AS cMobile,
        wtr.created as taskCreated
        FROM
        work_task_record wtr
        LEFT JOIN work_node_task wnt ON wnt.id = wtr.task_id
        inner JOIN (
        SELECT
        wn.name,
        wn.CODE,
        wfni.id,
        wfni.business_id ,
        wfni.business_sub_id,
        wfni.business_min_id,
        wfni.state
        FROM
        work_flow_node_instance wfni
        LEFT JOIN work_node wn ON wn.id = wfni.node_id
        WHERE
        wfni.business_type = 'MAJOR_BUSINESS'
        AND wfni.state = 'PENDING'
        AND wfni.deleted = 0
        AND wn.deleted = 0
        ) wfni ON wtr.instance_id = wfni.id
        LEFT JOIN major m ON m.id = wfni.business_id
        LEFT JOIN user u ON u.id = wtr.handler_id
        left join user u1 on u1.id = m.client_manager_id
        left join user u2 on u2.id = m.principal_id
        left join department d on d.id = m.department_id
        LEFT JOIN customer_company cc1 ON cc1.id = m.clientele_id
        LEFT JOIN customer_company cc2 ON cc2.id = m.clientele_sub_id
        LEFT JOIN customer_linkman cl1 ON cl1.id = m.clientele_contact_id
        WHERE
            wtr.handler_id =#{todoVO.handlerId}
          AND u.deleted = 0
          AND wnt.deleted = 0
          AND wtr.deleted = 0
        <if test="keyword!=null and keyword!='' ">
            and(
            m.name like concat ('%',#{keyword},'%')
            or m.order_id like concat('%',#{keyword},'%')
            or wfni.business_sub_id like concat('%',#{keyword},'%')
            or u1.name like concat ('%',#{keyword},'%')
            or u2.name like concat ('%',#{keyword},'%')
            or m.business_object_type like concat ('%',#{keyword},'%')
            or u.name like concat ('%',#{keyword},'%')
                or wfni.name like concat('%',#{keyword},'%')
            )
        </if>
        <if test="todoVO!=null and todoVO.nodeName!=null and todoVO.nodeName!='' ">
            and wfni.name = #{todoVO.nodeName}
        </if>
        <if test="todoVO!=null and todoVO.startDate!=null and todoVO.startDate!=''">
            and m.created &gt;= #{todoVO.startDate}
        </if>
        <if test="todoVO!=null and todoVO.endDate!=null and todoVO.endDate!=''">
            and m.created &lt;= #{todoVO.endDate}
        </if>
        <if test="todoVO != null and todoVO.getNodeIsBookbindingStamp">
            AND (wfni.code = 'STATEMENT_BOOKBINDING_STAMP' OR wfni.code = 'REPORT_BOOKBINDING_STAMP' OR wfni.code = 'LETTER_BOOKBINDING_STAMP')
        </if>
        <if test="todoVO != null and todoVO.getNodeIsRecheckArchiving">
            AND wfni.code = 'RECHECK_ARCHIVING'
        </if>
        <if test="todoVO != null and todoVO.getNodeIsCheckArchiving">
            AND wfni.code = 'CHECK_ARCHIVING'
        </if>
        ORDER BY  wtr.created DESC
    </select>

    <select id="majorTaskDonePage" parameterType="com.dayou.vo.MajorTaskDoneVO" resultType="com.dayou.vo.MajorTaskDoneVO">
        SELECT
            wf.id,
            u.name AS handler,
            wf.handler_id,
        (case wf.state when 'PASS' then '提交' when 'REVERSE' then '退回' when 'TERMINATE' then '终止' else '-' end) as state,
            wf.handing_time as handingTimeLong,
            wf.comments,
            wf.business_type,
            m.id AS majorId,
            m.order_id,
            m.name AS majorName,
            wn.name AS nodeName,
            wf.created as finishTime,
            wf.business_sub_id as statementNo,
            ifnull(wf.business_min_id,wf.business_sub_id) as reportNo,
            wfni.state as instanceState,
            wfni.id as instanceId,
        (select name from major_production where deleted = 0 and report_no= (ifnull(wf.business_min_id,wf.business_sub_id))) as productionName,
        (select production  from major_production where deleted = 0 and report_no= (ifnull(wf.business_min_id,wf.business_sub_id))) as productionType

        FROM
            (
                    ( SELECT * FROM work_flow_log WHERE handler_id = #{todoVO.handlerId}  and business_type='MAJOR_BUSINESS' ) wf
                    LEFT JOIN ( SELECT id, node_id, business_id ,state FROM work_flow_node_instance ) wfni ON wf.instance_id = wfni.id
                    LEFT JOIN ( SELECT id, name FROM work_node WHERE deleted = 0 ) wn ON wn.id = wfni.node_id
                    LEFT JOIN ( SELECT id, order_id, name FROM major WHERE deleted = 0 ) m ON m.id = wf.business_id
                    LEFT JOIN user u ON u.id = wf.handler_id
                )
            <where>
                <if test="keyword!=null and keyword!='' ">
                    and (
                    m.name like concat ('%',#{keyword},'%')
                    or m.order_id like concat('%',#{keyword},'%')
                    or wf.business_sub_id like concat('%',#{keyword},'%')
                    )
                </if>
                <if test="todoVO!=null and todoVO.nodeName!=null and todoVO.nodeName!='' ">
                    and wn.name = #{todoVO.nodeName}
                </if>
                <if test="todoVO!=null and todoVO.state!=null and todoVO.state!='' ">
                    and wf.state = #{todoVO.state}
                </if>
                <if test="todoVO!=null and todoVO.startDate!=null and todoVO.startDate!=''">
                    and wf.created &gt;= #{todoVO.startDate}
                </if>
                <if test="todoVO!=null and todoVO.endDate!=null and todoVO.endDate!=''">
                    and wf.created &lt;= #{todoVO.endDate}
                </if>
            </where>
        ORDER BY
            wf.created DESC
    </select>

    <update id="batchFinishTask" parameterType="java.util.List">
        update work_task_record set finished = 1 ,finish_time = now() where id in (
            <foreach collection="ids" index="index" separator="," item="item">
                #{item}
            </foreach>
            )
    </update>

    <select id="personalTodoPage" parameterType="com.dayou.vo.TaskTodoVO" resultType="com.dayou.vo.PersonalTodoVO">
        SELECT
        wfni.id,
        wtr.id AS taskId,
        wfni.id AS currentNodeId,
        wfni.business_id,
        p.location,
        p.order_id,
        u1.name AS clientManager,
        wfni.name AS nodeName,
        wfni.code AS nodeCode,
        u.name AS handlerName,
        wtr.handler_id,
        wtr.finished AS taskFinished,
        p.created,
        cc.name AS clientName,
        cc1.name AS clientSubName,
        p.agent,
        cl.name AS contactName,
        cl.mobile AS contactTel,
        p.bailorA,
        p.bailorB,
        p.bailorA_tel,
        p.bailorB_tel,
        u2.name AS outwardStaffName,
        u3.name AS inwardStaffName,
        u4.name AS pricingStaffName,
        wfni.created AS nodeCreated,
        pt.acreage,
        pt.amount,
        pt.price,
        pt.is_online,
        c.comments
        FROM
        work_task_record wtr
        LEFT JOIN work_node_task wnt ON wnt.id = wtr.task_id
        INNER JOIN (
        SELECT
        wn.name,
        wn.CODE,
        wfni.id,
        wfni.business_id,
        wfni.state,
        wfni.created,
        wfni.modified
        FROM
        work_flow_node_instance wfni
        LEFT JOIN work_node wn ON wn.id = wfni.node_id
        WHERE
        wfni.business_type = 'PERSONAL_BUSINESS'
        AND wfni.state = 'PENDING'
        AND wfni.deleted = 0
        AND wn.deleted = 0
        ) wfni ON wtr.instance_id = wfni.id
        LEFT JOIN personal p ON p.id = wfni.business_id
        LEFT JOIN customer_company cc ON cc.id = p.clientele_id
        LEFT JOIN customer_company cc1 ON cc1.id = p.clientele_sub_id
        LEFT JOIN customer_linkman cl ON cl.id = p.clientele_contact_id
        LEFT JOIN user u ON u.id = wtr.handler_id
        LEFT JOIN user u1 ON u1.id = p.client_manager_id
        LEFT JOIN user u2 ON u2.id = p.outward_staff
        LEFT JOIN user u3 ON u3.id = p.inward_staff
        LEFT JOIN user u4 ON u4.id = p.pricing_staff
        LEFT JOIN personal_target pt ON pt.personal_id = p.id
        left join (
            select business_id,comments from work_flow_log where concat(business_id,created) in (
            select concat(business_id,max(created)) from work_flow_log where business_type = 'PERSONAL_BUSINESS' and comments is not null  group BY business_id
            )
        ) c on c.business_id = p.id
        WHERE
        wtr.handler_id = #{personal.handlerId}
        AND u.deleted = 0
        AND wnt.deleted = 0
        AND wtr.deleted = 0
        <if test="keyword!=null and keyword!='' ">
            and (
            p.location like concat ('%',#{keyword},'%')
            or p.order_id like concat('%',#{keyword},'%')
            or p.agent like concat('%',#{keyword},'%')
            or p.bailorA like concat('%',#{keyword},'%')
            or p.bailorB like concat('%',#{keyword},'%')
            or cc.name like concat('%',#{keyword},'%')
            or cc1.name like concat('%',#{keyword},'%')
            or cl.name like concat('%',#{keyword},'%')
            or p.bailorA_tel like concat('%',#{keyword},'%')
            or p.bailorB_tel like concat('%',#{keyword},'%')
            )
        </if>
        <if test="personal!=null and personal.startDate!=null and personal.startDate!=''">
            and p.created &gt;= #{personal.startDate}
        </if>
        <if test="personal!=null and personal.endDate!=null and personal.endDate!=''">
            and p.created &lt;= #{personal.endDate}
        </if>
        <if test="personal!=null and personal.nodeCode!=null and personal.nodeCode!=''">
            and wfni.code = #{personal.nodeCode}
        </if>
        <if test="personal!=null and personal.clientName!=null and personal.clientName!=''">
            and cc.name like concat('%',#{personal.clientName},'%')
        </if>
        <if test="personal!=null and personal.clientSubName!=null and personal.clientSubName!=''">
            and cc1.name like concat('%',#{personal.clientSubName},'%')
        </if>
        <if test="personal!=null and personal.clientManager!=null and personal.clientManager!='' ">
            and u1.name like concat('%',#{personal.clientManager},'%')
        </if>
        <if test="personal!=null and personal.outwardStaffName!=null and personal.outwardStaffName!='' ">
            and u2.name like concat('%',#{personal.outwardStaffName},'%')
        </if>
        <if test="personal!=null and personal.inwardStaffName!=null and personal.inwardStaffName!='' ">
            and u3.name like concat('%',#{personal.inwardStaffName},'%')
        </if>
        <if test="personal!=null and personal.pricingStaffName!=null and personal.pricingStaffName!='' ">
            and u4.name like concat('%',#{personal.pricingStaffName},'%')
        </if>
        <if test="personal!=null and personal.handlerName!=null and personal.handlerName!='' ">
            and u.name like concat('%',#{personal.handlerName},'%')
        </if>
        <if test="personal != null and personal.getNodeIsBookbindingStamp">
            AND (wfni.code = 'STATEMENT_BOOKBINDING_STAMP' OR wfni.code = 'REPORT_BOOKBINDING_STAMP' OR wfni.code = 'LETTER_BOOKBINDING_STAMP')
        </if>
        order by wfni.modified DESC
    </select>

    <select id="personalTaskDonePage" parameterType="com.dayou.vo.PersonalTaskDoneVO" resultType="com.dayou.vo.PersonalTaskDoneVO">
        SELECT
            wf.id,
            u.name AS handler,
            wf.handler_id,
            (case wf.state when 'PASS' then '提交' when 'REVERSE' then '退回' when 'TERMINATE' then '终止' else '-' end) as state,
            wf.handing_time as handingTimeLong,
            wf.comments,
            wf.business_type,
            p.id AS personalId,
            p.order_id,
            p.location,
            p.clientName,
            p.clientSubName,
            p.bailorA,
            p.bailorB,
            wn.name AS nodeName,
            wf.created as finishTime,
            ifnull(wf.business_min_id,wf.business_sub_id) as reportNo,
            wfni.state as instanceState,
            wfni.id as instanceId

        FROM
            (
                ( SELECT * FROM work_flow_log WHERE handler_id = #{todoVO.handlerId} and business_type='PERSONAL_BUSINESS' ) wf
                    LEFT JOIN ( SELECT id, node_id, business_id ,state FROM work_flow_node_instance ) wfni ON wf.instance_id = wfni.id
                    LEFT JOIN ( SELECT id, name FROM work_node WHERE deleted = 0 ) wn ON wn.id = wfni.node_id
                    LEFT JOIN ( 	SELECT p.bailorA_tel AS bailoraTel, p.bailorB_tel AS bailorbTel,  p.id, p.order_id, p.location,cc.name as clientName,cc1.name as clientSubName,p.bailorA,p.bailorB FROM personal p left join customer_company cc on cc.id =
                                                                                                                                                                                            p.clientele_id left join customer_company cc1 on cc1.id = p.clientele_sub_id
                                   WHERE p.deleted = 0) p ON p.id = wf.business_id
                    LEFT JOIN user u ON u.id = wf.handler_id
                )
        <where>
            <if test="keyword!=null and keyword!='' ">
                and (
                p.location like concat ('%',#{keyword},'%')
                or p.order_id like concat('%',#{keyword},'%')
                or p.clientName like concat('%',#{keyword},'%')
                or p.clientSubName like concat('%',#{keyword},'%')
                or p.bailoraTel like concat('%',#{keyword},'%')
                or p.bailorbTel like concat('%',#{keyword},'%')
                )
            </if>
            <if test="todoVO!=null and todoVO.nodeName!=null and todoVO.nodeName!='' ">
                and wn.name = #{todoVO.nodeName}
            </if>
            <if test="todoVO!=null and todoVO.state!=null and todoVO.state!='' ">
                and wf.state = #{todoVO.state}
            </if>
            <if test="todoVO!=null and todoVO.startDate!=null and todoVO.startDate!=''">
                and wf.created &gt;= #{todoVO.startDate}
            </if>
            <if test="todoVO!=null and todoVO.endDate!=null and todoVO.endDate!=''">
                and wf.created &lt;= #{todoVO.endDate}
            </if>
        </where>
        ORDER BY
        wf.created DESC
    </select>

    <select id="majorTodoSimpleList" parameterType="java.lang.Long" resultType="com.dayou.vo.TaskTodoVO">
        SELECT
            wfni.id,
            wtr.id as taskId,
            wfni.id AS currentNodeId,
            wfni.business_sub_id as statementNo,
            wfni.business_min_id as reportNo,
            wfni.business_id,
            m.name as orderName,
            m.order_id,
            wfni.name AS nodeName,
            wfni.code as nodeCode,
            wfni.created as instanceCreated
        FROM
            work_task_record wtr
                inner JOIN (
                SELECT
                    wn.name,
                    wn.CODE,
                    wfni.id,
                    wfni.business_id ,
                    wfni.business_sub_id,
                    wfni.business_min_id,
                    wfni.state,
                    wfni.created
                FROM
                    work_flow_node_instance wfni
                        LEFT JOIN work_node wn ON wn.id = wfni.node_id
                WHERE
                    wfni.business_type = 'MAJOR_BUSINESS'
                  AND wfni.state = 'PENDING'
                  AND wfni.deleted = 0
                  AND wn.deleted = 0
            ) wfni ON wtr.instance_id = wfni.id
                LEFT JOIN major m ON m.id = wfni.business_id

        WHERE
            wtr.handler_id = #{handlerId}
          AND wtr.deleted = 0
        order by wfni.created limit 8
    </select>

    <select id="personalTodoSimpleList" parameterType="java.lang.Long" resultType="com.dayou.vo.PersonalTodoVO">
        SELECT
            wfni.id,
            wtr.id AS taskId,
            wfni.id AS currentNodeId,
            wfni.business_id,
            p.location,
            p.order_id,
            wfni.name AS nodeName,
            wfni.code AS nodeCode,
            wfni.created as instanceCreated
        FROM
            work_task_record wtr
                LEFT JOIN work_node_task wnt ON wnt.id = wtr.task_id
                INNER JOIN (
                SELECT
                    wn.name,
                    wn.CODE,
                    wfni.id,
                    wfni.business_id,
                    wfni.state,
                    wfni.created
                FROM
                    work_flow_node_instance wfni
                        LEFT JOIN work_node wn ON wn.id = wfni.node_id
                WHERE
                    wfni.business_type = 'PERSONAL_BUSINESS'
                  AND wfni.state = 'PENDING'
                  AND wfni.deleted = 0
                  AND wn.deleted = 0
            ) wfni ON wtr.instance_id = wfni.id
                LEFT JOIN personal p ON p.id = wfni.business_id
        WHERE
            wtr.handler_id = #{handlerId}

          AND wnt.deleted = 0
          AND wtr.deleted = 0
        order by wfni.created desc   limit 8
    </select>

    <select id="getTodoTotalNum" parameterType="java.lang.Long" resultType="com.dayou.vo.BusinessTodoNum">
        SELECT
            wfni.business_type,
            count( wfni.id ) AS totalNum
        FROM
            work_task_record wtr
                INNER JOIN ( SELECT wfni.business_type, wfni.id FROM work_flow_node_instance wfni WHERE wfni.state = 'PENDING' AND wfni.deleted = 0 ) wfni ON wtr.instance_id = wfni.id
        WHERE
            wtr.handler_id = #{handlerId}
          AND wtr.deleted = 0
        GROUP BY
            wfni.business_type
    </select>

    <!--获取资产业务已办列表-->
    <select id="getAssetsTaskDoneList" resultType="com.dayou.vo.AssetsTaskDoneVO">
        SELECT DISTINCT
        (@i := @i + 1) AS id,
        instance.recordId,
        log.id AS logId,
        user.name AS handlerName,
        log.handler_id,
        (case log.state
        when 'PASS' then '提交'
        when 'REVERSE' then '退回'
        when 'TERMINATE' then '终止'
        else '-' end) AS state,
        log.handing_time AS handingTimeLong,
        log.comments,
        log.business_type,
        assets.id AS assetsId,
        assets.order_id AS orderId,
        assets.name AS assetsName,
        node.name AS nodeName,
        node.code AS nodeCode,
        log.created AS finishTime,
        instance.business_sub_id AS statementNo,
        (SELECT assets_name FROM assets_production WHERE assets_production.production_no = instance.business_sub_id) AS
        statementName,
        (SELECT assets_name FROM assets_production WHERE assets_production.production_no = instance.business_min_id limit 1) AS
        reportName,
        instance.business_min_id AS reportNo,
        instance.state AS instanceState,
        instance.id AS instanceId,
        instance.created AS startDate
        FROM
            (SELECT @i := 0) AS sort,
        work_flow_log AS log
        LEFT JOIN (SELECT work_flow_node_instance.id, node_id, business_id, state, business_sub_id,
        business_min_id,work_task_record.created,work_task_record.id AS recordId
        FROM work_flow_node_instance,work_task_record
        WHERE state = 'FINISHED'
        AND work_flow_node_instance.id = work_task_record.instance_id
        AND work_flow_node_instance.deleted = 0) AS instance ON log.instance_id = instance.id
        LEFT JOIN (SELECT id, name, code FROM work_node WHERE deleted = 0) AS node ON node.id = instance.node_id
        LEFT JOIN (SELECT id, order_id, name, department_id FROM assets WHERE deleted = 0) AS assets ON assets.id = log.business_id
        LEFT JOIN user ON user.id = log.handler_id
        <if test="assetsSelectDTO != null and assetsSelectDTO.selectByDepartment">
            INNER JOIN (
            SELECT DISTINCT
            department.id,
            department.parent_id,
            user.id AS userId
            FROM
            user,
            post,
            department,
            user_post ,
            work_task_record AS record
            WHERE
            user.id = user_post.user_id
            AND user_post.post_id = post.id
            AND post.department_id = department.id
            AND user_post.deleted = 0
            ) AS department ON (department.id IN
            <foreach collection="assetsSelectDTO.departmentIdList" index="index" item="item" open="(" separator=","
                     close=")">
                #{item}
            </foreach>
            )
            AND department.userId = log.handler_id
        </if>
        WHERE instance.id IS NOT NULL
        AND log.business_type = 'ASSET_BUSINESS'
        <if test="assetsSelectDTO != null and assetsSelectDTO.handlerId != null">
            AND handler_id = #{assetsSelectDTO.handlerId}
        </if>
        <if test="assetsSelectDTO != null and assetsSelectDTO.keyWord != null and assetsSelectDTO.keyWord != '' ">
            AND (
            assets.order_id LIKE CONCAT('%',#{assetsSelectDTO.keyWord},'%') OR
            assets.name LIKE CONCAT('%',#{assetsSelectDTO.keyWord},'%') OR
            instance.business_sub_id LIKE CONCAT('%',#{assetsSelectDTO.keyWord},'%') OR
            instance.business_min_id LIKE CONCAT('%',#{assetsSelectDTO.keyWord},'%')
            )
        </if>
        <if test="assetsSelectDTO != null and assetsSelectDTO.nodeCode != null">
            AND node.code = #{assetsSelectDTO.nodeCode}
        </if>
        <if test="assetsSelectDTO != null and assetsSelectDTO.departmentId">
            AND assets.department_id = #{assetsSelectDTO.departmentId}
        </if>
        ORDER BY log.created DESC
    </select>

    <update id="batchFinishTaskByInstanceId" parameterType="java.util.List">
        update work_task_record set finished = 1 , finish_time = now() where instance_id in (
            <foreach collection="list" index="index" item="item" separator="," >
                #{item}
            </foreach>
            )
    </update>

    <select id="getTaskReminderInfo" parameterType="java.lang.Long" resultType="com.dayou.vo.TaskReminderVO">
        SELECT
            wtr.id,
            (case wfni.business_type when 'ITEM_BUSINESS' then '土规业务'
                                     when 'MAJOR_BUSINESS' then '大中型业务'
                                     when 'PERSONAL_BUSINESS' then '个贷业务'
                                     when 'ASSET_BUSINESS' then '资产业务'
                                     when 'COMMISSION_DECLARE_MAJOR_MARKET' then '大中型-提成申报-市场人员'
                                     when 'COMMISSION_DECLARE_MAJOR_EVALUATE' then '大中型-提成申报-评估人员'
                                     when 'COMMISSION_DECLARE_ASSET_MARKET' then '资产-提成申报-市场人员'
                                     when 'COMMISSION_DECLARE_ASSET_EVALUATE' then '资产-提成申报-评估人员'
                                     else '审批业务' end) as businessType,
            ifnull(wfni.business_min_id,
                   wfni.business_sub_id) as `no`,
            u.name as `user`,
            wfni.business_id,
            wfni.business_type as business
        FROM
            work_flow_node_instance wfni
                LEFT JOIN work_task_record wtr ON wfni.id = wtr.instance_id
                LEFT JOIN `user` u ON u.id = wtr.handler_id
        where wfni.id = #{instanceNodeId}
    </select>

    <select id="getHandlerIdByLastNode" resultType="java.lang.Long">
        SELECT
            handler_id
        FROM
            work_task_record
        WHERE
            instance_id = (
                SELECT
                    id
                FROM
                    work_flow_node_instance
                WHERE
                    business_type = #{businessType}
                  AND business_id = #{businessId}
                  AND state = 'PENDING'
                  AND deleted = 0
                <if test="businessSubId!=null and businessSubId!=''">
                    and business_sub_id = #{businessSubId}
                </if>
                <if test="businessMinId!=null and businessMinId!=''">
                    and business_min_id = #{businessMinId}
                </if>
            )
    </select>
</mapper>
