高章伟
2022-02-18 8b5f4c6c281cfa548f92de52c8021e37aa81901e
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
public without sharing class ReceivingNoteSummaryHandler extends Oly_TriggerHandler {
 
    private Map<Id, ReceivingNoteSummary__c> newMap;
    private Map<Id, ReceivingNoteSummary__c> oldMap;
    private List<ReceivingNoteSummary__c> newList;
    private List<ReceivingNoteSummary__c> oldList;
 
    public static String userId = UserInfo.getUserId();
    public static String nowStr = Datetime.now().format('yyyyMMddHHmmssms');
 
    public ReceivingNoteSummaryHandler() {
        this.newMap = (Map<Id, ReceivingNoteSummary__c>) Trigger.newMap;
        this.oldMap = (Map<Id, ReceivingNoteSummary__c>) Trigger.oldMap;
        this.newList = (List<ReceivingNoteSummary__c>) Trigger.new;
        this.oldList = (List<ReceivingNoteSummary__c>) Trigger.old;
    }
 
    protected override void beforeInsert() {
        beforeSetValue();
        setTextToHash();
    }
    protected override void beforeUpdate() {
        beforeSetValue();
        setTextToHash();
    }
 
    protected override void afterUpdate() {
        List<Id> rnsIdList = new List<Id>();
        for (ReceivingNoteSummary__c nObj : newList) {
            ReceivingNoteSummary__c oObj;
            if (Trigger.isUpdate) {
                oObj = oldMap.get(nObj.Id);
            }
            if(oObj.Fixture_Set__c != nObj.Fixture_Set__c) {
                rnsIdList.add(nObj.Id);
            }
        }
        if(rnsIdList.size()>0){
            cleanFixtureDetails(rnsIdList);
        }
        //add by rentx 20210726 start 154P课题_SAP传输至SFDC后推送
        setNoteModelNoEmail();
    }
 
 
    private void setNoteModelNoEmail() {
        Map<Id,Map<String,Integer>> idMessageMap = new Map<Id,Map<String,Integer>>();
        for (ReceivingNoteSummary__c notrdetail : newList) {
            //给 第一条收货清单配套一览明细 的时候 给发邮件的那个赋值
            if (Trigger.isUpdate && notrdetail.First_RND__c != null && oldMap.get(notrdetail.Id).First_RND__c == null) {
 
                //IDmessageMap 的key 用于过滤 备品收货清单
                //value 是 一览上的备品配套明细型号 * 个数 的map
                if (!idMessageMap.containsKey(notrdetail.ReceivingNote__c)) {
                    idMessageMap.put(notrdetail.ReceivingNote__c, new Map<String,Integer>());
                }
                if (notrdetail.Fixture_Model_No_F__c != null && notrdetail.Fixture_Model_No_F__c != '') {
                    Map<String,Integer> tempMap = idMessageMap.get(notrdetail.ReceivingNote__c);
                    if(!tempMap.containsKey(notrdetail.Fixture_Model_No_F__c)){
                        tempMap.put(notrdetail.Fixture_Model_No_F__c,0);
                    }
                    Integer num = tempMap.get(notrdetail.Fixture_Model_No_F__c);
                    num = num + 1;
                    tempMap.put(notrdetail.Fixture_Model_No_F__c,num);
                    idMessageMap.put(notrdetail.ReceivingNote__c, tempMap);
                }
            }
        }
 
        //设置收货清单的值 然后更新
        if (idMessageMap.size() > 0) {
            List<ReceivingNote__c> updateList = new List<ReceivingNote__c>();
            List<ReceivingNote__c> updateTempList = new List<ReceivingNote__c>();
            for (Id suId : idMessageMap.keySet()) {
                ReceivingNote__c receNote = new ReceivingNote__c();
                ReceivingNote__c receNoteTemp = new ReceivingNote__c();
                receNote.Id = suId;
                receNoteTemp.Id = suId;
                Map<String,Integer> tempMap = idMessageMap.get(suId);
                String message1 = '';
                for(String key : tempMap.keySet()){
                    message1 += key+'&nbsp;*&nbsp;'+tempMap.get(key);
                    message1 += '<BR>';
                }
                receNoteTemp.Fixture_Model_No_Email__c = null;
                receNote.Fixture_Model_No_Email__c = message1;
 
 
                receNoteTemp.Reception_mail_address__c = null;
 
                updateList.add(receNote);
                updateTempList.add(receNoteTemp);
 
            }
 
            if (updateList.size() > 0) {
                update updateList;
                update updateTempList;
            }
        }
        
 
 
 
 
 
        //这个是获取当前收货清单下所有的一览 的个数 再赋值 start ------
        /*for (ReceivingNoteSummary__c nObj : newList) {
            //给 第一条收货清单配套一览明细 的时候 给发邮件的那个赋值
            if (Trigger.isUpdate && nObj.First_RND__c != null && oldMap.get(nObj.Id).First_RND__c == null) {
                idMessageMap.put(nObj.ReceivingNote__c, new Map<String,Integer>());
            }
        }
        if (idMessageMap.size() > 0) {
            List<ReceivingNoteSummary__c> sumList = [select id,Fixture_Model_No_F__c,ReceivingNote__c from ReceivingNoteSummary__c where ReceivingNote__c in :idMessageMap.keySet()];
 
            if (sumList != null && sumList.size() > 0) {
                for (ReceivingNoteSummary__c notrdetail : sumList) {
 
                    if (!idMessageMap.containsKey(notrdetail.ReceivingNote__c)) {
                        idMessageMap.put(notrdetail.ReceivingNote__c, new Map<String,Integer>());
                    }
                    if (notrdetail.Fixture_Model_No_F__c != null && notrdetail.Fixture_Model_No_F__c != '') {
                        Map<String,Integer> tempMap = idMessageMap.get(notrdetail.ReceivingNote__c);
                        if(!tempMap.containsKey(notrdetail.Fixture_Model_No_F__c)){
                            tempMap.put(notrdetail.Fixture_Model_No_F__c,0);
                        }
                        Integer num = tempMap.get(notrdetail.Fixture_Model_No_F__c);
                        num = num + 1;
                        tempMap.put(notrdetail.Fixture_Model_No_F__c,num);
                        idMessageMap.put(notrdetail.ReceivingNote__c, tempMap);
                    }
    
                    
                }
            }
            List<ReceivingNote__c> updateList = new List<ReceivingNote__c>();
            for (Id suId : idMessageMap.keySet()) {
                ReceivingNote__c receNote = new ReceivingNote__c();
                receNote.Id = suId;
                Map<String,Integer> tempMap = idMessageMap.get(suId);
                String message1 = '';
                for(String key : tempMap.keySet()){
                    message1 += key+'&nbsp;*&nbsp;'+tempMap.get(key);
                    message1 += '<BR>';
                }
                receNote.Fixture_Model_No_Email__c = message1;
 
                updateList.add(receNote);
 
            }
 
            if (updateList.size() > 0) {
                update updateList;
            }
        }*/
        //这个是获取当前收货清单下所有的一览 的个数 再赋值 end ------
    }
 
    //add by rentx 20210726 end 154P课题_SAP传输至SFDC后推送
 
 
    private void cleanFixtureDetails(List<Id> rnsIdList) {
        List<ReceivingNoteDetail__c> rndList = [
                SELECT Id
                     , RNDAssert__c
                  FROM ReceivingNoteDetail__c
                 WHERE ReceivingNoteSummary__c IN :rnsIdList];
        List<ReceivingNoteDetail__c> updateRndList = new List<ReceivingNoteDetail__c>();
        List<ReceivingNoteDetail__c> deleteRndList = new List<ReceivingNoteDetail__c>();
        for(ReceivingNoteDetail__c rnd : rndList) {
            if(String.isNotBlank(rnd.RNDAssert__c)) {
                rnd.Fixture_Set_Detail__c = null;
                rnd.Fixture_Arrival_Process__c = null;
                rnd.Fixture_Arrival_Product__c = null;
                rnd.Product_Arrival_Product__c = null;
                rnd.Set_Arrival_Product__c = null;
                updateRndList.add(rnd);
            }
            else {
                deleteRndList.add(rnd);
            }
        }
        FixtureUtil.withoutUpdate(updateRndList);
        FixtureUtil.withoutDelete(deleteRndList);
    }
    // 以收货清单为单位发清点NG邮件,此处不需要
    //private void sendEmail() {
    //    Set<Id> needSendEmailSet = new Set<Id>();
    //    for (ReceivingNoteSummary__c nObj : newList) {
    //        ReceivingNoteSummary__c oObj;
    //        if (Trigger.isUpdate) {
    //            oObj = oldMap.get(nObj.Id);
    //        }
    //        if (Trigger.isUpdate) {
    //            if (oObj.Wei_Acceptance_Cnt__c != nObj.Wei_Acceptance_Cnt__c
    //                    && nObj.Wei_Acceptance_Cnt__c == 0) {
    //                needSendEmailSet.add(nObj.Id);
    //            }
    //        }
    //    }
    //    if (needSendEmailSet.size() > 0) {
    //        List<ReceivingNoteDetail__c> rndList = [SELECT Id
    //                    , ReceivingNoteSummary__c
    //                    , Name
    //                    , Change_Point__c
    //                    , AcceptanceResult__c
    //                 FROM ReceivingNoteDetail__c
    //                WHERE ReceivingNoteSummary__c = :needSendEmailSet
    //                  AND Delete_Flag__c = false
    //                  AND (AcceptanceResult__c = 'NG'
    //                          OR Change_Point__c = '增加'
    //                          OR Change_Point__c = '新建'
    //                      )
    //                ORDER BY ReceivingNoteSummary__c];
    //        if (rndList.size() > 0) {
    //            List<ReceivingNoteSummary__c> rnmList = new List<ReceivingNoteSummary__c>();
    //            List<ReceivingNoteSummary__c> rnmList1 = new List<ReceivingNoteSummary__c>();
    //            String oRnsId = '';
    //            String message = '';
    //            for (ReceivingNoteDetail__c rnd : rndList) {
    //                if (String.isBlank(oRnsId)) {
    //                    oRnsId = rnd.ReceivingNoteSummary__c;
    //                }
    //                if (oRnsId != rnd.ReceivingNoteSummary__c) {
    //                    ReceivingNoteSummary__c rns1 = new ReceivingNoteSummary__c(Id = oRnsId, Acceptance_NG_Email__c = null);
    //                    ReceivingNoteSummary__c rns2 = new ReceivingNoteSummary__c(Id = oRnsId, Acceptance_NG_Email__c = message);
 
    //                    rnmList.add(rns1);
    //                    rnmList1.add(rns2);
    //                    message = '';
    //                }
    //                oRnsId = rnd.ReceivingNoteSummary__c;
    //                message += rnd.Name + ':  ' + rnd.AcceptanceResult__c + '  ' + rnd.Change_Point__c + '\r\n';
    //            }
    //            if (String.isNotBlank(oRnsId)) {
    //                ReceivingNoteSummary__c rns1 = new ReceivingNoteSummary__c(Id = oRnsId, Acceptance_NG_Email__c = null);
    //                ReceivingNoteSummary__c rns2 = new ReceivingNoteSummary__c(Id = oRnsId, Acceptance_NG_Email__c = message);
    //                rnmList.add(rns1);
    //                rnmList1.add(rns2);
    //            }
    //            update rnmList;
    //            update rnmList1;
    //        }
    //    }
    //}
 
    private void setTextToHash() {
        for (ReceivingNoteSummary__c nObj : newList) {
            ReceivingNoteSummary__c oObj;
            if (Trigger.isUpdate) {
                oObj = oldMap.get(nObj.Id);
            }
            // if (Trigger.isInsert
            //         || oObj.No_Matching_Reminder_Email_Text__c !=  nObj.No_Matching_Reminder_Email_Text__c) {
            //     nObj.No_Matching_Reminder_Email_Hash__c = ReceivingNoteHandler.getHash('SHA-256', nObj.No_Matching_Reminder_Email_Text__c);
            // }
        }
    }
 
 
    public void beforeSetValue() {
        for (ReceivingNoteSummary__c nObj : newList) {
            nObj.Name = nObj.ReceivingNoteSummaryNo__c;
            nObj.Loaner_centre_mail_address__c = nObj.Loaner_centre_mail_address_F__c;
            if (String.isNotBlank(nObj.Fixture_Set__c)) {
                nObj.Select_Fixture_Set_Key__c = nObj.Fixture_Set__c + ':' + userId + ':' + nowStr;
            }
        }
    }
}