global class RepairProductGuaranteUpdateBatch implements Database.Batchable<sObject> {

	Boolean IsNeedExecute = false; // 2021-03-05  mzy  WLIG-BYHD79  SFDC环境batch合并调查  是否符合执行条件
	
	global RepairProductGuaranteUpdateBatch() {
		
	}

	// 2021-03-05  mzy  WLIG-BYHD79  SFDC环境batch合并调查  start
	global RepairProductGuaranteUpdateBatch(Boolean NeedExecute) {
	    this.IsNeedExecute = NeedExecute;
	}
	// 2021-03-05  mzy  WLIG-BYHD79  SFDC环境batch合并调查  end
	
	global Database.QueryLocator start(Database.BatchableContext BC) {
		if(System.test.isRunningTest()){
			return Database.getQueryLocator(
			[SELECT Id, Name, NewProductGuarante_Formula__c, NewProductGuarante_Txt__c
			 FROM Repair__c ]);
		}
		return Database.getQueryLocator(
			[SELECT Id, Name, NewProductGuarante_Formula__c, NewProductGuarante_Txt__c
			 FROM Repair__c 
			 WHERE (    (   NewProductGuarante_Formula__c = ''
				        AND NewProductGuarante_Txt__c != '')
				    OR (    NewProductGuarante_Formula__c != ''
				        AND NewProductGuarante_Txt__c = '')
				    OR (    NewProductGuarante_Formula__c = '1: 新品保修'
				        AND NewProductGuarante_Txt__c != '1: 新品保修')
				    OR (    NewProductGuarante_Formula__c = '2: 多年保修'
				        AND NewProductGuarante_Txt__c != '2: 多年保修')
				    // LHJ 多年保修 Start
				    OR (    NewProductGuarante_Formula__c = '2: 服务多年保修'
				        AND NewProductGuarante_Txt__c != '2: 服务多年保修')
				    OR (    NewProductGuarante_Formula__c = '8: 市场多年保修'
				        AND NewProductGuarante_Txt__c != '8: 市场多年保修')
				    // LHJ 多年保修 End
				    OR (    NewProductGuarante_Formula__c = '3: 销售特殊保修期'
				        AND NewProductGuarante_Txt__c != '3: 销售特殊保修期')
			        )
			    AND (
			        	Account__r.RecordType.DeveloperName = 'Department_GS'
					OR  Account__r.RecordType.DeveloperName = 'Department_OTH' 
					OR  Account__r.RecordType.DeveloperName = 'Department_ENT' 
					OR  Account__r.RecordType.DeveloperName = 'Department_GYN' 
					OR  Account__r.RecordType.DeveloperName = 'Department_URO' 
					OR  Account__r.RecordType.DeveloperName = 'Department_BF' 
					OR  Account__r.RecordType.DeveloperName = 'Department_GI')
			]
		);
	}

   	global void execute(Database.BatchableContext BC, List<SObject> sList) {
		if (sList != null && sList.size() > 0) {
			List<Repair__c> updateList = new List<Repair__c>();
			for (SObject sObj : sList) {
				Repair__c rc = (Repair__c) sObj;
				rc.NewProductGuarante_Txt__c = rc.NewProductGuarante_Formula__c;
				updateList.add(rc);
			}
			update updateList;
		}
	}
	
	global void finish(Database.BatchableContext BC) {

		//2021-03-10  mzy  WLIG-BYHD79  SFDC环境batch合并调查  start
		if(!Test.isRunningTest() &&IsNeedExecute==true){
		  	//batch里调用下一个batch时，希望跟原有的Schedule里面传的条数保持一致
		    Id execBTId = Database.executebatch(new Send_Have_Arrival_wh_Email_Batch(true),200);
		}
		//2021-03-10  mzy  WLIG-BYHD79  SFDC环境batch合并调查 end

		
	}
	
}