/** 
 | 
 * 同期MB_Opportunity__c、同期MB_Target__c、Opportunity_sub_owner__c => OpportunityTeamMember、Opportunity_stms_owner__c => OpportunityTeamMember 
 | 
 */ 
 | 
trigger SyncMBOpportunity on Opportunity (after insert, after update, before delete) { 
 | 
    if (StaticParameter.EscapeSyncOpportunityTrigger) { 
 | 
System.debug('Escape、EscapeSyncOpportunityTrigger:::::' + StaticParameter.EscapeSyncOpportunityTrigger); 
 | 
        return; 
 | 
    } 
 | 
/* 
 | 
    // MB_目标插入用 
 | 
    List<MB_Target__c> ins_tgts = new List<MB_Target__c>(); 
 | 
    // MB_目标删除用 
 | 
    List<Opportunity> del_parentTgts = new List<Opportunity>(); 
 | 
    // MB_询价插入用 
 | 
    List<MB_Opportunity__c> ins_opps = new List<MB_Opportunity__c>(); 
 | 
    // MB_询价删除用 
 | 
    List<Opportunity> del_parentOpps = new List<Opportunity>();*/ 
 | 
    // OppId ==> OpportunityTeamMember(Opportunity_sub_owner__c) 
 | 
    Map<Id, OpportunityTeamMember> otmMap = new Map<Id, OpportunityTeamMember>(); 
 | 
    // OppId ==> OpportunityTeamMember(Opportunity_stms_owner__c) 
 | 
    Map<Id, OpportunityTeamMember> otmMapSTMS = new Map<Id, OpportunityTeamMember>(); 
 | 
  
 | 
    if (Trigger.isInsert) { 
 | 
        System.debug('++++++++++2333333333'); 
 | 
        // CHAN-B4J9TC 2018-09-12 放开原来的注释的内容 
 | 
            List<Opportunity> opList = Trigger.new.clone(); 
 | 
            if(opList.size()==1){ 
 | 
                for(Opportunity op : opList ){ 
 | 
                    if(op.Old_Opportunity_ID__c !=null){ 
 | 
                        String ResultStr = ControllerUtil.CopyQuoteFromOppor(op.Old_Opportunity_ID__c,op.Opportunity_No__c,op.Id,op); 
 | 
                        System.debug('2333333333'+ResultStr); 
 | 
                    } 
 | 
                } 
 | 
            } 
 | 
        // 
 | 
        System.debug('==========2333333333'); 
 | 
        for (Opportunity opp : Trigger.new) { 
 | 
/*            if (opp.RecordTypeId == '01210000000QekP') { 
 | 
                // 目标 
 | 
                MB_Target__c mbopp = new MB_Target__c( 
 | 
                      Opportunity__c = opp.Id 
 | 
                    , OpportunityId__c = opp.Id 
 | 
                    , CurrencyIsoCode = opp.CurrencyIsoCode 
 | 
                    , RecordType_Name__c = opp.RecordType_Name__c 
 | 
// CIC 125725 start 
 | 
//                    , OwnerId = opp.OwnerId 
 | 
                    , State_Text__c = opp.State__c      // Owner.Province_address__c 
 | 
                    , Opp_OCM_text__c = opp.OCM__c 
 | 
                    , Opportunity_Category_text__c = opp.Opportunity_Category__c 
 | 
                    , Distributor_InCharge_opp__c = opp.Distributor_InCharge_opp__c 
 | 
// CIC 125725 end 
 | 
                ); 
 | 
                ins_tgts.add(mbopp); 
 | 
            }*/ 
 | 
            // 引合 と 目標 両方 
 | 
            { 
 | 
/*                MB_Opportunity__c mbopp = new MB_Opportunity__c( 
 | 
                      Opportunity__c = opp.Id 
 | 
                    , OpportunityId__c = opp.Id 
 | 
                    , CurrencyIsoCode = opp.CurrencyIsoCode 
 | 
                    , RecordType_Name__c = opp.RecordType_Name__c 
 | 
// CIC 125725 start 
 | 
//                    , OwnerId = opp.OwnerId 
 | 
                    , State_Text__c = opp.SaleProvince_SAP__c 
 | 
                    , Opp_OCM_text__c = opp.OCM__c 
 | 
                    , Opportunity_Category_text__c = opp.Opportunity_Category__c 
 | 
                    , Distributor_InCharge_opp__c = opp.Distributor_InCharge_opp__c 
 | 
                    , Group_purchase_PCL__c = opp.Group_purchase_PCL__c 
 | 
// CIC 125725 end 
 | 
                ); 
 | 
                if (opp.RecordTypeId == '01210000000QekP') { 
 | 
                    mbopp.State_Text__c = opp.State__c; 
 | 
                } 
 | 
                ins_opps.add(mbopp);*/ 
 | 
                if (opp.Opportunity_sub_owner__c != null) { 
 | 
                    if (OpportunityMemberTrigger.syncMBOpportunityOtmMap.get(opp.Id) == null) { 
 | 
                        otmMap.put(opp.Id, 
 | 
                                new OpportunityTeamMember( 
 | 
                                        UserId = opp.Opportunity_sub_owner__c, 
 | 
                                        TeamMemberRole = '副担当', 
 | 
                                        OpportunityId = opp.Id 
 | 
                                ) 
 | 
                        ); 
 | 
                    } 
 | 
                } 
 | 
                // CHAN-BB38N4 start stms 副担当以及没有了,去掉这个逻辑 
 | 
  
 | 
                //if (opp.Opportunity_stms_owner__c != null) { 
 | 
                //    if (OpportunityMemberTrigger.syncMBOpportunityOtmMapSTMS.get(opp.Id) == null) { 
 | 
                //        otmMapSTMS.put(opp.Id, 
 | 
                //                new OpportunityTeamMember( 
 | 
                //                        UserId = opp.Opportunity_stms_owner__c, 
 | 
                //                        TeamMemberRole = 'STMS担当', 
 | 
                //                        OpportunityId = opp.Id 
 | 
                //                ) 
 | 
                //        ); 
 | 
                //    } 
 | 
                //} 
 | 
                // CHAN-BB38N4 end stms 副担当以及没有了,去掉这个逻辑 
 | 
            } 
 | 
        } 
 | 
    } 
 | 
// CIC 130175 start 
 | 
    else if (Trigger.isUpdate) { 
 | 
            //如果询价编码变动,则报价编码一起变动 
 | 
        //if(Trigger.isAfter){ 
 | 
        //    List<id> updateQuoteOppoid = new List<id>(); 
 | 
        //    List<String> updateSodOppoid = new List<String>(); 
 | 
        //    for(Opportunity ops : Trigger.new){ 
 | 
        //        Opportunity oldOne = Trigger.oldMap.get(ops.id); 
 | 
        //        Opportunity newOne = Trigger.newMap.get(ops.id); 
 | 
        //        if(oldOne.Opportunity_No__c != newOne.Opportunity_No__c){ 
 | 
        //            updateQuoteOppoid.add(ops.id); 
 | 
        //        } 
 | 
        //    } 
 | 
        //    if(updateQuoteOppoid.size()>0){ 
 | 
        //        ControllerUtil.ResetQuoteNo(updateQuoteOppoid); 
 | 
        //    } 
 | 
        //    //如果特定字段发生变化,触发以下事件(仅限单一事件) 
 | 
        //    for(Opportunity ops : Trigger.new){ 
 | 
        //        Opportunity oldOne = Trigger.oldMap.get(ops.id); 
 | 
        //        Opportunity newOne = Trigger.newMap.get(ops.id); 
 | 
        //        if( (oldOne.S_Install_Date__c != newOne.S_Install_Date__c && oldOne.S_Install_Date__c == null)|| 
 | 
        //                (oldOne.SI_FinishedPorj_Date__c != newOne.SI_FinishedPorj_Date__c && oldOne.SI_FinishedPorj_Date__c == null 
 | 
        //            )){ 
 | 
        //            updateSodOppoid.add(ops.id); 
 | 
        //        } 
 | 
        //    } 
 | 
        //    if(updateSodOppoid.size()>0){ 
 | 
        //        ISO_DemandOperAndDemonsController.synchronousSodWithOppor(updateSodOppoid); 
 | 
        //    } 
 | 
        //} 
 | 
//        Map<Id, Opportunity> oppMap = new Map<Id, Opportunity>(); 
 | 
        for (Opportunity opp : Trigger.new) { 
 | 
            System.debug('==+++++===otmMap.put(' + opp.OwnerId + ', ' + Trigger.oldMap.get(opp.Id).OwnerId); 
 | 
            if (opp.RecordTypeId == '01210000000QekP') { 
 | 
                // 目标 の場合 delete insert しているはず、MB子供の処理がないです。 
 | 
            } else { 
 | 
                // 询价 引合 のみ対象 
 | 
/*                if (opp.SAP_Province__c !=  Trigger.oldMap.get(opp.Id).SAP_Province__c 
 | 
                        || opp.Opportunity_Category__c !=  Trigger.oldMap.get(opp.Id).Opportunity_Category__c 
 | 
                        || opp.Distributor_InCharge_opp__c !=  Trigger.oldMap.get(opp.Id).Distributor_InCharge_opp__c 
 | 
                        || opp.Group_purchase_PCL__c !=  Trigger.oldMap.get(opp.Id).Group_purchase_PCL__c 
 | 
                ) { 
 | 
                    oppMap.put(opp.Id, opp); 
 | 
                }*/ 
 | 
                if (opp.Opportunity_sub_owner__c !=  Trigger.oldMap.get(opp.Id).Opportunity_sub_owner__c 
 | 
                        || opp.OwnerId !=  Trigger.oldMap.get(opp.Id).OwnerId 
 | 
                ) { 
 | 
                System.debug('=====otmMap.put(' + opp.OwnerId + ', ' + Trigger.oldMap.get(opp.Id).OwnerId); 
 | 
                    if (OpportunityMemberTrigger.syncMBOpportunityOtmMap.get(opp.Id) == null) { 
 | 
System.debug('otmMap.put(' + opp.Id + ', ' + opp.Opportunity_sub_owner__c); 
 | 
                        otmMap.put(opp.Id, 
 | 
                                new OpportunityTeamMember( 
 | 
                                        UserId = opp.Opportunity_sub_owner__c,                          // 要注意 otmMap の UserId が nullの場合もある、後続の処理でチームを削除します。 
 | 
                                        TeamMemberRole = '副担当', 
 | 
                                        OpportunityId = opp.Id 
 | 
                                ) 
 | 
                        ); 
 | 
                    } 
 | 
                } 
 | 
                // CHAN-BB38N4 start stms 副担当以及没有了,去掉这个逻辑 
 | 
                //if (opp.Opportunity_stms_owner__c !=  Trigger.oldMap.get(opp.Id).Opportunity_stms_owner__c 
 | 
                //        || opp.OwnerId !=  Trigger.oldMap.get(opp.Id).OwnerId 
 | 
                //) { 
 | 
                //    if (OpportunityMemberTrigger.syncMBOpportunityOtmMapSTMS.get(opp.Id) == null) { 
 | 
                //        System.debug('otmMap.put(' + opp.Id + ', ' + opp.Opportunity_stms_owner__c); 
 | 
                //        otmMapSTMS.put(opp.Id, 
 | 
                //                new OpportunityTeamMember( 
 | 
                //                        UserId = opp.Opportunity_stms_owner__c,                          // 要注意 otmMapSTMS の UserId が nullの場合もある、後続の処理でチームを削除します。 
 | 
                //                        TeamMemberRole = 'STMS担当', 
 | 
                //                        OpportunityId = opp.Id 
 | 
                //                ) 
 | 
                //        ); 
 | 
                //    } 
 | 
                //} 
 | 
                // // CHAN-BB38N4 start stms 副担当以及没有了,去掉这个逻辑 
 | 
            } 
 | 
        } 
 | 
        // MBこどもを更新 
 | 
//        ControllerUtil.updMBChildFromOpp(oppMap); 
 | 
    } 
 | 
// CIC 130175 end 
 | 
/*    else if (Trigger.isDelete) { 
 | 
        for (Opportunity opp : Trigger.old) { 
 | 
            if (opp.RecordTypeId == '01210000000QekP') { 
 | 
                // 目标 
 | 
                del_parentTgts.add(opp); 
 | 
            } 
 | 
            // 引合 と 目標 両方 
 | 
            { 
 | 
                // 询价 引合 
 | 
                del_parentOpps.add(opp); 
 | 
            } 
 | 
        } 
 | 
    } 
 | 
  
 | 
    if (del_parentTgts.size() > 0) { 
 | 
        ControllerUtil.delMBTgtList(del_parentTgts); 
 | 
    } 
 | 
    if (ins_tgts.size() > 0) { 
 | 
        ControllerUtil.insMBTgtList(ins_tgts); 
 | 
    } 
 | 
    if (del_parentOpps.size() > 0) { 
 | 
        ControllerUtil.delMBOppList(del_parentOpps); 
 | 
    } 
 | 
    if (ins_opps.size() > 0) { 
 | 
        ControllerUtil.insMBOppList(ins_opps); 
 | 
    }*/ 
 | 
    if (otmMap.size() > 0) { 
 | 
        OpportunityMemberTrigger.delInsOpportunityTeamMember(otmMap); 
 | 
    } 
 | 
    // CHAN-BB38N4 start stms 副担当以及没有了,去掉这个逻辑 
 | 
    //if (otmMapSTMS.size() > 0) { 
 | 
    //    OpportunityMemberTrigger.delInsOpportunityTeamMemberSTMS(otmMapSTMS); 
 | 
    //} 
 | 
    //// CHAN-BB38N4 end stms 副担当以及没有了,去掉这个逻辑 
 | 
} 
 |