global with sharing class SyncLostOppLineItemBatch implements Database.Batchable<sObject>, Database.Stateful {
    
    public List<Product2> productList;

    global SyncLostOppLineItemBatch(List<Product2> proList) {
        productList = proList;
    }
    global Database.QueryLocator start(Database.BatchableContext bc) {
        List<String> proIdList = new List<String>();
        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<OpportunityLineItem> oppLIList) {

        System.debug('20240305---lt---oppLIList:'+oppLIList);
        Map<String,Integer> oppNumMap = new Map<String,Integer>();
        List<Opportunity> oppList = new List<Opportunity>();
        Set<String> tenSet = new Set<String>();
        List<Tender_information__c> tenList = new List<Tender_information__c>();
        
        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<Tender_Opportunity_Link__c> 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) {
    }
}