global class OPDPlanSystemDelayBatch implements Database.Batchable<sObject> {
    // SWAG-BXW4AH XHL 2021/02/27 【委托】 OPD计划再申请逻辑设置
    public String query;
    List<String> oPDPlanIdList = null;
    global OPDPlanSystemDelayBatch() {
        this.query = query;
    }
    global OPDPlanSystemDelayBatch(List<String> oPDPlanIdList) {
        this.oPDPlanIdList = oPDPlanIdList;
    }

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

        if (oPDPlanIdList <> null) {
            return Database.getQueryLocator([SELECT Id, Name, OPDPlan_ImplementDate__c, SystemDelayFlag__c, Status__c, SystemDelayMark__c FROM OPDPlan__c WHERE SystemDelayFlag__c = true AND Id In :oPDPlanIdList]);
        } else {
            return Database.getQueryLocator([SELECT Id, Name, OPDPlan_ImplementDate__c, SystemDelayFlag__c, Status__c, SystemDelayMark__c FROM OPDPlan__c WHERE SystemDelayFlag__c = true]);
        }
    }

    global void execute(Database.BatchableContext BC, List<OPDPlan__c> oPDPlanList) {
        List<CancelPostponePlan__c> insertCancelPostponePlanList = new List<CancelPostponePlan__c> ();
        for(OPDPlan__c opdp : oPDPlanList) {
            if (opdp.SystemDelayFlag__c) {
                CancelPostponePlan__c cpp = new CancelPostponePlan__c();
                cpp.CancelOPDPlan__c = opdp.Id;
                cpp.cancelReasonCombobox__c = '系统自动延期';
                cpp.NextPlanDate__c = opdp.OPDPlan_ImplementDate__c.addDays(30);
                cpp.Status__c = '延期成功';
                cpp.RecordTypeId = System.Label.learn_delay;
                insertCancelPostponePlanList.add(cpp);
            }
        }
        if (insertCancelPostponePlanList.size() > 0) {
            insert insertCancelPostponePlanList;

        }
    }

    global void finish(Database.BatchableContext BC) {
        // 20240109 ssm 备品可视化 延期后先自动排序
        // Id execBTId = Database.executebatch(new OPDPlanMarkBatch(), 100);
        Id execBTId = Database.executebatch(new OPDAutomaticSortBatch(), 1);
    }
       
}