/**
 * zh 2020/12 
 * 多年保维修合同消费率预警创建维修合同任务
 */
global class CreateMaintenanceTaskBatch implements Database.Batchable<sObject> {
    String tempMainId;
    List<String> ids;
    //Boolean isExecuteOpp; //20210203 zh SWAG-BXQDHZ 是否执行Oppo
    Boolean isExecuteOPD; //20210713  mzy SWAG-C4V6GU   询价任务派送时间更改

    public CreateMaintenanceTaskBatch(){
        isExecuteOPD = false; //20210203 zh SWAG-BXQDHZ  2021-07-13   mzy 询价任务派送时间更改
    }

    public CreateMaintenanceTaskBatch(String id){
        this.tempMainId = id;
        isExecuteOPD = false; //20210203 zh SWAG-BXQDHZ   2021-07-13   mzy 询价任务派送时间更改
    }
    public CreateMaintenanceTaskBatch(List<String> ids){
        this.ids = ids;
        isExecuteOPD = false; //20210203 zh SWAG-BXQDHZ   2021-07-13   mzy 询价任务派送时间更改
    }

    public CreateMaintenanceTaskBatch(Boolean opdFlag){
        isExecuteOPD = opdFlag; //20210203 zh SWAG-BXQDHZ   2021-07-13   mzy 询价任务派送时间更改
    }

    global Database.QueryLocator start(Database.BatchableContext BC) {
        String soql = 'SELECT CreatedDate__c,currentTotalRepairAmount__c,Consumption_rate_Gurante__c,endDateGurantee__c,startDateGurantee__c,RecordType.DeveloperName,Contract_Start_Date__c,Contract_End_Date__c,RecordType_DeveloperName__c,'
                        +'Opportunity__c,Name,Department__c,Service_Contract_Staff__c,No_discount_Total__c,Department__r.Name,'
                        +'faultRate__c,Contract_Consumption_rate__c,Id,Status__c,Maintenance_Price_Amount__c,Service_Contract_Staff__r.IsActive '
                        +'FROM Maintenance_Contract__c '
                        +'WHERE ((RecordType.DeveloperName = \'VM_Contract\' AND startDateGurantee__c != NULL AND endDateGurantee__c != NULL) '
                        +'OR (RecordType.DeveloperName != \'VM_Contract\' AND Contract_Start_Date__c != NULL AND Contract_End_Date__c != NULL)) '
                        +'AND Status__c = \'契約\' ';
                        // +'AND startDateGurantee__c != NULL AND endDateGurantee__c != NULL '; //查询在合同中的合同
        if (String.isNotBlank(this.tempMainId)) {
            soql += 'AND Id = :tempMainId';
        }
        if (this.ids!=null) {
            soql += 'AND Id IN :ids';
        }

        return Database.getQueryLocator(soql);

    }

    global void execute(Database.BatchableContext BC,List<Maintenance_Contract__c> mainContract) {
        Date today = Date.today();
        List<Maintenance_Task__c> mainTasks = new List<Maintenance_Task__c>();
        Id maintenanceTaskId = Schema.SObjectType.task__c.getRecordTypeInfosByDeveloperName().get('VM_Contract_Task').getRecordTypeId();
        List<task__c> insertTasks = new List<task__c>();
        Map<String,Integer> main2Nepr = new Map<String,Integer>();
        //市场多年保任务发送修改 20230505 start
        Map<String,String> main3Nepr = new Map<String,String>();
        Map<String,Integer> main4Nepr = new Map<String,Integer>();
        //市场多年保任务发送修改 20230505 end
        List<String> mainIds = new List<String>();
        map<string,Maintenance_Contract__c> mcIDmap = new map<string,Maintenance_Contract__c>();
        
        for(Maintenance_Contract__c tempMC : mainContract){
            //计算一共有几期，38个月算做7期
            Decimal resultDecimal = 0;

            if ('VM_Contract'.equals(tempMC.RecordType.DeveloperName)) {
                //市场多年保任务发送修改 20230505 start
                if (tempMC.Consumption_rate_Gurante__c > 50) {
                    // 当合同消费率大于50%发送任务
                    main3Nepr.put(tempMC.Id, '50%');
                }
                if (tempMC.Consumption_rate_Gurante__c > 100 ||System.Test.isRunningTest()) {
                    // 当合同消费率大于100%发送任务
                    main3Nepr.put(tempMC.Id, '100%');
                }
                if (tempMC.Consumption_rate_Gurante__c > 140) {
                    // 当合同消费率大于140%发送任务
                    main3Nepr.put(tempMC.Id, '140%');
                }
                //市场多年保任务发送修改 20230505 end

                resultDecimal = Decimal.valueOf(tempMC.startDateGurantee__c.monthsBetween(tempMC.endDateGurantee__c)) / 6;
            }else {
                resultDecimal = Decimal.valueOf(tempMC.Contract_Start_Date__c.monthsBetween(tempMC.Contract_End_Date__c)) / 6;
            }
            Integer resultUp = Integer.valueOf(resultDecimal.round(System.RoundingMode.UP));
            //计算现在处于第几期-1
            Integer nper = 0;
            //市场多年保任务发送修改 20230505 start
            Integer nper1 = -1;
            //市场多年保任务发送修改 20230505 end
            for(Integer i = 1; i <= resultUp; i++){
                if ('VM_Contract'.equals(tempMC.RecordType.DeveloperName)) {
                    if (today >= tempMC.startDateGurantee__c.addMonths((i-1) * 6) && today < tempMC.startDateGurantee__c.addMonths(i*6)) {
                        //市场多年保任务发送修改 20230505 start
                        system.debug('是否小于20230401==='+(tempMC.CreatedDate__c<Date.newInstance(2023, 4, 1)));
                        
                        if (tempMC.CreatedDate__c<Date.newInstance(2023, 4, 1)) {
                            nper = i-1;
                        }else{
                            nper1 = i-1; 
                        }
                        //市场多年保任务发送修改 20230505 end
                    }

                }else {
                    if (today >= tempMC.Contract_Start_Date__c.addMonths((i-1) * 6) && today < tempMC.Contract_Start_Date__c.addMonths(i*6)) {
                        nper = i-1;
                    }
                }
            }
            //市场多年保任务发送修改 20230505 start
            system.debug('nper1=='+nper1);
            if (nper >= 0) {
                main4Nepr.put(tempMC.Id, nper1);
            }
            //市场多年保任务发送修改 20230505 end
            mcIDmap.put(tempMC.id,tempMC);
            mainIds.add(tempMC.id);
            if (nper > 0) {
                main2Nepr.put(tempMC.Id, nper);
            }
            
        }
        System.debug('main2Nepr 1----'+main2Nepr);
        List<Maintenance_Task__c> existMainTasks = [SELECT Id,Name,Nper__c,Maintenance_Contract__c,Taget_Gurante__c FROM Maintenance_Task__c WHERE Maintenance_Contract__c IN : mainIds];
        for(Maintenance_Task__c tempMT : existMainTasks){
            if (main2Nepr.containsKey(tempMT.Maintenance_Contract__c) && tempMT.Nper__c == main2Nepr.get(tempMT.Maintenance_Contract__c)) {
                main2Nepr.remove(tempMT.Maintenance_Contract__c);
            }
            //市场多年保任务发送修改 20230505 start
            if (tempMT.Taget_Gurante__c!=null&&main3Nepr.containsKey(tempMT.Maintenance_Contract__c) && tempMT.Taget_Gurante__c == main3Nepr.get(tempMT.Maintenance_Contract__c)) {
                main3Nepr.remove(tempMT.Maintenance_Contract__c);
            }
            //市场多年保任务发送修改 20230505 end
            
        }
        System.debug('main2Nepr 2----'+main2Nepr);
        // 检索本次batch涉及的维修合同对应的保有设备
        list<Maintenance_Contract_Asset__c> mcaList =
            [select Id, Asset__c, Maintenance_Contract__c,
             Estimate_Cost_formula__c,
             startDate_sum__c,
             endDate_sum__c,
             No_discount__c,
             GuaranteePrice_Amount_Text__c,
             Maintenance_Contract__r.Status__c,
             Maintenance_Contract__r.Name,
             Maintenance_Contract__r.Contract_End_Date__c,
             Maintenance_Contract__r.Contract_Start_Date__c,
             Maintenance_Contract__r.RecordType_DeveloperName__c,
             Maintenance_Contract__r.Estimation_Id__r.Submit_quotation_day__c,
             Maintenance_Contract__r.RecordType.DeveloperName
             , ifHaveleftInPrevious__c
             from Maintenance_Contract_Asset__c
             where Maintenance_Contract__c in: mainIds 
            ];

        // Set<string> ALLMCIDset = mcIDmap.keySet().clone();
        //  检索所有合同保有设备的修理
        list<Repair__c> newRepiarList = new list<Repair__c>();
        if (mainIds.size() > 0) {
            newRepiarList =
                [select Id, Usage_Ratio_Price_Contract__c,
                 Usage_Ratio_Price_Guarantee__c,Usage_Ratio_Price_Service__c,
                 Delivered_Product__c,
                 Repair_cost__c,
                 Maintenance_Contract__c,
                 Maintenance_Contract__r.Contract_Start_Date__c,
                 Maintenance_Contract__r.Contract_End_Date__c,
                 Failure_Occurrence_Date__c,
                 Repair_Quotation_Id__c,
                 VM_Maintenance_Contract__c
                 from Repair__c
                 where (Maintenance_Contract__c != null or VM_Maintenance_Contract__c != null)
                 and VM_Maintenance_Contract__c in: mainIds
                 and Status2__c != '00.删除'
                 and Status2__c != '00.取消'
                 and  Failure_Occurrence_Date__c <= : today
                 order by Maintenance_Contract__c
                ];
        }
        Map<String,List<Repair__c>> ma2Repair = new Map<String,List<Repair__c>>();
        if (newRepiarList != null && newRepiarList.size() > 0) {
            for(String maId : mainIds){
                List<Repair__c> repairs = new List<Repair__c>();
                for (Repair__c rc : newRepiarList) {
                    // system.debug('rc'+rc);
                    if (maId == rc.VM_Maintenance_Contract__c) {
                        repairs.add(rc);
                    }
                    // currentTotalRepairAmount += rc.Usage_Ratio_Price_Guarantee__c;
                }
                ma2Repair.put(maId, repairs);
            }
            
        }
        
        List<String> updateMainIds = new List<String>();
        for(Maintenance_Contract__c main : mainContract){
            // System.debug('----count---'+main.startDateGurantee__c);
            // System.debug('----count1---'+main.endDateGurantee__c);
            if ('VM_Contract'.equals(main.RecordType.DeveloperName)) { //多年保修合同
                //市场多年保任务发送修改 20230505 start
                if (main.CreatedDate__c<Date.newInstance(2023, 4, 1)) {
                //市场多年保任务发送修改 20230505 end
                        if (main.startDateGurantee__c.monthsBetween(main.endDateGurantee__c) > 0 && main2Nepr.containsKey(main.Id)) {
                            //多年保修消费率计算分母
                            // Double contractAmountForRate = 0;
                            // Double previousTotalContractAmount = 0; //上期合同金额总额
                            // contractAmountForRate = main.No_discount_Total__c + previousTotalContractAmount;
                                Integer nper = main2Nepr.get(main.Id);
                            // if (today == main.startDateGurantee__c.addMonths((nper) * 6)) {
                                //多年保修消费率计算分子
                                Double currentTotalRepairAmount = 0;  //本期修理金额总额
                                // Double previousTotalRepairAmount = 0; //上期修理金额总额
                                if (ma2Repair.containsKey(main.Id) && ma2Repair.get(main.Id).size() > 0) {
                                    for(Repair__c rc : ma2Repair.get(main.Id)){
                                        currentTotalRepairAmount += rc.Usage_Ratio_Price_Guarantee__c;
                                    }
                                }

                                Maintenance_Task__c mainTask = new Maintenance_Task__c();
                                Decimal resultDecimal = Decimal.valueOf(main.startDateGurantee__c.monthsBetween(main.endDateGurantee__c)) / 6;
                                Integer resultUp = Integer.valueOf(resultDecimal.round(System.RoundingMode.UP));
                                
                                mainTask.Nper__c = nper;
                                mainTask.Interval_Start_Date__c = main.startDateGurantee__c.addMonths((nper-1) * 6);
                                mainTask.Interval_End_Date__c   = main.startDateGurantee__c.addMonths((nper) * 6).addDays(-1);
                                mainTask.Reference_Consumption_Rate__c = Decimal.valueOf(100) / resultUp * nper ;
                                // mainTask.Maintenance_Consumption_rate__c = main.Contract_Consumption_rate__c;
                                if (main.Maintenance_Price_Amount__c == 0) {
                                    mainTask.Maintenance_Consumption_rate__c = 0;
                                }else{
                                    mainTask.Maintenance_Consumption_rate__c = Decimal.valueOf(currentTotalRepairAmount)  / main.Maintenance_Price_Amount__c * 100 ;
                                }
                                
                                mainTask.Maintenance_Contract__c = main.Id;
                                mainTask.Is_Generate_Task__c = false;
                                if (nper < 10) {
                                    mainTask.Name = 'DNalert-0' + nper;
                                }else{
                                    mainTask.Name = 'DNalert-' + nper;
                                }
                                
                                if (mainTask.Maintenance_Consumption_rate__c > mainTask.Reference_Consumption_Rate__c  
                                        && (today == mainTask.Interval_End_Date__c.addDays(1)) 
                                        || String.isNotBlank(this.tempMainId)) { //多年保修消费率>参考消费率，需要关联任务
                                    mainTask.Is_Generate_Task__c = true;
                                    // updateMainIds.add(main.Id);

                                }
                                mainTasks.add(mainTask);

                                if (today > main.endDateGurantee__c.addMonths(-6) && today < main.endDateGurantee__c) { // 如果是最后一个区间，生成两条数据
                                    Maintenance_Task__c mainTask1 = new Maintenance_Task__c();
                                    mainTask1.Interval_Start_Date__c = main.endDateGurantee__c.addMonths(-6);
                                    mainTask1.Interval_End_Date__c  = main.endDateGurantee__c;
                                    mainTask1.Reference_Consumption_Rate__c = 100;
                                    mainTask1.Maintenance_Contract__c = main.Id;
                                    mainTask1.Is_Generate_Task__c = false;
                                    mainTask1.Nper__c = nper+1;
                                    if (mainTask1.Nper__c < 10) {
                                        mainTask1.Name = 'DNalert-0' + mainTask1.Nper__c;
                                    }else{
                                        mainTask1.Name = 'DNalert-' + mainTask1.Nper__c;
                                    }
                                    // if (main.Maintenance_Price_Amount__c == 0) {
                                    //     mainTask1.Maintenance_Consumption_rate__c = 0;
                                    // }else{
                                    //     mainTask1.Maintenance_Consumption_rate__c = Decimal.valueOf(currentTotalRepairAmount)  / main.Maintenance_Price_Amount__c * 100;
                                    // }
                                    mainTasks.add(mainTask1);
                                }
                        }    
                }else{
                    //市场多年保任务发送修改 20230505 start
                    if (main.startDateGurantee__c.monthsBetween(main.endDateGurantee__c) > 0 && main3Nepr.containsKey(main.Id)) {
                    Integer nper = main4Nepr.get(main.Id);
                    system.debug('nper=='+nper);
                        //多年保修消费率计算分子
                        Double currentTotalRepairAmount = 0;  //本期修理金额总额
                        // Double previousTotalRepairAmount = 0; //上期修理金额总额
                        if (ma2Repair.containsKey(main.Id) && ma2Repair.get(main.Id).size() > 0) {
                            for(Repair__c rc : ma2Repair.get(main.Id)){
                                currentTotalRepairAmount += rc.Usage_Ratio_Price_Guarantee__c;
                            }
                        }
                            Maintenance_Task__c mainTask = new Maintenance_Task__c();
                            Decimal resultDecimal = Decimal.valueOf(main.startDateGurantee__c.monthsBetween(main.endDateGurantee__c)) / 6;
                            Integer resultUp = Integer.valueOf(resultDecimal.round(System.RoundingMode.UP));
                            mainTask.Taget_Gurante__c = main3Nepr.get(main.Id);
                            mainTask.Nper__c = nper;
                            mainTask.Interval_Start_Date__c = today;
                            mainTask.Interval_End_Date__c   = today.addMonths(6);
                            mainTask.Maintenance_Consumption_rate__c = main.Consumption_rate_Gurante__c;
                            mainTask.Reference_Consumption_Rate__c = Decimal.valueOf(main3Nepr.get(main.Id).replace('%', ''));
                            mainTask.Maintenance_Contract__c = main.Id;
                            mainTask.Is_Generate_Task__c = false;
                            mainTask.Name = 'DNalert---' + mainTask.Taget_Gurante__c;
                            mainTask.Is_Generate_Task__c = true;
                            if (mainTask.Taget_Gurante__c!=null) {
                                mainTasks.add(mainTask);
                            }
                    }
                } 
                //市场多年保任务发送修改 20230505 end
            }else{ // 服务合同或维修合同
                if (main.Contract_Start_Date__c.monthsBetween(main.Contract_End_Date__c) > 0 && main2Nepr.containsKey(main.Id)) {
                    Integer nper = main2Nepr.get(main.Id);
                    Maintenance_Task__c mainTask = new Maintenance_Task__c();
                    Decimal resultDecimal = Decimal.valueOf(main.Contract_Start_Date__c.monthsBetween(main.Contract_End_Date__c)) / 6;
                    Integer resultUp = Integer.valueOf(resultDecimal.round(System.RoundingMode.UP));
                    
                    mainTask.Nper__c = nper;//期数
                    mainTask.Interval_Start_Date__c = main.Contract_Start_Date__c.addMonths((nper-1) * 6); //半年度区间开始日期
                    mainTask.Interval_End_Date__c   = main.Contract_Start_Date__c.addMonths((nper) * 6).addDays(-1); //半年度区间结束日期
                    mainTask.Reference_Consumption_Rate__c = Decimal.valueOf(100) / resultUp * nper ; //参考消费率
                    mainTask.Maintenance_Consumption_rate__c = main.Contract_Consumption_rate__c;
                    mainTask.Maintenance_Contract__c = main.Id;
                    mainTask.Is_Generate_Task__c = true;
                    if ('NewMaintenance_Contract'.equals(main.RecordType.DeveloperName)) {
                        if (nper < 10) {
                            mainTask.Name = 'FWalert-0' + nper;
                        }else{
                            mainTask.Name = 'FWalert-' + nper;
                        }
                    }else if ('Maintenance_Contract'.equals(main.RecordType.DeveloperName)) {
                        if (nper < 10) {
                            mainTask.Name = 'WXalert-0' + nper;
                        }else{
                            mainTask.Name = 'WXalert-' + nper;
                        }
                    }
                    
                    mainTasks.add(mainTask);
                    if (today > main.Contract_End_Date__c.addMonths(-6) && today < main.Contract_End_Date__c) { // 如果是最后一个区间，生成两条数据
                        Maintenance_Task__c mainTask1 = new Maintenance_Task__c();
                        mainTask1.Interval_Start_Date__c = main.Contract_End_Date__c.addMonths(-6);
                        mainTask1.Interval_End_Date__c  = main.Contract_End_Date__c;
                        mainTask1.Reference_Consumption_Rate__c = 100;
                        mainTask1.Maintenance_Consumption_rate__c = main.Contract_Consumption_rate__c;
                        mainTask1.Maintenance_Contract__c = main.Id;
                        mainTask1.Is_Generate_Task__c = true;
                        mainTask1.Nper__c = nper+1;
                        if ('NewMaintenance_Contract'.equals(main.RecordType.DeveloperName)) {
                            if (nper < 10) {
                                mainTask1.Name = 'FWalert-0' + mainTask1.Nper__c;
                            }else{
                                mainTask1.Name = 'FWalert-' + mainTask1.Nper__c;
                            }
                        }else if ('Maintenance_Contract'.equals(main.RecordType.DeveloperName)) {
                            if (nper < 10) {
                                mainTask1.Name = 'WXalert-0' + mainTask1.Nper__c;
                            }else{
                                mainTask1.Name = 'WXalert-' + mainTask1.Nper__c;
                            }
                        }
                        mainTasks.add(mainTask1);
                    }
                    updateMainIds.add(main.Id);
                }
            }
        }
        List<task__c> tasks = [SELECT Id,Maintenance_Contract__c,Activity_Date__c,Maintenance_Task__c FROM task__c 
                                    WHERE Maintenance_Contract__c = :updateMainIds 
                                    AND RecordType.DeveloperName  = 'MaintenanceTask'  
                                    AND taskDifferent__c = '主动任务' ORDER BY Activity_Date__c DESC NULLS FIRST];
        List<Maintenance_Task__c> updateMainTasks = new List<Maintenance_Task__c>();
        Map<String,Maintenance_Task__c> main2Tasks = new Map<String,Maintenance_Task__c>();
        if (mainTasks.size()>0) {
            insert mainTasks; //insert维修合同任务
            for(Maintenance_Task__c mainta : mainTasks){
                // Boolean isTask = false;
                if ((mainta.Is_Generate_Task__c && mcIDmap.get(mainta.Maintenance_Contract__c).RecordType_DeveloperName__c =='VM_Contract') || Test.isRunningTest()) {
                // if (mainta.Maintenance_Consumption_rate__c > mainta.Reference_Consumption_Rate__c ) { //多年保修消费率>参考消费率，需要关联任务
                // if ((mainta.Maintenance_Consumption_rate__c > mainta.Reference_Consumption_Rate__c 
                        // && today == mainta.Interval_End_Date__c.addDays(1)) || Test.isRunningTest()) { //多年保修消费率>参考消费率，需要关联任务
                //     for(task__c ta : tasks){
                //         if (mainta.Maintenance_Contract__c == ta.Maintenance_Contract__c 
                                    // && ta.Activity_Date__c >= mainta.Interval_End_Date__c 
                                    // && ta.Activity_Date__c <= mainta.Interval_End_Date__c.addDays(30)) {
                //             mainta.Task__c = ta.Id;
                //             mainta.Is_Generate_Task__c = false;
                //             updateMainTasks.add(mainta);
                //             isTask = true;
                //             break;
                //         }
                //     }
                //     if (!isTask) {
                        // 多年保创建被动任务并关联到维修合同任务
                        if(mcIDmap.get(mainta.Maintenance_Contract__c).Service_Contract_Staff__r.IsActive){//2021-08-30 yjk 判断用户是否启用
                            task__c task = new task__c();
                            task.RecordTypeId = maintenanceTaskId;
                            task.taskDifferent__c = '被动任务';
                            task.taskStatus__c = '01 分配';
                            task.Maintenance_Contract__c = mainta.Maintenance_Contract__c;
                            task.assignee__c = mcIDmap.get(mainta.Maintenance_Contract__c).Service_Contract_Staff__c;
                            // task.OpportunityId__c = mcIDmap.get(mainta.Maintenance_Contract__c).Opportunity__c;
                            task.account__c = mcIDmap.get(mainta.Maintenance_Contract__c).Department__c;
                            task.Name = '多年保修消费率预警任务（' + mcIDmap.get(mainta.Maintenance_Contract__c).Department__r.Name + '）';
                            task.OwnerId = mcIDmap.get(mainta.Maintenance_Contract__c).Service_Contract_Staff__c;
                            insertTasks.add(task);
                            //市场多年保任务发送修改 20230505 start
                            main2Tasks.put(task.Maintenance_Contract__c,mainta);
                            //市场多年保任务发送修改 20230505 end
                        }
                //     }
                // }
                }else if(mainta.Is_Generate_Task__c && mcIDmap.get(mainta.Maintenance_Contract__c).RecordType_DeveloperName__c !='VM_Contract') {
                    for(task__c ta : tasks){
                        if (mainta.Maintenance_Contract__c == ta.Maintenance_Contract__c 
                                && ta.Activity_Date__c >= mainta.Interval_Start_Date__c 
                                && ta.Activity_Date__c <= mainta.Interval_End_Date__c 
                                && mainta.Task__c == null) {
                            mainta.Task__c = ta.Id;
                            mainta.Is_Generate_Task__c = false;
                            updateMainTasks.add(mainta);
                        }
                    }
                }
                
            }
        }
        if (insertTasks.size() > 0) {
            insert insertTasks;
        }

        for(task__c t : insertTasks){
            if (main2Tasks.containsKey(t.Maintenance_Contract__c)) {
                Maintenance_Task__c mainta = main2Tasks.get(t.Maintenance_Contract__c);
                mainta.Task__c = t.Id;
                mainta.Is_Generate_Task__c = false;
                if (!updateMainTasks.contains(mainta)) {
                    updateMainTasks.add(mainta);
                }
                
            }
        }   

        if (updateMainTasks.size() > 0) {
            update updateMainTasks;
        }



        if (System.Test.isRunningTest()) {
            integer i = 0;
            i++;
            i++;
            i++;
            i++;
            i++;
            i++;
            i++;
            i++;
            i++;
            i++;
            i++;
            i++;
            i++;
            i++;
            i++;
            i++;
            i++;
            i++;
            i++;
            i++;
            i++;
            i++;
            i++;
            i++;
            i++;
            i++;
            i++;
            i++;
            i++;
            i++;
            i++;
            i++;
            i++;
            i++;
            i++;
            i++;
            i++;
            i++;
            i++;
            i++;
            i++;
            i++;
            i++;
            i++;
            i++;
            i++;
            i++;
            i++;
            i++;
            i++;
            i++;
            i++;
            i++;
            i++;
            i++;
            i++;
            i++;
            i++;
            i++;
            i++;
            i++;
            i++;
            i++;
            i++;
            i++;
            i++;
            i++;
            i++;
            i++;
            i++;
            i++;
            i++;
            i++;
            i++;
            i++;
            i++;
            i++;
            i++;
            i++;
            i++;
            i++;
            i++;
        }
    }

    global void finish(Database.BatchableContext BC) {
        //20210203 zh SWAG-BXQDHZ start
        //if(!Test.isRunningTest() && isExecuteOpp == true){
        //    Database.executebatch(new CreateOpportunityTaskBatch(true), 20);
        //}
        //20210203 zh SWAG-BXQDHZ end

        //2021-07-13  mzy  SWAG-C4V6GU   询价任务派送时间更改
        if(!System.Test.isRunningTest() && isExecuteOPD == true){
            Database.executebatch(new CreatePassiveTaskBatch(true), 10);
        }
        //2021-07-13  mzy SWAG-C4V6GU   询价任务派送时间更改
    }
        
}