global class UpdateTotalCostPriceSetTextBatch implements Database.Batchable<sObject> {
    private List<String> idList = null;
    global UpdateTotalCostPriceSetTextBatch() {}
    global UpdateTotalCostPriceSetTextBatch(List<String> temp) {
        idList = temp;
    }

    global Database.QueryLocator start(Database.BatchableContext BC) {
        String query;
        query = 'select Id,Order.SAP_Order__c,Order.ProductSegment__c,Order.TradeType__c,Order.ApproveStatus__c,Order.SAP_Contract__c,TotalCostPrice__c,TotalCostPrice_Text__c,Order.IsCopy__c from OrderItem';
        if(idList != null){
            query = 'select Id,Order.SAP_Order__c,Order.ProductSegment__c,Order.TradeType__c,Order.ApproveStatus__c,Order.SAP_Contract__c,TotalCostPrice__c,TotalCostPrice_Text__c,Order.IsCopy__c from OrderItem where Order.Id =: idList';
        }
        return Database.getQueryLocator(query);
    }
    
    
     WebService static String executeWebSide1(String Orderid){
        List<OrderItem> updList = new List<OrderItem>();
        BatchIF_Log__c iflog = new BatchIF_Log__c();
        iflog.Type__c = 'PushNotification';
        iflog.Is_Error__c = 0;
        iflog.Log__c = 'UpdateTotalCostPriceSetTextBatch start0\n';
        iflog.ErrorLog__c = '';
        insert iflog;

        List<OrderItem> OrderDataList1 = [select Id,Order.SAP_Order__c,Order.ProductSegment__c,Order.TradeType__c,Order.ApproveStatus__c,Order.SAP_Contract__c,TotalCostPrice__c,TotalCostPrice_Text__c,Order.IsCopy__c                                            
                                        from OrderItem where Order.Id = :Orderid];
        List<Delivery__c> deliveryList = new List<Delivery__c>();
        deliveryList = [select id from Delivery__c where SSBD_Contract__c = :Orderid and Delivery_Status__c = '全部发货'];
        if (OrderDataList1.size() == 0){ 
        return '没有符合条件的合同数据';  
        } else {
                 for (OrderItem orderData:orderDataList1){
            //王鹏伟新加条件自动更新条件添加到手动更新
            //        (orderData.order.ProductSegment__c == 'BS' && orderData.order.SAP_Order__c == null && orderData.order.TradeType__c == '外贸') ||
            //        (orderData.order.ProductSegment__c != 'BS'  && orderData.order.TradeType__c == '外贸' && orderData.order.ApproveStatus__c !=  'OrderPass') ||
            //        (orderData.order.TradeType__c == '内贸' && orderData.order.SAP_Contract__c == null) ||
            //        (orderData.order.IsCopy__c == true)
            if (orderData.TotalCostPrice__c != orderData.TotalCostPrice_Text__c &&  deliveryList.size() == 0 && 
                (
                    (orderData.order.ProductSegment__c == 'BS' && orderData.order.SAP_Order__c == null && orderData.order.TradeType__c == '外贸') ||
                    (orderData.order.ProductSegment__c != 'BS'  && orderData.order.TradeType__c == '外贸' && orderData.order.ApproveStatus__c !=  'OrderPass') ||
                    (orderData.order.TradeType__c == '内贸' && orderData.order.SAP_Contract__c == null) ||
                    (orderData.order.IsCopy__c == true)
                  )
                  /*(
                    (orderData.order.ProductSegment__c == 'BS' && orderData.order.SAP_Order__c == null && orderData.order.TradeType__c == '外贸') ||
                    (( orderData.order.ProductSegment__c == 'IE' || orderData.order.ProductSegment__c == 'RVI') && orderData.order.TradeType__c == '外贸' && orderData.order.ApproveStatus__c !=  'OrderPass') ||
                    (( orderData.order.ProductSegment__c == 'BS' || orderData.order.ProductSegment__c == 'IE' || orderData.order.ProductSegment__c == 'RVI') && orderData.order.TradeType__c == '内贸' && orderData.order.SAP_Contract__c == null) ||
                    (( orderData.order.ProductSegment__c == 'BS' || orderData.order.ProductSegment__c == 'IE' || orderData.order.ProductSegment__c == 'RVI') && orderData.order.IsCopy__c == true)
                  )*/
            ) {
                orderData.TotalCostPrice_Text__c = (Decimal)orderData.TotalCostPrice__c;
                updList.add(orderData);
            }else{
                //王鹏伟添加else返回
                //return '该合同不符合更新条件，不需要更新';
            }

        }
        if (updList.size() > 0){ 
            update updList;
            return '更新成功';
        }else{
             return '该合同不符合更新条件，不需要更新';
        }
    }
    }

    global void execute(Database.BatchableContext BC, List<OrderItem> orderDataList) {
        List<OrderItem> updList = new List<OrderItem>();
        for (OrderItem orderData:orderDataList){
            if (orderData.TotalCostPrice__c != orderData.TotalCostPrice_Text__c &&
                 /* (
                    (orderData.order.ProductSegment__c == 'BS' && orderData.order.SAP_Order__c == null && orderData.order.TradeType__c == '外贸') ||
                    (( orderData.order.ProductSegment__c == 'IE' || orderData.order.ProductSegment__c == 'RVI') && orderData.order.TradeType__c == '外贸' && orderData.order.ApproveStatus__c !=  'OrderPass') ||
                    (( orderData.order.ProductSegment__c == 'BS' || orderData.order.ProductSegment__c == 'IE' || orderData.order.ProductSegment__c == 'RVI') && orderData.order.TradeType__c == '内贸' && orderData.order.SAP_Contract__c == null) ||
                    (( orderData.order.ProductSegment__c == 'BS' || orderData.order.ProductSegment__c == 'IE' || orderData.order.ProductSegment__c == 'RVI') && orderData.order.IsCopy__c == true)
                  )*/
                   (
                    (orderData.order.ProductSegment__c == 'BS' && orderData.order.SAP_Order__c == null && orderData.order.TradeType__c == '外贸') ||
                    (orderData.order.ProductSegment__c != 'BS'  && orderData.order.TradeType__c == '外贸' && orderData.order.ApproveStatus__c !=  'OrderPass') ||
                    (orderData.order.TradeType__c == '内贸' && orderData.order.SAP_Contract__c == null) ||
                    (orderData.order.IsCopy__c == true)
                  )
            ) {
                orderData.TotalCostPrice_Text__c = (Decimal)orderData.TotalCostPrice__c;
                updList.add(orderData);
            }

        }
        if (updList.size() > 0) update updList;
    }

    global void finish(Database.BatchableContext BC) {
        // 今回はやることないです
    }
}