global class Create888AlarmBatch implements Database.Batchable , Database.AllowsCallouts, Database.Stateful { public Date installDate = Date.newInstance(1900, 1, 1); public Date nowDate = Date.today(); global Create888AlarmBatch() { } //Database.QueryLocator global List start(Database.BatchableContext bc) { // test 02i10000002dHUPAA2 // CurrentContract__r.RecordType_Name__c ---最近一期维修合同类型,Is_Add_888_Alarm__c --是否生成过888预警对象 List assetList = [Select Id,InstallDate,SerMarGuranteeType__c,InstallDate__c,Extend_Gurantee_DateTo__c,CurrentContract__c,CurrentContract_Start_Date__c,CurrentContract_End_Date__c ,Is_Add_888_Alarm__c,CurrentContract__r.RecordType_Name__c FROM Asset WHERE (MDM_Model_No__c = 'OER-AW' OR MDM_Model_No__c = 'OER-Smart') AND Is_Add_888_Alarm__c = false AND (InstallDate!= null AND InstallDate!= :installDate) AND ((CurrentContract_End_Date__c >=: nowDate AND CurrentContract_Start_Date__c <= :nowDate) OR (Extend_Gurantee_DateTo__c >= :nowDate AND InstallDate__c <= :nowDate ))]; return assetList; } global void execute(Database.BatchableContext BC,List assetList) { System.debug(LoggingLevel.INFO, '*** assetList: ' + assetList); if (assetList.size() > 0) { List alarm888List= new List(); for (Asset asset : assetList) { Integer alarmNumber = 1; if (asset.InstallDate__c != null && asset.Extend_Gurantee_DateTo__c != null && asset.Extend_Gurantee_DateTo__c >= nowDate && asset.InstallDate__c <= nowDate) { //多年保 第一次提醒日-1天 Date startDate = asset.InstallDate.addMonths(6).addDays(-1); //monthsBetween Integer count = (asset.Extend_Gurantee_DateTo__c.year()-asset.InstallDate__c.year()) * 2; while(alarmNumber<= count && startDate <= asset.Extend_Gurantee_DateTo__c){ if (startDate >= nowDate) { Alarm_888_Month__c alarm888 = new Alarm_888_Month__c(); alarm888.Number_Of_Operations__c = '第'+alarmNumber+'次作业'; alarm888.Nth_Operating_Day__c = startDate; alarm888.Alarm_Date__c = alarm888.Nth_Operating_Day__c.addMonths(-2); alarm888.Asset_ID__c = asset.Id; // 2023/08/24 888添加提醒类型字段 根据保有设备.多年保修类型 判断是哪种多年保修 if (asset.SerMarGuranteeType__c == '服务多年保修') { alarm888.Alarm_Type__c = '服务多年保修'; }else if (asset.SerMarGuranteeType__c == '市场多年保修'){ alarm888.Alarm_Type__c = '市场多年保修'; } alarm888List.add(alarm888); alarmNumber++; } startDate = startDate.addMonths(6); } }else if (asset.CurrentContract_Start_Date__c != null && asset.CurrentContract_End_Date__c != null){ //维修合同 第一次提醒日-1天 Date startDate = asset.InstallDate.addMonths(6).addDays(-1); //monthsBetween Integer count = (asset.CurrentContract_End_Date__c.year()-asset.CurrentContract_Start_Date__c.year()) * 2; //结束时间大于上一次作业时间3个月则生成 while(alarmNumber<= count && startDate <= asset.CurrentContract_End_Date__c){ if (startDate >= nowDate) { Alarm_888_Month__c alarm888 = new Alarm_888_Month__c(); alarm888.Number_Of_Operations__c = '第'+alarmNumber+'次作业'; alarm888.Nth_Operating_Day__c = startDate; alarm888.Alarm_Date__c = alarm888.Nth_Operating_Day__c.addMonths(-2); alarm888.Asset_ID__c = asset.Id; // 2023/08/24 888添加提醒类型字段 alarm888.Alarm_Type__c = '服务合同'; alarm888List.add(alarm888); alarmNumber++; } startDate = startDate.addMonths(6); } } asset.Is_Add_888_Alarm__c = true; } StaticParameter.EscapeVMCTrigger = true; update assetList; StaticParameter.EscapeVMCTrigger = false; insert alarm888List; System.debug(LoggingLevel.INFO, '*** assetList: ' + assetList); System.debug(LoggingLevel.INFO, '*** alarm888List: ' + alarm888List); } } global void finish(Database.BatchableContext BC) { //发送邮件 Id execEmailId = Database.executeBatch(new Email888AlarmBatch(),100); } }