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

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.lz.modules.flow.entity.FlowChart">
        <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="name" property="name"/>
        <result column="process_id" property="processId"/>
        <result column="status" property="status"/>
        <result column="type" property="type"/>
        <result column="step_index" property="stepIndex"/>
        <result column="flow_process" property="flowProcess"/>
        <result column="desc" property="desc"/>
    </resultMap>


    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        id AS id, is_delete AS isDelete, gmt_create AS gmtCreate, gmt_modified AS gmtModified, name AS name, process_id AS processId, status AS status, type AS type, step_index AS stepIndex, flow_process AS flowProcess, desc AS desc
    </sql>




    <select id="selectFlowChartById" resultType="FlowChart" >
        select * from lz_flow_chart  where id=#{id} and is_delete = 0 limit 1
    </select>


    <insert id="insertFlowChart" parameterType="FlowChart" useGeneratedKeys="true" keyProperty="id" >
        insert into lz_flow_chart(
        <if test="name != null">name, </if>
        <if test="processId != null">process_id, </if>
        <if test="status != null">status, </if>
        <if test="type != null">type, </if>
        <if test="stepIndex != null">step_index, </if>
        <if test="flowProcess != null">flow_process, </if>
        <if test="desc != null">desc, </if>
        is_delete,
        gmt_create,
        gmt_modified
        )values(
        <if test="name != null">#{ name}, </if>
        <if test="processId != null">#{ processId}, </if>
        <if test="status != null">#{ status}, </if>
        <if test="type != null">#{ type}, </if>
        <if test="stepIndex != null">#{ stepIndex}, </if>
        <if test="flowProcess != null">#{ flowProcess}, </if>
        <if test="desc != null">#{ desc}, </if>
        0,
        now(),
        now()
        )
    </insert>


    <update id="updateFlowChartById" parameterType="FlowChart" >
        update
        lz_flow_chart
        <trim prefix="set" suffixOverrides=",">
            <if test="isDelete != null">is_delete = #{isDelete},</if>
            <if test="gmtCreate != null">gmt_create = #{gmtCreate},</if>
            <if test="name != null">name = #{name},</if>
            <if test="processId != null">process_id = #{processId},</if>
            <if test="status != null">status = #{status},</if>
            <if test="type != null">type = #{type},</if>
            <if test="stepIndex != null">step_index = #{stepIndex},</if>
            <if test="flowProcess != null">flow_process = #{flowProcess},</if>
            <if test="desc != null">desc = #{desc}</if>
        </trim>
        ,gmt_modified = now()
        where id = #{id}
    </update>


    <update id="updateCoverFlowChartById" parameterType="FlowChart" >
        update
            lz_flow_chart
        set
            is_delete = #{isDelete},
            gmt_create = #{gmtCreate},
            name = #{name},
            process_id = #{processId},
            status = #{status},
            type = #{type},
            step_index = #{stepIndex},
            flow_process = #{flowProcess},
            desc = #{desc}
        ,gmt_modified = now()
        where id = #{id}
    </update>


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

    <select id="selectFlowChartByFlowManagerId" resultType="FlowChart" >
        select * from lz_flow_chart  where process_id=#{id} and is_delete = 0 order by step_index asc
    </select>

    <select id="selectChartRoleByChartId" resultType="com.lz.modules.flow.model.FlowChartRoleDto" >
        SELECT crole.id as id, crole.chart_id as chart_id, crole.role_id as role_id, role.name as role_name, crole.type as type FROM lz_flow_chart_role crole left join lz_record_role role on role.id=crole.role_id where crole.chart_id=#{id}
    </select>

    <select id="selectFlowChartDtoByFlowManagerId" resultType="com.lz.modules.flow.model.FlowChartDto" >
        select * from lz_flow_chart  where process_id=#{id} and is_delete = 0 order by step_index asc
    </select>

    <select id="selectFlowChartsByGroupId" resultType="com.lz.modules.flow.entity.FlowChart" >
        select * from lz_flow_chart where process_id=(
            select chart.process_id from lz_flow_chart as chart
            left JOIN lz_flow_chart_detail_record as record on record.chart_id=chart.id
            where record.evaluation_group_id=#{groupId} and record.is_delete=0 and chart.is_delete=0 limit 1
        ) and is_delete=0 order by step_index asc
    </select>

    <select id="selectCanSetChartRoleByChartId" resultType="com.lz.modules.flow.model.FlowChartRoleDto" >
        SELECT crole.id as id, crole.chart_id as chart_id, crole.role_id as role_id, role.name as role_name,
        crole.type as type FROM lz_flow_chart_role crole left join lz_record_role role on role.id=crole.role_id
        where crole.chart_id=#{id} and crole.type = 0 and crole.is_delete=0
    </select>

</mapper>

