//将学会上的工作流规则 '更新会议开始前7天提醒邮件标识' 复制为batch
public without sharing class updateCampaignMailflg7BeTrueBatch implements Database.Batchable<SObject>,Database.Stateful {
    public String query;
    // public String campaignId;
    public List<String> campaignIds;
    public String logId;
    private BatchIF_Log__c iflog;
    private String errorlog = '';
    private String succlog = '';
    private String errorIdLog = '';

    public updateCampaignMailflg7BeTrueBatch() {
        this.query = query;
        iflog = new BatchIF_Log__c();
        iflog.Type__c = 'updateCampaignMailflg7BeTrue';
        iflog.Log__c = 'start --\n';
        iflog.ErrorLog__c = '';
    }

    // public updateCampaignMailflg7BeTrueBatch(List<String> camIds) {
    //     this.query = query;
    //     this.campaignIds = camIds;
    //     iflog = new BatchIF_Log__c();
    //     iflog.Type__c = 'updateCampaignMailflg7BeTrue';
    //     iflog.Log__c = 'start --\n';
    //     iflog.ErrorLog__c = '';
    // }

    public updateCampaignMailflg7BeTrueBatch(String logId){
        this.query = query;
        this.logId = logId;
        iflog = new BatchIF_Log__c();
        iflog.Type__c = 'updateCampaignMailflg7BeTrue';
        iflog.Log__c = 'start --\n';
        iflog.ErrorLog__c = '';
    }



    public Database.QueryLocator start(Database.BatchableContext bc) {
        //如果用户传入logid,则说明存在更新失败的数据
        if (logId != null && logId != '') {
            query = 'select id,errorlog__c from BatchIF_Log__c where id = :logId';
        }else{
            query = 'select id,Mailflg_before7__c,Name from Campaign where Open_day__c != null ';
            query += ' and Status != \'已提交报告\' and Status != \'已结束\' and Status != \'取消\' and Status != \'已结束(没公开))\'';
            query += ' and BetweenTodayStartDate__c = 7 and RecordType.DeveloperName != \'5.服务培训/技师培养\' and Mailflg_before7__c = false ';
            // if (campaignIds != null && campaignIds.size() > 0) {
            //     query += ' and id in :campaignId ';
            // }
        }
        return Database.getQueryLocator(query);
    }

    public void execute(Database.BatchableContext BC, list<SObject> scope) {
        List<Campaign> updateCamList = new List<Campaign>();
        //说明当前查到的集合为log集合
        if (logId != null && logId != '') {
            //截取字符串,获取更新失败的学会id和学会的字段
            List<BatchIF_Log__c> logs = scope;
            BatchIF_Log__c log = logs.get(0);
            String logStr = log.ErrorLog__c.substring(log.ErrorLog__c.indexOf(':')+1,log.ErrorLog__c.indexOf('失败'));
            System.debug('123333::::logstr:'+logStr);
            String[] errIds = logStr.split(',');
            for (String id : errIds) {
                Campaign cam =  new Campaign();
                cam.Id = id;
                cam.Mailflg_before7__c = true;
                updateCamList.add(cam);
            }

        }else{
            updateCamList = scope;
            for (Campaign cam : updateCamList) {
                cam.Mailflg_before7__c = true;
            }
            System.debug('20------'+updateCamList);
            
        }


        Database.SaveResult[] lsr = Database.update(updateCamList, false);
        for (Integer tIdx = 0; tIdx < lsr.size(); tIdx++) {
            Database.SaveResult sr = lsr[tIdx];
            //如果更新失败
            if (!sr.isSuccess()) {
                Database.Error emsg = sr.getErrors()[0];
                errorIdLog += updateCamList[tIdx].Id+',';
                errorlog += '更新学会:'+updateCamList[tIdx].Id +'失败,错误出现在:'+emsg.getMessage() +'\n';
            }else{
                succlog += '更新学会:'+updateCamList[tIdx].Id +'成功\n';
            }
        }

        // for (Campaign cam : scope) {
        //     cam.Mailflg_before7__c = true;
        // }
        // System.debug('20------'+scope);
        // Database.SaveResult[] lsr = Database.update(scope, false);
        // for (Integer tIdx = 0; tIdx < lsr.size(); tIdx++) {
        //     Database.SaveResult sr = lsr[tIdx];
        //     //如果更新失败
        //     if (!sr.isSuccess()) {
        //         Database.Error emsg = sr.getErrors()[0];
        //         errorIdLog += scope[tIdx].Id+',';
        //         errorlog += '更新学会:'+scope[tIdx].Name +'失败,错误出现在:'+emsg.getMessage() +'\n';
        //     }else{
        //         succlog += '更新学会:'+scope[tIdx].Name +'成功\n';
        //     }
        // }

        System.debug('55::'+updateCamList);
        // update scope;
    }

    public void finish(Database.BatchableContext BC) {
        System.debug('60001111:::');
        if (errorIdLog.length() > 0) {
            iflog.ErrorLog__c += '更新:'+errorIdLog.substring(0,errorIdLog.length()-1) +'失败\n';
        }
        iflog.ErrorLog__c += errorlog;
        iflog.Log__c += succlog;
        iflog.Log__c += 'end --';
        insert iflog;
    }
}
/*//将学会上的工作流规则 '更新会议开始前7天提醒邮件标识' 复制为batch
public without sharing class updateCampaignMailflg7BeTrueBatch implements Database.Batchable<SObject>,Database.Stateful {
    public String query;
    public String campaignId;
    private BatchIF_Log__c iflog;
    private String errorlog = '';
    private String succlog = '';
    private String errorIdLog = '';

    public updateCampaignMailflg7BeTrueBatch() {
        this.query = query;
        iflog = new BatchIF_Log__c();
        iflog.Type__c = 'updateCampaignMailflg7BeTrue';
        iflog.Log__c = 'start --\n';
        iflog.ErrorLog__c = '';
    }

    public updateCampaignMailflg7BeTrueBatch(String camId) {
        this.query = query;
        this.campaignId = camId;
        iflog = new BatchIF_Log__c();
        iflog.Type__c = 'updateCampaignMailflg7BeTrue';
        iflog.Log__c = 'start --\n';
        iflog.ErrorLog__c = '';
    }



    public Database.QueryLocator start(Database.BatchableContext bc) {
        query = 'select id,Mailflg_before7__c,Name from Campaign where Open_day__c != null ';
        query += ' and Status != \'已提交报告\' and Status != \'已结束\' and Status != \'取消\' and Status != \'已结束(没公开))\'';
        query += ' and BetweenTodayStartDate__c = 7 and RecordType.DeveloperName != \'5.服务培训/技师培养\' and Mailflg_before7__c = false ';
        if (campaignId != null && campaignId != '') {
            query += ' and id = :campaignId ';
        }
        return Database.getQueryLocator(query);
    }

    public void execute(Database.BatchableContext BC, list<Campaign> scope) {
        for (Campaign cam : scope) {
            cam.Mailflg_before7__c = true;
        }
        System.debug('20------'+scope);
        Database.SaveResult[] lsr = Database.update(scope, false);
        for (Integer tIdx = 0; tIdx < lsr.size(); tIdx++) {
            Database.SaveResult sr = lsr[tIdx];
            //如果更新失败
            if (!sr.isSuccess()) {
                Database.Error emsg = sr.getErrors()[0];
                errorIdLog += 'Id:'+scope[tIdx].Id+'\n';
                errorlog += '更新学会:'+scope[tIdx].Name +'失败,错误出现在:'+emsg.getMessage() +'\n';
            }else{
                succlog += '更新学会:'+scope[tIdx].Name +'成功\n';
            }
        }
        System.debug('55::'+iflog);
        // update scope;
    }

    public void finish(Database.BatchableContext BC) {
        System.debug('60001111:::');
        iflog.ErrorLog__c += '更新:\n';
        iflog.ErrorLog__c += errorIdLog +'失败';
        iflog.ErrorLog__c += errorlog;
        iflog.Log__c += succlog;
        iflog.Log__c += 'end --';
        insert iflog;
    }
}*/


