<?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.anomaly.mapper.ReportMapper">

    <resultMap id="IntegrationLogDto" type="com.foreverwin.mesnac.anomaly.dto.IntegrationLogDto">
        <result column="INTEGRATION_TYPE" property="integrationType"/>
        <result column="CATEGORY" property="category"/>
        <result column="STATUS" property="status"/>
        <result column="REQUEST_DATE_TIME" property="requestDateTime"/>
        <result column="RESULT_MESSAGE" property="resultMessage"/>
        <result column="PARAM" property="param"/>
    </resultMap>
    <select id="findIntegrationLog" resultMap="IntegrationLogDto">
        SELECT   INTEGRATION_TYPE, CATEGORY, INTEGRATION_WAY,STATUS,
               REQUEST_DATE_TIME,RESULT_MESSAGE RESULT_MESSAGE,PARAM PARAM
        FROM Z_INTEGRATION_LOG
        <where>
            SITE = #{site}
            <if test="param.integrationType != null and param.integrationType != ''">
                AND INTEGRATION_TYPE = #{param.integrationType}
            </if>
            <if test="param.integrationWay != null and param.integrationWay != ''">
                AND INTEGRATION_WAY = #{param.integrationWay}
            </if>
            <if test="param.status != null and param.status != ''">
                AND STATUS = #{param.status}
            </if>
            <if test="param.startDateTime != null">
                AND REQUEST_DATE_TIME &gt;=  TO_DATE(#{param.startDateTime}, 'YYYY-MM-DD HH24:MI:SS')
            </if>
            <if test="param.endDateTime != null">
                AND REQUEST_DATE_TIME &lt;= TO_DATE(#{param.endDateTime}, 'YYYY-MM-DD HH24:MI:SS')
            </if>
            <if test="param.param != null and param.param != ''">
                AND PARAM LIKE '%${param.param}%'
            </if>
        </where>
        ORDER BY REQUEST_DATE_TIME DESC
    </select>

    <select id="partsManufacturing" resultType="Map">
        SELECT CF.VALUE ITEM_NUMBER,ZSD.SHOP_ORDER SHOP_ORDER,I.ITEM ITEM,IT.DESCRIPTION ITEM_DESCRIPTION,
               SO.QTY_TO_BUILD SHOP_ORDER_QTY , ZSD.OPERATION ,OT.DESCRIPTION OPERATION_DESCRIPTION,
        ZNU.FULL_NAME OPERATION_USER,ZSD.SFC SFC,
        CASE WHEN S2.STATUS = '405' THEN '是' ELSE '否' END DISPATCH_STATUS,
        ZSD.EARLIEST_START_DATE EARLIEST_START_DATE,ZSD.LATEST_END_DATE LATEST_END_DATE,
        ZSD.ACTUAL_START_DATE ACTUAL_START_DATE,ZSD.ACTUAL_COMPLETE_DATE ACTUAL_COMPLETE_DATE,
        CF2.VALUE WORK_ORDER
        FROM SFC S
        INNER JOIN SHOP_ORDER SO ON SO.HANDLE = S.SHOP_ORDER_BO
        INNER JOIN ITEM I ON I.HANDLE = SO.ITEM_BO
        LEFT JOIN ITEM_T IT ON IT.ITEM_BO = I.HANDLE AND IT.LOCALE = #{locale}
        INNER JOIN Z_SFC_DISPATCH ZSD ON S.SFC = ZSD.SFC AND S.SITE = ZSD.SITE
        INNER JOIN OPERATION O ON O.OPERATION = ZSD.OPERATION AND O.SITE = S.SITE AND O.CURRENT_REVISION = 'true'
        INNER JOIN OPERATION_T OT ON OT.OPERATION_BO = O.HANDLE AND OT.LOCALE = #{locale}
        LEFT JOIN Z_NWA_USER ZNU ON ZNU.USER_NAME = ZSD.EMPLOYEE AND ZSD.DISPATCH_SEQ = (
        SELECT MAX(ZSD2.DISPATCH_SEQ) FROM Z_SFC_DISPATCH ZSD2 WHERE S.SFC = ZSD2.SFC AND S.SITE = ZSD2.SITE
        )
        LEFT JOIN CUSTOM_FIELDS CF ON CF.HANDLE = S.SHOP_ORDER_BO AND CF."ATTRIBUTE" = 'ITEM_NUMBER'
        LEFT JOIN CUSTOM_FIELDS CF2 ON CF2.HANDLE = S.SHOP_ORDER_BO AND CF2."ATTRIBUTE" = 'WORK_ORDER'
        INNER JOIN STATUS S2 ON S2.HANDLE  = S.STATUS_BO
        <where>
            S.SITE = #{site}
            <if test="param != null">
                <if test="param.itemNumber != null and partparamsManufacturing.itemNumber != ''">
                    AND CF.VALUE LIKE '%${param.itemNumber}%'
                </if>
                <if test="param.shopOrder != null and param.shopOrder != ''">
                    AND ZSD.SHOP_ORDER LIKE '%${param.shopOrder}%'
                </if>
                <if test="param.item != null and param.item != ''">
                    AND I.ITEM LIKE '%${param.item}%'
                </if>
                <if test="param.itemDescription != null and param.itemDescription != ''">
                    AND IT.DESCRIPTION LIKE '%${param.itemDescription}%'
                </if>
                <if test="param.operation != null and param.operation != ''">
                    AND (O.OPERATION LIKE '%${param.operation}%'
                        OR OT.DESCRIPTION LIKE '%${param.operation}%')
                </if>
                <if test="param.workOrder != null and param.workOrder != ''">
                    AND CF2.VALUE = #{param.workOrder}
                </if>
                <if test="param.status != null and param.status != ''">
                    <if test="param.status == 'Y'.toString()">
                        AND S2.STATUS = '405'
                    </if>
                    <if test="param.status == 'N'.toString()">
                        AND S2.STATUS != '405'
                    </if>
                </if>
                <if test="param.startDateTime != null">
                    AND ZSD.ACTUAL_START_DATE &gt;= TO_DATE(#{param.startDateTime}, 'YYYY-MM-DD HH24:MI:SS')
                </if>
                <if test="param.endDateTime != null">
                    AND ZSD.ACTUAL_START_DATE &lt;= TO_DATE(#{param.endDateTime}, 'YYYY-MM-DD HH24:MI:SS')
                </if>
                <if test="param.completeStartDateTime != null">
                    AND ZSD.ACTUAL_COMPLETE_DATE &gt;= TO_DATE(#{param.completeStartDateTime}, 'YYYY-MM-DD HH24:MI:SS')
                </if>
                <if test="param.completeEndDateTime != null">
                    AND ZSD.ACTUAL_COMPLETE_DATE &lt;= TO_DATE(#{param.endDateTime}, 'YYYY-MM-DD HH24:MI:SS')
                </if>

            </if>
        </where>
    </select>

    <select id="surplusReturnTable" resultType="map">
        SELECT ZSR.INVENTORY INVENTORY,ZSR.ITEM_BO ITEM_BO,IT.DESCRIPTION ITEM_DESCRIPTION,ZSR.LENGHT LENGHT,
        ZSR.WIDTH WIDTH,ZSR.NUMBERS NUMBERS,ZSR.QTY QTY,ZSR.CREATED_DATE_TIME CREATED_DATE_TIME,I.ITEM ITEM
        FROM
        Z_SURPLUS_RETURN ZSR
        INNER JOIN ITEM I ON I.HANDLE = ZSR.ITEM_BO
        INNER JOIN ITEM_T IT ON IT.ITEM_BO = I.HANDLE
        <where>
            ZSR.SITE = #{site}
            <if test="item != null and item != ''">
                AND I.ITEM LIKE '%${item}%'
            </if>
            <if test="itemDescription != null and itemDescription != ''">
                AND IT.DESCRIPTION LIKE '%${itemDescription}%'
            </if>
        </where>
    </select>

    <select id="resourceRepair" resultType="map">
        SELECT
            ZRRT.HANDLE HANDLE,
            CASE WHEN WC2.WORK_CENTER != 'NULL' THEN WC2.WORK_CENTER
             ELSE WC.WORK_CENTER
                END WORK_CENTER,
            CASE WHEN WCT.DESCRIPTION != 'NULL' THEN WCT.DESCRIPTION
                 ELSE  WCT2.DESCRIPTION
                END WORK_CENTER_DESCRIPTION,
            ZRRT.RESOURCE_NO,R.DESCRIPTION RESOURCE_DESCRIPTION,ZRRT.NC_CODE_NO, /*不良代码描述,维修人员*/
            ZRRT.PRELIMINARY_REASON ,ZRRT.DEAL_WAY ,ZRRT.DEAL_WAY_DESCRIPTION ,
            I.ITEM ITEM,
            CASE WHEN ZRITS.ITEM_BO != 'NULL' THEN IT.DESCRIPTION
                 ELSE ZRITS.DESCRIPTION
                END ITEM_DESCRIPTION,
            CF.VALUE ITEM_SPEC,
            ZRRT.ACTUAL_START_DATE ACTUAL_START_DATE,ZRRT.ACTUAL_COMPLETE_DATE ACTUAL_COMPLETE_DATE,
            CASE WHEN ZRRT.STOP_MACHINE = 'Y' THEN TRUNC(TO_NUMBER((ZRRT.ACTUAL_COMPLETE_DATE-ZRRT.CREATED_DATE_TIME)*24),2)
                 WHEN ZRRT.STOP_MACHINE = 'N' THEN TRUNC(TO_NUMBER((SYSDATE-ZRRT.CREATED_DATE_TIME)*24),2)
                 END SHUT_DOWN_TIME,
            CASE WHEN ZRRT.STOP_MACHINE = 'Y' THEN TRUNC(TO_NUMBER((ZRRT.WAIT_END_DATE -ZRRT.WAIT_START_DATE)*24),2)
                 WHEN ZRRT.STOP_MACHINE = 'N' THEN TRUNC(TO_NUMBER((SYSDATE-ZRRT.WAIT_START_DATE)*24),2)
                 END REPAIR_WAIT_TIME,

            CASE WHEN ZRRT.STOP_MACHINE = 'Y' THEN TRUNC(TO_NUMBER((ZRRT.ACTUAL_COMPLETE_DATE-ZRRT.CREATED_DATE_TIME-ZRRT.WAIT_TIME)*24),2)
                 WHEN ZRRT.STOP_MACHINE = 'N' THEN TRUNC(TO_NUMBER((SYSDATE-ZRRT.CREATED_DATE_TIME-ZRRT.WAIT_TIME)*24),2)
                 END REPAIR_TIME,
            CASE WHEN ZRRT.STATUS = '401' THEN '待维修'
                 WHEN ZRRT.STATUS = '501' THEN '维修中'
                 WHEN ZRRT.STATUS = '502' THEN '维修等待'
                 WHEN ZRRT.STATUS = '302' THEN '关闭'
                 WHEN ZRRT.STATUS = '301' THEN '维修完成'
                 WHEN ZRRT.STATUS = '601' THEN '报修单确认'
                END STATUS
            ,ZRRT.REPORT_USER REPORT_USER,ZNU.FULL_NAME REPORT_USER_DESCRIPTION,
            ZRRT.REPAIR_USERS ,ZRRT.REMARK
        FROM
            Z_RESOURCE_REPAIR_TASK ZRRT
                INNER JOIN RESRCE R ON R.RESRCE = ZRRT.RESOURCE_NO AND R.SITE = ZRRT.SITE
                INNER JOIN WORK_CENTER_MEMBER WCM ON WCM.WORK_CENTER_OR_RESOURCE_GBO = R.HANDLE
                LEFT JOIN WORK_CENTER WC ON WC.HANDLE = WCM.WORK_CENTER_BO AND WC.WC_CATEGORY ='LEVEL3'

                LEFT JOIN WORK_CENTER_MEMBER WCM2 ON WCM2.WORK_CENTER_OR_RESOURCE_GBO = WC.HANDLE
                LEFT JOIN WORK_CENTER WC2 ON WC2.HANDLE = WCM.WORK_CENTER_BO AND WC2.WC_CATEGORY = 'LEVEL4'

                LEFT JOIN WORK_CENTER_T WCT ON WCT.WORK_CENTER_BO = WC.HANDLE AND WCT.LOCALE = #{locale}
                LEFT JOIN WORK_CENTER_T WCT2 ON WCT2.WORK_CENTER_BO = WC2.HANDLE AND WCT2.LOCALE = #{locale}
                LEFT JOIN Z_RESOURCE_INSPECT_TASK_SPARE ZRITS ON ZRITS.RESOURCE_INSPECT_TASK_BO = ZRRT.HANDLE
                LEFT JOIN ITEM I ON I.HANDLE = ZRITS.ITEM_BO
                LEFT JOIN ITEM_T IT ON IT.ITEM_BO = ZRITS.ITEM_BO AND IT.LOCALE = #{locale}
                LEFT JOIN CUSTOM_FIELDS CF ON CF.HANDLE = ZRITS.ITEM_BO AND CF."ATTRIBUTE" = 'MAT_SPEC'

                LEFT JOIN Z_NWA_USER ZNU ON ZNU.USER_NAME = ZRRT.REPORT_USER AND ZNU.SITE = ZRRT.SITE
        <where>
            ZRRT.SITE = #{site}
            <if test="param.workCenter != null and param.workCenter != ''">
                AND (WC.WORK_CENTER LIKE '%${param.workCenter}%' OR WC2.WORK_CENTER LIKE '%${param.workCenter}%' OR
                    WCT.DESCRIPTION LIKE '%${param.workCenter}%' OR WCT2.DESCRIPTION LIKE '%${param.workCenter}%')
            </if>
            <if test="param.resource != null and param.resource != ''">
                AND (R.RESRCE LIKE '%${param.resource}%' OR R.DESCRIPTION LIKE '%${param.resource}%')
            </if>
        </where>
    </select>




    <select id="findInspectionTask" resultType="java.util.HashMap" parameterType="java.util.HashMap">
        SELECT 	WT.DESCRIPTION  WORK_CENTER_DESCRIPTION, C2.VALUE ITEM_NUMBER, C3.VALUE PRODUCT_MODEL,IM.ITEM ITEM, IT.DESCRIPTION ITEM_DESCRIPTION,
                ZT.SFC, ZT.OPERATION, OT.DESCRIPTION OPERATION_DESCRIPTION, ZT.RESRCE, RS.DESCRIPTION RESOURCE_DESCRIPTION, ZT.TASK_NO, ZT.CATEGORY,
                ZT."RESULT", ZT.CREATE_USER, ZNU.FULL_NAME, ZT.MODIFIED_DATE_TIME
        FROM Z_INSPECTION_TASK ZT
        LEFT JOIN WORK_CENTER WC ON WC.SITE = ZT.SITE AND WC.WORK_CENTER = ZT.WORK_CENTER
        LEFT JOIN WORK_CENTER_T WT ON WT.WORK_CENTER_BO = WC.HANDLE AND WT.LOCALE = 'zh'
        INNER JOIN SHOP_ORDER SO ON ZT.SITE = SO.SITE AND ZT.SHOP_ORDER = SO.SHOP_ORDER
        LEFT JOIN CUSTOM_FIELDS C1 ON C1.HANDLE = SO.HANDLE AND C1."ATTRIBUTE" = 'WORK_ORDER'
        LEFT JOIN CUSTOM_FIELDS C2 ON C2.HANDLE = SO.HANDLE AND C2."ATTRIBUTE" = 'ITEM_NUMBER'
        LEFT JOIN CUSTOM_FIELDS C3 ON C3.HANDLE = SO.HANDLE AND C3."ATTRIBUTE" = 'PRODUCT_MODEL'
        INNER JOIN ITEM IM ON IM.HANDLE = SO.ITEM_BO
        LEFT JOIN ITEM_T IT ON IT.ITEM_BO = IM.HANDLE AND IT.LOCALE = 'zh'
        INNER JOIN OPERATION OP ON OP.SITE = ZT.SITE AND OP.OPERATION = ZT.OPERATION AND OP.CURRENT_REVISION = 'true'
        LEFT JOIN OPERATION_T OT ON OT.OPERATION_BO = OP.HANDLE AND OT.LOCALE = 'zh'
        INNER JOIN RESRCE RS ON RS.SITE = ZT.SITE AND RS.RESRCE = ZT.RESRCE
        LEFT JOIN Z_NWA_USER ZNU ON ZNU.SITE = ZT.SITE AND ZNU.USER_NAME = ZT.CREATE_USER
        WHERE ZT.SITE = #{site}
        <if test="workCenter != null and workCenter != ''" >
          AND WC.WORK_CENTER = #{workCenter}
        </if>
        <if test="category != null and category != ''" >
            AND ZT.CATEGORY = #{category}
        </if>
        <if test="itemNumber != null and itemNumber != ''" >
            AND C2.VALUE LIKE '%'||#{itemNumber}||'%'
        </if>
        <if test="model != null and model != ''" >
            AND C3.VALUE LIKE '%'||#{model}||'%'
        </if>
        <if test="operation != null and operation != ''" >
            AND ZT.OPERATION = #{operation}
        </if>
        <if test="resrce != null and resrce != ''" >
            AND ZT.RESRCE = #{resrce}
        </if>
        <if test="result != null and result != ''" >
            AND ZT.RESULT = #{result}
        </if>
        <if test="startFromDate != null and startFromDate != ''" >
            AND ZT.MODIFIED_DATE_TIME >= TO_DATE(#{startFromDate}, 'YYYY-MM-DD')
        </if>
        <if test="startToDate != null and startToDate != ''" >
            AND ZT.MODIFIED_DATE_TIME &lt;= TO_DATE(#{startToDate}|| '23:59:59', 'YYYY-MM-DD HH24:MI:SS')
        </if>
    </select>

    <select id="findQualityInspectionReport"  parameterType="java.util.HashMap" resultType="java.util.HashMap">
        SELECT  zab.CREATED_DATE_TIME,zab.ABNORMAL_NO ABNORMAL_NO,
                dflt.DATA_TAG PRODUCT_CATEGORY,
                CASE WHEN zab.DISCOVER = 'Z' THEN '自制'
                     WHEN zab.DISCOVER = 'W' THEN '外协'
                     WHEN zab.DISCOVER = 'D' THEN '到货'
                     WHEN zab.DISCOVER = 'K' THEN '客户'
                     ELSE ''
                    END DISCOVER ,
                i.ITEM ITEM ,it.DESCRIPTION ITEM_DESCRIPTION,itemNumber.VALUE ITEM_NUMBER,
                mapNo.VALUE MAP_NO,zab.PB_DESCRIPTION PB_DESCRIPTION,zsd.EMPLOYEE_DESCRIPTION OPERATOR,
               CASE WHEN zab."TYPE" = 'Z' THEN '质量异常'
                    WHEN zab."TYPE" = 'Q' THEN '其他异常'
                    WHEN zab."TYPE" = 'S' THEN '设备异常' END "TYPE",
               zab.REPORT_SEND_USER_GROUP INSPECTOR ,
                zab.PB_GRADE PB_GRADE ,
                zabd.PRINCIPAL_USER PRINCIPAL_USER,
                LISTAGG(nct.DESCRIPTION , ';') WITHIN GROUP(ORDER BY zab.ABNORMAL_NO) NC_CODE_DESCRIPTION,
                zabd.DUTY_CAUSE_DESCRIPTION DUTY_CAUSE_DESCRIPTION,zabd.BEFORE_MEASURE BEFORE_MEASURE,
                CASE WHEN zabd.ABNORMAL_METHOD = 'X' THEN '线下换料'
                     WHEN zabd.ABNORMAL_METHOD = 'S' THEN '试装'
                     WHEN zabd.ABNORMAL_METHOD = 'R' THEN '让步放心'
                     WHEN zabd.ABNORMAL_METHOD = 'C' THEN '报废'
                     WHEN zabd.ABNORMAL_METHOD = 'P' THEN '配作'
                     WHEN zabd.ABNORMAL_METHOD = 'F' THEN '返修'
                     ELSE ''
                    END ABNORMAL_METHOD,zabd.RESOLVE_REMARK RESOLVE_REMARK,zabd.RESOLVE_USER RESOLVE_USER,
                zabd.CLOSED_DATE_TIME CLOSED_DATE_TIME,zab.NC_QTY NC_QTY,zab.PB_QTY PB_QTY,
                CASE WHEN (zabd.ABNORMAL_METHOD ='F' AND zab.SHOP_ORDER IS NOT NULL) THEN TO_CHAR(so.QTY_TO_BUILD,'9999')
                     WHEN zabd.ABNORMAL_METHOD = 'F' THEN  TO_CHAR(zab.PB_QTY,'9999')  ELSE '' END NUMBERS,
                zsd.EMPLOYEE_DESCRIPTION  DISPATCH_USER ,ot.DESCRIPTION OPERATION ,
                r2.RESRCE RESRCE,r2.DESCRIPTION RESRCE_DESCRIPTION,zsd.RESOURCE_TYPE RESOURCE_TYPE,
               rt.DESCRIPTION RESOURCE_TYPE_DESCRIPTION,zabd.DUTY_CAUSE_TYPE DUTY_CAUSE_TYPE,zabd.DUTY_TYPE DUTY_TYPE,
               zabd.DUTY_DEPART DUTY_DEPART
        FROM Z_ABNORMAL_BILL zab
                 INNER JOIN Z_ABNORMAL_BILL_DISPOSE zabd ON zab.HANDLE = zabd.ABNORMAL_BILL_BO
                 LEFT JOIN Z_ABNORMAL_NC_CODE zanc ON zab.HANDLE = zanc.ABNORMAL_BILL_BO
                 LEFT JOIN NC_CODE nc ON zanc.NC_CODE = nc.NC_CODE AND nc.SITE = zab.SITE
                 LEFT JOIN NC_CODE_T nct ON nct.NC_CODE_BO = nc.HANDLE AND nct.LOCALE = #{locale}
                 INNER JOIN ITEM i ON i.HANDLE = zab.ITEM_BO
                 LEFT JOIN ITEM_T it ON it.ITEM_BO = i.HANDLE AND it.LOCALE = #{locale}
                 INNER JOIN DATA_FIELD df ON df.DATA_FIELD = 'PRODUCT_CATEGORY'
                 LEFT JOIN DATA_FIELD_LIST dfl ON dfl.DATA_FIELD_BO = df.HANDLE AND dfl.DATA_VALUE = zab.PRODUCT_CATEGORY
                 LEFT JOIN DATA_FIELD_LIST_T dflt ON dflt.DATA_FIELD_LIST_BO = dfl.HANDLE AND dflt.LOCALE = #{locale}
                 LEFT JOIN CUSTOM_FIELDS mapNo ON mapNo."ATTRIBUTE" = 'DWG_NO' AND mapNo.HANDLE = i.HANDLE
                 LEFT JOIN SHOP_ORDER so ON so.SHOP_ORDER = zab.SHOP_ORDER AND so.SITE = zab.SITE
                 LEFT JOIN ROUTER r ON r.HANDLE = zabd.ROUTER_BO
                 LEFT JOIN CUSTOM_FIELDS itemNumber ON itemNumber.HANDLE = so.HANDLE AND itemNumber."ATTRIBUTE" = 'ITEM_NUMBER'
                 LEFT JOIN SFC s ON s.SFC = zab.SFC AND s.SITE = zab.SITE
                 LEFT JOIN SFC_ROUTING SR ON SR.SFC_BO  = S.HANDLE
                 LEFT JOIN SFC_ROUTER ST ON ST.SFC_ROUTING_BO  = SR.HANDLE
                 LEFT JOIN SFC_STEP SS ON SS.SFC_ROUTER_BO = ST.HANDLE  AND (SS.QTY_IN_QUEUE > 0 OR SS.QTY_IN_WORK > 0)
                 LEFT JOIN Z_SFC_DISPATCH zsd ON zsd.DISPATCH_NO = S.SFC || '-' || SS.STEP_ID
                 LEFT JOIN OPERATION o ON o.OPERATION = zsd.OPERATION AND o.SITE = zab.SITE AND o.CURRENT_REVISION ='true'
                 LEFT JOIN OPERATION_T ot ON ot.OPERATION_BO = o.HANDLE AND ot.LOCALE = #{locale}
                 LEFT JOIN RESRCE r2 ON r2.RESRCE = zsd.RESRCE AND r2.SITE = zab.SITE
                 LEFT JOIN RESOURCE_TYPE rt ON rt.RESOURCE_TYPE = zsd.RESOURCE_TYPE AND rt.SITE = zab.SITE
                 LEFT JOIN WORK_CENTER wc ON wc.WORK_CENTER  = SUBSTR(o.OPERATION,1,INSTR(o.OPERATION,'_')-1) AND wc.site = zab.SITE
        <where>
            zab."TYPE" ='Z'
            <if test="workCenter != null and workCenter != ''">
                AND wc.WORK_CENTER LIKE '%${workCenter}%'
            </if>
            <if test="itemNumber != null and itemNumber != ''">
                AND itemNumber.VALUE = #{itemNumber}
            </if>
            <if test="productCategory != null and productCategory != ''">
                AND (dflt.DATA_TAG = #{productCategory} OR
                     zab.PRODUCT_CATEGORY = #{productCategory}
                )
            </if>
            <if test="operation != null and operation != ''">
                AND (o.OPERATION LIKE '%${operation}%' OR
                    ot.DESCRIPTION LIKE '%${operation}%'
                )
            </if>
            <if test="resrce != null and resrce != ''">
                AND (r2.RESRCE LIKE '%${resrce}%' OR
                    r2.DESCRIPTION LIKE '%${resrce}%'
                )
            </if>
            <if test="startDateTime != null">
                AND zab.CREATED_DATE_TIME &gt;= TO_DATE(#{startDateTime},'YYYY-MM-DD')
            </if>
            <if test="endDateTime != null">
                AND zab.CREATED_DATE_TIME &lt;= TO_DATE(#{endDateTime},'YYYY-MM-DD')
            </if>
        </where>
        GROUP BY zab.CREATED_DATE_TIME ,zab.ABNORMAL_NO ,
                 i.ITEM  ,it.DESCRIPTION ,itemNumber.VALUE ,
                 mapNo.VALUE ,zab.PB_DESCRIPTION ,zsd.EMPLOYEE_DESCRIPTION ,
                 zab."TYPE" ,zab.REPORT_SEND_USER_GROUP  ,zabd.DUTY_DEPART ,zabd.PRINCIPAL_USER,DISCOVER ,
                 zab.PB_GRADE,zabd.DUTY_CAUSE_DESCRIPTION ,zabd.BEFORE_MEASURE,ABNORMAL_METHOD,
                 zabd.RESOLVE_REMARK ,zabd.RESOLVE_USER ,
                 zabd.CLOSED_DATE_TIME ,zab.NC_QTY ,zab.PB_QTY ,zab.SHOP_ORDER,so.QTY_TO_BUILD,
                 zsd.EMPLOYEE_DESCRIPTION  ,ot.DESCRIPTION  ,
                 r2.RESRCE,r2.DESCRIPTION,zsd.RESOURCE_TYPE,
                 rt.DESCRIPTION, dflt.DATA_TAG,zabd.DUTY_CAUSE_TYPE,zabd.DUTY_TYPE ,zabd.DUTY_DEPART
        ORDER BY zab.CREATED_DATE_TIME DESC
    </select>

    <select id="findAbnormalRecord" parameterType="java.util.HashMap" resultType="java.util.HashMap">
        SELECT wc.WORK_CENTER ,zab.PRODUCT_CATEGORY ,cf.VALUE WORK_ORDER,so.SHOP_ORDER SHOP_ORDER ,
               i.ITEM ITEM,it.DESCRIPTION ITEM_DESCRIPTION,zab.SFC SFC,zab.ABNORMAL_NO ABNORMAL_NO,
               CASE WHEN zab."TYPE" = 'Z' THEN '质量异常'
                    WHEN zab."TYPE" = 'S' THEN '设备异常'
                    WHEN zab."TYPE" = 'Q' THEN '其他异常' END ABNORMAL_TYPE,
               zab.OPERATION OPERATION ,ot.DESCRIPTION OPERATION_DESCRIPTION,r.RESRCE RESRCE,r.DESCRIPTION RESRCE_DESCRIPTION,
               zab.CREATED_DATE_TIME CREATED_DATE_TIME ,znu.FULL_NAME PB_USER,znu2.FULL_NAME REPORT_USER,
               LISTAGG(nct.DESCRIPTION , ';') WITHIN GROUP(ORDER BY zab.ABNORMAL_NO) NC_CODE_DESCRIPTION,
               CASE WHEN zab.STATUS = 'N' THEN '新建'
                    WHEN zab.STATUS = 'X' THEN '响应中'
                    WHEN zab.STATUS = 'F' THEN '方案确认'
                    WHEN zab.STATUS = 'J' THEN '纠防确认'
                    WHEN zab.STATUS = 'Q' THEN '取消'
                    WHEN zab.STATUS = 'G' THEN '关闭' END ABNORMAL_STATUS,
               CASE WHEN zabd.ABNORMAL_METHOD = 'X' THEN '线下换料'
                    WHEN zabd.ABNORMAL_METHOD = 'S' THEN '试装'
                    WHEN zabd.ABNORMAL_METHOD = 'R' THEN '让步放心'
                    WHEN zabd.ABNORMAL_METHOD = 'C' THEN '报废'
                    WHEN zabd.ABNORMAL_METHOD = 'P' THEN '配作'
                    WHEN zabd.ABNORMAL_METHOD = 'F' THEN '返修'
                    ELSE ''
                   END ABNORMAL_METHOD,zabd.RESOLVE_DATE_TIME RESOLVE_DATE_TIME,
               znu3.FULL_NAME RESOLVE_USER,zabd.CLOSED_DATE_TIME CANCEL_DATE_TIME ,
               znu4.FULL_NAME CANCEL_USER,zabd.RESOLVE_REMARK RESOLVE_REMARK,
               zabd.ABNORMAL_REASON ABNORMAL_REASON,zabd.BEFORE_MEASURE BEFORE_MEASURE
        FROM Z_ABNORMAL_BILL zab
                 LEFT JOIN Z_ABNORMAL_NC_CODE zanc ON zanc.ABNORMAL_BILL_BO = zab.HANDLE
                 LEFT JOIN Z_ABNORMAL_BILL_DISPOSE zabd ON zabd.ABNORMAL_BILL_BO = zab.HANDLE
                 LEFT JOIN WORK_CENTER wc ON wc.WORK_CENTER = SUBSTR(zab.OPERATION,1,INSTR(zab.OPERATION,'_')-1)
                 LEFT JOIN NC_CODE nc ON zanc.NC_CODE = nc.NC_CODE AND nc.SITE = zab.SITE
                 LEFT JOIN NC_CODE_T nct ON nct.NC_CODE_BO = nc.HANDLE AND nct.LOCALE = 'zh'
                 LEFT JOIN SHOP_ORDER so ON so.SHOP_ORDER = zab.SHOP_ORDER AND so.SITE = zab.SITE
                 LEFT JOIN CUSTOM_FIELDS cf ON cf.HANDLE = so.HANDLE
                 INNER JOIN ITEM i ON i.HANDLE = zab.ITEM_BO
                 LEFT JOIN ITEM_T it ON it.ITEM_BO = i.HANDLE AND it.LOCALE = 'zh'
                 LEFT JOIN OPERATION o ON o.OPERATION = zab.OPERATION AND o.SITE = zab.SITE
                 LEFT JOIN OPERATION_T ot ON ot.OPERATION_BO = o.HANDLE AND ot.LOCALE = 'zh'
                 LEFT JOIN RESRCE r ON r.RESRCE = zab.RESRCE AND r.SITE = zab.SITE
                 LEFT JOIN Z_NWA_USER znu ON znu.USER_NAME = zab.PB_USER
                 LEFT JOIN Z_NWA_USER znu2 ON znu2.USER_NAME = zab.CREATED_USER
                 LEFT JOIN Z_NWA_USER znu3 ON znu3.USER_NAME = zabd.RESOLVE_USER
                 LEFT JOIN Z_NWA_USER znu4 ON znu4.USER_NAME = zabd.CLOSED_USER
        GROUP BY wc.WORK_CENTER ,zab.PRODUCT_CATEGORY ,cf.VALUE ,so.SHOP_ORDER ,
                 i.ITEM ,it.DESCRIPTION,zab.SFC,zab."TYPE" ,zab.ABNORMAL_NO,
                 zab.OPERATION,ot.DESCRIPTION,r.RESRCE,r.DESCRIPTION,
                 zab.CREATED_DATE_TIME ,znu.FULL_NAME,znu2.FULL_NAME,
                 zab.STATUS,zabd.ABNORMAL_METHOD,zabd.RESOLVE_DATE_TIME ,
                 znu3.FULL_NAME,zabd.CLOSED_DATE_TIME ,znu4.FULL_NAME,
                 zabd.RESOLVE_REMARK,zabd.ABNORMAL_REASON,zabd.BEFORE_MEASURE
        ORDER BY zab.CREATED_DATE_TIME DESC
    </select>
</mapper>