global class Opportunity_Shipments_ForecastBatch implements Database.Batchable<sObject>, Database.AllowsCallouts {
	public String query;
	public List<String> opportunity_Noss;
	public String qDate;
	public Date queryDate;
	private String nextmonth_Lastday;
	private String thismonth_Firstday;
	global Opportunity_Shipments_ForecastBatch() {	
		nextmonth_Lastday = String.valueOf(Date.toDay().addMonths(2).toStartOfMonth().addDays(-1));
		thismonth_Firstday = String.valueOf(Date.newInstance(Date.today().year(), Date.today().month(), 1));
	}

	global Opportunity_Shipments_ForecastBatch(List<String> opportunity_Nos) {
		opportunity_Noss = opportunity_Nos;
	}
	
	global Opportunity_Shipments_ForecastBatch(String querydate) {
		qDate = querydate;
		nextmonth_Lastday = String.valueOf(Date.toDay().addMonths(2).toStartOfMonth().addDays(-1));
	}

	global Database.QueryLocator start(Database.BatchableContext BC) {

		//List<String> competitorList = new List<String>();
		//competitorList.add('A1');
		//competitorList.add('A');

		if(opportunity_Noss != null && opportunity_Noss.size() > 0) {

			System.debug('+++++++++++指定询价编码+++++++++++' );
			return Database.getQueryLocator(
						// [select Id from Opportunity where Competitor__c In :competitorList and Id In :opportunityIds]
						[select Id from Opportunity where Opportunity_No__c In :opportunity_Noss and RecordTypeId != '01210000000QekP']
				);

		} else if(qDate != null) {

			System.debug('+++++++++++指定日期+++++++++++' );
			queryDate = NFMUtil.parseStr2Date(qDate);
			query = 'select Id from Opportunity';
			//query += ' where CloseDate >= '+ String.valueOf(queryDate) + ' and CloseDate <= ' + String.valueOf(queryDate.addMonths(2)) + ' and Competitor__c In (\'A1\', \'A\')';
			query += ' where CloseDate >= '+ String.valueOf(queryDate) + ' and CloseDate <= ' + nextmonth_Lastday ;
			query += ' and RecordTypeId != \'01210000000QekP\'';
			//20190107 --------addStart---------
			query += ' and StageName__c != \'取消\'';
			query += ' and StageName__c != \'失单\'';
			query += ' and Sales_Root__c != \'\'';
			//20190107 --------addEnd---------
			query += ' and Salesdepartment_Owner__c != \'外科事业本部\'';
			System.debug('+++11111111--query--111111111111+++++' + query);
			return Database.getQueryLocator(query);
			
		} else {
			System.debug('+++++++++++正常Batch执行+++++++++++' );

			query = 'select Id from Opportunity';
			// query +=  ' where CloseDate >= '+ String.valueOf(Date.toDay()) + ' and CloseDate <= ' + String.valueOf(Date.toDay().addMonths(2)) + ' and Competitor__c In (\'A1\', \'A\')';
			query +=  ' where CloseDate >= '+ thismonth_Firstday + ' and CloseDate <= ' + nextmonth_Lastday ;
			query += ' and RecordTypeId != \'01210000000QekP\'';
			query += ' and StageName__c != \'取消\'';
			query += ' and StageName__c != \'失单\'';
			query += ' and Sales_Root__c != \'\'';
			query += ' and Salesdepartment_Owner__c != \'外科事业本部\'';
			System.debug('+++11111111--正常Batch执行query--111111111111+++++' + query);
			return Database.getQueryLocator(query);
			
		}

	}

   	global void execute(Database.BatchableContext BC, List<Opportunity> oppList) {
   			System.debug('+++11111111 oppList 111111111111+++++' + oppList);
			List<String> oppIds = new List<String>();
            for(Opportunity oppt : oppList){
                oppIds.add(oppt.Id);
                System.debug('----------+++++++++++++++++++++----------- ' + oppt.Id);
            }

            BatchIF_Log__c iflog = new BatchIF_Log__c();
	        iflog.Type__c = 'NFM009';
	        iflog.Log__c  = 'callout start\n';
            NFM009Controller.callout(iflog, oppIds);
	}
	
	global void finish(Database.BatchableContext BC) {
		
	}
	
}