<?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="cn.fateverse.workflow.mapper.ProcessBusinessMapper">
    <sql id="WorkflowBusinessResult">
        select business_id,
               group_id,
               deployment_id,
               process_definition_key,
               deployment_name,
               remark,
               create_by,
               create_time,
               update_by,
               update_time
        from wk_re_process_business
    </sql>

    <select id="selectWorkflowBusinessSimple" resultType="cn.fateverse.workflow.entity.vo.ProcessDefinitionVo">
        select business_id, arp.ID_ as process_definition_id,
        group_id,rb.deployment_id,process_definition_key, rb.deployment_name,rd.logo,concat('v', arp.VERSION_) as version
        ,update_time,rb.remark,arp.SUSPENSION_STATE_ as state
        from wk_re_process_business rb
        left join act_re_procdef arp on arp.DEPLOYMENT_ID_ = rb.deployment_id
        left join wk_re_process_data rd on rb.deployment_id = rd.deployment_id
        <where>
            <if test="deploymentName != null and deploymentName != ''">and rb.deployment_name like
                concat('%',#{deploymentName},'%')
            </if>
            <if test="startTime != null and endTime != null">and update_time between #{startTime} and #{endTime}</if>
            <if test="state != null">and arp.SUSPENSION_STATE_ = #{state}</if>
        </where>
        order by create_time desc
    </select>


    <insert id="insert" keyProperty="businessId" useGeneratedKeys="true">
        insert into wk_re_process_business
        <trim prefix=" ( " suffix=" ) " suffixOverrides=",">
            <if test="groupId != null and groupId != ''">group_id,</if>
            <if test="deploymentId != null and deploymentId != ''">deployment_id,</if>
            <if test="processDefinitionKey != null and processDefinitionKey != ''">process_definition_key,</if>
            <if test="deploymentName != null and deploymentName != ''">deployment_name,</if>
            <if test="remark != null and remark != ''">remark,</if>
            <if test="createBy != null and createBy != ''">create_by,</if>
            <if test="createTime != null ">create_time,</if>
        </trim>
        <trim prefix=" values ( " suffix=" ) " suffixOverrides=",">
            <if test="groupId != null and groupId != ''">#{groupId},</if>
            <if test="deploymentId != null and deploymentId != ''">#{deploymentId},</if>
            <if test="processDefinitionKey != null and processDefinitionKey != ''">#{processDefinitionKey},</if>
            <if test="deploymentName != null and deploymentName != ''">#{deploymentName},</if>
            <if test="remark != null and remark != ''">#{remark},</if>
            <if test="createBy != null and createBy != ''">#{createBy},</if>
            <if test="createTime != null ">#{createTime},</if>
        </trim>
    </insert>
    <select id="selectByDeploymentKey"
            resultType="cn.fateverse.workflow.entity.ProcessBusiness">
        <include refid="WorkflowBusinessResult"/>
        where process_definition_key = #{processDefinitionKey}
    </select>
    <select id="selectByDeploymentId" resultType="cn.fateverse.workflow.entity.ProcessBusiness">
        <include refid="WorkflowBusinessResult"/>
        where deployment_id = #{deploymentId}
    </select>
    <update id="update">
        update wk_re_process_business
        <set>
            <if test="groupId != null and groupId != ''">group_id = #{groupId},</if>
            <if test="deploymentId != null and deploymentId != ''">deployment_id = #{deploymentId},</if>
            <if test="processDefinitionKey != null and processDefinitionKey != ''">process_definition_key = #{processDefinitionKey},</if>
            <if test="deploymentName != null and deploymentName != ''">deployment_name = #{deploymentName},</if>
            <if test="remark != null and remark != ''">remark = #{remark},</if>
            <if test="updateBy != null and updateBy != ''">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
        </set>
        where business_id = #{businessId}
    </update>
</mapper>