<?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.lz.modules.flow.dao.FlowRecordMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.lz.modules.flow.entity.FlowRecord">
        <id column="id" property="id"/>
        <result column="is_delete" property="isDelete"/>
        <result column="gmt_create" property="gmtCreate"/>
        <result column="gmt_modified" property="gmtModified"/>
        <result column="department_level" property="departmentLevel"/>
        <result column="record_id" property="recordId"/>
        <result column="approval_staff_id" property="approvalStaffId"/>
        <result column="approval_staff_name" property="approvalStaffName"/>
        <result column="flow_name" property="flowName"/>
        <result column="record_staff_id" property="recordStaffId"/>
        <result column="flow_id" property="flowId"/>
        <result column="flow_index" property="flowIndex"/>
        <result column="status" property="status"/>
        <result column="flow_staff_id_role" property="flowStaffIdRole"/>
        <result column="status_name" property="statusName"/>
        <result column="type" property="type"/>
        <result column="process_id" property="processId"/>
        <result column="flow_process" property="flowProcess"/>
    </resultMap>


    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        id AS id, is_delete AS isDelete, gmt_create AS gmtCreate, gmt_modified AS gmtModified, department_level AS departmentLevel, record_id AS recordId, approval_staff_id AS approvalStaffId, approval_staff_name AS approvalStaffName, flow_name AS flowName, record_staff_id AS recordStaffId, flow_id AS flowId, flow_index AS flowIndex, status AS status, flow_staff_id_role AS flowStaffIdRole, status_name AS statusName, type AS type, process_id AS processId, flow_process AS flowProcess
    </sql>




    <select id="selectFlowRecordById" resultType="FlowRecord" >
        select * from lz_flow_record  where id=#{id} and is_delete = 0 limit 1
    </select>


    <insert id="insertFlowRecord" parameterType="FlowRecord" useGeneratedKeys="true" keyProperty="id" >
        insert into lz_flow_record(
        <if test="departmentLevel != null">department_level, </if>
        <if test="recordId != null">record_id, </if>
        <if test="approvalStaffId != null">approval_staff_id, </if>
        <if test="approvalStaffName != null">approval_staff_name, </if>
        <if test="flowName != null">flow_name, </if>
        <if test="recordStaffId != null">record_staff_id, </if>
        <if test="flowId != null">flow_id, </if>
        <if test="flowIndex != null">flow_index, </if>
        <if test="status != null">status, </if>
        <if test="flowStaffIdRole != null">flow_staff_id_role, </if>
        <if test="statusName != null">status_name, </if>
        <if test="type != null">type, </if>
        <if test="processId != null">process_id, </if>
        <if test="flowProcess != null">flow_process, </if>
        is_delete,
        gmt_create,
        gmt_modified
        )values(
        <if test="departmentLevel != null">#{ departmentLevel}, </if>
        <if test="recordId != null">#{ recordId}, </if>
        <if test="approvalStaffId != null">#{ approvalStaffId}, </if>
        <if test="approvalStaffName != null">#{ approvalStaffName}, </if>
        <if test="flowName != null">#{ flowName}, </if>
        <if test="recordStaffId != null">#{ recordStaffId}, </if>
        <if test="flowId != null">#{ flowId}, </if>
        <if test="flowIndex != null">#{ flowIndex}, </if>
        <if test="status != null">#{ status}, </if>
        <if test="flowStaffIdRole != null">#{ flowStaffIdRole}, </if>
        <if test="statusName != null">#{ statusName}, </if>
        <if test="type != null">#{ type}, </if>
        <if test="processId != null">#{ processId}, </if>
        <if test="flowProcess != null">#{ flowProcess}, </if>
        0,
        now(),
        now()
        )
    </insert>


    <update id="updateFlowRecordById" parameterType="FlowRecord" >
        update
        lz_flow_record
        <trim prefix="set" suffixOverrides=",">
            <if test="isDelete != null">is_delete = #{isDelete},</if>
            <if test="gmtCreate != null">gmt_create = #{gmtCreate},</if>
            <if test="departmentLevel != null">department_level = #{departmentLevel},</if>
            <if test="recordId != null">record_id = #{recordId},</if>
            <if test="approvalStaffId != null">approval_staff_id = #{approvalStaffId},</if>
            <if test="approvalStaffName != null">approval_staff_name = #{approvalStaffName},</if>
            <if test="flowName != null">flow_name = #{flowName},</if>
            <if test="recordStaffId != null">record_staff_id = #{recordStaffId},</if>
            <if test="flowId != null">flow_id = #{flowId},</if>
            <if test="flowIndex != null">flow_index = #{flowIndex},</if>
            <if test="status != null">status = #{status},</if>
            <if test="flowStaffIdRole != null">flow_staff_id_role = #{flowStaffIdRole},</if>
            <if test="statusName != null">status_name = #{statusName},</if>
            <if test="type != null">type = #{type},</if>
            <if test="processId != null">process_id = #{processId},</if>
            <if test="flowProcess != null">flow_process = #{flowProcess}</if>
        </trim>
        ,gmt_modified = now()
        where id = #{id}
    </update>


    <update id="updateCoverFlowRecordById" parameterType="FlowRecord" >
        update
            lz_flow_record
        set
            is_delete = #{isDelete},
            gmt_create = #{gmtCreate},
            department_level = #{departmentLevel},
            record_id = #{recordId},
            approval_staff_id = #{approvalStaffId},
            approval_staff_name = #{approvalStaffName},
            flow_name = #{flowName},
            record_staff_id = #{recordStaffId},
            flow_id = #{flowId},
            flow_index = #{flowIndex},
            status = #{status},
            flow_staff_id_role = #{flowStaffIdRole},
            status_name = #{statusName},
            type = #{type},
            process_id = #{processId},
            flow_process = #{flowProcess}
        ,gmt_modified = now()
        where id = #{id}
    </update>


    <update id="deleteFlowRecordById" parameterType="java.lang.Long">
        update lz_flow_record set is_delete = 1 where id=#{id} limit 1
    </update>


    <select id="selectLastFlowRecordByRecordId" resultType="com.lz.modules.flow.entity.FlowRecord">
        select * from lz_flow_record  where  is_delete = 0 and record_id = #{recordId} and status = 0  order by id desc  limit 1
    </select>

    <select id="selectNotApprovalStaffIdFlowRecords" resultType="com.lz.modules.flow.entity.FlowRecord">
        select * from lz_flow_record  where  is_delete = 0 and record_id = #{recordId} and status = 0 and (approval_staff_id is null or approval_staff_id = '')   order by id desc  limit 1
    </select>

    <select id="selectFlowRecordByRecordId" resultType="com.lz.modules.flow.entity.FlowRecord">
        select * from lz_flow_record  where  is_delete = 0 and record_id = #{recordId}
    </select>


    <select id="selectFlowRecordByResultRecordIdFlowId" resultType="com.lz.modules.flow.entity.FlowRecord">
         select * from lz_flow_record  where  is_delete = 0 and record_id = #{recordId}  and status = 0
    </select>


    <select id="selectFlowRecordByFlowId" resultType="com.lz.modules.flow.entity.FlowRecord">
        select * from lz_flow_record  where  is_delete = 0 and record_id = #{recordId}
    </select>

    <select id="selectLastFlowNoStatusRecordByRecordId" resultType="com.lz.modules.flow.entity.FlowRecord">
        select * from lz_flow_record  where  is_delete = 0 and record_id = #{recordId}  order by id desc  limit 1
    </select>



    <select id="selectFlowRecordByResultRecordIdType" resultType="com.lz.modules.flow.entity.FlowRecord">
        select * from lz_flow_record  where  is_delete = 0 and record_id = #{resultRecordId} and type = #{type}  and status = 0
    </select>



    <select id="selectFlowRecordByResultRecordIdTypeStatus" resultType="com.lz.modules.flow.entity.FlowRecord">
         select * from lz_flow_record  where  is_delete = 0 and record_id = #{recordResultId} and type = #{type}  and status = #{status}
    </select>


    <select id="selectFinishTasksByApprovalStaffId" resultType="com.lz.modules.performance.dto.TaskListDto">
        SELECT DISTINCT(record_id),f.flow_process,f.flow_name,f.approval_staff_id,f.record_staff_id,start_id,r.gmt_modified,f.type from lz_flow_record f
        LEFT JOIN lz_result_record r
        ON f.record_id = r.id
        where f.is_delete=0 and r.is_delete=0
        and approval_staff_id = #{approvalStaffId}
        and f.status = 1
        and f.flow_process != 2
        order by r.gmt_modified desc
    </select>

    <select id="selectTodoTasksByApprovalStaffId" resultType="com.lz.modules.performance.dto.TaskListDto">
        SELECT DISTINCT(record_id),f.flow_process,f.flow_name,f.approval_staff_id,f.record_staff_id,start_id,r.gmt_modified,f.type from lz_flow_record f
        LEFT JOIN lz_result_record r
        ON f.record_id = r.id
        where f.is_delete=0 and r.is_delete=0
        and approval_staff_id = #{approvalStaffId}
        and f.status = 2
        and f.flow_process != 2
        order by r.gmt_modified desc
    </select>

    <insert id="insertFlowRecords">
        insert into lz_flow_record(
            department_level,
            record_id,
            approval_staff_id,
            approval_staff_name,
            flow_name,
            record_staff_id,
            flow_id,
            flow_index,
            status,
            flow_staff_id_role,
            status_name,
            type,
            is_delete,
            gmt_create,
            gmt_modified
        )values
        <foreach collection="flowRecords" item="item" separator=",">
            ( #{  item.departmentLevel},
            #{  item.recordId},
            #{  item.approvalStaffId},
            #{  item.approvalStaffName},
            #{  item.flowName},
            #{  item.recordStaffId},
            #{  item.flowId},
            #{  item.flowIndex},
            #{  item.status},
            #{  item.flowStaffIdRole},
            #{  item.statusName},
            #{  item.type},
            0,
            now(),
            now()
        )
        </foreach>
    </insert>




    <select id="selectFlowRecordByRecordIdStatus" resultType="com.lz.modules.flow.entity.FlowRecord">
        select * from lz_flow_record  where  is_delete = 0 and record_id = #{resultRecordId} and status = #{status} order by flow_index asc
    </select>

    <select id="selectFlowRecordByRecordIdAndFlowId" resultType="com.lz.modules.flow.entity.FlowRecord">
         select * from lz_flow_record  where  is_delete = 0 and record_id = #{resultRecordId} and  #{flowId} > id order by id desc limit 1
    </select>


    <select id="selectFlowRecordByRecordIdStatusFlowIndex" resultType="com.lz.modules.flow.entity.FlowRecord">
        select * from lz_flow_record  where  is_delete = 0 and record_id = #{resultRecordId} and status = #{status} and flow_index = #{flowIndex}
    </select>

    <select id="selectPreFlowRecordByRecordIdMaxIdStatus" resultType="com.lz.modules.flow.entity.FlowRecord">
        select * from lz_flow_record  where  is_delete = 0 and record_id = #{resultRecordId} and  #{id} > id and status = #{status}  order by id desc limit 1
    </select>


    <select id="selectFlowRecordByRecordIdFlowIndex" resultType="com.lz.modules.flow.entity.FlowRecord">
        select * from lz_flow_record  where  is_delete = 0 and record_id = #{resultRecordId} and flow_index = #{flowIndex}
    </select>
    <select id="selectFlowRecordByResultRecordId" resultType="com.lz.modules.flow.entity.FlowRecord">
        select * from lz_flow_record  where  is_delete = 0 and record_id = #{resultRecordId}
    </select>

    <select id="selectFlowRecordByRecordIdMinIdStatus" resultType="com.lz.modules.flow.entity.FlowRecord">
        select * from lz_flow_record  where  is_delete = 0 and record_id = #{resultRecordId} and   id > #{id} and status = #{status}  order by id asc limit 1
    </select>

    <update id="batchUpdateSkip">
        UPDATE lz_flow_record
        SET status = 4
        ,gmt_modified = now()
        where is_delete = 0 and status !=4 and flow_process = 2
        and record_id in (
        <foreach collection="recordIds" item="record_id" separator=",">
            #{record_id}
        </foreach>
        )
    </update>

    <select id="selectFlowRecordByRecordIdFlowProcess" resultType="com.lz.modules.flow.entity.FlowRecord">
        select * from lz_flow_record  where  is_delete = 0 and record_id = #{id} and flow_process = #{flowProcess}
    </select>


    <update id="batchDeleteByRecordIds">
        UPDATE lz_flow_record
        SET is_delete = 1
        ,gmt_modified = now()
        where is_delete = 0
        and record_id in (
        <foreach collection="recordIds" item="record_id" separator=",">
            #{record_id}
        </foreach>
        )

    </update>
</mapper>

