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


    <insert id="addSysWorkflowRecord" useGeneratedKeys="true" keyProperty="id">
        insert into sys_workflow_record
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="taskId != null">task_id,</if>
            <if test="taskType != null">task_type,</if>
            <if test="taskCode != null">task_code,</if>
            <if test="workflowId != null">workflow_id,</if>
            <if test="workflowStatus != null">workflow_status,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="taskId != null">#{taskId},</if>
            <if test="taskType != null">#{taskType},</if>
            <if test="taskCode != null">#{taskCode},</if>
            <if test="workflowId != null">#{workflowId},</if>
            <if test="workflowStatus != null">#{workflowStatus},</if>
        </trim>
    </insert>
    <update id="updateSysWorkflowRecord">
        update sys_workflow_record set workflow_status = #{workflowStatus} where task_id = #{taskId}
        <if test="taskType != null">
            and task_type = #{taskType}
        </if>
    </update>
    <update id="updateSysWorkflowType">
        update sys_workflow_record set workflow_id = #{workflowId} where id = #{id}
    </update>

    <select id="getSysWorkflowRecodeByTaskId" parameterType="Integer" resultType="com.bonus.material.work.domain.dto.SysWorkflowAuditDto">
         select
             a.task_id as taskId,
             a.task_type as taskType,
             a.task_code as taskCode,
             a.workflow_id as workflowId,
             a.workflow_status as workflowStatus
         from sys_workflow_record a
         <where>
             <if test="taskId != null">
                and a.task_id = #{taskId}
             </if>
         </where>
     </select>

    <select id="getSysWorkflowRecodeByTaskIdAndType" resultType="com.bonus.material.work.domain.dto.SysWorkflowAuditDto">
        select
             a.task_id as taskId,
             a.task_type as taskType,
             a.task_code as taskCode,
             a.workflow_id as workflowId,
             a.workflow_status as workflowStatus
         from sys_workflow_record a
         <where>
             <if test="taskId != null">
                and a.task_id = #{taskId}
             </if>
             <if test="taskType != null">
                and a.task_type = #{taskType}
             </if>
             <if test="workflowId != null">
                and a.workflow_id = #{workflowId}
             </if>
         </where>
        LIMIT 1
    </select>

    <select id="getAuditStatus" resultType="com.bonus.material.work.domain.SysWorkflowRecordHistory">
        select
            a.id as id,
            a.task_id as taskId,
            a.task_type as taskType,
            a.task_code as taskCode,
            a.workflow_id as workflowId,
            a.workflow_status as workflowStatus
        from sys_workflow_record a

        where task_id = #{taskId}
        LIMIT 1
    </select>

    <select id="getAuditUserByTaskId" resultType="com.bonus.material.work.domain.SysWorkflowRecordHistory">
        select
            swr.task_id,
            swr.id,
            sp.receiver_username AS receiveUsername,
            sp.is_examine AS isExamine
        from
            uni_org.sys_information si

                LEFT JOIN sys_workflow_record swr on swr.id = si.business_id
                LEFT JOIN uni_org.sys_information_people sp on si.id = sp.information_id
        WHERE  swr.task_id=#{taskId} and si.web_app_id ="9fa73f046ef520b09e94bbffc3b07702"

    </select>

    <select id="getAuditUserByRecordId" resultType="com.bonus.material.work.domain.SysWorkflowRecordHistory">
        select
            swr.task_id,
            swr.id,
            sp.receiver_username AS receiveUsername,
            sp.is_examine AS isExamine
        from
            uni_org.sys_information si

                LEFT JOIN sys_workflow_record swr on swr.id = si.business_id
                LEFT JOIN uni_org.sys_information_people sp on si.id = sp.information_id
        WHERE  si.business_id=#{recordId} and si.web_app_id ="9fa73f046ef520b09e94bbffc3b07702"
        and  sp.is_examine = 0

    </select>

    <select id="selectSysWorkflowRecordByTaskId" resultType="java.lang.Integer">
        select
            count(1)
        from sys_workflow_record
        where task_id = #{taskId}
    </select>
    <select id="selectLeaseAgreementIdByRecordId" resultType="java.lang.Integer">
        select
            dai.lease_agreement_id as leaseAgreementId
        from
            direct_apply_info dai
        WHERE  id= #{taskId}
    </select>
</mapper>
