<?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.ximai.mes.pro.mapper.ProRouteProcessMapper">

    <resultMap type="ProRouteProcess" id="ProRouteProcessResult">
        <result property="recordId" column="record_id"/>
        <result property="routeId" column="route_id"/>
        <result property="processId" column="process_id"/>
        <result property="processCode" column="process_code"/>
        <result property="processName" column="process_name"/>
        <result property="orderNum" column="order_num"/>
        <result property="nextProcessId" column="next_process_id"/>
        <result property="nextProcessCode" column="next_process_code"/>
        <result property="nextProcessName" column="next_process_name"/>
        <result property="linkType" column="link_type"/>
        <result property="defaultPreTime" column="default_pre_time"/>
        <result property="defaultSufTime" column="default_suf_time"/>
        <result property="colorCode" column="color_code"/>
        <result property="keyFlag" column="key_flag"/>
        <result property="isCheck" column="is_check"/>
        <result property="remark" column="remark"/>
        <result property="workstationId" column="workstation_id"/>
        <result property="stdWorkingTime" column="std_working_time"/>
        <result property="workstationName" column="workstation_name"/>
        <result property="createBy" column="create_by"/>
        <result property="createTime" column="create_time"/>
        <result property="updateBy" column="update_by"/>
        <result property="updateTime" column="update_time"/>
    </resultMap>

    <sql id="selectProRouteProcessVo">
        select prp.record_id,
               prp.route_id,
               prp.process_id,
               prp.workstation_id,
               prp.std_working_time,
               prp.process_code,
               prp.process_name,
               prp.order_num,
               prp.next_process_id,
               prp.next_process_code,
               prp.next_process_name,
               prp.link_type,
               prp.default_pre_time,
               prp.default_suf_time,
               prp.color_code,
               prp.key_flag,
               prp.is_check,
               prp.remark,
               prp.create_by,
               prp.create_time,
               prp.update_by,
               prp.update_time,
               mw.workstation_name
        from pro_route_process prp
                 left join md_workstation mw on mw.workstation_id = prp.workstation_id
    </sql>


    <select id="selectProRouteProcessList" parameterType="ProRouteProcess" resultMap="ProRouteProcessResult">
        <include refid="selectProRouteProcessVo"/>
        <where>
            <if test="routeId != null ">and prp.route_id = #{routeId}</if>
            <if test="processId != null ">and prp.process_id = #{processId}</if>
            <if test="processCode != null  and processCode != ''">and prp.process_code = #{processCode}</if>
            <if test="processName != null  and processName != ''">and prp.process_name like concat('%', #{processName},
                '%')
            </if>
            <if test="orderNum != null ">and prp.order_num = #{orderNum}</if>
            <if test="nextProcessId != null ">and prp.next_process_id = #{nextProcessId}</if>
            <if test="nextProcessCode != null  and nextProcessCode != ''">and prp.next_process_code = #{nextProcessCode}
            </if>
            <if test="nextProcessName != null  and nextProcessName != ''">and prp.next_process_name like concat('%',
                #{nextProcessName}, '%')
            </if>
            <if test="linkType != null  and linkType != ''">and prp.link_type = #{linkType}</if>
            <if test="defaultPreTime != null ">and prp.default_pre_time = #{defaultPreTime}</if>
            <if test="defaultSufTime != null ">and prp.default_suf_time = #{defaultSufTime}</if>
            <if test="colorCode != null  and colorCode != ''">and prp.color_code = #{colorCode}</if>
            <if test="keyFlag !=null and keyFlag !=''">and prp.key_flag = #{keyFlag}</if>
            <if test="isCheck !=null and isCheck !=''">and prp.is_check = #{isCheck}</if>
            <if test="workstationId !=null and workstationId !=''">and prp.workstation_id = #{workstationId}</if>
            <if test="stdWorkingTime !=null and stdWorkingTime !=''">and prp.std_working_time = #{stdWorkingTime}</if>
        </where>
        order by prp.order_num asc
    </select>

    <select id="selectProRouteProcessByRecordId" parameterType="Long" resultMap="ProRouteProcessResult">
        <include refid="selectProRouteProcessVo"/>
        where prp.record_id = #{recordId}
    </select>

    <select id="checkOrderNumExists" parameterType="ProRouteProcess" resultMap="ProRouteProcessResult">
        <include refid="selectProRouteProcessVo"/>
        where prp.route_id = #{routeId} and prp.order_num = #{orderNum} limit 1
    </select>

    <select id="checkProcessExists" parameterType="ProRouteProcess" resultMap="ProRouteProcessResult">
        <include refid="selectProRouteProcessVo"/>
        where prp.route_id = #{routeId} and prp.process_id = #{processId} limit 1
    </select>

    <select id="checkUpdateFlagUnique" parameterType="ProRouteProcess" resultMap="ProRouteProcessResult">
        <include refid="selectProRouteProcessVo"/>
        where prp.route_id = #{routeId} and prp.key_flag = 'Y' limit 1
    </select>

    <select id="findPreProcess" parameterType="ProRouteProcess" resultMap="ProRouteProcessResult">
        <include refid="selectProRouteProcessVo"/>
        where prp.route_id = #{routeId}
        <choose>
            <when test="orderNum != null ">and prp.order_num &lt; #{orderNum}</when>
            <otherwise>
                AND prp.order_num &lt; (
                SELECT order_num
                FROM pro_route_process
                WHERE route_id = #{routeId} limit 1
                )
            </otherwise>
        </choose>
        ORDER BY prp.order_num DESC LIMIT 1
    </select>

    <select id="findNextProcess" parameterType="ProRouteProcess" resultMap="ProRouteProcessResult">
        <include refid="selectProRouteProcessVo"/>
        where prp.route_id = #{routeId}
        <choose>
            <when test="orderNum != null ">and prp.order_num &gt; #{orderNum}</when>
            <otherwise>
                AND prp.order_num &gt; (
                SELECT order_num
                FROM pro_route_process
                WHERE route_id = #{routeId} limit 1
                )
            </otherwise>
        </choose>
        ORDER BY prp.order_num ASC LIMIT 1
    </select>
    <select id="selectProRouteProcessByRouteIdAndProcessId"
            parameterType="Long" resultMap="ProRouteProcessResult">
        <include refid="selectProRouteProcessVo"/>
        <where>
        <if test="routeId != null ">and prp.route_id = #{routeId}</if>
        <if test="processId != null ">and prp.process_id = #{processId}</if>
        </where>
    </select>

    <insert id="insertProRouteProcess" parameterType="ProRouteProcess" useGeneratedKeys="true" keyProperty="recordId">
        insert into pro_route_process
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="routeId != null">route_id,</if>
            <if test="processId != null">process_id,</if>
            <if test="processCode != null">process_code,</if>
            <if test="processName != null">process_name,</if>
            <if test="orderNum != null">order_num,</if>
            <if test="nextProcessId != null">next_process_id,</if>
            <if test="nextProcessCode != null">next_process_code,</if>
            <if test="nextProcessName != null">next_process_name,</if>
            <if test="linkType != null">link_type,</if>
            <if test="defaultPreTime != null">default_pre_time,</if>
            <if test="defaultSufTime != null">default_suf_time,</if>
            <if test="colorCode != null">color_code,</if>
            <if test="keyFlag !=null">key_flag,</if>
            <if test="isCheck !=null">is_check,</if>
            <if test="remark != null">remark,</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="workstationId != null">workstation_id,</if>
            <if test="stdWorkingTime != null">std_working_time,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="routeId != null">#{routeId},</if>
            <if test="processId != null">#{processId},</if>
            <if test="processCode != null">#{processCode},</if>
            <if test="processName != null">#{processName},</if>
            <if test="orderNum != null">#{orderNum},</if>
            <if test="nextProcessId != null">#{nextProcessId},</if>
            <if test="nextProcessCode != null">#{nextProcessCode},</if>
            <if test="nextProcessName != null">#{nextProcessName},</if>
            <if test="linkType != null">#{linkType},</if>
            <if test="defaultPreTime != null">#{defaultPreTime},</if>
            <if test="defaultSufTime != null">#{defaultSufTime},</if>
            <if test="colorCode != null">#{colorCode},</if>
            <if test="keyFlag !=null">#{keyFlag},</if>
            <if test="isCheck !=null">#{isCheck},</if>
            <if test="remark != null">#{remark},</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="workstationId != null">#{workstationId},</if>
            <if test="stdWorkingTime != null">#{stdWorkingTime},</if>
        </trim>
    </insert>

    <update id="updateProRouteProcess" parameterType="ProRouteProcess">
        update pro_route_process
        <trim prefix="SET" suffixOverrides=",">
            <if test="routeId != null">route_id = #{routeId},</if>
            <if test="processId != null">process_id = #{processId},</if>
            <if test="processCode != null">process_code = #{processCode},</if>
            <if test="processName != null">process_name = #{processName},</if>
            <if test="orderNum != null">order_num = #{orderNum},</if>
            <if test="nextProcessId != null">next_process_id = #{nextProcessId},</if>
            <if test="nextProcessCode != null">next_process_code = #{nextProcessCode},</if>
            <if test="nextProcessName != null">next_process_name = #{nextProcessName},</if>
            <if test="linkType != null">link_type = #{linkType},</if>
            <if test="defaultPreTime != null">default_pre_time = #{defaultPreTime},</if>
            <if test="defaultSufTime != null">default_suf_time = #{defaultSufTime},</if>
            <if test="keyFlag !=null">key_flag = #{keyFlag},</if>
            <if test="isCheck !=null">is_check = #{isCheck},</if>
            <if test="colorCode != null">color_code = #{colorCode},</if>
            <if test="remark != null">remark = #{remark},</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="workstationId != null">workstation_id = #{workstationId},</if>
            <if test="stdWorkingTime != null">std_working_time = #{stdWorkingTime},</if>
        </trim>
        where record_id = #{recordId}
    </update>

    <delete id="deleteProRouteProcessByRecordId" parameterType="Long">
        delete
        from pro_route_process
        where record_id = #{recordId}
    </delete>

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

    <delete id="deleteByRouteId" parameterType="Long">
        delete
        from pro_route_process
        where route_id = #{routeId}
    </delete>

</mapper>
