public with sharing class OpportunityLineItemTrigger {
    public static void setOpportunityAfter(List<OpportunityLineItem> newList,
        List<OpportunityLineItem> oldList, Map<Id, OpportunityLineItem> newMap, Map<Id, OpportunityLineItem> oldMap) {
        List<String> selIds = new List<String>();
        Map<String, OpportunityLineItem> deleteOpportunityLineItemMap = new Map<String, OpportunityLineItem>();
        if (Trigger.isInsert || Trigger.isUpdate) {
            // OpportunityLineItemのOpportunityIdを集める
            for (OpportunityLineItem oppli : newList) {
                if (oppli.OpportunityId != null) {
                    selIds.add(oppli.OpportunityId);
                }
            }
        } else if (Trigger.isDelete) {
            // OpportunityLineItemのOpportunityIdを集める
            for (OpportunityLineItem oppli : oldList) {
                if (oppli.OpportunityId != null) {
                    selIds.add(oppli.OpportunityId);
                    deleteOpportunityLineItemMap.put(oppli.Id, oppli);
                }
            }
        }

        List<OpportunityLineItem> retList =
            [Select Id, OpportunityId, Category3__c, Product2.MDM_Model_No__c, Opportunity.STMS_Package_Sale__c
             , Opportunity.State_Owner__c, Product2.Family, Opportunity.EnergyHost__c
             from OpportunityLineItem
             where OpportunityId in: selIds
             order by OpportunityId];


        if (retList.size() > 0) {

            Map<String, Map<String, OpportunityLineItem>> opportunityMap = new Map<String, Map<String, OpportunityLineItem>>();

            for (OpportunityLineItem oli : retList) {
                // 排除待删除的数据 防止对现有数据的干扰
                if (deleteOpportunityLineItemMap.size() > 0 && deleteOpportunityLineItemMap.containsKey(oli.Id)) {
                    continue;
                }

                Map<String, OpportunityLineItem> familyMap = new Map<String, OpportunityLineItem>();
                // 产品的第3分类 是 主机 或 能量主机
                if (String.isNotBlank(oli.Category3__c) && oli.Category3__c.contains('主机')) {
                    string mapkey = oli.Product2.Family + '_' + oli.Category3__c;
                    if (opportunityMap.containsKey(oli.OpportunityId)) {
                        familyMap = opportunityMap.get(oli.OpportunityId);
                    }
                    familyMap.put(mapkey, oli);

                    opportunityMap.put(oli.OpportunityId, familyMap);
                }
            }
            Map<String, Opportunity> updateOpportunityMap = new Map<String, Opportunity>();
            for (OpportunityLineItem oli : retList) {
                // 获取原字段值
                String oppId = oli.OpportunityId;
                //能量打包
                Boolean getOldStmsPackageSale  = oli.Opportunity.STMS_Package_Sale__c;
                //能量主机
                Boolean getOldEnergyHost  = oli.Opportunity.EnergyHost__c;
                Opportunity updateOpportunity = new Opportunity();
                updateOpportunity.Id = oppId;
                // 判断 询价产品中是否含有主机产品
                if (!updateOpportunityMap.containsKey(oppId)) {
                    if (opportunityMap.containsKey(oppId) ) {
                        Map<String, OpportunityLineItem> familyMap =   opportunityMap.get(oppId);
                        // 判断 询价产品是否含有 ENG 主机 产品
                        string mapkey1 = 'ENG_主机';
                        string mapkey2 = 'ENG_能量主机';
                        if (familyMap.containsKey(mapkey1) || familyMap.containsKey(mapkey2)) {
                            // 只含有 ENG 主机产品
                            if (familyMap.size() == 1 ) {
                                //能量打包 False
                                //能量主机 True
                                if (getOldStmsPackageSale || !getOldEnergyHost) {
                                    updateOpportunity.STMS_Package_Sale__c = false;
                                    updateOpportunity.EnergyHost__c =  true;
                                    updateOpportunityMap.put(oppId, updateOpportunity);
                                }
                            } else {
                                //判断是否都是主机
                                Boolean principalMachine = true;
                                //判断是否都是能量主机
                                Boolean energyHost = true;
                                Boolean notContains = true;
                                Boolean principalMachinENG = false;
                                for(String key :familyMap.keySet()) {


                                    OpportunityLineItem mapOli = familyMap.get(key);
                                    System.debug('mapOli--->'+mapOli);
                                    if (!'能量主机'.equals(mapOli.Category3__c)) {
                                        energyHost = false;
                                        System.debug('test--1-->');
                                        if(!'ENG'.equals(mapOli.Product2.Family)){
                                            principalMachine = false;
                                        } 
                                        else {
                                            principalMachinENG = true;
                                        }
                                    } 
                                }
                                
                                if (energyHost ) {
                                    // 都是能量主机
                                    //能量打包 False
                                    //能量主机 True
                                    if (getOldStmsPackageSale || !getOldEnergyHost) {
                                        updateOpportunity.STMS_Package_Sale__c = false;
                                        updateOpportunity.EnergyHost__c =  true;
                                        updateOpportunityMap.put(oppId, updateOpportunity);
                                    }     
                                   
                                   
                                } else {
                                    if (principalMachine) {
                                        // 只有 ENG 主机
                                        //能量打包 False
                                        //能量主机 True
                                        if (getOldStmsPackageSale || !getOldEnergyHost) {
                                            updateOpportunity.STMS_Package_Sale__c = false;
                                            updateOpportunity.EnergyHost__c =  true;
                                            updateOpportunityMap.put(oppId, updateOpportunity);
                                        }     
                                    } else {
                                        if (principalMachinENG ) {
                                            // 能量打包 True
                                            // 能量主机 False
                                            if (!getOldStmsPackageSale || getOldEnergyHost) {
                                                updateOpportunity.STMS_Package_Sale__c = true;
                                                updateOpportunity.EnergyHost__c =  false;
                                                updateOpportunityMap.put(oppId, updateOpportunity);
                                            }    
                                        } else {
                                            if (getOldStmsPackageSale || getOldEnergyHost) {
                                                updateOpportunity.STMS_Package_Sale__c = false;
                                                updateOpportunity.EnergyHost__c =  false;
                                                updateOpportunityMap.put(oppId, updateOpportunity);

                                            }
                                        }
                                    }
                                }
                                /*
                                // 含有 多种 主机 产品
                                // 能量打包 True
                                // 能量主机 False
                                if (!getOldStmsPackageSale || getOldEnergyHost) {
                                    updateOpportunity.STMS_Package_Sale__c = true;
                                    updateOpportunity.EnergyHost__c =  false;
                                    updateOpportunityMap.put(oppId, updateOpportunity);
                                }
                                */
                            }
                        } else {
                            // 不含有 ENG 主机 产品
                            // 能量打包 False
                            // 能量主机 False
                            if (getOldStmsPackageSale || getOldEnergyHost) {
                                updateOpportunity.STMS_Package_Sale__c = false;
                                updateOpportunity.EnergyHost__c =  false;
                                updateOpportunityMap.put(oppId, updateOpportunity);
                            }
                        }
                    } else {
                        // 不含有 主机 产品
                        // 能量打包 False
                        // 能量主机 False
                        if (getOldStmsPackageSale || getOldEnergyHost) {
                            updateOpportunity.STMS_Package_Sale__c = false;
                            updateOpportunity.EnergyHost__c =  false;
                            updateOpportunityMap.put(oppId, updateOpportunity);

                        }

                    }
                }

            }


            if (updateOpportunityMap.size() > 0) {
                update updateOpportunityMap.values();
            }

        }

        // String opportunityId = null;
        // // STMS打包销售
        // boolean flg_category3 = false;
        // boolean flg_mdm = false;
        // boolean stms_package_sale = false;
        // boolean oldStmsPackageSale = false;
        // //boolean state_Owner = false;
        // List<Opportunity> oppUpd = new List<Opportunity>();
        // //項目判断
        // for (OpportunityLineItem oli : retList) {
        //     if (opportunityId == null) {
        //         opportunityId = oli.OpportunityId;
        //         oldStmsPackageSale = oli.Opportunity.STMS_Package_Sale__c;
        //     }

        //     if (opportunityId != oli.OpportunityId) {
        //         //stms_package_sale = ((flg_category3 == true && flg_mdm == true) || state_Owner == true) ? true : false;
        //         stms_package_sale = (flg_category3 == true && flg_mdm == true) ? true : false;
        //         if (stms_package_sale != oldStmsPackageSale) {
        //             Opportunity opportunity = new Opportunity(
        //                 Id = opportunityId,
        //                 STMS_Package_Sale__c = stms_package_sale
        //             );
        //             oppUpd.add(opportunity);
        //         }
        //         opportunityId = oli.OpportunityId;
        //         oldStmsPackageSale = oli.Opportunity.STMS_Package_Sale__c;
        //         flg_category3 = false;
        //         flg_mdm = false;
        //         stms_package_sale = false;
        //     }

        //     if (oli.Category3__c != null && oli.Category3__c.contains('主机')) { //zh 2020/09/15
        //         flg_category3 = true;
        //     }
        //     if (oli.Product2.MDM_Model_No__c != null && oli.Product2.MDM_Model_No__c.contains('USG-400')) {
        //         flg_mdm = true;
        //     }
        //     //if (oli.Opportunity.State_Owner__c == '能量') {
        //     //  state_Owner = true;
        //     //}
        // }

        // //stms_package_sale = ((flg_category3 == true && flg_mdm == true) || state_Owner == true) ? true : false;
        // stms_package_sale = (flg_category3 == true && flg_mdm == true) ? true : false;
        // if (opportunityId != null && stms_package_sale != oldStmsPackageSale) {
        //     Opportunity opportunity = new Opportunity(
        //         Id = opportunityId,
        //         STMS_Package_Sale__c = stms_package_sale
        //     );
        //     oppUpd.add(opportunity);
        // }

        // if (oppUpd.size() > 0) {
        //     update oppUpd;
        // }

        //List<Opportunity> oppProductEPT1 = new List<Opportunity>();
        //CHAN-BCNCRB ---XHL----Start-----
        //Map<String,String> appMap = new Map<String,String>();
        //for (OpportunityLineItem oli : retList) {
        //  appMap.put(oli.OpportunityId,oli.OpportunityId);
        //}
        //Map<String,Opportunity> oppProductEPT1Map = new Map<String,Opportunity>();
        //for (OpportunityLineItem oli : retList) {

        //  Opportunity opportunity = new Opportunity();
        //  opportunity.Id = oli.OpportunityId;
        //  if (oli.Product2.MDM_Model_No__c != null && (oli.Product2.MDM_Model_No__c.contains('EP-T1(A)') ||
        //          oli.Product2.MDM_Model_No__c.contains('EP-T1(B)'))) {
        //      opportunity.If_Have_EP_T1__c = true;
        //      oppProductEPT1Map.put(oli.OpportunityId,opportunity);
        //  } else {
        //      opportunity.If_Have_EP_T1__c = false;
        //      if (!oppProductEPT1Map.containsKey(oli.OpportunityId)) {
        //          oppProductEPT1Map.put(oli.OpportunityId,opportunity);
        //      }
        //  }
        //  //oppProductEPT1Map.put(oli.OpportunityId,opportunity);
        //}
        //if (oppProductEPT1Map.size() > 0) {
        //  update oppProductEPT1Map.values();
        //}
        //if (oppProductEPT1.size() > 0) {
        //  update oppProductEPT1;
        //}
        //CHAN-BCNCRB ---XHL----End-----

    }

}