<?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.ruoyi.project.business.mapper.flow.FlowTaskUserMapper">

    <resultMap type="FlowTaskUser" id="FlowTaskUserResult">
        <result property="id" column="id"/>
        <result property="taskId" column="task_id"/>
        <result property="status" column="status"/>
        <result property="remark" column="remark"/>
        <result property="auditTime" column="audit_time"/>
        <result property="auditUser" column="audit_user"/>
        <result property="delFlag" column="del_flag"/>
        <result property="createBy" column="create_by"/>
        <result property="createTime" column="create_time"/>
        <result property="updateBy" column="update_by"/>
        <result property="updateTime" column="update_time"/>
        <result property="nickName" column="nick_name"/>
        <result property="auditorName" column="auditor_name"/>
    </resultMap>

    <insert id="saveBatch" useGeneratedKeys="true" keyProperty="id">
        insert into flow_task_user
        (task_id, status, remark, audit_time, audit_user, del_flag, create_by, create_time, update_by, update_time)
        values
        <foreach collection="list" item="item" separator=",">
            (#{item.taskId}, #{item.status}, #{item.remark}, #{item.auditTime}, #{item.auditUser},
            '0', #{item.createBy}, #{item.createTime}, #{item.updateBy}, #{item.updateTime})
        </foreach>;
    </insert>

    <update id="updateStatusBatch">
        update flow_task_user
        set status = #{param.status},
        remark = #{param.remark},
        auditor = #{param.auditor},
        audit_time = #{param.auditTime},
        update_by = #{param.updateBy},
        update_time = #{param.updateTime}
        where id in
        <foreach collection="param.ids" item="id" open="(" close=")" separator=",">
            #{id}
        </foreach>
    </update>

    <select id="listTaskUsers" resultMap="FlowTaskUserResult">
        select flow_task_user.*,user.user_name as nick_name,auditor.user_name as auditor_name
        from flow_task_user
        left join sys_user user on user.user_id = flow_task_user.audit_user
        left join sys_user auditor on auditor.user_id = flow_task_user.auditor
        where flow_task_user.del_flag = 0 and flow_task_user.task_id in
        <foreach collection="taskIds" item="taskId" open="(" separator="," close=")">
            #{taskId}
        </foreach>
    </select>

    <resultMap id="map" type="FlowTask">
        <result property="id" column="id"/>
        <result property="flowId" column="flow_id"/>
        <result property="nodeId" column="node_id"/>
        <result property="prevTasks" column="prev_tasks"/>
        <result property="nextTasks" column="next_tasks"/>
        <result property="status" column="status"/>
        <result property="auditorType" column="auditor_type"/>
        <result property="signType" column="sign_type"/>
        <result property="delFlag" column="del_flag"/>
        <result property="createBy" column="create_by"/>
        <result property="createTime" column="create_time"/>
        <result property="updateBy" column="update_by"/>
        <result property="updateTime" column="update_time"/>
        <result property="nickName" column="nick_name"/>
        <result property="auditTime" column="audit_time"/>
        <result property="roleName" column="role_name"/>
    </resultMap>

    <resultMap id="configMap" type="FlowConfig">
        <result property="id" column="id"/>
        <result property="flowData" column="flow_data"/>
    </resultMap>

    <select id="getAuditConfig" resultMap="configMap">
        select flow.id,
               config.flow_data
        from flow flow
                 left join flow_config config on config.id = flow.flow_config_id
        where flow.business_id = #{businessId}
          and flow.del_flag = '0'
          and config.del_flag = '0'
    </select>

    <select id="getAuditNode" parameterType="long" resultMap="map">
        SELECT task.id,
               task.flow_id,
               task.node_id,
               task.prev_tasks,
               task.next_tasks,
               task.STATUS,
               task.auditor_type,
               task.sign_type
        FROM flow_task task
        where task.del_flag = '0'
          and task.flow_id = #{flowId}
    </select>


    <select id="getAuditTaskUser" resultMap="FlowTaskUserResult">
        select task_user.id,
               task_user.task_id,
               task_user.status,
               task_user.remark,
               task_user.audit_time,
               task_user.audit_user,
               user.nick_name
        from flow_task_user task_user
                 left join flow_task task on task.id = task_user.task_id
                 left join sys_user user
        on user.user_id = task_user.audit_user
        where task_user.del_flag = '0'
          and task.flow_id = #{flowId}
          and task.node_id = #{nodeId}
    </select>

    <select id="getAuditUserToEmployeeId" parameterType="Flow" resultType="java.lang.Long">
        select distinct bei.id
        from flow_task_user task_user
                 left join flow_task task on task.id = task_user.task_id
                 left join flow as flow on task.flow_id = flow.id
                 left join sys_user user
        on user.user_id = task_user.audit_user
            left join basic_employee_info bei on user.job_no = bei.job_no
        where task_user.del_flag = '0'
          and task.del_flag = '0'
          and flow.del_flag = '0'
          and user.del_flag = '0'
          and bei.del_flag = '0'
          and flow.business_id = #{businessId}
          and flow.status = #{status}
          and flow.audit_flow = #{auditFlow}
    </select>

    <select id="getAuditTaskUsers" resultType="java.lang.Long">
        SELECT
        info.id as employee_id
        FROM
        flow
        left join flow_task task on task.flow_id = flow.id and task.del_flag ='0'
        left join flow_task_user task_user on task_user.task_id = task.id and task_user.del_flag = '0'
        left join sys_user on sys_user.user_id = task_user.audit_user
        left join basic_employee_info info on info.job_no = sys_user.job_no

        <where>
            flow.del_flag = '0' and task.status = '1' and task_user.status = '1'
            <if test="businessId != null">
                and flow.business_id = #{businessId}
            </if>
            <if test="auditFlow != null">
                and flow.audit_flow = #{auditFlow}
            </if>
        </where>
    </select>
</mapper>
