<?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.production.mapper.SfcCrossMapper">

    <resultMap id="StepOperationMap" type="com.foreverwin.mesnac.production.model.StepOperation">
        <result property="operationBo" column="OPERATION_BO" />
        <result property="stepId" column="STEP_ID" />
        <result property="sequence" column="SEQUENCE" javaType="java.math.BigDecimal" />
        <result property="reportingCenterBo" column="REPORTING_CENTER_BO" />
        <result property="operation" column="OPERATION" />
        <result property="description" column="DESCRIPTION" />
        <result property="resourceBo" column="RESOURCE_BO" />
    </resultMap>

    <select id="findBindingBarcode" resultMap="com.foreverwin.mesnac.meapi.mapper.SfcMapper.BaseResultMap">
        SELECT
            SFC.HANDLE,
            SFC.SFC,
            SFC.SHOP_ORDER_BO,
            SFC.STATUS_BO
        FROM  SFC SFC
        JOIN SFC_DATA DT ON DT.SFC_BO = SFC.HANDLE AND DT.DATA_FIELD = #{dataField}
        WHERE DT.DATA_ATTR = #{dataAttr}
              AND SFC.SITE = #{site}

        <if test="statusBoList != null and statusBoList.size() > 0">
              AND SFC.STATUS_BO IN
              <foreach collection="statusBoList" item="item" index="index" open="(" close=")" separator=",">
                  #{item}
              </foreach>
        </if>

        ORDER BY DT.DATE_TIME DESC
    </select>

    <select id="findRouterOperationByRouterBo" resultMap="StepOperationMap">
        SELECT
            STEP.STEP_ID ,
            STEP."SEQUENCE",
            STEP.REPORTING_CENTER_BO,
            STEP.ERP_OPERATION ,
            OP.HANDLE AS OPERATION_BO,
            OP.OPERATION ,
            OPT.DESCRIPTION
        FROM ROUTER R
        JOIN ROUTER_STEP STEP ON R.HANDLE = STEP.ROUTER_BO
        JOIN ROUTER_OPERATION RO ON STEP.HANDLE = RO.ROUTER_STEP_BO
        JOIN OPERATION OP ON 'OperationBO:' || #{site} || ',' || OP.OPERATION  || ',#' = RO.OPERATION_BO AND OP.CURRENT_REVISION = 'true'
        JOIN OPERATION_T OPT ON OP.HANDLE = OPT.OPERATION_BO AND OPT.LOCALE='zh'
        WHERE R.HANDLE = #{routerBo}
              AND OP.SITE = #{site}
              AND OPT.LOCALE = #{locale}
        ORDER BY STEP."SEQUENCE"
    </select>

    <select id="findRouterLastOperationByRouterBo" resultMap="StepOperationMap">
        SELECT
            STEP.STEP_ID ,
            STEP."SEQUENCE",
            RO.OPERATION_BO
        FROM ROUTER R
        JOIN ROUTER_STEP STEP ON R.HANDLE = STEP.ROUTER_BO
        JOIN ROUTER_OPERATION RO ON STEP.HANDLE = RO.ROUTER_STEP_BO
        WHERE R.HANDLE = #{routerBo}
        ORDER BY STEP."SEQUENCE" DESC
    </select>

    <select id="findShopOrderListByStatus" resultMap="com.foreverwin.mesnac.meapi.mapper.ShopOrderMapper.BaseResultMap">
        SELECT
            ORD.HANDLE,
            ORD.SHOP_ORDER,
            ORD.PLANNED_ITEM_BO,
            ORD.ITEM_BO
        FROM SHOP_ORDER ORD
        JOIN CUSTOM_FIELDS CF ON ORD.HANDLE = CF.HANDLE AND CF.ATTRIBUTE = 'WORK_ORDER_STATUS'
        WHERE ORD.PLANNED_WORK_CENTER_BO = #{plannedWorkCenterBo}
              AND CF.VALUE = #{status}
    </select>

    <select id="getResourceBySfc" resultMap="StepOperationMap">
     select siw.RESOURCE_BO
     from sfc  sfc
     inner join sfc_routing  sri on sfc.handle = sri.sfc_bo
     inner join sfc_router  srt on sri.handle = srt.sfc_routing_bo AND srt.IN_USE = 'true'
     inner join sfc_step  sep on srt.handle = sep.sfc_router_bo
     INNER JOIN SFC_IN_WORK siw ON sep.HANDLE = siw.SFC_STEP_BO
    WHERE sfc.site = #{site} and sfc.SFC = #{sfc}
    </select>
    <select id="querySfcData" resultType="map">
        SELECT SO.SHOP_ORDER, SO_CF.VALUE ITEM_NUMBER, SO_CF2.VALUE WORK_ORDER,I.ITEM, IT.DESCRIPTION ITEM_DESC,
            CASE WHEN ST.STATUS='403' AND SS.QTY_IN_QUEUE > 0 THEN N'402' ELSE ST.STATUS END STATUS,
            OP.OPERATION, OT.DESCRIPTION OPERATION_DESC,OP.OPERATION||'/'||RS.STEP_ID OPERATION_STEP,
            RO_CF2.VALUE TOOL ,SOD.HANDLE SFC_DISPATCH_DETAIL_BO,SOD.WORK_CENTER,
            S.SFC,S.QTY SFC_QTY,RS.STEP_ID, SOD.DISPATCH_NO ,
            TO_CHAR( SOD.PLANNED_START_DATE + INTERVAL '8' HOUR,'yyyy-mm-dd hh24:mi:ss')START_DATE,
            TO_CHAR(SOD.PLANNED_COMP_DATE + INTERVAL '8' HOUR ,'yyyy-mm-dd hh24:mi:ss')COMP_DATE,
            TO_CHAR((SOD.PLANNED_COMP_DATE - SOD.PLANNED_START_DATE )*24,'fm9999999990.00')||'H' ADD_DATE,
        CASE WHEN OT.DESCRIPTION LIKE 'CH%' THEN SUBSTR(OT.DESCRIPTION, 0, 3) ELSE N''  END SLOT
        FROM SFC S
        INNER JOIN STATUS ST ON ST.HANDLE = S.STATUS_BO
        INNER JOIN SFC_ROUTING SR ON SR.SFC_BO = S.HANDLE
        INNER JOIN SFC_ROUTER SRO ON SRO.SFC_ROUTING_BO = SR.HANDLE AND SRO.IN_USE = 'true'
        INNER JOIN SFC_STEP SS ON SS.SFC_ROUTER_BO = SRO.HANDLE AND (SS.QTY_IN_QUEUE > 0 OR SS.QTY_IN_WORK > 0)
        INNER JOIN ROUTER RO ON SRO.ROUTER_BO = RO.HANDLE
        INNER JOIN ROUTER_STEP RS ON RS.ROUTER_BO = RO.HANDLE AND RS.STEP_ID = SS.STEP_ID
        INNER JOIN ROUTER_OPERATION ROP ON ROP.HANDLE = RS.ROUTER_COMP_GBO AND SS.OPERATION_BO = ROP.OPERATION_BO
		LEFT JOIN CUSTOM_FIELDS RO_CF2 ON RO_CF2.HANDLE = ROP.HANDLE AND RO_CF2."ATTRIBUTE" = 'TOOL_NUMBER'
        INNER JOIN OPERATION OP ON 'OperationBO:'||OP.SITE||','||OP.OPERATION||',#' = SS.OPERATION_BO AND OP.CURRENT_REVISION='true'
            AND (#{dto.operation} IS NULL OR  OP.OPERATION = #{dto.operation})
        LEFT JOIN OPERATION_T OT ON OT.OPERATION_BO = OP.HANDLE AND OT.LOCALE =#{locale}
        INNER JOIN SHOP_ORDER SO ON SO.HANDLE = S.SHOP_ORDER_BO
        LEFT JOIN CUSTOM_FIELDS SO_CF ON SO_CF.HANDLE = SO.HANDLE AND SO_CF."ATTRIBUTE" = 'ITEM_NUMBER'
        LEFT JOIN CUSTOM_FIELDS SO_CF2 ON SO_CF2.HANDLE = SO.HANDLE AND SO_CF2."ATTRIBUTE" = 'WORK_ORDER'
        INNER JOIN ITEM I ON I.HANDLE = S.ITEM_BO
        LEFT JOIN ITEM_T IT ON IT.ITEM_BO = I.HANDLE AND IT.LOCALE =#{locale}
        INNER JOIN (
            SELECT SD.HANDLE, SD.SFC,SD.RESRCE,SD.OPERATION,SD.STEP_ID,SD.PLANNED_COMP_DATE, SD.PLANNED_START_DATE,SD.DISPATCH_NO,SD.WORK_CENTER
            FROM Z_PROD_READY_TASK RB
            INNER JOIN Z_SFC_DISPATCH SD ON SD.HANDLE = RB.SFC_DISPATCH_BO
            WHERE RB.SITE =#{site} AND RB.STATUS = 'FINISH' AND RB."RESULT"='OK'
        ) SOD ON SOD.RESRCE =#{dto.resrce} AND SOD.SFC= S.SFC AND SOD.OPERATION = OP.OPERATION
        WHERE S.SITE = #{site} AND S.SFC = #{dto.sfc} AND ROWNUM=1
        GROUP BY SO.SHOP_ORDER, SO_CF.VALUE , I.ITEM, IT.DESCRIPTION ,SOD.WORK_CENTER,SO_CF2.VALUE,
        CASE WHEN ST.STATUS='403' AND SS.QTY_IN_QUEUE > 0 THEN N'402' ELSE ST.STATUS END,
        OP.OPERATION, OT.DESCRIPTION ,OP.OPERATION||'/'||RS.STEP_ID  ,RO_CF2.VALUE,
        S.SFC,S.QTY ,RS.STEP_ID,SOD.DISPATCH_NO , SOD.HANDLE ,
        TO_CHAR( SOD.PLANNED_START_DATE + INTERVAL '8' HOUR,'yyyy-mm-dd hh24:mi:ss'),
        TO_CHAR(SOD.PLANNED_COMP_DATE + INTERVAL '8' HOUR,'yyyy-mm-dd hh24:mi:ss'),
        TO_CHAR((SOD.PLANNED_COMP_DATE - SOD.PLANNED_START_DATE )*24,'fm9999999990.00')||'H',
        CASE WHEN OT.DESCRIPTION LIKE 'CH%' THEN SUBSTR(OT.DESCRIPTION, 0, 3) ELSE N'' END
    </select>
    <select id="getSfcInfo" resultType="map">
       SELECT O.OPERATION,SSTEP.STEP_ID,IT.DESCRIPTION,S.SFC,S.QTY,
        CASE WHEN SSTEP.QTY_IN_WORK > 0 THEN '工作中'
        WHEN SSTEP.QTY_IN_QUEUE > 0 THEN '排队中'
        ELSE '新建'  END STEP_STATUS
        FROM SFC S
        LEFT JOIN ITEM_T IT ON S.ITEM_BO=IT.ITEM_BO AND IT.LOCALE='zh'
        INNER JOIN SFC_ROUTING SRI ON S.HANDLE=SRI.SFC_BO
        INNER JOIN SFC_ROUTER SR ON SRI.HANDLE=SR.SFC_ROUTING_BO AND SR.IN_USE='true'
        INNER JOIN ROUTER_STEP RS ON  RS.ROUTER_BO=sr.ROUTER_BO
        INNER JOIN SFC_STEP SSTEP ON SSTEP.SFC_ROUTER_BO=SR.HANDLE  AND RS.STEP_ID= SSTEP.STEP_ID
        AND (SSTEP.QTY_IN_QUEUE > 0 OR SSTEP.QTY_IN_WORK > 0)
        INNER JOIN STATUS ST ON ST.HANDLE = S.STATUS_BO
        INNER JOIN OPERATION O ON O.OPERATION=SPLIT(SSTEP.OPERATION_BO,2) AND O.SITE=S.SITE AND O.CURRENT_REVISION = 'true'
        AND  #{operation}  = O.OPERATION
        WHERE S.SFC=#{sfc} AND S.SITE=#{site}
    </select>
    <select id="getSfcDispatch" resultType="java.lang.String">
       	SELECT DISPATCH_STATUS FROM Z_SFC_DISPATCH zsd  WHERE OPERATION= #{operation} AND SFC= #{sfc} AND STEP_ID= #{stepId} AND SITE= #{site} AND RESRCE=#{resrce}
    </select>

    <select id="getSfcListByResrce" resultType="com.foreverwin.mesnac.meapi.dto.SfcDto">
       		SELECT ZSD.sfc,ZSD.operation,IT.DESCRIPTION item_Description,ST.STATUS,s.qty,ZSD.DISPATCH_NO,ZSD.STEP_ID
       		FROM Z_SFC_DISPATCH ZSD
            JOIN SFC S ON ZSD.SFC=s.SFC AND  ZSD.SITE=s.SITE
            LEFT JOIN ITEM_T IT ON S.ITEM_BO=IT.ITEM_BO AND  IT.LOCALE=#{locale}
            INNER JOIN STATUS ST ON ST.HANDLE = S.STATUS_BO
            WHERE  zsd.SITE=#{site}    AND zsd.DISPATCH_STATUS='START' AND ZSD.RESRCE=#{resrce}
    </select>

    <select id="resourceCheck" resultType="java.lang.Integer">
		SELECT count(HANDLE) FROM Z_RESOURCE_INSPECT_TASK zrit WHERE  site=#{site} and RESRCE=#{resrce} AND CATEGORY='DM' AND STATUS='COMPLETE' AND "RESULT"='OK' AND TASK_CREATE_DATE>=#{dateTime}
    </select>

    <select id="getInfoBySfc" resultType="com.foreverwin.mesnac.meapi.dto.SfcDto">
        SELECT DISTINCT S.SFC SFC, SO.SHOP_ORDER SHOP_ORDER,CF.VALUE WORK_ORDER,O.OPERATION OPERATION,
               CASE WHEN SSTEP.QTY_IN_WORK > 0 THEN '工作中'
                    WHEN SSTEP.QTY_IN_QUEUE > 0 THEN '排队中'
                    ELSE '新建'  END STATUS,SSTEP.STEP_ID STEP_ID,
               WC.WORK_CENTER WORK_CENTER,SO.PLANNED_START_DATE PLANNED_START_DATE,SO.PLANNED_COMP_DATE  PLANNED_COMP_DATE
        FROM SFC S
                 LEFT JOIN Z_SFC_DISPATCH ZSD ON S.SFC = ZSD.SFC AND S.SITE = ZSD.SITE
                 INNER JOIN SFC_ROUTING SRI ON S.HANDLE=SRI.SFC_BO
                 INNER JOIN SFC_ROUTER SR ON SRI.HANDLE=SR.SFC_ROUTING_BO AND SR.IN_USE='true'
                 INNER JOIN ROUTER_STEP RS ON  RS.ROUTER_BO=SR.ROUTER_BO
                 INNER JOIN SFC_STEP SSTEP ON SSTEP.SFC_ROUTER_BO=SR.HANDLE  AND RS.STEP_ID= SSTEP.STEP_ID
            AND (SSTEP.QTY_IN_QUEUE > 0 OR SSTEP.QTY_IN_WORK > 0)
                 INNER JOIN STATUS ST ON ST.HANDLE = S.STATUS_BO
                 LEFT JOIN OPERATION O ON O.OPERATION=SPLIT(SSTEP.OPERATION_BO,2) AND O.SITE=S.SITE AND O.CURRENT_REVISION = 'true'
                 LEFT JOIN SHOP_ORDER SO ON SO.SHOP_ORDER = ZSD.SHOP_ORDER AND SO.SITE = S.SITE
                 LEFT JOIN CUSTOM_FIELDS CF ON CF.HANDLE = SO.HANDLE AND CF."ATTRIBUTE" = 'WORK_ORDER'
                 LEFT JOIN WORK_CENTER WC ON WC.WORK_CENTER  = ZSD.WORK_CENTER AND WC.SITE = S.SITE
        <where>
            <if test="sfc != null">
                S.SITE = #{sfc.site} AND S.SFC = #{sfc.sfc}
            </if>
        </where>
    </select>
</mapper>
