张宇恒
2022-03-21 26d42e9bb57aec5d41276f01cfbe9d8a46d64b95
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/**
 * 同期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 副担当以及没有了,去掉这个逻辑
}