<?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.jeethink.flow.mapper.FlowMapper">

    <resultMap id="flowMap" type="Flow">
        <id property="flowId" column="flow_id"/>
        <result property="objectId" column="object_id"/>
        <result property="flowType"  column="flow_type"/>
        <result property="stepNum" column="step_num"/>
        <result property="flowStatus" column="flow_status"/>
        <result property="createByName" column="create_by_name"/>
        <result property="createTime" column="create_time"/>
        <result property="updateByName" column="update_by_name"/>
        <result property="updateTime" column="update_time"/>
    </resultMap>
    <insert id="insertFlow" parameterType="Flow">
        insert into flow (flow_id, object_id, flow_type, step_num, create_by, create_time, update_by, update_time)
        VALUES (#{flowId}, #{objectId}, #{flowType}, #{stepNum}, #{createBy}, now(), #{updateBy}, now())
    </insert>
    <insert id="insertRecord" parameterType="FlowRecord">
        insert into flow_record (record_id, flow_id, step_num, status, remark, create_by, create_time)
        values (#{recordId}, #{flowId}, #{stepNum}, #{status}, #{remark}, #{createBy}, now())
    </insert>
    <update id="updateFlow" parameterType="Flow">
        update flow set step_num = #{stepNum}, update_by = #{updateBy}, update_time = now()
    </update>
    <update id="changeStatusByObjectIdAndFlowType" parameterType="Flow">
        update flow set flow_status = #{flowStatus}, update_by = #{updateBy}, update_time = now()
        where flow_status != '2' and flow_status != '3' and object_id = #{objectId} and flow_type = #{flowType}
    </update>
    <select id="selectFlowByObjectIdAndFlowType" resultMap="flowMap">
        select f.flow_id, f.object_id, f.flow_type, f.step_num, f.flow_status,
               u1.nick_name as create_by_name, f.create_time, u2.nick_name as update_by_name, f.update_time
        from flow f
        left join sys_user u1 on f.create_by = u1.user_name
        left join sys_user u2 on f.update_by = u2.user_name
        where flow_status != '2' and flow_status != '3' and object_id = #{objectId} and flow_type = #{flowType}
    </select>
    <resultMap id="recordMap" type="FlowRecord">
        <id property="recordId" column="record_id"/>
        <result property="stepNum" column="step_num"/>
        <result property="status" column="status"/>
        <result property="remark" column="remark"/>
        <result property="createByName" column="create_by_name"/>
    </resultMap>
    <select id="selectRecordByObjectIdAndFlowType" resultMap="recordMap">
        select r.record_id, r.step_num, r.status, r.remark, u.nick_name as create_by_name, r.create_time
        from flow_record r
        left join sys_user u on r.create_by = u.user_name
        left join flow f on r.flow_id = f.flow_id
        where f.flow_status != '2' and f.flow_status != '3' and f.object_id = #{objectId} and f.flow_type = #{flowType}
    </select>
    <resultMap id="stepMap" type="FlowStep">
        <id property="stepId" column="step_id"/>
        <result property="flowType" column="flow_type"/>
        <result property="stepName" column="step_name"/>
        <result property="stepNum" column="step_num"/>
    </resultMap>
    <select id="selectStepsByFlowType" resultMap="stepMap">
        select step_id, flow_type, step_name, step_num
        from flow_step where flow_type = #{0}
        order by step_num
    </select>
</mapper>