GWY
2022-05-21 a3460549533111815e7f73d6cef601a58031525d
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 LoanerAppDetailTriggerHandler {
    public static Boolean runTrigger = true;
    //废弃
    public static string upsertDiscarded(List<loaner_application_detail__c> newList, Map<Id, loaner_application_detail__c> newMap, List<loaner_application_detail__c> oldList, Map<Id, loaner_application_detail__c> oldMap){
        List<String> assetIdList = new List<String>();
        List<string> ladcIdList = new List<string>();
        loaner_application_detail__c old = null;
        for(loaner_application_detail__c ladc : newList){
            old = oldMap.get(ladc.Id);
            if(old.RAESD_Status__c != ladc.RAESD_Status__c && ladc.RAESD_Status__c == '废弃'){
                assetIdList.add(ladc.LOANER__c);
                ladcIdList.add(ladc.id);            
            }
        }
 
        if(ladcIdList.size() == 0){
            return '';
        }
 
        List<loaner_application_detail__c> ladcList = [select id,Abandoned_Date__c from loaner_application_detail__c where id=:ladcIdList];
        List<Asset> assetList = [select id,RecordType.Id from Asset where id=:assetIdList];
 
        for(Asset asset : assetList){
            if(asset.RecordType.Id == System.label.Asset_Record_Type1){
                asset.Status = '废弃';
                asset.Abandoned_date__c = Date.today();
            }
        }
 
        for(loaner_application_detail__c la : ladcList){
            la.Abandoned_Date__c = Date.today();
        }
        update(ladcList); 
        update(assetList);
        return '';
    }
    //欠品确认
    public static string upsertCheckLost(List<loaner_application_detail__c> newList, Map<Id, loaner_application_detail__c> newMap, List<loaner_application_detail__c> oldList, Map<Id, loaner_application_detail__c> oldMap){
        
        List<Asset> assetList = new List<Asset>();
        List<String> assetIdList = new List<string>();
        List<string> ladcIdList = new List<string>();
        Set<string> lacIdSet = new Set<string>();
        loaner_application_detail__c old = null;
        //获取明细状态由'欠品中'修改为新状态的明细id
        for(loaner_application_detail__c ladc : newList){
            old = oldMap.get(ladc.Id);
            if(old.RAESD_Status__c != ladc.RAESD_Status__c && old.RAESD_Status__c == '欠品中'){
                ladcIdList.add(ladc.id);
            }
        }
        //如果没有符合条件的明细,跳出
        if(ladcIdList.size() == 0){
            return '';
        }
 
        List<loaner_application_detail__c> ladcList = [select id,LOANER__c,RAESD_Status__c,loaner_application__c from loaner_application_detail__c where id=:ladcIdList];
        
 
        for(loaner_application_detail__c la : ladcList){
            if(la.RAESD_Status__c == '丢失'){
                la.Lost_Date__c = Date.today();
                la.inTime__c = Datetime.now();
                assetIdList.add(la.LOANER__c);
 
            }else if(la.RAESD_Status__c == '已回收'){
                la.Received_loaner_Date__c = Date.today();
                la.inTime__c = Datetime.now();
                la.Check_lost_Item__c = 'ok';
            }
            la.Lost_item_check_Date__c = Date.today();
            lacIdSet.add(la.loaner_application__c);
        }
 
        List<Asset> allAssetList = [select id,RecordType.Id from Asset where id=:assetIdList];
        for(Asset asset : allAssetList){
            if(asset.RecordType.Id == System.label.Asset_Record_Type1){
                asset.Status = '丢失';
                asset.Lost_date__c = Date.today();
                assetList.add(asset);
            }
        }
 
        List<loaner_application__c> lacList = [select Id,Status__c,Equipment_Type__c from loaner_application__c where Id = :lacIdSet];
        for(loaner_application__c lac: lacList){
            if(lac.Status__c != '完毕'){
                lac.Status__c =  LoanerOrderState.getOrderStater(lac.Id);
                if(lac.Status__c =='全部回收'){
                   lac.LoanerClosedDateAll__c =Date.today();
                   if(lac.Equipment_Type__c == 'NDT' || lac.Equipment_Type__c == 'ANI'){
                        PrototypeToThaw.UserUnfreeze(lac.Id);
                   }
                }
            }
        }
        update(assetList);
        update(ladcList);
        update(lacList);
        return '';
    }
 
    //删除后状态更新
    public static void delStatusUp(List<loaner_application_detail__c> newList, Map<Id, loaner_application_detail__c> newMap, List<loaner_application_detail__c> oldList, Map<Id, loaner_application_detail__c> oldMap){
        Set<String> loanerAppSet = new Set<String>();
        Set<String> loanerSet = new Set<String>();
        for (loaner_application_detail__c local : oldList) {
            loanerSet.add(local.LOANER__c);
            loanerAppSet.add(local.loaner_application__c);
        }
        Map<String,Integer> resultsMap = new Map<String,Integer>();
        //王鹏伟 新加where条件  AND RAESD_Status_F__c != '取消'
        AggregateResult[] results = [select LOANER__c,Count(Id) LOANERNo
                                 from  loaner_application_detail__c
                                 where loaner_application__c not in :loanerAppSet
                                 and LOANER__c in :loanerSet
                                and (loaner_application__r.Approval_Date__c != null AND Received_loaner_Date__c = null AND RAESD_Status_F__c != '取消')
                                AND LOANER__r.RecordTypeId  = :System.label.Asset_Record_Type1
                                 GROUP by LOANER__c];
                                 System.debug('results +++++' + results);
        for(AggregateResult ar: results){
            resultsMap.put(String.valueOf(ar.get('LOANER__c')), Integer.valueOf(ar.get('LOANERNo')));
        }
        List<Asset> pss = [select Id,Status from Asset where RecordTypeId = :System.label.Asset_Record_Type1
                                  AND Id in : loanerSet];
        List<Asset> pssexit = new List<Asset>();
        for(Asset lId : pss){
            if(!resultsMap.containsKey(lId.Id)){
                Asset p2s = new Asset();
                p2s.Id =  lId.Id;
                p2s.Status = '在库';
                pssexit.add(p2s);
            }
        }
        System.debug('pss +++++' + pss);
        if(pssexit.size() > 0){
            upsert pssexit;
        }
    }
 
    //插入后状态更新
    public static void insStatusUp(List<loaner_application_detail__c> newList, Map<Id, loaner_application_detail__c> newMap, List<loaner_application_detail__c> oldList, Map<Id, loaner_application_detail__c> oldMap){
        Set<String> loanerSet = new Set<String>();
        for (loaner_application_detail__c local : newList) {
            if(local.RAESD_Status__c == '已出库指示' || local.RAESD_Status__c == '已批准' || local.RAESD_Status__c == '申请中'){
                loanerSet.add(local.LOANER__c);
            }
        }
        List<Asset> tmpList = new List<Asset>();
        for(Asset ass : [select Id,Status from Asset where Id in : loanerSet AND Status = '在库' AND RecordTypeId  = :System.label.Asset_Record_Type1]){
            ass.Status = '冻结';
            tmpList.add(ass);
        }
        List<loaner_application__c> laList = [select id,Status__c,Loaner_LendOrder__c from loaner_application__c where id = :loanerSet];
        List<loaner_application_detail__c> ladList = new List<loaner_application_detail__c>();
        for(loaner_application_detail__c local : newList){
            for(loaner_application__c temp : laList){
                if(temp.id == local.LOANER__c){
                    if(temp.Status__c != '草案中' && temp.Status__c != '已提交' && temp.Status__c != '申请中' && temp.Loaner_LendOrder__c == true){
                        loaner_application_detail__c lad = new loaner_application_detail__c();
                        lad.RAESD_Status__c = '已批准';
                        ladList.add(lad);
                    }
                }
             }
        }
        if(ladList.size() > 0) update ladList;
        if(tmpList.size() > 0) update tmpList;
    }
 
    public static void setUser(List<loaner_application_detail__c> newList, Map<Id, loaner_application_detail__c> newMap, List<loaner_application_detail__c> oldList, Map<Id, loaner_application_detail__c> oldMap){
        List<String> ladIdList = new List<String>();
        for(loaner_application_detail__c lad : newList){
            if(lad.loaner_Manager__c == null){
                ladIdList.add(lad.Id);
            }
        }
 
        if(ladIdList.size()>0){
            List<loaner_application_detail__c> ladList = [select id,loaner_application__r.loaner_Manager__c from loaner_application_detail__c where Id= : ladIdList];
            List<loaner_application_detail__c> updateList = new List<loaner_application_detail__c>();
            for(loaner_application_detail__c lad : ladList){
                lad.loaner_Manager__c = lad.loaner_application__r.loaner_Manager__c;
                updateList.add(lad);
            }
 
            update updateList;
        } 
    }
 
    public static void notReceivingNum(List<loaner_application_detail__c> newList, Map<Id, loaner_application_detail__c> newMap, List<loaner_application_detail__c> oldList, Map<Id, loaner_application_detail__c> oldMap){
        Map<String,Integer> leIdMap = new Map<String,Integer>();
        for(loaner_application_detail__c lad : newList){
            loaner_application_detail__c old = oldMap.get(lad.id);
            if(lad.RAESD_Status__c != old.RAESD_Status__c && lad.RAESD_Status__c == '申请者已收货' && lad.Loaner_Express__c != null){
                if(leIdMap.containsKey(lad.Loaner_Express__c)){
                    leIdMap.put(lad.Loaner_Express__c, (leIdMap.get(lad.Loaner_Express__c)+1));
                }else{
                    leIdMap.put(lad.Loaner_Express__c,1);
                }
            }
            if(lad.RAESD_Status__c != old.RAESD_Status__c && lad.RAESD_Status__c == '已回收' && lad.Return_Express__c != null){
                if(leIdMap.containsKey(lad.Return_Express__c)){
                    leIdMap.put(lad.Return_Express__c, (leIdMap.get(lad.Return_Express__c)+1));
                }else{
                    leIdMap.put(lad.Return_Express__c,1);
                }
            }
        }
        System.debug(leIdMap);
        if(leIdMap.size() == 0){
            return;
        }
        List<Loaner_Express__c> updateList = new List<Loaner_Express__c>();
        List<Loaner_Express__c> leList = [select id,NotReceivingNum__c from Loaner_Express__c where id= :leIdMap.keySet()];
        for(Loaner_Express__c le : leList){
            if(le.NotReceivingNum__c != null){
                le.NotReceivingNum__c = le.NotReceivingNum__c - leIdMap.get(le.id);
            }
            System.debug(le.NotReceivingNum__c);
            updateList.add(le);
        }
 
        update updateList;
    }
 
    //明细中计算自动收货日期和收货提醒日期
    public static void emailDate(List<loaner_application_detail__c> newList, Map<Id, loaner_application_detail__c> newMap, List<loaner_application_detail__c> oldList, Map<Id, loaner_application_detail__c> oldMap){
        List<String> autoId = new List<String>();
        List<String> remindId = new List<String>();
 
        for(loaner_application_detail__c lad : newList){
            loaner_application_detail__c old = oldMap.get(lad.id);
            if(lad.Automatic_Received_Date__c == null && lad.Shipment_Request_Date__c != null){
                autoId.add(lad.id);
            }
            if(lad.Automatic_Received_Date__c != old.Automatic_Received_Date__c){
                remindId.add(lad.id);
            }
        }
        Map<String,OlympusCalendar__c> oCalendar = calendarUtil.getCalendarMap(Date.today().addDays(-30),Date.today().addDays(240));
        System.debug(oCalendar);
        if(oCalendar == null){
            return;
        }
        List<loaner_application_detail__c> autoList = new List<loaner_application_detail__c>();
        
        List<loaner_application_detail__c> remindList = new List<loaner_application_detail__c>();
        
        if(autoId.size()>0){
            autoList = [select id,Shipment_Request_Date__c,Equipment_Type__c from loaner_application_detail__c where id= :autoId];
            for(loaner_application_detail__c auto : autoList){
                if(auto.Equipment_Type__c == 'NDT' || auto.Equipment_Type__c == 'ANI'){
                    auto.Automatic_Received_Date__c = calendarUtil.getCorrespondingDate(oCalendar,auto.Shipment_Request_Date__c,3);
                    auto.Remind_Received_Date__c = calendarUtil.getCorrespondingDate(oCalendar,auto.Automatic_Received_Date__c,-2);
                }else{
                    auto.Automatic_Received_Date__c = calendarUtil.getCorrespondingDate(oCalendar,auto.Shipment_Request_Date__c,5);
                    auto.Remind_Received_Date__c = calendarUtil.getCorrespondingDate(oCalendar,auto.Automatic_Received_Date__c,-3);
                }
            }
        }
        if(remindId.size()>0){
            remindList = [select id,Automatic_Received_Date__c from loaner_application_detail__c where id= :remindId];
            for(loaner_application_detail__c remind : remindList){
                remind.Remind_Received_Date__c = calendarUtil.getCorrespondingDate(oCalendar,remind.Automatic_Received_Date__c,-3);
            }
        }
        if(autoList.size()>0) update autoList;
        if(remindList.size()>0) update remindList;
 
    }
}