<?xml version="1.0" encoding="UTF-8"?>
<queryMap desc="权限功能表查询">

    <query id="CrmBill.selectCrmBill" desc="查询是否存在">
        SELECT COUNT(1) FROM CRM_BILL_M M LEFT JOIN CRM_BILL_C C ON M.BILL_NO = C.BILL_NO WHERE  M.BILL_NO = ? AND M.DATA_STATUS = ?
    </query>

    <query id="CrmBill.selectCrmBillIsPay" desc="查询是否结算">
        SELECT
        nvl( max( IS_PAY ), 0 ) IS_PAY
        FROM
        CRM_BILL_M M
        WHERE
        M.BILL_NO = ?
    </query>

    <query id="CrmBill.insertCrmBillM" desc="新增">
        INSERT INTO CRM_BILL_M(
            BILL_NO_SEQ,
            BILL_NO,
            ORD_PK,
            ORD_LN_PK,
            ORD_LN_DLY_PK,
            ORDER_NO,
            ORDER_SEQ,
            DELIVERY_NO,
            RECEIV_NO,
            RECEIV_NM,
            SALE_ORG,
            SALE_ORG_DESC,
            ACT_TRANS_TYPE,
            STATION_NO,
            STATION_NM,
            SPCL_LN_NO,
            SPCL_LN_NM,
            PLAN_NUM,
            LOADVEHICLE_TYPE,
            CREATE_NAME,
            CREATE_TIME,
            DATA_STATUS,STORAGE_NAME,TRANSPORT_STATUS,DELVRY_WHEREABOUTS)
            SELECT
            T.BILL_NO_SEQ,
            T.BILL_NO,
            T.ORD_PK,
            T.ORD_LN_PK,
            T.ORD_LN_DLY_PK,
            T.ORDER_NO,
            T.ORDER_SEQ,
            T.DELIVERY_NO,
            T.RECEIV_NO,
            T.RECEIV_NM,
            T.SALE_ORG,
            T.SALE_ORG_DESC,
            T.ACT_TRANS_TYPE,
            T.STATION_NO,
            T.STATION_NM,
            T.SPCL_LN_NO,
            T.SPCL_LN_NM,
            T.PLAN_NUM,
            DECODE(T.LOADVEHICLE_TYPE, '1', '实际', '2', '虚拟'),
            T.CREATE_NAME,
            SYSDATE,
            ?,(SELECT MAX(VSTEL_DESC) FROM YDM_ZC_BILL_C WHERE BILL_NO = T.BILL_NO),
            CASE
            WHEN ? = '1' THEN
                (
                SELECT
                DECODE( max( b.JUDGE_STOVE_NO ), NULL, '0', '1' )
                FROM
                YDM_ZC_BILL_C b
                INNER JOIN YDM_BC_INLIST c ON b.JUDGE_STOVE_NO = c.JUDGE_STOVE_NO
                AND b.BATCH_NO = c.BATCH_NO
                WHERE
                b.BILL_NO = T.BILL_NO
                AND c.OUT_TYPE = '2'
                AND c.DATA_SUORCE = '1'
                AND c.INSTOCK_TYPE_CODE = '800703' and  c.CREATE_TIME > t.CREATE_TIME and ROWNUM = '1'
                )
            ELSE
                '0'
            END TRANSPORT_STATUS,DELVRY_WHEREABOUTS
        FROM YDM_ZC_BILL_M T
        WHERE
        T.BILL_NO = ?
        AND T.VALIDFLAG = '1'
    </query>
    <query id="CrmBill.insertCrmBillC" desc="新增">
        INSERT INTO CRM_BILL_C(
        BILL_NO_SEQ,
        BILL_NO,
        ORD_PK,
        ORD_LN_PK,
        ORD_LN_DLY_PK,
        JUDGE_STOVE_NO,
        BATCH_NO,
        GROUP_NO,
        MATERIAL_NO,
        MATERIAL_NAME,
        PRODUCCODE,
        PRODUCNAME,
        STD_CODE,
        STD_NAME,
        GRADECODE,
        GRADENAME,
        STEELCODE,
        STEELNAME,
        STD_STYLE,
        STD_STYLE_DESC,
        SPEC_CODE,
        SPEC_NAME,
        MODEL_CODE,
        MODEL_DESC,
        ACT_LEN_MIN,
        ACT_LEN_MAX,
        ACT_COUNT,
        ACT_WEIGHT,
        ACT_LEN,
        CREATE_NAME,
        CREATE_TIME,
        DATA_STATUS,STORAGE_NAME)

        SELECT
        M.BILL_NO_SEQ,
        T.BILL_NO,
        T.ORD_PK,
        T.ORD_LN_PK,
        T.ORD_LN_DLY_PK,
        T.JUDGE_STOVE_NO,
        T.BATCH_NO,
        T.GROUP_NO,
        T.MATERIAL_NO,
        T.MATERIAL_NAME,
        T.PRODUCCODE,
        T.PRODUCNAME,
        MAX(T.STD_CODE),
        MAX(T.STD_NAME),
        T.GRADECODE,
        T.GRADENAME,
        T.STEELCODE,
        T.STEELNAME,
        T.STD_STYLE,
        T.STD_STYLE_DESC,
        T.SPEC_CODE,
        T.SPEC_NAME,
        T.MODEL_CODE,
        T.MODEL_DESC,
        T.ACT_LEN_MIN,
        T.ACT_LEN_MAX,
        Sum(T.ACT_COUNT),
        CASE
        MAX(m.LOADVEHICLE_TYPE)
        WHEN '1' THEN
        (
        SELECT
            trim(
                to_char(
                round(
                sum(
                decode(
                Y.PONDER_NO,
                NULL,
                DECODE( D.ORDER_UNIT, '吨', Y.SEND_NUM, Y.ACT_WEIGHT ) ,
                Y.PONDER_GROSS_WT
                )
                ),
                3
                ),
                '99999990.990'
                )
            ) ACT_WEIGHT
        FROM
        YDM_ZC_LOADVEHICLE_MAT Y,
        SLM_ORDER_LINE_DELIVERY D
        WHERE
        Y.ORD_PK = D.ORD_PK ( + )
        AND Y.ORD_LN_PK = D.ORD_LN_PK ( + )
        AND Y.ORD_LN_DLY_PK = D.ORD_LN_DLY_PK ( + )
        AND Y.ORD_LN_DLY_PK = t.ORD_LN_DLY_PK
        AND y.JUDGE_STOVE_NO = t.JUDGE_STOVE_NO
        AND y.BATCH_NO =T.BATCH_NO
        AND y.GROUP_NO =T.GROUP_NO
        AND DECODE( t.ACT_LEN_MIN, NULL,'1',y.ACT_LEN_MIN )= DECODE( t.ACT_LEN_MIN, NULL,'1',t.ACT_LEN_MIN )
        AND DECODE( t.ACT_LEN_MAX, NULL,'1',y.ACT_LEN_MAX )= DECODE( t.ACT_LEN_MAX, NULL,'1',t.ACT_LEN_MAX )
        AND DECODE( t.GRADECODE, NULL,'1',y.GRADECODE )= DECODE( t.GRADECODE, NULL,'1',t.GRADECODE )
        AND DECODE( t.STEELCODE, NULL,'1',y.STEELCODE )= DECODE( t.STEELCODE, NULL,'1',t.STEELCODE )
        AND DECODE( t.SPEC_CODE, NULL,'1',y.SPEC_CODE )= DECODE( t.SPEC_CODE, NULL,'1',t.SPEC_CODE )
        AND DECODE( t.MODEL_CODE, NULL,'1',y.MODEL_CODE )= DECODE( t.MODEL_CODE, NULL,'1',t.MODEL_CODE )
        AND DECODE( t.STORAGE_NO, NULL, '1', y.STORAGE_NO ) = DECODE( t.STORAGE_NO, NULL, '1', t.STORAGE_NO )
        AND Y.BILL_NO = t.BILL_NO
        GROUP BY
        Y.BILL_NO,
        Y.ORDER_NO,
        Y.ORD_LN_DLY_PK,
        y.JUDGE_STOVE_NO,
        to_char( Y.ORDER_SEQ, '000' )
        ) ELSE TO_CHAR(Sum( T.ACT_WEIGHT ))
        END ACT_WEIGHT,
        Sum(T.ACT_LEN),
        T.CREATE_NAME,
        SYSDATE,
        ?,(SELECT STORAGE_NAME FROM YDM_BS_STORAGE WHERE STORAGE_NO = T.STORAGE_NO)
        FROM YDM_ZC_BILL_C T,YDM_ZC_BILL_M M
        WHERE
        T.BILL_NO = M.BILL_NO
        and T.ORD_PK = M.ORD_PK
        and T.ORD_LN_PK = M.ORD_LN_PK
        and T.ORD_LN_DLY_PK = M.ORD_LN_DLY_PK
        AND T.BILL_NO = ?
        AND T.VALIDFLAG = '1'
        GROUP BY
        M.BILL_NO_SEQ,
        T.BILL_NO,
        T.ORD_PK,
        T.ORD_LN_PK,
        T.ORD_LN_DLY_PK,
        T.JUDGE_STOVE_NO,
        T.BATCH_NO,
        T.GROUP_NO,
        T.MATERIAL_NO,
        T.MATERIAL_NAME,
        T.PRODUCCODE,
        T.PRODUCNAME,
        T.GRADECODE,
        T.GRADENAME,
        T.STEELCODE,
        T.STEELNAME,
        T.STD_STYLE,
        T.STD_STYLE_DESC,
        T.SPEC_CODE,
        T.SPEC_NAME,
        T.MODEL_CODE,
        T.MODEL_DESC,
        T.ACT_LEN_MIN,
        T.ACT_LEN_MAX,
        T.CREATE_NAME,T.STORAGE_NO
    </query>
</queryMap>