| | |
| | | // public String campaignId; |
| | | public List<String> campaignIds; |
| | | public String logId; |
| | | |
| | | public Map<String,List<String>> errorMap = new Map<String,List<String>>(); |
| | | |
| | | //定义map 存储 字段值和字段名 |
| | | public Map<String,String> map1 = new Map<String,String>(); |
| | | 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 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,RecordType.DeveloperName,Open_day__c,Mailflg_before7_service__c,Mailflg_cancel__c,BetweenTodayStartDate__c,Status,StartDate,Mailflg_before15__c,Mailflg_after45__c,Mailflg_after3__c,BetweenTodayEndDate__c from Campaign where '; |
| | | //会议开始前7,15天提醒邮件标识 服务技师和 非服务技师 |
| | | query += '(Open_day__c != null and Status != \'已提交报告\' and Status != \'已结束\' AND Status != \'取消\' AND Status != \'已结束(没公开)\' AND (BetweenTodayStartDate__c = 15 OR BetweenTodayStartDate__c = 7) AND (Mailflg_before15__c = false or Mailflg_before7__c = false) ) or '; |
| | | //会议结束后3天提醒邮件标识 |
| | | query += '( (Status = \'公开中\' or Status = \'取消申请中\' or Status = \'报告提交\') AND BetweenTodayEndDate__c = -3 AND RecordType.DeveloperName != \'5.服务培训/技师培养\' AND Mailflg_after3__c = false) OR '; |
| | | //会议计划登录45天提醒邮件标识 |
| | | query += '(Open_day__c = null and Status != \'已结束\' and Status != \'取消\' and Status != \'已结束(没公开)\' AND RecordType.DeveloperName != \'5.服务培训/技师培养\' and BetweenTodayStartDate__c = -45 and Mailflg_after45__c = false) '; |
| | | } |
| | | return Database.getQueryLocator(query); |
| | | } |
| | | |
| | | public void execute(Database.BatchableContext BC, list<SObject> scope) { |
| | | List<Campaign> updateCamList = new List<Campaign>(); |
| | | //说明当前查到的集合为log集合 |
| | | if (logId != null && logId != '') { |
| | | /*现在batch日志里的内容类似: |
| | | log.ErrorLog__c = 'errorIdStart:{"Mailflg_before7__c":["ID1","ID2"],"Mailflg_before15__c":["ID3"]} |
| | | 更新学会:ID1 失败,错误出现在:XXX |
| | | 更新学会:ID2 失败,错误出现在:XXX |
| | | 更新学会:ID3 失败,错误出现在:XXX |
| | | */ |
| | | //获取错误日志并转为map进行赋值 |
| | | List<BatchIF_Log__c> logs = scope; |
| | | BatchIF_Log__c log = logs.get(0); |
| | | String jsonLogstr = log.ErrorLog__c.subString(log.ErrorLog__c.indexOf('{'),log.ErrorLog__c.indexOf('}')+1); |
| | | System.debug('jsonLogstr::'+jsonLogstr); |
| | | Map<String,Object> resultMap = (Map<String,Object>) JSON.deserializeUntyped(jsonLogstr); |
| | | System.debug('resultMap::'+resultMap); |
| | | List<SObject> sobjList = new List<SObject>(); |
| | | for (String key : resultMap.keySet()) { |
| | | String tempStr = String.valueOf(resultMap.get(key)); |
| | | String str = tempStr.substring(tempStr.indexOf('(')+1,tempStr.indexOf(')')); |
| | | for (String value : str.split(',')) { |
| | | Sobject cam = new Campaign(); |
| | | cam.put(key, true); |
| | | cam.put('Id', value.trim().substring(0,15)); |
| | | map1.put(value.trim().substring(0,15), key); |
| | | sobjList.add(cam); |
| | | } |
| | | } |
| | | |
| | | updateCamList = (List<Campaign>)sobjList; |
| | | |
| | | }else{ |
| | | Date today = Date.today(); |
| | | List<Campaign> camList = new List<Campaign>(); |
| | | camList = scope; |
| | | //判断各个需要更新的字段 |
| | | for (Campaign cam : camList) { |
| | | String camId = String.valueOf(cam.Id).substring(0,15); |
| | | //记录类型是 5.服务培训/技师培养 时,更新更新服务技师培训项目开始前7天提醒邮件标识为true |
| | | if (cam.RecordType.DeveloperName == 'ServiceEngineerTraining') { |
| | | if (cam.Open_day__c != null && cam.Status != '已提交报告' && cam.Status != '已结束' && cam.Status != '取消' && cam.Status != '已结束(没公开)' && cam.StartDate != null && today.daysBetween(cam.StartDate) == 7 && cam.Mailflg_before7_service__c == false) { |
| | | cam.Mailflg_before7_service__c = true; |
| | | updateCamList.add(cam); |
| | | map1.put(camId, 'Mailflg_before7_service__c'); |
| | | } |
| | | }else{ |
| | | 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; |
| | | map1.put(camId, 'Mailflg_before15__c'); |
| | | updateCamList.add(cam); |
| | | } |
| | | //会议开始前7天提醒邮件标识 |
| | | if (cam.Mailflg_before7__c == false && cam.StartDate != null && today.daysBetween(cam.StartDate) == 7) { |
| | | cam.Mailflg_before7__c = true; |
| | | map1.put(camId, 'Mailflg_before7__c'); |
| | | updateCamList.add(cam); |
| | | } |
| | | } |
| | | |
| | | //会议计划登录45天提醒邮件标识 |
| | | if (cam.Status != '已结束' && cam.Status != '取消' && cam.Status!= '已结束(没公开)' && cam.Open_day__c == null && cam.Mailflg_after45__c == false ) { |
| | | cam.Mailflg_after45__c = true; |
| | | map1.put(camId, 'Mailflg_after45__c'); |
| | | updateCamList.add(cam); |
| | | } |
| | | |
| | | //会议结束后3天提醒邮件标识 |
| | | if ((cam.Status== '公开中' || cam.Status == '取消申请中' || cam.Status == '报告提交' ) && cam.Mailflg_after3__c == false) { |
| | | cam.Mailflg_after3__c = true; |
| | | map1.put(camId, 'Mailflg_after3__c'); |
| | | updateCamList.add(cam); |
| | | |
| | | } |
| | | } |
| | | } |
| | | |
| | | } |
| | | 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]; |
| | | String errorId = updateCamList[tIdx].Id; |
| | | String errorColumn = map1.get(errorId.substring(0,15)); |
| | | if (!errorMap.containsKey(errorColumn)) { |
| | | errorMap.put(errorColumn, new List<String>()); |
| | | } |
| | | errorMap.get(errorColumn).add(errorId); |
| | | errorlog += '更新学会:'+errorId +'失败,错误出现在:'+emsg.getMessage() +'\n'; |
| | | }else{ |
| | | succlog += '更新学会:'+updateCamList[tIdx].Id +'成功\n'; |
| | | } |
| | | } |
| | | } |
| | | |
| | | public void finish(Database.BatchableContext BC) { |
| | | if (!errorMap.containsKey(null) && errorMap != null && errorMap.size() > 0) { |
| | | //转json 方便传id时转换 |
| | | iflog.ErrorLog__c = 'errorIdStart:\n'+JSON.serialize(errorMap)+'\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; |
| | | public List<String> campaignIds; |
| | | public String logId; |
| | | private BatchIF_Log__c iflog; |
| | | private String errorlog = ''; |
| | | private String succlog = ''; |
| | |
| | | insert iflog; |
| | | } |
| | | } |
| | | */ |
| | | /*//将学会上的工作流规则 '更新会议开始前7天提醒邮件标识' 复制为batch |
| | | public without sharing class updateCampaignMailflg7BeTrueBatch implements Database.Batchable<SObject>,Database.Stateful { |
| | | public String query; |