global class ProRegisterBatch implements Database.Batchable<sObject> {
	String regId;

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

	global ProRegisterBatch() {
		
	}

	// 2021-03-05  mzy  WLIG-BYHD79  SFDC环境batch合并调查  start
	global ProRegisterBatch(Boolean NeedExecute) {
	    this.IsNeedExecute = NeedExecute;
	}
	// 2021-03-05  mzy  WLIG-BYHD79  SFDC环境batch合并调查  end
	
	global Database.QueryLocator start(Database.BatchableContext BC) {
		
		if (String.isBlank(this.regId)) {
			return Database.getQueryLocator([select id, RegisterNoStatus__c 
				                               from Product_Register__c 
				                              where RegisterNoStatus__c = '有效'
				                                and ValidTo__c <= :Date.Today()]);

		} else {
			return Database.getQueryLocator([select id, RegisterNoStatus__c 
				                               from Product_Register__c 
				                              where RegisterNoStatus__c = '有效'
				                                and id = :this.regId
				                                and ValidTo__c <= :Date.Today()]);
		}
	}

   	global void execute(Database.BatchableContext BC, List<Product_Register__c> dcList) {
		List<Product_Register__c> updList = main(dcList);
		if (updList.size() > 0) update updList;
	}
	
	public static List<Product_Register__c> main(List<Product_Register__c> dcList) {     

		List<Product_Register__c> updList = new List<Product_Register__c>();

		for (Product_Register__c reg : dcList) {

			reg.RegisterNoStatus__c = '失效-不再注册';

			updList.add(reg);
		}
		return updList;
	}

	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 RentalApplyDailyBatch(true),100);
		}
		//2021-03-10  mzy  WLIG-BYHD79  SFDC环境batch合并调查 end
		
	}
	
}