<?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.sundot.airport.system.mapper.approval.ApprovalTaskMapper">

    <resultMap type="ApprovalTask" id="ApprovalTaskResult">
        <id property="id" column="id"/>
        <result property="taskNo" column="task_no"/>
        <result property="instanceId" column="instance_id"/>
        <result property="nodeId" column="node_id"/>
        <result property="taskName" column="task_name"/>
        <result property="assigneeId" column="assignee_id"/>
        <result property="assigneeName" column="assignee_name"/>
        <result property="assignTime" column="assign_time"/>
        <result property="status" column="status"/>
        <result property="completeTime" column="complete_time"/>
        <result property="comment" column="comment"/>
        <result property="formData" column="form_data"/>
        <result property="timeoutTime" column="timeout_time"/>
        <result property="isTimeout" column="is_timeout"/>
        <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="remark" column="remark"/>
        <association property="instance" javaType="ApprovalInstance" resultMap="InstanceResult"/>
        <association property="nodeDefinition" javaType="ApprovalNodeDefinition" resultMap="NodeResult"/>
    </resultMap>

    <resultMap id="InstanceResult" type="ApprovalInstance">
        <id property="id" column="instance_id"/>
        <result property="instanceNo" column="instance_no"/>
        <result property="title" column="instance_title"/>
        <result property="businessType" column="business_type"/>
        <result property="businessId" column="business_id"/>
        <result property="submitterId" column="submitter_id"/>
        <result property="submitterName" column="submitter_name"/>
        <result property="submitTime" column="submit_time"/>
        <result property="urgentLevel" column="urgent_level"/>
    </resultMap>

    <resultMap id="NodeResult" type="ApprovalNodeDefinition">
        <id property="id" column="node_id"/>
        <result property="nodeCode" column="node_code"/>
        <result property="nodeName" column="node_name"/>
        <result property="nodeType" column="node_type"/>
        <result property="canReject" column="can_reject"/>
    </resultMap>

    <sql id="selectApprovalTaskVo">
        select id,
               task_no,
               instance_id,
               node_id,
               task_name,
               assignee_id,
               assignee_name,
               assign_time,
               status,
               complete_time,
               comment,
               form_data,
               timeout_time,
               is_timeout,
               create_by,
               create_time,
               update_by,
               update_time,
               remark
        from approval_task
    </sql>

    <select id="selectApprovalTaskList" parameterType="ApprovalTask" resultMap="ApprovalTaskResult">
        <include refid="selectApprovalTaskVo"/>
        <where>
            <if test="taskNo != null  and taskNo != ''">and task_no like concat('%', #{taskNo}, '%')</if>
            <if test="instanceId != null ">and instance_id = #{instanceId}</if>
            <if test="nodeId != null ">and node_id = #{nodeId}</if>
            <if test="taskName != null  and taskName != ''">and task_name like concat('%', #{taskName}, '%')</if>
            <if test="assigneeId != null ">and assignee_id = #{assigneeId}</if>
            <if test="assigneeName != null  and assigneeName != ''">and assignee_name like concat('%', #{assigneeName},
                '%')
            </if>
            <if test="status != null  and status != ''">and status = #{status}</if>
            <if test="isTimeout != null  and isTimeout != ''">and is_timeout = #{isTimeout}</if>
            <if test="params != null and params.beginTime != null and params.beginTime != ''"><!-- 开始时间检索 -->
                and date_format(assign_time,'%y%m%d') &gt;= date_format(#{params.beginTime},'%y%m%d')
            </if>
            <if test="params != null and params.endTime != null and params.endTime != ''"><!-- 结束时间检索 -->
                and date_format(assign_time,'%y%m%d') &lt;= date_format(#{params.endTime},'%y%m%d')
            </if>
        </where>
        order by assign_time desc
    </select>

    <select id="selectApprovalTaskById" parameterType="Long" resultMap="ApprovalTaskResult">
        <include refid="selectApprovalTaskVo"/>
        where id = #{id}
    </select>

    <select id="selectApprovalTaskByNo" parameterType="String" resultMap="ApprovalTaskResult">
        <include refid="selectApprovalTaskVo"/>
        where task_no = #{taskNo}
    </select>

    <select id="selectApprovalTaskByInstanceId" parameterType="Long" resultMap="ApprovalTaskResult">
        <include refid="selectApprovalTaskVo"/>
        where instance_id = #{instanceId}
        order by assign_time
    </select>

    <select id="selectApprovalTaskByAssignee" resultMap="ApprovalTaskResult">
        select
        at.id, at.task_no, at.instance_id, at.node_id, at.task_name, at.assignee_id, at.assignee_name,
        at.assign_time, at.status, at.complete_time, at.comment, at.form_data, at.timeout_time, at.is_timeout,
        at.create_by, at.create_time, at.update_by, at.update_time, at.remark,
        ai.instance_no, ai.title as instance_title, ai.business_type, ai.business_id, ai.submitter_id,
        ai.submitter_name, ai.submit_time, ai.urgent_level,
        and1.node_code, and1.node_name, and1.node_type, and1.can_reject
        from approval_task at
        left join approval_instance ai on at.instance_id = ai.id
        left join approval_node_definition and1 on at.node_id = and1.id
        where at.assignee_id = #{assigneeId}
        <if test="status != null and status != ''">and at.status = #{status}</if>
        order by at.assign_time desc
    </select>

    <select id="selectApprovalTaskByInstanceAndNode" resultMap="ApprovalTaskResult">
        <include refid="selectApprovalTaskVo"/>
        where instance_id = #{instanceId} and node_id = #{nodeId}
        limit 1
    </select>

    <select id="selectApprovalTaskDetail" parameterType="Long" resultMap="ApprovalTaskResult">
        select at.id,
               at.task_no,
               at.instance_id,
               at.node_id,
               at.task_name,
               at.assignee_id,
               at.assignee_name,
               at.assign_time,
               at.status,
               at.complete_time,
               at.comment,
               at.form_data,
               at.timeout_time,
               at.is_timeout,
               at.create_by,
               at.create_time,
               at.update_by,
               at.update_time,
               at.remark,
               ai.instance_no,
               ai.title as instance_title,
               ai.business_type,
               ai.business_id,
               ai.submitter_id,
               ai.submitter_name,
               ai.submit_time,
               ai.urgent_level,
               and1.node_code,
               and1.node_name,
               and1.node_type,
               and1.can_reject
        from approval_task at
        left join approval_instance ai
        on at.instance_id = ai.id
            left join approval_node_definition and1 on at.node_id = and1.id
        where at.id = #{id}
    </select>

    <select id="generateTaskNo" resultType="String">
        SELECT CONCAT('TASK', DATE_FORMAT(NOW(), '%Y%m%d%H%i%s'), LPAD(FLOOR(RAND() * 1000), 3, '0')) AS new_task_no
    </select>

    <select id="countPendingTasksByAssignee" parameterType="Long" resultType="int">
        select count(1)
        from approval_task
        where assignee_id = #{assigneeId}
          and status = 'PENDING'
    </select>

    <insert id="insertApprovalTask" parameterType="ApprovalTask" useGeneratedKeys="true" keyProperty="id">
        insert into approval_task
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="taskNo != null and taskNo != ''">task_no,</if>
            <if test="instanceId != null">instance_id,</if>
            <if test="nodeId != null">node_id,</if>
            <if test="taskName != null and taskName != ''">task_name,</if>
            <if test="assigneeId != null">assignee_id,</if>
            <if test="assigneeName != null and assigneeName != ''">assignee_name,</if>
            <if test="assignTime != null">assign_time,</if>
            <if test="status != null">status,</if>
            <if test="completeTime != null">complete_time,</if>
            <if test="comment != null">comment,</if>
            <if test="formData != null">form_data,</if>
            <if test="timeoutTime != null">timeout_time,</if>
            <if test="isTimeout != null">is_timeout,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="remark != null">remark,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="taskNo != null and taskNo != ''">#{taskNo},</if>
            <if test="instanceId != null">#{instanceId},</if>
            <if test="nodeId != null">#{nodeId},</if>
            <if test="taskName != null and taskName != ''">#{taskName},</if>
            <if test="assigneeId != null">#{assigneeId},</if>
            <if test="assigneeName != null and assigneeName != ''">#{assigneeName},</if>
            <if test="assignTime != null">#{assignTime},</if>
            <if test="status != null">#{status},</if>
            <if test="completeTime != null">#{completeTime},</if>
            <if test="comment != null">#{comment},</if>
            <if test="formData != null">#{formData},</if>
            <if test="timeoutTime != null">#{timeoutTime},</if>
            <if test="isTimeout != null">#{isTimeout},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="remark != null">#{remark},</if>
        </trim>
    </insert>

    <update id="updateApprovalTask" parameterType="ApprovalTask">
        update approval_task
        <trim prefix="SET" suffixOverrides=",">
            <if test="taskNo != null and taskNo != ''">task_no = #{taskNo},</if>
            <if test="instanceId != null">instance_id = #{instanceId},</if>
            <if test="nodeId != null">node_id = #{nodeId},</if>
            <if test="taskName != null and taskName != ''">task_name = #{taskName},</if>
            <if test="assigneeId != null">assignee_id = #{assigneeId},</if>
            <if test="assigneeName != null and assigneeName != ''">assignee_name = #{assigneeName},</if>
            <if test="assignTime != null">assign_time = #{assignTime},</if>
            <if test="status != null">status = #{status},</if>
            <if test="completeTime != null">complete_time = #{completeTime},</if>
            <if test="comment != null">comment = #{comment},</if>
            <if test="formData != null">form_data = #{formData},</if>
            <if test="timeoutTime != null">timeout_time = #{timeoutTime},</if>
            <if test="isTimeout != null">is_timeout = #{isTimeout},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="remark != null">remark = #{remark},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteApprovalTaskById" parameterType="Long">
        delete
        from approval_task
        where id = #{id}
    </delete>

    <delete id="deleteApprovalTaskByIds" parameterType="String">
        delete from approval_task where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>

    <delete id="deleteApprovalTaskByInstanceId" parameterType="Long">
        delete
        from approval_task
        where instance_id = #{instanceId}
    </delete>
</mapper>