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

    global Database.QueryLocator start(Database.BatchableContext BC) {
       String query;
       //王鹏伟新加条件
       //'Opportunity.StageName != \'Closed Won\' and Opportunity.StageName != \'Closed Lost\' and Opportunity.StageName != \'Closed Cancel\' and '+
       //'Opportunity.Model_product_cnt__c != 0 and Opportunity.ProductCount__c != 0 and Opportunity.OppBatchUpdate__c = true'
       query = 'select TotalCostPrice__c,TotalCostPrice_Text__c,Opportunity.Model_product_cnt__c,Opportunity.ProductCount__c,Opportunity.OppBatchUpdate__c from OpportunityLineItem where '+
       'Opportunity.StageName != \'Closed Won\' and Opportunity.StageName != \'Closed Lost\' and Opportunity.StageName != \'Closed Cancel\' '
       /*+'and Opportunity.Model_product_cnt__c != 0 and Opportunity.ProductCount__c != 0 and Opportunity.OppBatchUpdate__c = true'*/;
       if(idList != null){
          query = 'select TotalCostPrice__c,TotalCostPrice_Text__c,Opportunity.Model_product_cnt__c,Opportunity.ProductCount__c,Opportunity.OppBatchUpdate__c from OpportunityLineItem where '+
          'Opportunity.StageName != \'Closed Won\' and Opportunity.StageName != \'Closed Lost\' and Opportunity.StageName != \'Closed Cancel\' and Opportunity.Id=:idList';
       }
        return Database.getQueryLocator(query);
        
    }

    WebService static String executeWebSide(String Opportunityid){
        BatchIF_Log__c iflog = new BatchIF_Log__c();
        iflog.Type__c = 'PushNotification';
        iflog.Is_Error__c = 0;
        iflog.Log__c = 'UpdateTotalCostPriceSetTextInquiryBatch start0\n';
        iflog.ErrorLog__c = '';
        insert iflog;
        List<OpportunityLineItem> updateList = new List<OpportunityLineItem>();
        List<OpportunityLineItem> OpportunityDataList1 = [select
                                                Opportunity.StageName,
                                                TotalCostPrice__c,
                                                TotalCostPrice_Text__c,
                                                Opportunity.Model_product_cnt__c,
                                                Opportunity.ProductCount__c,
                                                Opportunity.OppBatchUpdate__c                                              
                                        from OpportunityLineItem where Opportunity.Id = :Opportunityid and Opportunity.StageName <> 'Closed Won' and Opportunity.StageName <> 'Closed Lost' and Opportunity.StageName <> 'Closed Cancel' 
                                        /*and Opportunity.Model_product_cnt__c != 0 and Opportunity.ProductCount__c != 0 and Opportunity.OppBatchUpdate__c = true*/];
        if (OpportunityDataList1.size() == 0){ 
            return '没有符合条件的询价数据';  
        } else {
            for (OpportunityLineItem OpportunityData:OpportunityDataList1){
                //王鹏伟 添加if筛选条件  && OpportunityData.Opportunity.Model_product_cnt__c != 0 && OpportunityData.Opportunity.ProductCount__c != 0 &&
                //OpportunityData.Opportunity.OppBatchUpdate__c == true   
                if (OpportunityData.TotalCostPrice__c != OpportunityData.TotalCostPrice_Text__c && 
                  OpportunityData.Opportunity.Model_product_cnt__c == 0 && OpportunityData.Opportunity.ProductCount__c != 0 &&
                  OpportunityData.Opportunity.OppBatchUpdate__c == true){ 
                      OpportunityData.TotalCostPrice_Text__c = (Decimal)OpportunityData.TotalCostPrice__c;
                      updateList.add(OpportunityData);
                }
            }
            if(updateList.size() >0){
                update updateList;
                return '更新成功';
            }else{
                //王鹏伟新加else返回
                return '该询价不符合更新条件，不需要更新。';
            }
        }
    }
    
    global void execute(Database.BatchableContext BC, List<OpportunityLineItem> OpportunityDataList) {
      //王鹏伟 添加if筛选条件
      //&& OpportunityData.Opportunity.Model_product_cnt__c != 0 && OpportunityData.Opportunity.ProductCount__c != 0 &&
      // OpportunityData.Opportunity.OppBatchUpdate__c == true
      List<OpportunityLineItem> itemList = new List<OpportunityLineItem>();
               for (OpportunityLineItem OpportunityData:OpportunityDataList){   
                      if (OpportunityData.TotalCostPrice__c != OpportunityData.TotalCostPrice_Text__c  && 
                        OpportunityData.Opportunity.Model_product_cnt__c == 0 && OpportunityData.Opportunity.ProductCount__c != 0 &&
                        OpportunityData.Opportunity.OppBatchUpdate__c == true) 
                      { 
                        OpportunityData.TotalCostPrice_Text__c = (Decimal)OpportunityData.TotalCostPrice__c;
                        itemList.add(OpportunityData);
            }

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

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