<?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.dispatch.mapper.ShopOrderReleaseMapper">

    <resultMap id="BaseResultMap" type="com.foreverwin.mesnac.dispatch.model.ShopOrderRelease">
        <id column="HANDLE" property="handle" />
        <result column="SITE" property="site" />
        <result column="SHOP_ORDER" property="shopOrder" />
        <result column="SHOP_ORDER_BO" property="shopOrderBo" />
        <result column="STATUS" property="status" />
        <result column="RESOURCE_TYPE" property="resourceType" />
        <result column="WORK_CENTER" property="workCenter" />
        <result column="SHOP_ORDER_TYPE" property="shopOrderType" />
        <result column="WORK_ORDER" property="workOrder" />
        <result column="COMMENTS" property="comments" />
        <result column="IS_MAJOR" property="isMajor" />
        <result column="ITEM_DESCRIPTION" property="itemDescription" />
        <result column="QTY_TO_BUILD" property="qtyToBuild" />
        <result column="PLANNED_START_DATE" property="plannedStartDate" />
        <result column="PLANNED_COMP_DATE" property="plannedCompDate" />
        <result column="DISPATCH_COMPLETE_DATE" property="dispatchCompleteDate" />
    </resultMap>

    <select id="findShopOrderList" resultMap="BaseResultMap">
        SELECT MVP.*
        FROM (
            SELECT  SO.HANDLE, SO.HANDLE SHOP_ORDER_BO, SO.SITE, SO.SHOP_ORDER, CF1.VALUE WORK_ORDER, CF2.VALUE SHOP_ORDER_TYPE, CF3.VALUE COMMENTS,
                    IM.ITEM, IMT.DESCRIPTION ITEM_DESCRIPTION, SO.QTY_TO_BUILD, SO.PLANNED_START_DATE, SO.PLANNED_COMP_DATE, MAX(ZSD.PLANNED_COMP_DATE) DISPATCH_COMPLETE_DATE,
                    SUBSTR(SPLIT(OP.RESOURCE_TYPE_BO, 2),0,INSTR(SPLIT(OP.RESOURCE_TYPE_BO, 2), '_')-1) WORK_CENTER,
                    CASE WHEN ST.STATUS = '502' OR ST.STATUS = '503' OR ST.STATUS = '504' THEN ST.STATUS
                         WHEN ST.STATUS = '501' AND SO.QTY_RELEASED &lt;= 0 THEN N'501'
                         WHEN MAX(SS.SHOP_ORDER_BO) IS NULL THEN N'505' ELSE N'506' END STATUS,
                    CASE WHEN IM.LOT_SIZE = 1 THEN N'是' ELSE N'否' END IS_MAJOR
            FROM SHOP_ORDER SO
            INNER JOIN ITEM IM ON SO.PLANNED_ITEM_BO = IM.HANDLE
            INNER JOIN STATUS ST ON SO.STATUS_BO = ST.HANDLE AND ST.STATUS_GROUP = 'ORDERS'
            INNER JOIN ROUTER RT ON SO.PLANNED_ROUTER_BO = RT.HANDLE OR (SO.PLANNED_ROUTER_BO = 'RouterBO:'||RT.SITE||','||RT.ROUTER||','||RT.ROUTER_TYPE||',#' AND RT.CURRENT_REVISION = 'true')
            INNER JOIN ROUTER_OPERATION RO ON RT.ENTRY_ROUTER_STEP_BO = RO.ROUTER_STEP_BO
            INNER JOIN OPERATION OP ON SO.SITE = OP.SITE AND SPLIT(RO.OPERATION_BO,2) = OP.OPERATION AND OP.CURRENT_REVISION = 'true'
            LEFT JOIN (
                SELECT SHOP_ORDER_BO
                FROM SFC SC
                INNER JOIN STATUS ST ON SC.STATUS_BO = ST.HANDLE AND ST.STATUS_GROUP = 'SFC'
                WHERE ST.STATUS != '401'
                GROUP BY SHOP_ORDER_BO
            ) SS ON SS.SHOP_ORDER_BO = SO.HANDLE
            LEFT JOIN ITEM_T IMT ON IM.HANDLE = IMT.ITEM_BO AND IMT.LOCALE = 'zh'
            LEFT JOIN CUSTOM_FIELDS CF1 ON CF1.HANDLE = SO.HANDLE AND CF1.ATTRIBUTE = 'WORK_ORDER'
            LEFT JOIN CUSTOM_FIELDS CF2 ON CF2.HANDLE = SO.HANDLE AND CF2.ATTRIBUTE = 'SHOP_ORDER_TYPE'
            LEFT JOIN CUSTOM_FIELDS CF3 ON CF3.HANDLE = SO.HANDLE AND CF3.ATTRIBUTE = 'COMMENTS'
            LEFT JOIN Z_SFC_DISPATCH ZSD ON SO.SITE = ZSD.SITE AND SO.SHOP_ORDER = ZSD.SHOP_ORDER
            WHERE SO.SITE = #{site}
            <if test="shopOrder != null and shopOrder != ''">
                AND SO.SHOP_ORDER = #{shopOrder}
            </if>
            <if test="workOrder != null and workOrder != ''">
                AND CF1.VALUE LIKE  '%'|| #{workOrder} || '%'
            </if>
            <if test="shopOrderType != null and shopOrderType != ''">
                AND CF2.VALUE LIKE  '%'|| #{shopOrderType} || '%'
            </if>
            <if test="item != null and item != ''">
                AND IM.ITEM = #{item}
            </if>
            <if test="startFromDate != null">
                AND SO.PLANNED_START_DATE >= #{startFromDate}
            </if>
            <if test="startToDate != null">
                AND SO.PLANNED_START_DATE &lt;= #{startToDate}
            </if>
            <if test="completeFromDate != null">
                AND SO.PLANNED_COMP_DATE >= #{completeFromDate}
            </if>
            <if test="completeToDate != null">
                AND SO.PLANNED_COMP_DATE &lt;= #{completeToDate}
            </if>
            GROUP BY SO.HANDLE, SO.SITE, SO.SHOP_ORDER, SO.QTY_RELEASED, ST.STATUS, CF1.VALUE, CF2.VALUE, CF3.VALUE,
                     OP.RESOURCE_TYPE_BO, IM.ITEM, IMT.DESCRIPTION, SO.QTY_TO_BUILD, SO.PLANNED_START_DATE, SO.PLANNED_COMP_DATE,
                     SO.PLANNED_WORK_CENTER_BO, IM.LOT_SIZE
        ) MVP
        WHERE 1 = 1
        <if test="status != null and status != ''">
            AND MVP.STATUS = #{status}
        </if>
        <if test="workCenter != null and workCenter != ''">
            AND MVP.WORK_CENTER = #{workCenter}
        </if>
    </select>

    <select id="selectShopOrderRouter" resultType="com.foreverwin.mesnac.dispatch.dto.RouterDTO">
        SELECT RT.HANDLE, RT.HANDLE ROUTER_BO, RT.SITE, RT.ROUTER, RT.DESCRIPTION, RT.REVISION, RT.ENTRY_ROUTER_STEP_BO, RT.STATUS_BO, RS.HANDLE ROUTER_STEP_BO, RS.STEP_ID, OP.OPERATION, OP.HANDLE OPERATION_BO,
               RNS.NEXT_STEP_BO, OT.DESCRIPTION OPERATION_DESCRIPTION, RT.RESOURCE_TYPE, CF1.VALUE PROD_HOURS
        FROM ROUTER RT
        INNER JOIN ROUTER_STEP RS ON RT.HANDLE = RS.ROUTER_BO AND RS.QUEUE_DECISION_TYPE IS NOT NULL
        LEFT JOIN ROUTER_OPERATION RO ON RS.HANDLE = RO.ROUTER_STEP_BO
        LEFT JOIN OPERATION OP ON RO.OPERATION_BO = 'OperationBO:'||OP.SITE||','||OP.OPERATION||',#' AND OP.CURRENT_REVISION = 'true'
        LEFT JOIN RESOURCE_TYPE RT ON OP.RESOURCE_TYPE_BO = RT.HANDLE
        LEFT JOIN ROUTER_NEXT_STEP RNS ON RS.HANDLE = RNS.ROUTER_STEP_BO
        LEFT JOIN OPERATION_T OT ON OP.HANDLE = OT.OPERATION_BO AND OT.LOCALE = 'zh'
        LEFT JOIN CUSTOM_FIELDS CF1 ON CF1.HANDLE = RO.HANDLE AND CF1.ATTRIBUTE = 'PROD_TIME'
        WHERE RT.HANDLE = #{routerBo}
        ORDER BY TO_NUMBER(RS.STEP_ID)
    </select>

    <select id="selectRouterStepGroup" resultType="com.foreverwin.mesnac.dispatch.dto.RouterDTO">
        SELECT RS.HANDLE ROUTER_STEP_BO, RS.STEP_ID, OP.OPERATION, OP.HANDLE OPERATION_BO, RNS.NEXT_STEP_BO, OT.DESCRIPTION OPERATION_DESCRIPTION, RT.RESOURCE_TYPE, CF1.VALUE PROD_HOURS
        FROM ROUTER_STEP_GROUP RSG
        INNER JOIN ROUTER_STEP_GROUP_STEP RSGS ON RSGS.ROUTER_STEP_GROUP_BO = RSG.HANDLE
        INNER JOIN ROUTER_STEP RS ON RS.HANDLE = RSGS.ROUTER_STEP_BO AND RS.QUEUE_DECISION_TYPE IS NOT NULL
        LEFT JOIN ROUTER_OPERATION RO ON RS.HANDLE = RO.ROUTER_STEP_BO
        LEFT JOIN OPERATION OP ON RO.OPERATION_BO = 'OperationBO:'||OP.SITE||','||OP.OPERATION||',#'  AND OP.CURRENT_REVISION = 'true'
        LEFT JOIN RESOURCE_TYPE RT ON OP.RESOURCE_TYPE_BO = RT.HANDLE
        LEFT JOIN ROUTER_NEXT_STEP RNS ON RS.HANDLE = RNS.ROUTER_STEP_BO
        LEFT JOIN OPERATION_T OT ON OP.HANDLE = OT.OPERATION_BO AND OT.LOCALE = 'zh'
        LEFT JOIN CUSTOM_FIELDS CF1 ON CF1.HANDLE = RO.HANDLE AND CF1.ATTRIBUTE = 'PROD_TIME'
        WHERE RSG.ROUTER_STEP_BO = #{routerStepBo}
        ORDER BY TO_NUMBER(RS.STEP_ID)
    </select>
</mapper>