global class OrderWebService {

    WebService static String checkproduct(String oppid){
        List<OpportunityLineItem> qliList= [select id,Product2.NMPAStatus__c,Product2.ProductCode,OppliOrderIsChange__c,oppLiVerifyProductValid__c from OpportunityLineItem where opportunityId = :oppId];
        String str = '';
        Boolean isok = true;
        for(OpportunityLineItem qli : qliList){
            // WLIG-BTA8C2 XHL 20200925 -Start
            // 合同变更 并且 询价产品的 验证产品有效性 为 false 时跳过验证
            if (qli.OppliOrderIsChange__c && qli.oppLiVerifyProductValid__c == false){
            // WLIG-BTA8C2 XHL 20200925 -End
            } else {
                if(qli.Product2.NMPAStatus__c == '首营状态变更' ||qli.Product2.NMPAStatus__c == '首营不通过'){
                    str += (' '+qli.Product2.ProductCode);
                    isok = false;
                }
            }
            
        }
        System.debug(str);
        if(isok == true){
            return '1';
        }else{
            return str;
        }
    }

    WebService static String checkLicense(String oppid,String ordId){
        String str = '1';
        Date endDate = Date.valueOf('2019-12-28');
        Order ord = [select id,date_P__c from Order where id =:ordId];
         System.debug('endDate:'+endDate+'       date_P__c:'+ord.date_P__c);
        Opportunity oppor = [select id,AccountId,Dealer__r.DummyDealer__c,Dealer__r.ParentID,Account.MarketVerticals__c,Trade_Type_D__c from Opportunity where id = :oppid];
        if(oppor.Account.MarketVerticals__c == 'Clinical'&&oppor.Trade_Type_D__c == 'Taxation' && (ord.date_P__c > endDate || ord.date_P__c == null) ){
            if(oppor.Dealer__r.DummyDealer__c == true){
                str = LicenseCheckUtil.LicenseCheck(oppor.AccountId); 
            }else{
                str = LicenseCheckUtil.LicenseCheck(oppor.Dealer__r.ParentID); 
            }
        }else{
            System.debug('endDate:'+endDate+'       date_P__c:'+ord.date_P__c);
        }
        return str;

    }

    WebService static String checkaddress(String oppid,String address,String ordId){
        String str = '1';
        Date endDate = Date.valueOf('2019-12-28');
        Opportunity oppor = [select id,AccountId,Dealer__r.DummyDealer__c,Dealer__r.ParentID,Account.MarketVerticals__c,Trade_Type_D__c from Opportunity where id = :oppId];
        Order ord = [select id,date_P__c,Level_Category__c from Order where id =:ordId];
        List<String> LcList = new List<String>();
        LcList.add(ordId);
        String LevelCategory = OrderTriggerHandler.levelCheck(LcList).get(ordId);
        if(oppor.Account.MarketVerticals__c == 'Clinical'&&oppor.Trade_Type_D__c == 'Taxation' && (ord.date_P__c > endDate || ord.date_P__c == null) && LevelCategory =='二类'){
            if(oppor.Dealer__r.DummyDealer__c == true){
                str = LicenseCheckUtil.AddressCheck(oppor.AccountId,address) == true ? '1' : '0';    
            }else{
                str = LicenseCheckUtil.AddressCheck(oppor.Dealer__r.ParentID,address) == true ? '1' : '0';

            }
        }else{
            System.debug('endDate:'+endDate+'       date_P__c:'+ord.date_P__c);
        }
        return str;
    }

    WebService static String checkLicense1(String oppid){
        String str = '1';
        List<OpportunityLineItem> oliList = [select id,Product2.If_Exempt_Product__c,Product2.If_Radiation_Product__c,Opportunity.DealerId__c from OpportunityLineItem where opportunityId = :oppId];
            Boolean hasA = false;
            for(OpportunityLineItem oli : oliList){
                if(oli.Product2.If_Exempt_Product__c == false && oli.Product2.If_Radiation_Product__c == true){
                    hasA = true;
                }
            }
            if(hasA == true){
                str =  LicenseCheckUtil.LicenseCheck1(oliList[0].Opportunity.DealerId__c);
            }
        return str;
    }
    /**
     * 检查合同提交审批时，所购买的辐射产品数量，是否超过"奥林巴斯代理商与合同代理商"的可售限制
     * 目前用在-合同：提交审批按钮
     * @Author   XHL
     * @DateTime 2020-02-11
     * @param    orderId    [合同Id]
     * @param    dealerId   [合同对应的代理商Id]
     * @return              [异常信息]
     */
     WebService static String checkUpperLimitOfSales(String orderId,String dealerId){
        String result = '';
        String olympusAccountId = System.label.Olympus_Id;
        Map<String, Integer> getModelQuantityMap = RadiationUtil.GetModelQuantityMap(orderId);
        String errorStr = RadiationUtil.GetCertificationDetail(dealerId,getModelQuantityMap);
        if(errorStr != ''){
           return '-1';
        }
        String accountDealerErrorMessage = RadiationUtil.updateCertificationDetails(dealerId, null, getModelQuantityMap, false);
        //String olympusDealerErrorMessage = RadiationUtil.updateCertificationDetails(olympusAccountId, null, getModelQuantityMap, false);
        result = accountDealerErrorMessage;
        Boolean temp = result.startsWith('你不能');
        if(temp){
            return '-1';
        }else{
            return result;
        }
        return result;
    }

    WebService static String checkUpperLimitOfSalesOly(String orderId,String dealerId){
        String result = '';
        String olympusAccountId = System.label.Olympus_Id;
        Map<String, Integer> getModelQuantityMap = RadiationUtil.GetModelQuantityMap(orderId);
        /*String errorStr = RadiationUtil.GetCertificationDetail(dealerId,getModelQuantityMap);
        if(errorStr != ''){
           return '-1';
        }
        String accountDealerErrorMessage = RadiationUtil.updateCertificationDetails(dealerId, null, getModelQuantityMap, false);*/
        String olympusDealerErrorMessage = RadiationUtil.updateCertificationDetails(olympusAccountId, null, getModelQuantityMap, false);
        result = olympusDealerErrorMessage == '' ? '' : '奥林巴斯可销售数量不足' ;
        Boolean temp = result.startsWith('你不能');
        if(temp){
            return '-1';
        }else{
            return result;
        }
        return result;
    }


    WebService static String updItems(String oppId, String orderId) {
        String rs = '';

        List<Order> odrList = [select Id, OpportunityId, Opportunity.Model_product_cnt__c, QuoteId from Order where Id = :orderId];
        Order odr = odrList.size() > 0 ? odrList[0] : null;
        /*if (odr.Opportunity.Model_product_cnt__c > 0) {
            return '询价产品中有产品型号，不能同步。';
        }*/
        List<OrderItem> oiList = [select Id from OrderItem where OrderId = :orderId];

        List<Opportunity> oppList = [select Id, SyncedQuoteId, Quote_Discount__c, Quote_Subtotal__c, Quote_TotalPrice__c,
                                            Quote_Warranty__c, Quote_PaymentTerms_Text__c, Quote_ShipmentTerm__c, Quote_ShipmentTerm2__c,
                                            Quote_CustomPrice__c
                                       from Opportunity where Id = :oppId];
        Opportunity opp = oppList.size() > 0 ? oppList[0] : null;
        List<OpportunityLineItem> oliList = [select Id, OpportunityId, Description, Quantity, PricebookEntryId, UnitPrice from OpportunityLineItem where OpportunityId = :oppId order by id];

        Quote quo = null;
        List<QuoteLineItem> qliList = null;
         List<DecideQuoteProductSet__c> dqpsList = new  List<DecideQuoteProductSet__c>();
        if (opp != null && opp.SyncedQuoteId != null) {
            List<Quote> quoList = [select Id from Quote where Id = :opp.SyncedQuoteId];
            quo = quoList.size() > 0 ? quoList[0] : null;

            qliList = [select Id, QuoteId, Description, Quantity, PricebookEntryId, UnitPrice from QuoteLineItem where QuoteId = :opp.SyncedQuoteId];
            dqpsList = [select id from DecideQuoteProductSet__c where DecideQuote__c =: opp.SyncedQuoteId];
            System.debug(dqpsList);
            for(DecideQuoteProductSet__c dqps : dqpsList){
                dqps.DecideOrder__c = orderId;
            }
        }

        boolean needupd = false;
//        if (odr.QuoteId != opp.SyncedQuoteId) {
            
            odr.QuoteId = opp.SyncedQuoteId;
            Decimal quoteDiscount = opp.Quote_Discount__c;
            //odr.Discount__c = opp.Quote_Discount__c;
            odr.Discount_D__c = quoteDiscount.setScale(4, System.RoundingMode.HALF_UP);
            Decimal quoteSubtotal = opp.Quote_Subtotal__c;
            //odr.Olympus_Price_BeforeDiscount__c = opp.Quote_Subtotal__c;
            odr.Olympus_Price_BeforeDiscount_D__c = quoteSubtotal.setScale(2, System.RoundingMode.HALF_UP);
            Decimal quoteTotalPrice = opp.Quote_TotalPrice__c;
            //odr.OlympusContractPrices__c = opp.Quote_TotalPrice__c;
            odr.OlympusContractPricesD__c = quoteTotalPrice.setScale(0, System.RoundingMode.HALF_UP);
            //odr.SpecialWarranty__c = opp.Quote_Warranty__c;
            odr.SpecialWarranty_D__c = String.valueOf(opp.Quote_Warranty__c);
            //odr.PaymentCondition__c = opp.Quote_PaymentTerms_Text__c;
            odr.PaymentCondition_D__c = opp.Quote_PaymentTerms_Text__c;
            //odr.Shipment_Term__c = opp.Quote_ShipmentTerm__c;
            odr.Shipment_Term_D__c = opp.Quote_ShipmentTerm__c;
            //odr.Shipment_Term2__c = opp.Quote_ShipmentTerm2__c;
            odr.Shipment_Term2_D__c = opp.Quote_ShipmentTerm2__c;
            Decimal quoteCustomPrice = opp.Quote_CustomPrice__c;
            //odr.CustomerContractPrice__c = opp.Quote_CustomPrice__c;
            odr.CustomerContractPriceD__c = quoteCustomPrice.setScale(2, System.RoundingMode.HALF_UP);

            odr.product_approval__c = false;
            needupd = true;
//        }

        List<OrderItem> insList = new List<OrderItem>();
        if (opp.SyncedQuoteId != null) {
            for (QuoteLineItem qli : qliList) {
                OrderItem oi = new OrderItem();
                oi.Description = qli.Description;
                oi.Quantity = qli.Quantity;
                oi.PricebookEntryId = qli.PricebookEntryId;
                oi.UnitPrice = qli.UnitPrice;
                oi.OrderId = orderId;
                oi.QuoteLineItemId = qli.Id;
                insList.add(oi);
            }
        } else {
            for (OpportunityLineItem oli : oliList) {
                OrderItem oi = new OrderItem();
                oi.Description = oli.Description;
                oi.Quantity = oli.Quantity;
                oi.PricebookEntryId = oli.PricebookEntryId;
                oi.UnitPrice = oli.UnitPrice;
                oi.OrderId = orderId;
                insList.add(oi);
            }
        }

        try {
            if (needupd == true){ 
                update odr;
                update dqpsList;
            }
            if (oiList.size() > 0) delete oiList;
            if (insList.size() > 0) insert insList;
            rs = '1';
        } catch (Exception e) {
            rs = e.getMessage();
        }

        return rs;
    }

    WebService static String copyOrder(String orderId) {
        String rs = '';

        // 取得合同、合同产品
        Order old_odr = null;
        List<OrderItem> old_oi = new List<OrderItem>();

        Schema.DescribeSobjectResult d_odr = Order.sObjectType.getDescribe();
        Map<String, Schema.SObjectField> d_odr_map = d_odr.fields.getMap();
        String soql_odr = 'select ';
        String fields_odr = '';
        for (String field : d_odr_map.keySet()) {
            if (fields_odr.length() > 0) {
                fields_odr += ', ';
            }
            fields_odr += field;
        }
        soql_odr += fields_odr;
        soql_odr += ' from Order where Id = \'' + orderId + '\'';
        //List<Order> odrList = [select Id, OpportunityId, QuoteId from Order where Id = :orderId];
        List<Order> odrList = Database.query(soql_odr);

        if (odrList.size() > 0) {
            old_odr = odrList[0];
        }

        // 合同存在check
        if (old_odr == null) {
            rs = 'SSBD合同不存在。';
            return rs;
        }

        Schema.DescribeSobjectResult d_oi = OrderItem.sObjectType.getDescribe();
        Map<String, Schema.SObjectField> d_oi_map = d_oi.fields.getMap();
        String soql_oi = 'select ';
        String fields_oi = '';
        for (String field : d_oi_map.keySet()) {
            if (fields_oi.length() > 0) {
                fields_oi += ', ';
            }
            fields_oi += field;
        }
        soql_oi += fields_oi;
        soql_oi += ' from OrderItem where OrderId = \'' + orderId + '\'';
        //List<OrderItem> old_oi = [select Id from OrderItem where OrderId = :orderId];
        old_oi = Database.query(soql_oi);

        // 询价存在check
        if (old_odr.OpportunityId == null) {
            rs = 'SSBD合同数据不正确，询价为空。';
            return rs;
        }

        // 取得询价、询价产品
        Opportunity old_opp = null;
        List<Opportunity> oppList = [select Id, SyncedQuoteId from Opportunity where Id = :old_odr.OpportunityId];
        //List<OpportunityLineItem> old_oli = [select Id from OpportunityLineItem where OpportunityId = :old_odr.OpportunityId];
        if (oppList.size() > 0) {
            old_opp = oppList[0];
        }

        // 同步中报价一致check
        if (old_opp.SyncedQuoteId != old_odr.QuoteId) {
            rs = 'SSBD合同数据不正确，同步中报价错误。';
            return rs;
        }

        // 同步中报价、取得同步中报价、报价产品
        Quote old_quo = null;
        List<QuoteLineItem> old_qli = new List<QuoteLineItem>();
        List<DecideQuoteProductSet__c> oldDqps = new List<DecideQuoteProductSet__c>();
        if (old_odr.QuoteId == null) {

        } else {
            Schema.DescribeSobjectResult d_quo = Quote.sObjectType.getDescribe();
            Map<String, Schema.SObjectField> d_quo_map = d_quo.fields.getMap();
            String soql_quo = 'select ';
            String fields_quo = '';
            for (String field : d_quo_map.keySet()) {
                if (fields_quo.length() > 0) {
                    fields_quo += ', ';
                }
                fields_quo += field;
            }
            soql_quo += fields_quo;
            soql_quo += ' from Quote where Id = \'' + old_odr.QuoteId + '\'';
            //List<Quote> quoList = [select Id from Quote where Id = :old_odr.QuoteId];
            List<Quote> quoList = Database.query(soql_quo);

            if (quoList.size() > 0) {
                old_quo = quoList[0];

                oldDqps = [select id,DecideProductSet__c,DecideProductSetCount__c,DecideProductSetCode__c,DecideProductSetName__c from DecideQuoteProductSet__c where DecideQuote__c =:old_quo.id];
                
                //delete oldDqps;
            }



            Schema.DescribeSobjectResult d_qli = QuoteLineItem.sObjectType.getDescribe();
            Map<String, Schema.SObjectField> d_qli_map = d_qli.fields.getMap();
            String soql_qli = 'select ';
            String fields_qli = '';
            for (String field : d_qli_map.keySet()) {
                if (fields_qli.length() > 0) {
                    fields_qli += ', ';
                }
                fields_qli += field;
            }
            soql_qli += fields_qli;
            soql_qli += ' from QuoteLineItem where QuoteId = \'' + old_odr.QuoteId + '\'';
            //old_qli = [select Id from QuoteLineItem where QuoteId = :old_odr.QuoteId];
            old_qli = Database.query(soql_qli);
        }

        Savepoint sp = Database.setSavepoint();
        try {
            Quote new_quo = null;
            if (old_quo != null) {
                new_quo = old_quo.clone();
                new_quo.Id = null;
                new_quo.Is_Decided__c = false;
                new_quo.Is_upload__c = false;
                new_quo.OrderIsChange__c = true;
                insert new_quo;

                List<QuoteLineItem> new_qli = new List<QuoteLineItem>();
                for (QuoteLineItem qli : old_qli) {
                    QuoteLineItem new_i = qli.clone();
                    new_i.Id = null;
                    new_i.QuoteId = new_quo.Id;
                    new_qli.add(new_i);
                }

                insert new_qli;

                old_quo.Is_Decided__c = false;
                //old_quo.Is_upload__c = fasle;
                update old_quo;

                //old_opp.SyncedQuoteId = new_quo.Id;
                old_opp.Is_Decided__c = false;
                //old_opp.SAP_Upload__c = false;
                //old_opp.The_Upload_of_quotation_number__c = null;
                // WLIG-BTA8C2 XHL 20200925 -Start
                old_opp.OrderIsChange__c = true;
                // WLIG-BTA8C2 XHL 20200925 -End
                update old_opp;
            } else {
                // WLIG-BTA8C2 XHL 20200925 -Start
                old_opp.OrderIsChange__c = true;
                update old_opp;
                // WLIG-BTA8C2 XHL 20200925 -End
            }

            Order new_odr = old_odr.clone();
            new_odr.Id = null;
            new_odr.Status__c = 'Active';
            new_odr.ContractCode__c = null;
            new_odr.IsNew__c = true;
            new_odr.IsSpPassed__c = false;
            //new_odr.product_approval__c = false;
            new_odr.IsOrderPassed__c = false;
            new_odr.IsCopy__c = true;
            new_odr.IsUpload__c = false;
            new_odr.Is_Already_Splited__c = false;
            new_odr.CopyStatus__c = new_odr.ApproveStatus__c;
            new_odr.original_order__c = old_odr.Id;
//            if (new_quo != null) {
//                new_odr.QuoteId = new_quo.Id;
//            }
            // WLIG-BTA8C2 XHL 20200925 -Start
            new_odr.OrderIsChange__c = true;
            // WLIG-BTA8C2 XHL 20200925 -End
            old_odr.Status__c = 'Inactive';
            List<Order> updateList = new List<Order>();
            updateList.add(old_odr);
            update old_odr;

            insert new_odr;
            if(old_odr.Association_Order__c != null){
                Order o = new Order();
                o.Id = old_odr.Association_Order__c;
                o.Association_Order__c = new_odr.Id;
                update o;
            }

//            List<OrderItem> new_oi = new List<OrderItem>();
//            for (OrderItem oi : old_oi) {
//                OrderItem new_i = oi.clone();
//                new_i.Id = null;
//                new_i.OrderId = new_odr.Id;
//                new_oi.add(new_i);
//            }

//            insert new_oi;
            if(oldDqps.size() > 0){
                for(DecideQuoteProductSet__c dqps : oldDqps){
                    dqps.DecideQuote__c = new_quo.Id;
                    dqps.DecideOrder__c = new_odr.Id;
                }

                update oldDqps;
            }


            //王鹏伟 开始 合同变更（复制源合同(变更)）同步复制贸易管理客户申请表
            if(new_odr!=null && new_odr.Id !=null){
                Schema.DescribeSobjectResult tradeFields = TradeCustomerManager__c.sObjectType.getDescribe();
                Map<String, Schema.SObjectField> tradeFields_map = tradeFields.fields.getMap();
                String tradeList_sql = 'select ';
                String fields_trade = '';
                for (String field : tradeFields_map.keySet()) {
                    if (fields_trade.length() > 0) {
                        fields_trade += ', ';
                    }
                    fields_trade += field;
                }
                tradeList_sql += fields_trade;
                tradeList_sql += ' from TradeCustomerManager__c where order__c = \'' + old_odr.Id + '\'';
                List<TradeCustomerManager__c> tradeList = Database.query(tradeList_sql);
                if(tradeList!=null && tradeList.size()!=0){
                    TradeCustomerManager__c newTrade = tradeList[0].clone();
                    newTrade.Id = null;
                    newTrade.OrderIdSave__c = new_odr.Id;
                    newTrade.order__c = new_odr.Id;
                    insert newTrade;
                }
            }
            //王鹏伟 结束
            
            rs = new_odr.Id;
        } catch (Exception ex) {
            Database.rollback(sp);
            rs = ex.getMessage() + '=====' + ex.getStackTraceString();
        }

        return rs;
    }

    WebService static String selectOrder(String orderId) {
        String rs = '';

        // 取得目标合同、合同产品
        List<Order> odrList = [select Id, OpportunityId, QuoteId, Quote.Is_Decided__c, Quote.Is_upload__c, Quote.QuoteNumber from Order where Id = :orderId];
        Order odr = null;
        if (odrList.size() > 0) {
            odr = odrList[0];
        }

        // 合同存在check
        if (odr == null) {
            rs = 'SSBD合同不存在。';
            return rs;
        }

        List<OrderItem> odr_oi = new List<OrderItem>();
        if (odr.QuoteId == null) {
            odr_oi = [select Id, Description, Quantity, UnitPrice, PricebookEntryId, PricebookEntry.Product2Id from OrderItem where OrderId = :orderId];
        }

        // 取得询价
        List<Opportunity> oppList = [select Id from Opportunity where Id = :odr.OpportunityId];
        Opportunity opp = null;
        if (oppList.size() > 0) {
            opp = oppList[0];
        }
        List<OpportunityLineItem> oliList = [select Id from OpportunityLineItem where OpportunityId = :odr.OpportunityId];

        // 取得其他有效合同
        List<Order> odrList2 = [select Id from Order where OpportunityId = :opp.Id and Status__c = 'Active' and Id <> :orderId];

        Savepoint sp = Database.setSavepoint();
        try {
            if (odr.QuoteId != null) {
                opp.SyncedQuoteId = odr.QuoteId;
                opp.Is_Decided__c = odr.Quote.Is_Decided__c;
                opp.SAP_Upload__c = odr.Quote.Is_upload__c;
                opp.The_Upload_of_quotation_number__c = odr.Quote.Is_upload__c == true ? odr.Quote.QuoteNumber : null;
            } else {
                opp.SyncedQuoteId = null;
                opp.Is_Decided__c = false;
                opp.SAP_Upload__c = false;
                opp.The_Upload_of_quotation_number__c = null;

                List<String> prdList = new List<String>();
                for (OrderItem oi : odr_oi) {
                    prdList.add(oi.PricebookEntry.Product2Id);
                }
                Map<String, String> prdMap = new Map<String, String>();
                if (prdList.size() > 0) {
                    List<Product_Search__c> psList = [select Id,Product__c from Product_Search__c where Product__c = :prdList];
                    for (Product_Search__c ps : psList) {
                        prdMap.put(ps.Product__c, ps.Id);
                    }
                }

                List<OpportunityLineItem> ins_oli = new List<OpportunityLineItem>();
                for (OrderItem oi : odr_oi) {
                    OpportunityLineItem oli = new OpportunityLineItem();
                    oli.OpportunityId = opp.Id;
                    oli.Description = oi.Description;
                    oli.DescriptionD__c = oi.Description;
                    oli.Quantity = oi.Quantity;
                    oli.QuantityD__c = oi.Quantity;
                    oli.UnitPrice = oi.UnitPrice;
                    oli.UnitPriceD__c = oi.UnitPrice;
                    oli.PricebookEntryId = oi.PricebookEntryId;
                    oli.Product_Search__c = prdMap.get(oi.PricebookEntry.Product2Id);
                    ins_oli.add(oli);
                }

                if (oliList.size() > 0) {
                    delete oliList;
                }

                insert ins_oli;
            }

            update opp;

            odr.Status__c = 'Active';
            update odr;

            if (odrList2.size() > 0) {
                for (Order o : odrList2) {
                    o.Status__c = 'Inactive';
                }
                update odrList2;
            }

            rs = '1';
        } catch (Exception ex) {
            Database.rollback(sp);
            rs = ex.getMessage() + '=====' + ex.getStackTraceString();
        }

        return rs;
    }

    WebService static String checkOly() { 
         return LicenseCheckUtil.LicenseCheckOly();
    }

    WebService static Boolean IfspecialDealer(String oppDealerId) {
        if (String.isBlank(oppDealerId)) {
            return false;
        }
        return StaticParameter.specialDealerMap1.containsKey(oppDealerId.substring(0, 15));
    }

    WebService static Boolean checkSpecialDeliveryContact(String contactId) {
        if (String.isBlank(contactId)){
            return false;
        }
        List<Contact> contactList = [select Id from Contact 
                                    where Id = :contactId 
                                    AND ContactStatus__c = 'Cancel'];
        if (contactList.size() > 0){
            return true;
        } else {
            return false;
        }
    }
    //王鹏伟新建方法 【委托】上传合同时增加对客户/联系人审批状态的检查 WLIG-BVE68E
    WebService static String checkAccountContact(String orderId){
        String str = '';

        List<Order> orderList = [select Id,AccountId,SpecialDeliveryAccount_D__c,ForeignTradeCompany_D__c,EndUserD__c,SpecialDeliveryContact2_D__c,SpecialDeliveryContact2__c from Order where Id=:orderId];
        if(orderList!=null && orderList.size()!=0){
            List<String> accountId = new List<String>();
            accountId.add(orderList[0].AccountId);
            accountId.add(orderList[0].SpecialDeliveryAccount_D__c);
            accountId.add(orderList[0].ForeignTradeCompany_D__c);
            List<Account> accountList = [select Id,Name,stautesD__c from Account where id=:accountId];
            if(accountList!=null && accountList.size()!=0){
                for(Account acc:accountList){
                    if(String.isNotBlank(orderList[0].AccountId) && acc.Id == orderList[0].AccountId && acc.stautesD__c != 'Pass'){
                        str += '客户审批未通过；\r';
                    }
                    if(String.isNotBlank(orderList[0].SpecialDeliveryAccount_D__c) && acc.Id == orderList[0].SpecialDeliveryAccount_D__c && acc.stautesD__c != 'Pass'){
                        str += '送达方审批未通过；\r';
                    }
                    if(String.isNotBlank(orderList[0].ForeignTradeCompany_D__c) && acc.Id == orderList[0].ForeignTradeCompany_D__c && acc.stautesD__c != 'Pass'){
                        str += '外贸公司审批未通过；\r';
                    }
                }
            }
            List<String> contactId = new List<String>();
            contactId.add(orderList[0].EndUserD__c);
            contactId.add(orderList[0].SpecialDeliveryContact2__c);
            contactId.add(orderList[0].SpecialDeliveryContact2_D__c);
            if(contactId.size()!=0){
                List<Contact> contactList = [select Id,StatusD__c from Contact where Id=:contactId];
                if(contactList!=null && contactList.size()!=0){
                    for(Contact con:contactList){
                        if(String.isNotBlank(orderList[0].EndUserD__c) && con.Id == orderList[0].EndUserD__c && con.StatusD__c != 'Pass'){
                            str += '最终用户审批未通过；\r';
                        }
                        if(String.isNotBlank(orderList[0].SpecialDeliveryContact2_D__c) && con.Id == orderList[0].SpecialDeliveryContact2_D__c && con.StatusD__c != 'Pass'){
                            str += '送达方联系人审批未通过；\r';
                        }
                        if((orderList[0].SpecialDeliveryContact2__c!=orderList[0].SpecialDeliveryContact2_D__c)&&(String.isNotBlank(orderList[0].SpecialDeliveryContact2__c) && con.Id == orderList[0].SpecialDeliveryContact2__c && con.StatusD__c != 'Pass')){
                            str += '送达方（外贸公司）联系人审批未通过；\r';
                        }
                    }
                }
            }
        }
        if(str == ''){
            return '1';
        }
        return str;
    }
//产品首营状态Z5 XHL 2021/02/07 Start
    // 报价库存检查
    WebService static String OrderInventoryCheck(String orderId) {
        String result = '1';
        if (String.isNotBlank(orderId)) {
            String FGSPStr = '';
            // 获取合同的 整机/零件(询价上的)
            List<Order> orderList = [SELECT Id, Name, Opportunity.Machine_Parts__c FROM Order WHERE Id = :orderId limit 1];
            if(orderList.size() > 0) {
                String machine_Parts = orderList[0].Opportunity.Machine_Parts__c;
                if (String.isNotBlank(machine_Parts) && 'Machine'.equals(machine_Parts)) {
                    FGSPStr = 'FG';    
                } else if (String.isNotBlank(machine_Parts) && 'Parts'.equals(machine_Parts)){
                    FGSPStr = 'SP';
                }
            }

            // 获取产品的首营状态与数量
            List<OrderItem> ordLiList = [SELECT Id, Quantity, PricebookEntry.ProductCode, Order.ProductSegment__c, PricebookEntry.Product2.NMPAStatus__c
            FROM OrderItem WHERE OrderId = :orderId ];
            Map<String,Decimal> productQuantityMap = new  Map<String,Decimal>();
            for (OrderItem orderItem : ordLiList) {
                if ('停产'.equals(orderItem.PricebookEntry.Product2.NMPAStatus__c)) {
                    String productSegment = orderItem.Order.ProductSegment__c == 'BS' ? 'LS':orderItem.Order.ProductSegment__c;
                    String managementCode = orderItem.PricebookEntry.ProductCode + '_' + productSegment  + '_' + FGSPStr;

                    Decimal quantity = orderItem.Quantity;
                    if (productQuantityMap.containsKey(managementCode)) {
                       quantity +=  productQuantityMap.get(managementCode);
                    }
                    productQuantityMap.put(managementCode,quantity);

                }
            }

            if (productQuantityMap != null && productQuantityMap.size() > 0) {
                result = ProductInventoryCheck(productQuantityMap);
            }
        } else {
            return '0';
        }

        return result;
    }
    // 询价库存检查
    WebService static String  OpportunityInventoryCheck(String opportunityId) {
        String result = '1';
        if (String.isNotBlank(opportunityId)) {
            String FGSPStr = '';
            
            List<Opportunity> opportunityList = [SELECT Id, Name,Machine_Parts__c,ProductSegment__c FROM Opportunity WHERE Id = :opportunityId limit 1];
            if(opportunityList.size() > 0) {
                String machine_Parts = opportunityList[0].Machine_Parts__c;
                if (String.isNotBlank(machine_Parts) && 'Machine'.equals(machine_Parts)) {
                    FGSPStr = 'FG';    
                } else if (String.isNotBlank(machine_Parts) && 'Parts'.equals(machine_Parts)){
                    FGSPStr = 'SP';
                }
            }

            // 获取产品的首营状态与数量
            List<OpportunityLineItem> oppLiList = [SELECT Id, Quantity, PricebookEntry.ProductCode, ProductSegment__c, PricebookEntry.Product2.NMPAStatus__c
            FROM OpportunityLineItem WHERE OpportunityId = :opportunityId ];
            Map<String,Decimal> productQuantityMap = new  Map<String,Decimal>();
            for (OpportunityLineItem oppItem : oppLiList) {
                if ('停产'.equals(oppItem.PricebookEntry.Product2.NMPAStatus__c)) {
                    String productSegment = oppItem.ProductSegment__c == 'BS' ? 'LS':oppItem.ProductSegment__c;
                    String managementCode = oppItem.PricebookEntry.ProductCode + '_' + productSegment  + '_' + FGSPStr;

                    Decimal quantity = oppItem.Quantity;
                    if (productQuantityMap.containsKey(managementCode)) {
                        quantity += productQuantityMap.get(managementCode);
                    }
                    productQuantityMap.put(managementCode,quantity);

                }
            }

            if (productQuantityMap != null && productQuantityMap.size() > 0) {
                result = ProductInventoryCheck(productQuantityMap);
            }
        } else {
            return '0';
        }

        return result;
    }
    // 报价库存检查
    WebService static String  QuoteInventoryCheck(String quoteId) {
        String result = '1';
        if (String.isNotBlank(quoteId)) {
            String FGSPStr = '';
            String productSegment = '';
            
            List<Quote> quoteList = [SELECT Id, Name, Opportunity.Machine_Parts__c,ProductSegment__c FROM Quote WHERE Id = :quoteId limit 1];
            if(quoteList.size() > 0) {
                String machine_Parts = quoteList[0].Opportunity.Machine_Parts__c;
                productSegment = quoteList[0].ProductSegment__c == 'BS' ? 'LS':quoteList[0].ProductSegment__c;
                if (String.isNotBlank(machine_Parts) && 'Machine'.equals(machine_Parts)) {
                    FGSPStr = 'FG';    
                } else if (String.isNotBlank(machine_Parts) && 'Parts'.equals(machine_Parts)){
                    FGSPStr = 'SP';
                }
            }

            // 获取产品的首营状态与数量
            List<QuoteLineItem> quoLiList = [SELECT Id, Quantity, PricebookEntry.ProductCode,PricebookEntry.Product2.NMPAStatus__c
            FROM QuoteLineItem  WHERE QuoteId = :quoteId ];
            Map<String,Decimal> productQuantityMap = new  Map<String,Decimal>();
            for (QuoteLineItem  quoItem : quoLiList) {
                if ('停产'.equals(quoItem.PricebookEntry.Product2.NMPAStatus__c)) {
                    
                    String managementCode = quoItem.PricebookEntry.ProductCode + '_' + productSegment  + '_' + FGSPStr;
                    Decimal quantity = quoItem.Quantity;
                    if (productQuantityMap.containsKey(managementCode)) {
                        quantity += productQuantityMap.get(managementCode);
                    }
                    productQuantityMap.put(managementCode,quantity);

                }
            }

            if (productQuantityMap != null && productQuantityMap.size() > 0) {
                result = ProductInventoryCheck(productQuantityMap);
            }
        } else {
            return '0';
        }

        return result;
    }
    // 产品库存检查
    public static String ProductInventoryCheck(Map<String,Decimal> productQuantityMap) {
        String result = '1';
        System.debug('productQuantityMap--->'+productQuantityMap);
        Date today = Date.today();
       // 为了避免出现问题库存数量，验证只取当天的库存
        List<ProductInventory__c> getProductInventoryList = [SELECT Id, Name,TransforDate__c, ProductCode__c, Plant__c, ProductSegment__c, ProductType__c, InventoryQuantity__c, ManagementCode__c,TradeType__c FROM ProductInventory__c WHERE ManagementCode__c IN :productQuantityMap.keySet() AND TransforDate__c =today AND TradeType__c = 'Taxation'];
        System.debug('getProductInventoryList--->'+getProductInventoryList);
        List<String> errorList = new List<String>();
        if (getProductInventoryList.size() > 0) {
            // 
            Map<String,Decimal> existingStockMap = new  Map<String,Decimal>();
            for (ProductInventory__c inventory :getProductInventoryList) {
                String managementCode = inventory.ManagementCode__c;
                Decimal inventoryQuantity = inventory.InventoryQuantity__c;

                if (existingStockMap.containsKey(managementCode)) {
                    inventoryQuantity += existingStockMap.get(managementCode);
                } 
                existingStockMap.put(managementCode, inventoryQuantity);
            }
            // 
            
            System.debug('existingStockMap--->'+existingStockMap);
            for (String managementCode:productQuantityMap.keySet()) {

                Decimal salesQuantity =  productQuantityMap.get(managementCode);
                String productCode = managementCode.split('_')[0];
                String error = '';

                if(existingStockMap.containsKey(managementCode)) {

                    Decimal inventoryQuantity = existingStockMap.get(managementCode);

                    if (salesQuantity > inventoryQuantity) {
                        productCode = productCodeFormat(productCode);
                        error = '产品 [ ' +productCode+' ] 库存不足';
                        errorList.add(error);   
                    } 
                } else {
                    // 提示产品无库存
                    productCode = productCodeFormat(productCode);
                    error = '产品 [ ' +productCode+' ] 库存不足';
                    errorList.add(error);
                }
            }
        } else {
            // 提示产品无库存
            for (String managementCode:productQuantityMap.keySet()) {

                String productCode = managementCode.split('_')[0];
                productCode = productCodeFormat(productCode);
                String error = '产品 [ ' +productCode+' ] 库存不足';
                errorList.add(error);
            }
        }
        

        if (errorList.size() > 0) {
            result = '';
            for (String error:errorList){
                result += error + ';';
            }
        }
        System.debug('result--->'+result);
        return result;
    }
    // 产品库存变更
    WebService static String OrderUpdateInventory(String orderId) {
        String result = '1';
        if (String.isNotBlank(orderId)) {
            String FGSPStr = '';
            // 获取合同的 整机/零件(询价上的)
            List<Order> orderList = [SELECT Id, Name, Opportunity.Machine_Parts__c FROM Order WHERE Id = :orderId limit 1];
            if (orderList.size() > 0) {
                String machine_Parts = orderList[0].Opportunity.Machine_Parts__c;
                if (String.isNotBlank(machine_Parts) && 'Machine'.equals(machine_Parts)) {
                    FGSPStr = 'FG';
                } else if (String.isNotBlank(machine_Parts) && 'Parts'.equals(machine_Parts)) {
                    FGSPStr = 'SP';
                }
            }

            // 获取产品的首营状态与数量
            List<OrderItem> ordLiList = [SELECT Id, Quantity, PricebookEntry.ProductCode, Order.ProductSegment__c, PricebookEntry.Product2.NMPAStatus__c
                                         FROM OrderItem WHERE OrderId = :orderId ];
            Map<String, Decimal> productQuantityMap = new  Map<String, Decimal>();
            for (OrderItem orderItem : ordLiList) {
                if ('停产'.equals(orderItem.PricebookEntry.Product2.NMPAStatus__c)) {
                    String productSegment = orderItem.Order.ProductSegment__c == 'BS' ? 'LS' : orderItem.Order.ProductSegment__c;
                    String managementCode = orderItem.PricebookEntry.ProductCode + '_' + productSegment  + '_' + FGSPStr;
                    Decimal quantity = orderItem.Quantity;
                    if (productQuantityMap.containsKey(managementCode)) {
                        quantity += productQuantityMap.get(managementCode);
                    }

                    productQuantityMap.put(managementCode, quantity);

                }
            }
            List<String> errorList = new List<String>();
            if (productQuantityMap != null && productQuantityMap.size() > 0) {
                System.debug('productQuantityMap--->'+productQuantityMap);
                Date today = Date.today();
                // 为了避免出现问题库存数量，验证只取当天的库存
                List<ProductInventory__c> getProductInventoryList = [SELECT Id, Name,TransforDate__c, ProductCode__c, Plant__c, ProductSegment__c, ProductType__c, InventoryQuantity__c, ManagementCode__c FROM ProductInventory__c WHERE ManagementCode__c IN :productQuantityMap.keySet() AND TransforDate__c = today AND TradeType__c = 'Taxation'];

                if (getProductInventoryList.size() > 0) {
                    String managementCode = '';
                    Decimal inventoryQuantity = 0;
                    Decimal salesQuantity = 0;
                    for (ProductInventory__c inventory : getProductInventoryList) {
                        managementCode = inventory.ManagementCode__c;
                        inventoryQuantity = inventory.InventoryQuantity__c;
                        salesQuantity = productQuantityMap.get(managementCode);
                        if (inventoryQuantity >= salesQuantity) {
                            inventory.InventoryQuantity__c = inventoryQuantity - salesQuantity;
                            productQuantityMap.put(managementCode, 0);
                        } else {
                            inventory.InventoryQuantity__c = 0;
                            productQuantityMap.put(managementCode,salesQuantity-inventoryQuantity);
                        }

                        
                    }
                    
                    for (String managementCodeStr:productQuantityMap.keySet()) {
                        
                        salesQuantity = productQuantityMap.get(managementCodeStr);
                        if (salesQuantity > 0) {
                            String productCode = productCodeFormat(managementCodeStr.split('_')[0]);
                            String error = '产品 [ '+ productCode +' ] 库存不足，无法上传合同';
                            errorList.add(error);
                        }
                    }

                    if (errorList.size() > 0) {
                        result = '';
                        for (String error : errorList) {
                            result += error + ';';
                        }
                    } else {
                        update getProductInventoryList;
                    }

                }
            } 

            

        }

        return result;
    }

    //产品编码纯数字补零
    public static String productCodeFormat(String productCode){
        if (productCode.startsWith('00000000000')) {
            productCode = productCode.substring(10);
        }
        return productCode;
    }
//产品首营状态Z5 XHL 2021/02/07 End
    @TestVisible 
    public static void testI() {
        integer i = 0;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
    }
}