<?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.bonus.material.work.mapper.DirectAuditMapper">

    <select id="getList" resultType="com.bonus.material.ma.domain.DirectApplyInfo">
        SELECT
        dai.Id,
        dai.create_time AS createTime,
        su.user_name AS createName,
        bui.unit_id AS backUnitId,
        bui.unit_name AS backUnitName,
        bpl.pro_id AS backProId,
        bpl.pro_name AS backProName,
        bui1.unit_id AS leaseUnitId,
        bui1.unit_name AS leaseUnitName,
        bpl1.pro_id AS leaseProId,
        bpl1.pro_name AS leaseProName,
        dai.lease_man as leaseMan ,
        swr.workflow_status as flowStatus,
        swr.task_id as flowId,
        swrs.node_id AS nodeId,
        swrs.next_node_id AS nextNodeId,
        swrs.record_id AS recordId,
        swn.node_sign_type as nodeSignType
        FROM
        direct_apply_info dai
        LEFT JOIN bm_agreement_info bai ON dai.back_agreement_id = bai.agreement_id
        LEFT JOIN bm_agreement_info bai1 ON dai.lease_agreement_id = bai1.agreement_id
        LEFT JOIN bm_project bpl ON bpl.pro_id = bai.project_id
        LEFT JOIN bm_project bpl1 ON bpl1.pro_id = bai1.project_id
        LEFT JOIN bm_unit bui ON bui.unit_id = bai.unit_id
        LEFT JOIN bm_unit bui1 ON bui1.unit_id = bai1.unit_id
        LEFT JOIN direct_apply_details dad on dad.direct_id=dai.id
        left join sys_user su on dai.create_by = su.user_id
        left join sys_workflow_record swr on dai.id = swr.task_id
        left join sys_workflow_type swt on swr.workflow_id = swt.id
        left join sys_workflow_node swn on swt.id = swn.type_id
        left join sys_workflow_config swc on swn.id = swc.node_id
        left join sys_workflow_record_history  swrs on swr.id = swrs.record_id
        <where>
            swr.task_id is not null
            <if test="keyWord != null and keyWord != ''">
                and (
                bui.unit_name like concat('%', #{keyWord}, '%') or
                bpl.pro_name like concat('%', #{keyWord}, '%') or
                bui1.unit_name like concat('%', #{keyWord}, '%') or
                bpl1.pro_name like concat('%', #{keyWord}, '%'))
            </if>
            <if test="lotId != null and lotId != ''">
                and bpl.pro_id = #{lotId}
            </if>
            <if test="unitId != null and unitId != ''">
                and bui.unit_id = #{unitId}
            </if>

            <if test="status != null and status != ''">
                and swr.workflow_status = #{status}
            </if>
            <if test="isApp != null and taskStatus==0">and dai.status = 0 </if>
            <if test="isApp != null and taskStatus==1">and (dai.status = 1 or dai.status = 2)</if>
            <if test="startTime != null and startTime != '' and endTime != null and endTime != ''">
                AND DATE_FORMAT( dai.create_time, '%Y-%m-%d' ) BETWEEN #{startTime} AND #{endTime}
            </if>
        </where>
        GROUP BY swr.task_id order by dai.create_time desc
    </select>



    <select id="getAuditInfo" resultType="com.bonus.material.work.domain.DirectAudit">
        SELECT
            wh.record_id AS flowId,
            wh.node_id AS nodeId,
            wn.node_name AS nodeName,
            wn2.node_name AS nextNodeName,
            wh.next_node_id AS nextNodeId,
            wh.create_time AS createTime,
            wh.create_by AS creator,
            wh.is_accept as isAccept,
            wc.config_value as userId,
            wh.remark as remark
        FROM
            sys_workflow_record_history wh
                LEFT JOIN sys_workflow_config wc on wh.next_node_id = wc.node_id
                LEFT JOIN sys_workflow_node wn on wh.node_id = wn.id
                LEFT JOIN sys_workflow_node wn2 on wc.node_id = wn2.id
        WHERE wh.record_id = #{flowId}
    </select>

    <update id="updateWorkflowRecord">
        update sys_workflow_record set workflow_status = #{flowStatus} where id = #{id}
    </update>
    <update id="updateDirectAudit">
        update direct_apply_info set status = #{status} where id = #{id}
    </update>
</mapper>
