/************************************************************************************************** @Author: Denny陈帮才 @Name: SummaryContractFMBatch @CreateDate: 2023/01/15 @Description: 市场多年保续签至合同的价格开发 @Version 1.0 *****************************************************************************************************/ global class SummaryContractFMBatch implements Database.Batchable,Database.Stateful { public String query; public List < String > contactLists; private BatchIF_Log__c iflog; global SummaryContractFMBatch() { this.query = query; } global SummaryContractFMBatch(List < String > contactLists) { this.contactLists = contactLists; } global Database.QueryLocator start(Database.BatchableContext bc) { system.debug('执行start'); iflog = new BatchIF_Log__c(); iflog.Type__c = 'PushNotification'; iflog.Log__c = 'SummaryContractFMBatch start\n'; iflog.ErrorLog__c = ''; insert iflog; // query ='select Id, Hospital__c from Maintenance_Contract__c where ((Contract_End_Date__c>=:sTime and Contract_End_Date__c<=:eTime) or (Contract_Start_Date__c >=:sTime and Contract_Start_Date__c <=:eTime) or(Contract_Start_Date__c <:sTime and Contract_End_Date__c>:eTime)) and RecordType.Name!=\'多年保修合同\' '; // query ='select Id,FM__c from Maintenance_Contract__c where RecordType.Name = \'多年保修合同\''; query ='SELECT Asset__c,Asset__r.Guarantee_period_for_products__c,Asset__r.Posting_Date__c,Asset__r.InstallDate FROM Maintenance_Contract_Asset__c where Maintenance_Contract__r.RecordType_DeveloperName__c =\'VM_Contract\' and Asset__r.RecordType.Name!=\'备品\' and (Maintenance_Contract__r.Status__c=\'契約\' or Maintenance_Contract__r.Status__c=\'契約満了\')'; if (contactLists != null && contactLists.size() > 0) { query += ' AND Id IN :contactLists '; } System.debug(LoggingLevel.INFO, '*** query: ' + query); return Database.getQueryLocator(query); } global void execute(Database.BatchableContext BC, list scope) { Date today = Date.today(); List assetIds = new List(); for (Maintenance_Contract_Asset__c mc : scope) { assetIds.add(mc.Asset__c); } // List updateContractAssets = [SELECT Asset__r.Intra_Trade_Gurantee_RMB__c,Maintenance_Contract__c FROM Maintenance_Contract_Asset__c WHERE Maintenance_Contract__c = :mcIds] //汇总分子:多年保修期间的所有【修理原价】累计的金额,请注意多年保修期间内,需要计算经历月数内的如上修理金额;如果是多年保修期已经结束,需要计算3年多年保修期间的如上修理金额 MapmcMoney = new Map(); for (Repair__c rc :[SELECT Delivered_Product__c,ListPrice__c,Delivered_Product__r.Guarantee_period_for_products__c,Delivered_Product__r.InstallDate__c,Failure_Occurrence_Date__c // (SELECT ListPrice__c FROM RepairRelation__r WHERE ListPrice__c >0) FROM Repair__c WHERE Delivered_Product__c IN:assetIds AND ListPrice__c > 0 AND Status1__c!='0.删除' AND Status1__c!='0.取消' AND Status2__c!='00.删除' AND Status2__c!='00.取消']){ // AND Failure_Occurrence_Date__c < Delivered_Product__r.Guarantee_period_for_products__c // AND((Failure_Occurrence_Date__c > Delivered_Product__r.Posting_Date__c) OR (Failure_Occurrence_Date__c > Delivered_Product__r.InstallDate))] ) { // if((rc.Failure_Occurrence_Date__c < rc.Delivered_Product__r.Guarantee_period_for_products__c) && ((rc.Failure_Occurrence_Date__c > rc.Delivered_Product__r.Posting_Date__c) || (rc.Failure_Occurrence_Date__c > rc.Delivered_Product__r.InstallDate)) //保修期限内 // if(today< rc.Delivered_Product__r.Guarantee_period_for_products__c){ // Date today=td.toStartOfMonth(); //如 2022.10.1 // }else{ //汇总 故障发生日处于保有设备 保修开始日 及 保修期限内 的所有修理原价 if((rc.Failure_Occurrence_Date__c < rc.Delivered_Product__r.Guarantee_period_for_products__c) && (rc.Failure_Occurrence_Date__c > rc.Delivered_Product__r.InstallDate__c)){ // Date purDate = rc.Delivered_Product__r.Guarantee_period_for_products__c; Decimal rcPrice = rc.ListPrice__c; // if(today < purDate){ // Decimal bDay = purDate.daysBetween(today); // rcPrice = mcMoney.get(mca.Asset__c) * (bDay /30) /12; // } if(!mcMoney.containsKey(rc.Delivered_Product__c)){ mcMoney.put(rc.Delivered_Product__c,rcPrice); }else{ mcMoney.put(rc.Delivered_Product__c,mcMoney.get(rc.Delivered_Product__c) + rcPrice); } } } for (Maintenance_Contract_Asset__c mca : scope) { if(mcMoney.containsKey(mca.Asset__c)){ // Date purDate = mca.Asset__r.Guarantee_period_for_products__c; // if(today > purDate){ // Decimal bDay = purDate.daysBetween(today); // mca.Summary_FZ__c = mcMoney.get(mca.Asset__c) * (bDay /30) /12; // }else{ // mca.Summary_FZ__c = mcMoney.get(mca.Asset__c); // } mca.Summary_FZ__c = mcMoney.get(mca.Asset__c); } } update scope; } global void finish(Database.BatchableContext BC) { iflog.Log__c += '\nEquipmentRepairBatch 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; } }