<?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.foreverwin.mesnac.meapi.mapper.RouterSubstepMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.foreverwin.mesnac.meapi.model.RouterSubstep">
        <result column="HANDLE" property="handle" />
        <result column="ROUTER_STEP_BO" property="routerStepBo" />
        <result column="SUBSTEP" property="substep" />
        <result column="SUBSTEP_ID" property="substepId" />
        <result column="SUBSTEP_GROUP" property="substepGroup" />
        <result column="SUBSTEP_TYPE" property="substepType" />
        <result column="TIMER" property="timer" />
        <result column="SEQUENCE" property="sequence" />
        <result column="DESCRIPTION" property="description" />
        <result column="LONG_DESCRIPTION" property="longDescription" />
    </resultMap>

    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        HANDLE, ROUTER_STEP_BO, SUBSTEP, SUBSTEP_ID, SUBSTEP_GROUP, SUBSTEP_TYPE, TIMER, SEQUENCE, DESCRIPTION, LONG_DESCRIPTION
    </sql>

    <!-- BaseMapper标准查询/修改/删除 -->

    <select id="selectByMap" resultMap="BaseResultMap">
        SELECT <include refid="Base_Column_List"></include>
        FROM ROUTER_SUBSTEP
        <if test="cm!=null and !cm.isEmpty">
            <where>
                <foreach collection="cm.keys" item="k" separator="AND">
                    <if test="cm[k] != null">
                        ${k} = #{cm[${k}]}
                    </if>
                </foreach>
            </where>
        </if>
    </select>


    <select id="selectOne" resultMap="BaseResultMap">
        SELECT <include refid="Base_Column_List"></include> FROM ROUTER_SUBSTEP
        <where>
            <if test="ew.entity.handle!=null">
              HANDLE=#{ew.handle}
            </if>
            <if test="ew.entity.routerStepBo!=null"> AND ROUTER_STEP_BO=#{ew.entity.routerStepBo}</if>
            <if test="ew.entity.substep!=null"> AND SUBSTEP=#{ew.entity.substep}</if>
            <if test="ew.entity.substepId!=null"> AND SUBSTEP_ID=#{ew.entity.substepId}</if>
            <if test="ew.entity.substepGroup!=null"> AND SUBSTEP_GROUP=#{ew.entity.substepGroup}</if>
            <if test="ew.entity.substepType!=null"> AND SUBSTEP_TYPE=#{ew.entity.substepType}</if>
            <if test="ew.entity.timer!=null"> AND TIMER=#{ew.entity.timer}</if>
            <if test="ew.entity.sequence!=null"> AND SEQUENCE=#{ew.entity.sequence}</if>
            <if test="ew.entity.description!=null"> AND DESCRIPTION=#{ew.entity.description}</if>
            <if test="ew.entity.longDescription!=null"> AND LONG_DESCRIPTION=#{ew.entity.longDescription}</if>
        </where>
    </select>

    <select id="selectCount" resultType="Integer">
        SELECT COUNT(1) FROM ROUTER_SUBSTEP
        <where>
            <if test="ew!=null">
                <if test="ew.entity!=null">
                <if test="ew.entity.handle!=null">
                    HANDLE=#{ew.entity.handle}
                </if>
                <if test="ew.entity.routerStepBo!=null"> AND ROUTER_STEP_BO=#{ew.entity.routerStepBo}</if>
                <if test="ew.entity.substep!=null"> AND SUBSTEP=#{ew.entity.substep}</if>
                <if test="ew.entity.substepId!=null"> AND SUBSTEP_ID=#{ew.entity.substepId}</if>
                <if test="ew.entity.substepGroup!=null"> AND SUBSTEP_GROUP=#{ew.entity.substepGroup}</if>
                <if test="ew.entity.substepType!=null"> AND SUBSTEP_TYPE=#{ew.entity.substepType}</if>
                <if test="ew.entity.timer!=null"> AND TIMER=#{ew.entity.timer}</if>
                <if test="ew.entity.sequence!=null"> AND SEQUENCE=#{ew.entity.sequence}</if>
                <if test="ew.entity.description!=null"> AND DESCRIPTION=#{ew.entity.description}</if>
                <if test="ew.entity.longDescription!=null"> AND LONG_DESCRIPTION=#{ew.entity.longDescription}</if>
                </if>
                <if test="ew!=null and ew.sqlSegment!=null and ew.nonEmptyOfWhere">
                    ${ew.sqlSegment}
                </if>
            </if>
        </where>
        <if test="ew!=null and ew.sqlSegment!=null and ew.emptyOfWhere">
            ${ew.sqlSegment}
        </if>
    </select>

    <select id="selectList" resultMap="BaseResultMap">
        SELECT <choose><when test="ew != null and ew.sqlSelect != null">${ew.sqlSelect}</when><otherwise><include refid="Base_Column_List"></include></otherwise></choose> FROM ROUTER_SUBSTEP
        <where>
            <if test="ew!=null">
                <if test="ew.entity!=null">
                    <if test="ew.entity.handle!=null">
                        HANDLE=#{ew.entity.handle}
                    </if>
                    <if test="ew.entity.routerStepBo!=null"> AND ROUTER_STEP_BO=#{ew.entity.routerStepBo}</if>
                    <if test="ew.entity.substep!=null"> AND SUBSTEP=#{ew.entity.substep}</if>
                    <if test="ew.entity.substepId!=null"> AND SUBSTEP_ID=#{ew.entity.substepId}</if>
                    <if test="ew.entity.substepGroup!=null"> AND SUBSTEP_GROUP=#{ew.entity.substepGroup}</if>
                    <if test="ew.entity.substepType!=null"> AND SUBSTEP_TYPE=#{ew.entity.substepType}</if>
                    <if test="ew.entity.timer!=null"> AND TIMER=#{ew.entity.timer}</if>
                    <if test="ew.entity.sequence!=null"> AND SEQUENCE=#{ew.entity.sequence}</if>
                    <if test="ew.entity.description!=null"> AND DESCRIPTION=#{ew.entity.description}</if>
                    <if test="ew.entity.longDescription!=null"> AND LONG_DESCRIPTION=#{ew.entity.longDescription}</if>
                </if>
                <if test="ew!=null and ew.sqlSegment!=null and ew.nonEmptyOfWhere">
                    ${ew.sqlSegment}
                </if>
            </if>
        </where>
        <if test="ew!=null and ew.sqlSegment!=null and ew.emptyOfWhere">
            ${ew.sqlSegment}
        </if>
    </select>

    <select id="selectMaps" resultType="HashMap">
        SELECT <choose><when test="ew != null and ew.sqlSelect != null">${ew.sqlSelect}</when><otherwise><include refid="Base_Column_List"></include></otherwise></choose> FROM ROUTER_SUBSTEP
        <where>
            <if test="ew!=null">
                <if test="ew.entity!=null">
                    <if test="ew.entity.handle!=null">
                        HANDLE=#{ew.entity.handle}
                    </if>
                    <if test="ew.entity.routerStepBo!=null"> AND ROUTER_STEP_BO=#{ew.entity.routerStepBo}</if>
                    <if test="ew.entity.substep!=null"> AND SUBSTEP=#{ew.entity.substep}</if>
                    <if test="ew.entity.substepId!=null"> AND SUBSTEP_ID=#{ew.entity.substepId}</if>
                    <if test="ew.entity.substepGroup!=null"> AND SUBSTEP_GROUP=#{ew.entity.substepGroup}</if>
                    <if test="ew.entity.substepType!=null"> AND SUBSTEP_TYPE=#{ew.entity.substepType}</if>
                    <if test="ew.entity.timer!=null"> AND TIMER=#{ew.entity.timer}</if>
                    <if test="ew.entity.sequence!=null"> AND SEQUENCE=#{ew.entity.sequence}</if>
                    <if test="ew.entity.description!=null"> AND DESCRIPTION=#{ew.entity.description}</if>
                    <if test="ew.entity.longDescription!=null"> AND LONG_DESCRIPTION=#{ew.entity.longDescription}</if>
                </if>
                <if test="ew!=null and ew.sqlSegment!=null and ew.nonEmptyOfWhere">
                    ${ew.sqlSegment}
                </if>
            </if>
        </where>
        <if test="ew!=null and ew.sqlSegment!=null and ew.emptyOfWhere">
            ${ew.sqlSegment}
        </if>
    </select>

    <select id="selectObjs" resultType="Object">
        SELECT <choose><when test="ew != null and ew.sqlSelect != null">${ew.sqlSelect}</when><otherwise><include refid="Base_Column_List"></include></otherwise></choose> FROM ROUTER_SUBSTEP
        <where>
            <if test="ew!=null">
                <if test="ew.entity!=null">
                    <if test="ew.entity.handle!=null">
                        HANDLE=#{ew.entity.handle}
                    </if>
                    <if test="ew.entity.routerStepBo!=null"> AND ROUTER_STEP_BO=#{ew.entity.routerStepBo}</if>
                    <if test="ew.entity.substep!=null"> AND SUBSTEP=#{ew.entity.substep}</if>
                    <if test="ew.entity.substepId!=null"> AND SUBSTEP_ID=#{ew.entity.substepId}</if>
                    <if test="ew.entity.substepGroup!=null"> AND SUBSTEP_GROUP=#{ew.entity.substepGroup}</if>
                    <if test="ew.entity.substepType!=null"> AND SUBSTEP_TYPE=#{ew.entity.substepType}</if>
                    <if test="ew.entity.timer!=null"> AND TIMER=#{ew.entity.timer}</if>
                    <if test="ew.entity.sequence!=null"> AND SEQUENCE=#{ew.entity.sequence}</if>
                    <if test="ew.entity.description!=null"> AND DESCRIPTION=#{ew.entity.description}</if>
                    <if test="ew.entity.longDescription!=null"> AND LONG_DESCRIPTION=#{ew.entity.longDescription}</if>
                </if>
                <if test="ew!=null and ew.sqlSegment!=null and ew.nonEmptyOfWhere">
                    ${ew.sqlSegment}
                </if>
            </if>
        </where>
        <if test="ew!=null and ew.sqlSegment!=null and ew.emptyOfWhere">
            ${ew.sqlSegment}
        </if>
    </select>

    <select id="selectPage" resultMap="BaseResultMap">
        SELECT <choose><when test="ew != null and ew.sqlSelect != null">${ew.sqlSelect}</when><otherwise><include refid="Base_Column_List"></include></otherwise></choose> FROM ROUTER_SUBSTEP
        <where>
            <if test="ew!=null">
                <if test="ew.entity!=null">
                    <if test="ew.entity.handle!=null">
                        HANDLE=#{ew.entity.handle}
                    </if>
                    <if test="ew.entity.routerStepBo!=null"> AND ROUTER_STEP_BO=#{ew.entity.routerStepBo}</if>
                    <if test="ew.entity.substep!=null"> AND SUBSTEP=#{ew.entity.substep}</if>
                    <if test="ew.entity.substepId!=null"> AND SUBSTEP_ID=#{ew.entity.substepId}</if>
                    <if test="ew.entity.substepGroup!=null"> AND SUBSTEP_GROUP=#{ew.entity.substepGroup}</if>
                    <if test="ew.entity.substepType!=null"> AND SUBSTEP_TYPE=#{ew.entity.substepType}</if>
                    <if test="ew.entity.timer!=null"> AND TIMER=#{ew.entity.timer}</if>
                    <if test="ew.entity.sequence!=null"> AND SEQUENCE=#{ew.entity.sequence}</if>
                    <if test="ew.entity.description!=null"> AND DESCRIPTION=#{ew.entity.description}</if>
                    <if test="ew.entity.longDescription!=null"> AND LONG_DESCRIPTION=#{ew.entity.longDescription}</if>
                </if>
                <if test="ew!=null and ew.sqlSegment!=null and ew.nonEmptyOfWhere">
                    ${ew.sqlSegment}
                </if>
            </if>
        </where>
        <if test="ew!=null and ew.sqlSegment!=null and ew.emptyOfWhere">
            ${ew.sqlSegment}
        </if>
    </select>

    <select id="selectMapsPage" resultType="HashMap">
        SELECT <choose><when test="ew != null and ew.sqlSelect != null">${ew.sqlSelect}</when><otherwise><include refid="Base_Column_List"></include></otherwise></choose> FROM ROUTER_SUBSTEP
        <where>
            <if test="ew!=null">
                <if test="ew.entity!=null">
                    <if test="ew.entity.handle!=null">
                        HANDLE=#{ew.entity.handle}
                    </if>
                    <if test="ew.entity.routerStepBo!=null"> AND ROUTER_STEP_BO=#{ew.entity.routerStepBo}</if>
                    <if test="ew.entity.substep!=null"> AND SUBSTEP=#{ew.entity.substep}</if>
                    <if test="ew.entity.substepId!=null"> AND SUBSTEP_ID=#{ew.entity.substepId}</if>
                    <if test="ew.entity.substepGroup!=null"> AND SUBSTEP_GROUP=#{ew.entity.substepGroup}</if>
                    <if test="ew.entity.substepType!=null"> AND SUBSTEP_TYPE=#{ew.entity.substepType}</if>
                    <if test="ew.entity.timer!=null"> AND TIMER=#{ew.entity.timer}</if>
                    <if test="ew.entity.sequence!=null"> AND SEQUENCE=#{ew.entity.sequence}</if>
                    <if test="ew.entity.description!=null"> AND DESCRIPTION=#{ew.entity.description}</if>
                    <if test="ew.entity.longDescription!=null"> AND LONG_DESCRIPTION=#{ew.entity.longDescription}</if>
                </if>
                <if test="ew!=null and ew.sqlSegment!=null and ew.nonEmptyOfWhere">
                    ${ew.sqlSegment}
                </if>
            </if>
        </where>
        <if test="ew!=null and ew.sqlSegment!=null and ew.emptyOfWhere">
            ${ew.sqlSegment}
        </if>
    </select>

    <insert id="insert" parameterType="com.foreverwin.mesnac.meapi.model.RouterSubstep">
        INSERT INTO ROUTER_SUBSTEP
        <trim prefix="(" suffix=")" suffixOverrides=",">
            HANDLE,
            <if test="routerStepBo!=null">ROUTER_STEP_BO,</if>
            <if test="substep!=null">SUBSTEP,</if>
            <if test="substepId!=null">SUBSTEP_ID,</if>
            <if test="substepGroup!=null">SUBSTEP_GROUP,</if>
            <if test="substepType!=null">SUBSTEP_TYPE,</if>
            <if test="timer!=null">TIMER,</if>
            <if test="sequence!=null">SEQUENCE,</if>
            <if test="description!=null">DESCRIPTION,</if>
            <if test="longDescription!=null">LONG_DESCRIPTION,</if>
        </trim> VALUES
        <trim prefix="(" suffix=")" suffixOverrides=",">
            #{handle},
            <if test="routerStepBo!=null">#{routerStepBo},</if>
            <if test="substep!=null">#{substep},</if>
            <if test="substepId!=null">#{substepId},</if>
            <if test="substepGroup!=null">#{substepGroup},</if>
            <if test="substepType!=null">#{substepType},</if>
            <if test="timer!=null">#{timer},</if>
            <if test="sequence!=null">#{sequence},</if>
            <if test="description!=null">#{description},</if>
            <if test="longDescription!=null">#{longDescription},</if>
        </trim>
    </insert>

    <insert id="insertAllColumn" parameterType="com.foreverwin.mesnac.meapi.model.RouterSubstep">
        INSERT INTO ROUTER_SUBSTEP
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <include refid="Base_Column_List"></include>
        </trim> VALUES
        <trim prefix="(" suffix=")" suffixOverrides=",">
            #{handle},
            #{routerStepBo},
            #{substep},
            #{substepId},
            #{substepGroup},
            #{substepType},
            #{timer},
            #{sequence},
            #{description},
            #{longDescription},
        </trim>
    </insert>






    <update id="update">
        UPDATE ROUTER_SUBSTEP <trim prefix="SET" suffixOverrides=",">
        <if test="et.handle!=null">HANDLE=#{et.handle},</if>
        <if test="et.routerStepBo!=null">ROUTER_STEP_BO=#{et.routerStepBo},</if>
        <if test="et.substep!=null">SUBSTEP=#{et.substep},</if>
        <if test="et.substepId!=null">SUBSTEP_ID=#{et.substepId},</if>
        <if test="et.substepGroup!=null">SUBSTEP_GROUP=#{et.substepGroup},</if>
        <if test="et.substepType!=null">SUBSTEP_TYPE=#{et.substepType},</if>
        <if test="et.timer!=null">TIMER=#{et.timer},</if>
        <if test="et.sequence!=null">SEQUENCE=#{et.sequence},</if>
        <if test="et.description!=null">DESCRIPTION=#{et.description},</if>
        <if test="et.longDescription!=null">LONG_DESCRIPTION=#{et.longDescription},</if>
    </trim>
        <where>
            <if test="ew!=null">
                <if test="ew.entity!=null">
                    HANDLE=#{ew.entity.handle}
                    <if test="ew.entity.routerStepBo!=null"> AND ROUTER_STEP_BO=#{ew.entity.routerStepBo}</if>
                    <if test="ew.entity.substep!=null"> AND SUBSTEP=#{ew.entity.substep}</if>
                    <if test="ew.entity.substepId!=null"> AND SUBSTEP_ID=#{ew.entity.substepId}</if>
                    <if test="ew.entity.substepGroup!=null"> AND SUBSTEP_GROUP=#{ew.entity.substepGroup}</if>
                    <if test="ew.entity.substepType!=null"> AND SUBSTEP_TYPE=#{ew.entity.substepType}</if>
                    <if test="ew.entity.timer!=null"> AND TIMER=#{ew.entity.timer}</if>
                    <if test="ew.entity.sequence!=null"> AND SEQUENCE=#{ew.entity.sequence}</if>
                    <if test="ew.entity.description!=null"> AND DESCRIPTION=#{ew.entity.description}</if>
                    <if test="ew.entity.longDescription!=null"> AND LONG_DESCRIPTION=#{ew.entity.longDescription}</if>
                </if>
                <if test="ew!=null and ew.sqlSegment!=null and ew.nonEmptyOfWhere">
                    ${ew.sqlSegment}
                </if>
            </if>
        </where>
        <if test="ew!=null and ew.sqlSegment!=null and ew.emptyOfWhere">
            ${ew.sqlSegment}
        </if>
    </update>


    <delete id="deleteByMap">
        DELETE FROM ROUTER_SUBSTEP
        <if test="cm!=null and !cm.isEmpty">
            <where>
                <foreach collection="cm.keys" item="k" separator="AND">
                    <if test="cm[k] != null">
                        ${k} = #{cm[${k}]}
                    </if>
                </foreach>
            </where>
        </if>
    </delete>

    <delete id="delete">
        DELETE FROM ROUTER_SUBSTEP
        <where>
            <if test="ew!=null">
                <if test="ew.entity!=null">
                    <if test="ew.entity.handle!=null">
                        HANDLE=#{ew.entity.handle}
                    </if>
                    <if test="ew.entity.routerStepBo!=null"> AND ROUTER_STEP_BO=#{ew.entity.routerStepBo}</if>
                    <if test="ew.entity.substep!=null"> AND SUBSTEP=#{ew.entity.substep}</if>
                    <if test="ew.entity.substepId!=null"> AND SUBSTEP_ID=#{ew.entity.substepId}</if>
                    <if test="ew.entity.substepGroup!=null"> AND SUBSTEP_GROUP=#{ew.entity.substepGroup}</if>
                    <if test="ew.entity.substepType!=null"> AND SUBSTEP_TYPE=#{ew.entity.substepType}</if>
                    <if test="ew.entity.timer!=null"> AND TIMER=#{ew.entity.timer}</if>
                    <if test="ew.entity.sequence!=null"> AND SEQUENCE=#{ew.entity.sequence}</if>
                    <if test="ew.entity.description!=null"> AND DESCRIPTION=#{ew.entity.description}</if>
                    <if test="ew.entity.longDescription!=null"> AND LONG_DESCRIPTION=#{ew.entity.longDescription}</if>
                </if>
                <if test="ew!=null and ew.sqlSegment!=null and ew.nonEmptyOfWhere">
                    ${ew.sqlSegment}
                </if>
            </if>
        </where>
        <if test="ew!=null and ew.sqlSegment!=null and ew.emptyOfWhere">
            ${ew.sqlSegment}
        </if>
    </delete>

    <!-- BaseMapper标准查询/修改/删除 -->

    <select id="findRoutetStepBoByOperation" resultMap="BaseResultMap">
        SELECT DISTINCT RS2.SUBSTEP ,RS2.DESCRIPTION
        FROM OPERATION O
                 LEFT JOIN OPERATION_T OT ON O.HANDLE = OT.OPERATION_BO AND OT.LOCALE=#{language}
                 INNER JOIN ROUTER_OPERATION RO ON RO.OPERATION_BO = (SUBSTR(O.HANDLE,1,(SELECT INSTR(O.HANDLE , ',', -1, 1)
                                                                                         FROM DUAL)-1) || ',#' )
                 INNER JOIN ROUTER_STEP RS ON RS.HANDLE = RO.ROUTER_STEP_BO
                 INNER JOIN ROUTER_SUBSTEP RS2 ON RS2.ROUTER_STEP_BO  = RS.HANDLE
                 INNER JOIN ROUTER R ON R.HANDLE  = RS.ROUTER_BO
                 INNER JOIN SHOP_ORDER SO ON SO.PLANNED_ROUTER_BO = R.HANDLE
                 INNER JOIN ITEM I ON I.HANDLE = SO.PLANNED_ITEM_BO
        <where>
            O.SITE = #{site}
            <if test="operation!=null and !operation.isEmpty">
                AND O.OPERATION = #{operation}
            </if>
            <if test="item!=null and !item.isEmpty">
                AND I.ITEM = #{item}
            </if>
            <if test="globalQuery!=null and !globalQuery.isEmpty">
                AND (
                rs2.SUBSTEP LIKE '%${globalQuery}%'
                OR rs2.DESCRIPTION LIKE '%${globalQuery}%'
                )
            </if>
        </where>
        ORDER BY rs2.SUBSTEP
    </select>
</mapper>
