global class NextMonthVisitBatch implements Database.Batchable<sObject>, Database.Stateful {
    
    //HWAG-BML3AC 询价任务创建 精琢技术 wql 2020/04/10 start
    //soql语句
    private String query;
    //用于单条传入id更新询价
    private String oppoId;
    //用于批量传入id更新询价
    private List<String> oppoList;
    //用于执行batch同时记录日志信息
    private BatchIF_Log__c iflog;



    global NextMonthVisitBatch() {    
        // this.query = query;
        iflog = new BatchIF_Log__c();
        iflog.Type__c = 'PushNotification';
        iflog.Is_Error__c = 0;
        iflog.Log__c = 'NextMonthVisitBatch start1\n';
        iflog.ErrorLog__c = '';
        insert iflog;
    }
    global NextMonthVisitBatch(String oppoId) {
        this.oppoId = oppoId;
        iflog = new BatchIF_Log__c();
        iflog.Type__c = 'PushNotification';
        iflog.Is_Error__c = 0;
        iflog.Log__c = 'NextMonthVisitBatch start2\n';
        iflog.ErrorLog__c = '';
        insert iflog;
    }

    global NextMonthVisitBatch(List<String> oppoList) {
        this.oppoList = oppoList;
        iflog = new BatchIF_Log__c();
        iflog.Type__c = 'PushNotification';
        iflog.Is_Error__c = 0;
        iflog.Log__c = 'NextMonthVisitBatch start3\n';
        iflog.ErrorLog__c = '';
        insert iflog;
    }
    // global NextMonthVisitBatch(boolean refresh) {    
    //     // this.query = query;
    //     iflog = new BatchIF_Log__c();
    //     iflog.Type__c = 'PushNotification';
    //     iflog.Is_Error__c = 0;
    //     iflog.Log__c = 'NextMonthVisitBatch start1\n';
    //     iflog.ErrorLog__c = '';
    //     insert iflog;
    //     thisMonthExecuteFlag = refresh;
    // }

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

        String query ='select id  from Opportunity where IsNextMonthOfVisit__c  =true';

        if (!string.isblank(oppoId)) {
            query += ' and id = :oppoId ';
        }

        if (oppoList != null) {
            query += ' and id in: oppoList ';
        }
            System.debug('sql语句：'+query);


        return Database.getQueryLocator(query);
    }

    global void execute(Database.BatchableContext BC, list<Opportunity> scList) {
        try{
             List<Opportunity> opportunityList  = new List<Opportunity>();

            for(Opportunity o:scList){
                Opportunity oppo = new Opportunity();
                oppo.Id = o.Id;
                oppo.IsNextMonthOfVisit__c = false;
                opportunityList.add(oppo);
            }
             if (opportunityList.size() > 0){
                Database.SaveResult[] lsr = Database.update(opportunityList, false);
                for (Integer tIdx = 0; tIdx < lsr.size(); tIdx++) {
                    Database.SaveResult sr = lsr[tIdx];
                    System.debug('sr.isSuccess:' + sr.isSuccess());
                    if (!sr.isSuccess()) {
                        Database.Error emsg = sr.getErrors()[0];
                        iflog.ErrorLog__c += 'ERROR ' + opportunityList[tIdx].Id + ' Opportunity:' + emsg + '\n';
                    }
                }
                // update opportunityList;
             } 
        }catch(Exception e){
            iflog.ErrorLog__c += 'ERROR ： Opportunity:' + e.getMessage() + '\n';
       
        }
    }

    global void finish(Database.BatchableContext BC) {
        //更新该日志的数据信息
        iflog.Log__c += '\nNextMonthVisitTwoBatch end';
        String tmp = iflog.ErrorLog__c;
        if (tmp.length() > 65000) {
            tmp = tmp.substring(0, 65000);
            tmp += ' ...have more lines...';
            iflog.ErrorLog__c = tmp;
        }
        update iflog;

    }
    //HWAG-BML3AC 询价任务创建 精琢技术 wql 2020/04/10 end
}