/**
 * 20240106 sx add 处理OPD没有备品借出申请的batch
 *  */

global class exceteOPDHistoryNoRentalApplyBatch implements Database.Batchable<sObject>,Database.Stateful{
    public String query;
    public String aimDate = '';

    global exceteOPDHistoryNoRentalApplyBatch() {
        this.query = query;
    }

    global exceteOPDHistoryNoRentalApplyBatch(String tempdate) {
        this.aimDate = tempdate;
    }

    global Database.QueryLocator start(Database.BatchableContext bc) {
        String query = 'SELECT Id, OPDPlan_ImplementDate__c FROM OPDPlan__c where (Status__c = \'计划中\' OR Status__c = \'审批中\')  AND Rental_Apply2__c = null ';
        if(String.isNotBlank(aimDate)){
            query += 'AND OPDPlan_ImplementDate__c >=' + aimDate;
        }else{
            Date toDay = Date.today();
            query += 'AND OPDPlan_ImplementDate__c >= :toDay';
        }
        System.debug('query====' + query);
        return Database.getQueryLocator(query);
    }

    global void execute(Database.BatchableContext BC, List<sObject> scope) {
        
        List<OPDPlan__c> opdPlanList = scope;
        System.debug('opdPlanList====' + opdPlanList);
        if(opdPlanList.size() > 0){
            for(OPDPlan__c opd : opdPlanList){
                LexOPDSupplementaryController.newRentalApply(opd.Id);
            }
        }
    }

    global void finish(Database.BatchableContext BC) {

    }
}