/*//将学会上基于时间的更新赋值复制到batch中
public without sharing class updateCampaignMailflg7BeTrueBatch implements Database.Batchable<SObject>,Database.Stateful {
    public String query;
    public String campaignId;
    private BatchIF_Log__c iflog;
    private String errorlog = '';
    private String succlog = '';

    public updateCampaignMailflg7BeTrueBatch() {
        this.query = query;
        iflog = new BatchIF_Log__c();
        iflog.Type__c = 'updateCampaignMailflg7BeTrue';
        iflog.Log__c = 'start --\n';
        iflog.ErrorLog__c = '';
    }

    public updateCampaignMailflg7BeTrueBatch(String camId) {
        this.query = query;
        this.campaignId = camId;
        iflog = new BatchIF_Log__c();
        iflog.Type__c = 'updateCampaignMailflg7BeTrue';
        iflog.Log__c = 'start --\n';
        iflog.ErrorLog__c = '';
    }



    public Database.QueryLocator start(Database.BatchableContext bc) {
        if (campaignId != null && campaignId != '') {
            return Database.getQueryLocator(
                [select id,Mailflg_before7__c,Name,RecordType.DeveloperName,Open_day__c,Mailflg_before7_service__c,Mailflg_cancel__c,BetweenTodayStartDate__c,Status from Campaign where id = :campaignId and (Mailflg_before15__c = false or Mailflg_cancel__c = false or Mailflg_after45__c = false or Mailflg_after3__c = false or Mailflg_before7_service__c = false or Mailflg_before7__c = false)]
            );
        }else{
            return Database.getQueryLocator([
                select id,Mailflg_before7__c,Name,RecordType.DeveloperName,Open_day__c,Mailflg_before7_service__c,Mailflg_cancel__c,BetweenTodayStartDate__c,Status from Campaign where Mailflg_before15__c = false or Mailflg_cancel__c = false or Mailflg_after45__c = false or Mailflg_after3__c = false or Mailflg_before7_service__c = false or Mailflg_before7__c = false
            ]);
        }

        
        // return Database.getQueryLocator(query);
    }

    public void execute(Database.BatchableContext BC, list<Campaign> scope) {
        Date today = Date.today();
        //查到的学会就是需要更新相关字段为true的学会,需要判断把谁变成true
        for (Campaign cam : scope) {
            //记录类型是 5.服务培训/技师培养 时,更新更新服务技师培训项目开始前7天提醒邮件标识为true
            if (cam.RecordType.DeveloperName == '5.服务培训/技师培养') {
                cam.Mailflg_before7_service__c = true;
                succlog += '72:--'; 
            }else{
                //会议计划取消提醒邮件标识
                if (cam.Status== '取消' && cam.Open_day__c != null ) {
                    cam.Mailflg_cancel__c = true;
                    succlog += '77:--'; 
                }
                if (cam.Open_day__c != null && cam.Status != '已提交报告' && cam.Status != '已结束' && cam.Status != '取消' && cam.Status != '已结束(没公开)' ) {
                    //会议开始前15天提醒邮件标识
                    if (cam.Mailflg_before15__c == false && cam.StartDate != null && today.daysBetween(cam.StartDate) == 15) {
                        cam.Mailflg_before15__c = true;
                        succlog += '83:--'; 
                    }   
                    //会议开始前7天提醒邮件标识
                    if (cam.Mailflg_before15__c == false && cam.StartDate != null && today.daysBetween(cam.StartDate) == 7) {
                        cam.Mailflg_before7__c = true;
                        succlog += '88:--'; 
                    }
                }

                //会议计划登录45天提醒邮件标识 
                if (cam.Status != '已结束' && cam.Status != '取消' && cam.Status!= '已结束(没公开)' && cam.Open_day__c == null) {
                    cam.Mailflg_after45__c = true;
                    succlog += '95:--'; 
                }

                //会议结束后3天提醒邮件标识
                if ((cam.Status== '公开中' || cam.Status == '取消申请中' || cam.Status == '报告提交' ) && cam.BetweenTodayEndDate__c >= -3) {
                    cam.Mailflg_after3__c = true;
                    succlog += '94:--'; 
                }
            }
            // cam.Mailflg_before7__c = true;
            // cam.HostName__c = null;
        }

        System.debug('10111::'+scope);
        System.debug('20------'+scope);
        Database.SaveResult[] lsr = Database.update(scope, false);
        for (Integer tIdx = 0; tIdx < lsr.size(); tIdx++) {
            Database.SaveResult sr = lsr[tIdx];
            //如果更新失败
            if (!sr.isSuccess()) {
                Database.Error emsg = sr.getErrors()[0];
                errorlog += '更新学会:'+scope[tIdx].Id +scope[tIdx].Name +'失败,错误出现在:'+emsg.getMessage() +'\n';
            }else{
                succlog += '更新学会:'+scope[tIdx].Name +'成功\n';
            }
        }
        System.debug('55::'+iflog);
        // update scope;
    }

    public void finish(Database.BatchableContext BC) {
        System.debug('60001111:::');
        iflog.ErrorLog__c += errorlog;
        iflog.Log__c += succlog;
        iflog.Log__c += 'end --';
        insert iflog;
    }
}

*/
/*
        // query = 'select id,Mailflg_before7__c,Name,RecordType.DeveloperName,Open_day__c,Mailflg_before7_service__c,Mailflg_cancel__c,BetweenTodayStartDate__c,Status from Campaign where Mailflg_before15__c = false or Mailflg_cancel__c = false or Mailflg_after45__c = false or Mailflg_after3__c = false or Mailflg_before7_service__c = false or Mailflg_before7__c = false ';
        // if (campaignId != null && campaignId != '') {
        //     query += 'and id = :campaignId';
        // }

        //会议开始前15天提醒邮件标识
        // query += ' (Open_day__c != null and Status != \'已提交报告\' and Status != \'已结束\' and Status != \'取消\' and Status != \'已结束(没公开))\' and BetweenTodayStartDate__c >= 15 and RecordType.DeveloperName != \'5.服务培训/技师培养\' and Mailflg_before15__c = false ) ';

        // //会议计划取消提醒邮件标识
        // query += '  or (Status = \'取消\' and Open_day__c != null and RecordType.DeveloperName != \'5.服务培训/技师培养\' and Mailflg_cancel__c = false)  ';

        // //会议计划登录45天提醒邮件标识 
        // query += '( Open_day__c = null and Status != \'已结束\' and Status != \'取消\' and Status != \'已结束(没公开))\' and RecordType.DeveloperName != \'5.服务培训/技师培养\'  and  Mailflg_after45__c = false) OR';

        // //会议结束后3天提醒邮件标识
        // query += '( (Status = \'公开中\' or Status = \'取消申请中\' or Status = \'报告提交\') and BetweenTodayEndDate__c >= -3 and RecordType.DeveloperName != \'5.服务培训/技师培养\' and Mailflg_after3__c = false ) OR';

        // //更新服务技师培训项目开始前7天提醒邮件标识
        // query += '(Open_day__c != null and Status != \'已提交报告\' and Status != \'已结束\' and Status != \'取消\' and Status != \'已结束(没公开))\'  and BetweenTodayStartDate__c >= 7  and RecordType.DeveloperName = \'5.服务培训/技师培养\' and Mailflg_before7_service__c = false ) or';

        // //会议开始前7天提醒邮件标识
        // query += '( Open_day__c != null  and Status != \'已提交报告\' and Status != \'已结束\' and Status != \'取消\' and Status != \'已结束(没公开))\' and BetweenTodayStartDate__c = 7 and RecordType.DeveloperName != \'5.服务培训/技师培养\' and Mailflg_before7__c = false )';
*/