global with sharing class SyncLostOppLineItemBatch implements Database.Batchable, Database.Stateful { public List productList; global SyncLostOppLineItemBatch(List proList) { productList = proList; } global Database.QueryLocator start(Database.BatchableContext bc) { List proIdList = new List(); for(Product2 pro: productList){ proIdList.add(pro.Id); } String query = 'select Id,OpportunityId,Product2Id,Quantity,OpportunityId__c,ProductClass__c from OpportunityLineItem where Product2Id in: proIdList'; return Database.getQueryLocator(query); } global void execute(Database.BatchableContext BC, List oppLIList) { System.debug('20240305---lt---oppLIList:'+oppLIList); Map oppNumMap = new Map(); List oppList = new List(); Set tenSet = new Set(); List tenList = new List(); for(OpportunityLineItem oppLi: oppLIList){ System.debug('20240305---lt---oppLi.ProductClass__c:'+oppLi.ProductClass__c); if(oppNumMap.containsKey(oppLi.OpportunityId__c)){ Integer a = oppNumMap.get(oppLi.OpportunityId__c); if(oppLi.ProductClass__c == '主机' && String.isNotBlank(String.ValueOf(oppLi.Quantity))){ a += Integer.ValueOf(oppLi.Quantity); } oppNumMap.put(oppLi.OpportunityId__c,a); }else{ if(oppLi.ProductClass__c == '主机' && String.isNotBlank(String.ValueOf(oppLi.Quantity))){ oppNumMap.put(oppLi.OpportunityId__c,Integer.ValueOf(oppLi.Quantity)); }else{ oppNumMap.put(oppLi.OpportunityId__c,0); } } } System.debug('20240305---lt---oppNumMap:'+oppNumMap); for(String oppId : oppNumMap.keyset()){ Opportunity opp = new Opportunity(); opp.Id = oppId; opp.OlyNumberHosts__c = oppNumMap.get(oppId); oppList.add(opp); } if(oppList.Size() > 0){ StaticParameter.EscapeOppandStaTrigger = true; update oppList; StaticParameter.EscapeOppandStaTrigger = false; List tolinkList = [select id,Opportunity__c,Tender_information__c from Tender_Opportunity_Link__c where Opportunity__c in :oppList]; if(tolinkList.Size() > 0){ for(Tender_Opportunity_Link__c tol : tolinkList){ tenSet.add(tol.Tender_information__c); } for(String tenId : tenSet){ Tender_information__c ten = new Tender_information__c(); ten.Id = tenId; ten.IsReactionOpp__c = true; tenList.add(ten); } if(tenList.Size() > 0){ StaticParameter.EscapeOtherUpdateTenOwner = true; update tenList; StaticParameter.EscapeOtherUpdateTenOwner = false; } } } } global void finish(Database.BatchableContext BC) { } }