高章伟
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
public with sharing class RentalEquipmentCreateController{
    public String campaignId {get;set;}
    public String opdId {get;set;}
    public List<Rental_Equipment__c> prods ;
    // public Map<String,String> planProducts {get; set;}
    public List<Plan_Rental_Equipment__c> campaignEquipments;
    public List<Plan_Rental_Equipment__c> opdEquipments;
    public String eventRecordType;
    public String campaignRecordType;
    public String otherRecordType;
    public Boolean isEdit {get;set;}
    public OPDPlan__c targetOPD;
    public Map<String,String> planProductMap{get;set;}
    public Campaign targetCampaign; 
 
    public RentalEquipmentCreateController(){
        this.prods = [SELECT Name FROM Rental_Equipment__c ORDER BY Serial_Number__c ASC NULLS FIRST];
        this.campaignEquipments = new List<Plan_Rental_Equipment__c>();
        this.opdEquipments = new List<Plan_Rental_Equipment__c>();
        this.planProductMap = new Map<String,String>();
        List<RecordType> recordTypes = [SELECT Id,Name FROM RecordType WHERE SobjectType = 'Plan_Rental_Equipment__c'];
        for(RecordType re : recordTypes){
            if ('事件'.equals(re.Name)) {
                eventRecordType = re.Id;
            }
            if ('学会'.equals(re.Name)) {
                campaignRecordType = re.Id;
            }
            if ('其他'.equals(re.Name)) {
                otherRecordType = re.Id;
            }
        }
    }
 
    public List<Rental_Equipment__c> getProds() {
        // Map<String,String> planProducts = new Map<String,String>();
        System.debug('-----camp---'+campaignId);
        if (campaignId != null && campaignId != '') {
            UserRecordAccess useraccess = [SELECT recordId,HasEditAccess FROM UserRecordAccess WHERE userId = :UserInfo.getUserId() AND recordId = :campaignId];
            this.isEdit = useraccess.HasEditAccess;
            // this.isEdit = Campaign.sObjectType.getDescribe().isUpdateable();
            this.targetCampaign = [SELECT Id,LoadNum__c,Status,Is_LendProduct__c FROM Campaign WHERE Id = :campaignId]; 
            if ((!('草案中').equals(targetCampaign.Status)) && isEdit) {
                this.isEdit = false;
            }
            // else if (targetCampaign.Is_LendProduct__c == '否' && isEdit) {
            //     this.isEdit = false;
            // }
            System.debug('-----campisEdit---'+isEdit);
            this.campaignEquipments = [SELECT Rental_Equipment__c,Rental_Quantity__c FROM Plan_Rental_Equipment__c WHERE Campaign__c = :campaignId];
            this.planProductMap = campaignMap();
 
        }
        if (opdId != null && opdId != '') {
            UserRecordAccess useraccess = [SELECT recordId,HasEditAccess FROM UserRecordAccess WHERE userId = :UserInfo.getUserId() AND recordId = :opdId];
            this.isEdit = useraccess.HasEditAccess;
            this.targetOPD = [SELECT Id,PlanProdDetail__c,Campaign__c,OPDType__c,Status__c FROM OPDPlan__c WHERE Id = :opdId]; 
            // ***SWAG-BZCAHL***XHL***20210331***START*********
            // 因OPD计划再申请生成的OPD计划 状态是 草案中 无法创建 计划出借备品 
            // 同时 从事件与学会创建的OPD计划 不会是草案中,因此将其注掉,不影响其它逻辑
            // if ((('事件').equals(targetOPD.OPDType__c) || ('学会').equals(targetOPD.OPDType__c)) && isEdit) {
            //     if (!targetOPD.Reapply__c) {
            //         this.isEdit = false;
            //     }
                
            // }
            // ***SWAG-BZCAHL***XHL***20210331***END*********
            if ((!('草案中').equals(targetOPD.Status__c)) && isEdit) {
                this.isEdit = false;
            }
            this.opdEquipments = [SELECT Rental_Equipment__c,Rental_Quantity__c FROM Plan_Rental_Equipment__c WHERE OPD_Plan__c = :opdId];
            this.planProductMap = eventMap();
        }
        
        return this.prods;
    }
 
    public Map<String,String> campaignMap(){
        System.debug('-----campaignMap111---'+campaignId);
        // campaignEquipments = new List<Plan_Rental_Equipment__c>();
        
        Map<String,String> tempPlanProducts = new Map<String,String>();
        // if (campaignId != null && campaignId != '') {
        //     campaignEquipments = [SELECT Rental_Equipment__c,Rental_Quantity__c FROM Plan_Rental_Equipment__c WHERE Campaign__c = :campaignId];
        // }
        Map<String,String> tempMap = new Map<String,String>();
        if (campaignEquipments.size() > 0) {
            for(Plan_Rental_Equipment__c rentalEquipment : campaignEquipments){
                if (String.isBlank(rentalEquipment.Rental_Quantity__c)) {
                    tempMap.put(rentalEquipment.Rental_Equipment__c, '');
                }else{
                    tempMap.put(rentalEquipment.Rental_Equipment__c, rentalEquipment.Rental_Quantity__c);
                }
            }
        }
        if (prods.size() > 0 ) {
            if (campaignEquipments.size() > 0) {
                for(Rental_Equipment__c prod : prods){
                    if (tempMap.containsKey(prod.Name)) {
                        if (String.isBlank(tempMap.get(prod.Name)) || tempMap.get(prod.Name) == '0') {
                            tempPlanProducts.put(prod.Name, '');
                        }else{
                            tempPlanProducts.put(prod.Name, tempMap.get(prod.Name));
                        }
                        
                    }else{
                        tempPlanProducts.put(prod.Name, '');
                    }
                }
            }else{
                for(Rental_Equipment__c prod : prods){
                    tempPlanProducts.put(prod.Name, '');
                }
            }
            
        }
        return tempPlanProducts;
    }
 
    public Map<String,String> eventMap(){
        // opdEquipments = new List<Plan_Rental_Equipment__c>();
        Map<String,String> tempPlanProducts = new Map<String,String>();
        // if (opdId != null && opdId != '') {
        //     opdEquipments = [SELECT Rental_Equipment__c,Rental_Quantity__c FROM Plan_Rental_Equipment__c WHERE OPD_Plan__c = :opdId];
        // }
        Map<String,String> tempMap = new Map<String,String>();
        if (opdEquipments.size() > 0) {
            for(Plan_Rental_Equipment__c rentalEquipment : opdEquipments){
                if (String.isBlank(rentalEquipment.Rental_Quantity__c)) {
                    tempMap.put(rentalEquipment.Rental_Equipment__c, '');
                }else{
                    tempMap.put(rentalEquipment.Rental_Equipment__c, rentalEquipment.Rental_Quantity__c);
                }
            }
        }
        if (prods.size() > 0 ) {
            if (opdEquipments.size() > 0) {
                for(Rental_Equipment__c prod : prods){
                    if (tempMap.containsKey(prod.Name)) {
                        if (String.isBlank(tempMap.get(prod.Name)) || tempMap.get(prod.Name) == '0') {
                            tempPlanProducts.put(prod.Name, '');
                        }else{
                            tempPlanProducts.put(prod.Name, tempMap.get(prod.Name));
                        }
                    }else{
                        tempPlanProducts.put(prod.Name, '');
                    }
                }
            }else{
                for(Rental_Equipment__c prod : prods){
                    tempPlanProducts.put(prod.Name, '');
                }
            }
            
        }
        return tempPlanProducts;
    }
 
    
 
    public void save(){
        List<Plan_Rental_Equipment__c> ineuqipments = new List<Plan_Rental_Equipment__c>();
        List<Plan_Rental_Equipment__c> upeuqipments = new List<Plan_Rental_Equipment__c>();
        Map<String,Plan_Rental_Equipment__c> camTemp = new Map<String,Plan_Rental_Equipment__c>();
        Map<String,Plan_Rental_Equipment__c> eveTemp = new Map<String,Plan_Rental_Equipment__c>();    
        // OPDPlan__c targetOPD = new OPDPlan__c();
        Event targetEvent = new Event();
        for(Plan_Rental_Equipment__c plan : campaignEquipments){
            if (String.isBlank(plan.Rental_Quantity__c)) {
                plan.Rental_Quantity__c = '0';
            }
            camTemp.put(plan.Rental_Equipment__c, plan);
        }            
        for(Plan_Rental_Equipment__c plan : opdEquipments){
            if (String.isBlank(plan.Rental_Quantity__c)) {
                plan.Rental_Quantity__c = '0';
            }
            eveTemp.put(plan.Rental_Equipment__c, plan);
        }
        if (campaignId != null && campaignId != '') {
            if (campaignEquipments.size() > 0) { //学会已经有出借备品
                for(Rental_Equipment__c prod : prods){
                    if (camTemp.containsKey(prod.Name)) {
                        if (!camTemp.get(prod.Name).Rental_Quantity__c.equals(planProductMap.get(prod.Name))) { //出借备品数量有变化
                            if (String.isBlank(planProductMap.get(prod.Name))) {
                                camTemp.get(prod.Name).Rental_Quantity__c = '0';
                            }else {
                                camTemp.get(prod.Name).Rental_Quantity__c = planProductMap.get(prod.Name);
                            }
                            upeuqipments.add(camTemp.get(prod.Name));
                        }
                    }else{ //新增的出借备品数量
                        if (!'0'.equals(planProductMap.get(prod.Name)) && String.isNotBlank(planProductMap.get(prod.Name))) {
                            Plan_Rental_Equipment__c planeuqipment = new Plan_Rental_Equipment__c();
                            planeuqipment.Campaign__c = campaignId;
                            planeuqipment.RecordTypeId = campaignRecordType;
                            planeuqipment.Name = prod.Name;
                            planeuqipment.Rental_Equipment__c = prod.Name;
                            planeuqipment.Rental_Quantity__c = planProductMap.get(prod.Name);
                            ineuqipments.add(planeuqipment);
                            campaignEquipments.add(planeuqipment);
                        }
                    }
                }
            }else{ //学会没有出借备品,需要新增
                for(Rental_Equipment__c prod : prods){
                    if (!'0'.equals(planProductMap.get(prod.Name)) && String.isNotBlank(planProductMap.get(prod.Name))) {
                        Plan_Rental_Equipment__c planeuqipment = new Plan_Rental_Equipment__c();
                        planeuqipment.Campaign__c = campaignId;
                        planeuqipment.RecordTypeId = campaignRecordType;
                        planeuqipment.Name = prod.Name;
                        planeuqipment.Rental_Equipment__c = prod.Name;
                        planeuqipment.Rental_Quantity__c = planProductMap.get(prod.Name);
                        ineuqipments.add(planeuqipment);
                        campaignEquipments.add(planeuqipment);
                    }
                }
            }
            
        }
        else if (opdId != null && opdId != '') {
            // targetOPD = [SELECT Id,PlanProdDetail__c,Campaign__c FROM OPDPlan__c WHERE Id = :opdId]; 
            
            if (targetOPD.OPDType__c == '事件') {
                List<Event> getEventList = new List<Event>();
                getEventList = [SELECT Id,PlanProdDetail__c FROM Event WHERE Opd_Plan__c = :opdId];
                if (getEventList.size() > 0) {
                    targetEvent = getEventList[0];
                }
            }
            if (opdEquipments.size() > 0) { //OPD已经有出借备品
                for(Rental_Equipment__c prod : prods){
                    if (eveTemp.containsKey(prod.Name)) {
                        if (!eveTemp.get(prod.Name).Rental_Quantity__c.equals(planProductMap.get(prod.Name))) { //出借备品数量有变化
                            if (String.isBlank(planProductMap.get(prod.Name))) {
                                eveTemp.get(prod.Name).Rental_Quantity__c = '0';
                            }else{
                                eveTemp.get(prod.Name).Rental_Quantity__c = planProductMap.get(prod.Name);
                            }
                            upeuqipments.add(eveTemp.get(prod.Name));
                        }
                    }else{ //新增的出借备品数量
                        if (!'0'.equals(planProductMap.get(prod.Name)) && String.isNotBlank(planProductMap.get(prod.Name))) {
                            Plan_Rental_Equipment__c planeuqipment = new Plan_Rental_Equipment__c();
                            planeuqipment.OPD_Plan__c = opdId;
                            planeuqipment.Name = prod.Name;
                            planeuqipment.Rental_Equipment__c = prod.Name;
                            planeuqipment.Rental_Quantity__c = planProductMap.get(prod.Name);
                            if (targetOPD.OPDType__c == '事件') {
                                if (String.isNotBlank(targetEvent.Id)) {
                                    planeuqipment.Event_ID__c = targetEvent.Id;
                                    planeuqipment.RecordTypeId = eventRecordType;    
                                } else {
                                    planeuqipment.RecordTypeId = otherRecordType;    
                                }
                                
                            }else if(targetOPD.OPDType__c == '学会') {
                                planeuqipment.Campaign__c = targetOPD.Campaign__c;
                                planeuqipment.RecordTypeId = campaignRecordType;
                            }else{
                                planeuqipment.RecordTypeId = otherRecordType;
                            }
                            ineuqipments.add(planeuqipment);
                            opdEquipments.add(planeuqipment);
                        }
                    }
                }
            }else{ //OPD没有出借备品,需要新增
                for(Rental_Equipment__c prod : prods){
                    if (!'0'.equals(planProductMap.get(prod.Name)) && String.isNotBlank(planProductMap.get(prod.Name))) {
                        Plan_Rental_Equipment__c planeuqipment = new Plan_Rental_Equipment__c();
                        planeuqipment.OPD_Plan__c = opdId;
                        planeuqipment.Name = prod.Name;
                        planeuqipment.Rental_Equipment__c = prod.Name;
                        planeuqipment.Rental_Quantity__c = planProductMap.get(prod.Name);
                        if (targetOPD.OPDType__c == '事件') {
                            if (String.isNotBlank(targetEvent.Id)) {
                                planeuqipment.Event_ID__c = targetEvent.Id;
                                planeuqipment.RecordTypeId = eventRecordType;
                            } else {
                                planeuqipment.RecordTypeId = otherRecordType;
                            }
                        }else if(targetOPD.OPDType__c == '学会') {
                            planeuqipment.Campaign__c = targetOPD.Campaign__c;
                            planeuqipment.RecordTypeId = campaignRecordType;
                        }else{
                            planeuqipment.RecordTypeId = otherRecordType;
                        }
                        ineuqipments.add(planeuqipment);
                        opdEquipments.add(planeuqipment);
                    }
                }
            }
        }
        System.debug('--ineuqipments--'+ineuqipments +' ----upeuqipments---'+upeuqipments);
        if (ineuqipments.size()>0) {
            insert ineuqipments;
        }
        if (upeuqipments.size()>0) {
            update upeuqipments;
        }
        if (campaignId != null && campaignId != '') {
            String quantity = '';
            for(Plan_Rental_Equipment__c plan : campaignEquipments){
                if (String.isNotBlank(plan.Rental_Quantity__c) && plan.Rental_Quantity__c != '0') {
                    quantity += plan.Rental_Equipment__c     + '*' + plan.Rental_Quantity__c + '; ';
                }
            }
            targetCampaign.LoadNum__c = quantity;
            update targetCampaign;
        }
        else if(opdId != null && opdId != ''){
            String quantity = '';
            for(Plan_Rental_Equipment__c plan : opdEquipments){
                if (String.isNotBlank(plan.Rental_Quantity__c) && plan.Rental_Quantity__c != '0') {
                    quantity += plan.Rental_Equipment__c     + '*' + plan.Rental_Quantity__c + '; ';
                }
            }
            // targetEvent.PlanProdDetail__c = quantity;
            // update targetEvent;
            targetOPD.PlanProdDetail__c = quantity;
            update targetOPD;
        }
        // return null;
 
    }
}