高章伟
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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
public with sharing class CreateTargetCmp {
    public CreateTargetCmp() {
        
    }
/*
    @AuraEnabled
    public static List<Agency_Hospital_Link__c> getHospitalList(String hospital_name) {
        hospital_name = '%' + hospital_name.trim() + '%'; 
        return [select Hospital_Name_readonly__c, Id, Hospital__c from Agency_Hospital_Link__c where Hospital_Name_readonly__c like :hospital_name];
    }
 
    @AuraEnabled
    public static Agency_Opportunity__c getEmptyOpp() {
        return [select Id, Agency_Hospital__c,Agency_Hospital__r.Name from Agency_Opportunity__c where Agency_Hospital__c <> null limit 1][0];
    }
*/
 
    @RemoteAction
    @AuraEnabled
    public static ProductTypes__c getProduct(String id){
        return [select Department_Cateogy__c, OPD_Flg__c, Id, SIS_Flg__c from ProductTypes__c where Id =:id];
    }
 
    @RemoteAction
    @AuraEnabled
    public static Map<String,Object> getProductList(){
        List<ProductTypes__c> ptList = [select Id, Name, Department_Cateogy__c, OPD_Flg__c, SIS_Flg__c from ProductTypes__c where DeleteFlg__c = false];
        Map<String,Object> resMap = new Map<String,Object>();
        for (ProductTypes__c pt : ptList) {
            Map<String,String> ptMap = new Map<String,String>();
            ptMap.put('label', pt.Name);
            ptMap.put('value', pt.Id);
            if (String.isBlank(pt.Department_Cateogy__c)) continue;
 
            String[] dcList = pt.Department_Cateogy__c.split(';');
 
            for (String dc : dcList) {
                List<Map<String,String>> pts;
                if (resMap.containsKey(dc)) {
                    pts = (List<Map<String,String>>)resMap.get(dc);
                } else {
                    pts = new List<Map<String,String>>();
                    Map<String,String> blank = new Map<String,String>();
                    blank.put('label', '');
                    blank.put('value', '');
                    pts.add(blank);
                }
                pts.add(ptMap);
                resMap.put(dc, pts);
            }
            
        }
        return resMap;
    }
 
    @AuraEnabled
    public static Map<String, String> getHospital(String recordId) {
        List<Agency_Opportunity__c> aoList = [select Id, Agency_Hospital_Target__c, Agency_Hospital_Target__r.Name from Agency_Opportunity__c where Id = :recordId];
        if (aoList.size() > 0) {
            Map<String,String> res = new Map<String, String>();
            res.put('Id', aoList[0].Agency_Hospital_Target__c);
            res.put('Name', aoList[0].Agency_Hospital_Target__r.Name);
            return res;
        } else {
            throw new AuraHandledException('找不到经销商医院');
        }    
    }
 
    @AuraEnabled
    public static Map<String,Object> getDeptAmount(String hospitalId) {
 
        Id rtId = [select Id,DeveloperName from RecordType where IsActive = true and SobjectType = 'Agency_Opportunity__c' and DeveloperName = 'Target'].Id;
        //Schema.SobjectType.Agency_Opportunity__c.getRecordTypeInfosByName().get('目标').getRecordTypeId();
 
        List<Agency_Opportunity__c> accList2 = [select Id, Department_Cateogy__c, Product_Category__c, Product_Category__r.Name,Aim_Price__c 
                                                    from Agency_Opportunity__c 
                                                    where Agency_Hospital_Target__c = :hospitalId and RecordTypeId = :rtId and Is_OPDSIS_Target__c != true];
        //Agency_Hospital_Link__c ahl = [select Id, Hospital__c, Name, TargetHospital__c from Agency_Hospital_Link__c where Id = :hospitalId];
        //deptAmount.put('target', String.valueOf(ahl.TargetHospital__c));
        Map<String,Object> deptAmount = new Map<String, Object>();
        
        List<Agency_Opportunity__c> aoppOTH = new List<Agency_Opportunity__c>();
        List<Agency_Opportunity__c> aoppBF = new List<Agency_Opportunity__c>();
        List<Agency_Opportunity__c> aoppGYN = new List<Agency_Opportunity__c>();
        List<Agency_Opportunity__c> aoppGS = new List<Agency_Opportunity__c>();
        List<Agency_Opportunity__c> aoppGI = new List<Agency_Opportunity__c>();
        List<Agency_Opportunity__c> aoppURO = new List<Agency_Opportunity__c>();
        List<Agency_Opportunity__c> aoppENT = new List<Agency_Opportunity__c>();
        List<Agency_Opportunity__c> aoppET = new List<Agency_Opportunity__c>();
        for (Agency_Opportunity__c acc2 : accList2) {
            if (acc2.Department_Cateogy__c == 'OTH') {
                aoppOTH.add(acc2);
            } else if (acc2.Department_Cateogy__c == 'BF') {
                aoppBF.add(acc2);
            } else if (acc2.Department_Cateogy__c == 'GYN') {
                aoppGYN.add(acc2);
            } else if (acc2.Department_Cateogy__c == 'GS') {
                aoppGS.add(acc2);
            } else if (acc2.Department_Cateogy__c == 'URO') {
                aoppURO.add(acc2);
            } else if (acc2.Department_Cateogy__c == 'GI') {
                aoppGI.add(acc2);
            } else if (acc2.Department_Cateogy__c == 'ENT') {
                aoppENT.add(acc2);
            } else if (acc2.Department_Cateogy__c == 'ET') {
                aoppET.add(acc2);
            }
        }
        deptAmount.put('OTH', aoppOTH);
        deptAmount.put('BF', aoppBF);
        deptAmount.put('GYN', aoppGYN);
        deptAmount.put('GS', aoppGS);
        deptAmount.put('URO', aoppURO);
        deptAmount.put('GI', aoppGI);
        deptAmount.put('ENT', aoppENT);
        deptAmount.put('ET', aoppET);
        return deptAmount;
    }
    
 
    @AuraEnabled
    public static string createSFTarget(String hospitalId, Boolean targetHospital, String[] GIProduct, String[] ETProduct, String[] BFProduct, String[] OTHProduct, String[] GSProduct, String[] UROProduct, String[] GYNProduct, String[] ENTProduct, String OCMTerm) {
        System.debug('createSFTarget');
        string returnContent = '';
        if (String.isEmpty(hospitalId)) {
            throw new AuraHandledException('医院不存在');
        } else {
            //if (!isNumericOrDecimal(GIAmount) ||!isNumericOrDecimal(ETAmount) ||!isNumericOrDecimal(BFAmount) ||!isNumericOrDecimal(OTHAmount) ||!isNumericOrDecimal(GSAmount) ||!isNumericOrDecimal(UROAmount) ||!isNumericOrDecimal(GYNAmount) ||!isNumericOrDecimal(ENTAmount)) {
            //    throw new AuraHandledException('请输入正确数据');
            //}
            String[] deptTypes = new String[] {'Department_Class_OTH', 'Department_Class_BF', 'Department_Class_GYN', 'Department_Class_GS', 'Department_Class_URO', 'Department_Class_GI', 'Department_Class_ENT', 'Department_Class_ET'};
            List<RecordType> deptRects = LightningUtil.getAccRecordType(deptTypes);
            Map<String, String> deptRectMap = new Map<String, String>();
            for (RecordType rect : deptRects) {
                deptRectMap.put(rect.Id, rect.DeveloperName);
            }
 
            Id rtId = [select Id,DeveloperName from RecordType where IsActive = true and SobjectType = 'Agency_Opportunity__c' and DeveloperName = 'Target'].Id;
 
            Set<String> recordIds = deptRectMap.keySet();
            Agency_Hospital_Link__c ahl = [select Id, Hospital__c, Name from Agency_Hospital_Link__c where Id = :hospitalId];
            //if (targetHospital == null) targetHospital = false;
         //   ahl.TargetHospital__c = targetHospital;
            //update ahl;
 
            List<Account> accList = LightningUtil.getDeptListByRecordType(ahl.Hospital__c, recordIds);
            List<Agency_Opportunity__c> upsertList = new List<Agency_Opportunity__c>();
 
            List<ProductTypes__c> productList = [select Id, Name, OPD_Flg__c, SIS_Flg__c, Name2__c from ProductTypes__c where DeleteFlg__c = false];
            Map<ID, ProductTypes__c> productMap = new Map<ID, ProductTypes__c>( productList );
            //Map<String, Agency_Opportunity__c> upsertMap = new Map<String, Agency_Opportunity__c>();
            //for (ProductTypes__c product : productList) {
            //    if (product.OPD_Flg__c) {
            //        Agency_Opportunity__c aopp = setAgencyOpp(ahl.Hospital__c, null, rtId, OCMTerm, hospitalId, ahl.Name+' OPD');
            //        aopp.Product_Category__c = product.Id;
            //        aopp.TargetInputKey__c = hospitalId+':OPD:'+OCMTerm+':'+product.Name2__c;
            //        upsertMap.put(aopp.TargetInputKey__c, aopp);
            //        //upsertList.add(aopp);
            //    }
            //    if (product.SIS_Flg__c) {
            //        Agency_Opportunity__c aopp = setAgencyOpp(ahl.Hospital__c, null, rtId, OCMTerm, hospitalId, ahl.Name+' SIS');
            //        aopp.Product_Category__c = product.Id;
            //        aopp.TargetInputKey__c = hospitalId+':SIS:'+OCMTerm+':'+product.Name2__c;
            //        upsertMap.put(aopp.TargetInputKey__c, aopp);
            //        //upsertList.add(aopp);
            //    }
            //}
            //upsertList.addAll(upsertMap.values());
 
            for (Account acc : accList) {
                String recordType = deptRectMap.get(String.valueOf(acc.RecordTypeId));
       //         Agency_Opportunity__c aopp = new Agency_Opportunity__c();
       //         aopp.Hospital_Target__c = ahl.Hospital__c;
       //         aopp.Department_Class__c = acc.Id;
                //aopp.RecordTypeId = rtId;
                //aopp.OCM_Term__c = OCMTerm;
                //aopp.TargetInputKey__c = hospitalId+':'+acc.Id+':'+aopp.OCM_Term__c;
                //aopp.Agency_Hospital_Target__c = hospitalId;
                String pc1 = '';
                String pc2 = '';
                String pc3 = '';
                
                Integer pc4_1 = 0;
                Integer pc4_2 = 0;
                Integer pc4_3 = 0;
                String dc = '';
                returnContent += ' = 0';
                if (recordType == 'Department_Class_OTH') {
                    //aopp.Department_Cateogy__c = 'OTH';
                    dc = 'OTH';
                    pc1 = OTHProduct[0];
                    pc2 = OTHProduct[1];
                    pc3 = OTHProduct[2];
 
                    pc4_1 = Integer.valueOf(OTHProduct[3]);
                    pc4_2 = Integer.valueOf(OTHProduct[4]);
                    pc4_3 = Integer.valueOf(OTHProduct[5]);
                    //aopp.Amount__c = Decimal.valueOf(OTHAmount);
                } else if (recordType == 'Department_Class_BF') {
                    dc = 'BF';
                    //aopp.Amount__c = Decimal.valueOf(BFAmount);
                    pc1 = BFProduct[0];
                    pc2 = BFProduct[1];
                    pc3 = BFProduct[2];
 
                    pc4_1 = Integer.valueOf(BFProduct[3]);
                    pc4_2 = Integer.valueOf(BFProduct[4]);
                    pc4_3 = Integer.valueOf(BFProduct[5]);
                } else if (recordType == 'Department_Class_GYN') {
                    dc = 'GYN';
                    pc1 = GYNProduct[0];
                    pc2 = GYNProduct[1];
                    pc3 = GYNProduct[2];
                    pc4_1 = Integer.valueOf(GYNProduct[3]);
                    pc4_2 = Integer.valueOf(GYNProduct[4]);
                    pc4_3 = Integer.valueOf(GYNProduct[5]);
                    //aopp.Amount__c = Decimal.valueOf(GYNAmount);
                } else if (recordType == 'Department_Class_GS') {
                    dc = 'GS';
                    pc1 = GSProduct[0];
                    pc2 = GSProduct[1];
                    pc3 = GSProduct[2];
 
                    pc4_1 = Integer.valueOf(GSProduct[3]);
                    pc4_2 = Integer.valueOf(GSProduct[4]);
                    pc4_3 = Integer.valueOf(GSProduct[5]);
                    //aopp.Amount__c = Decimal.valueOf(GSAmount);
                } else if (recordType == 'Department_Class_URO') {
                    dc = 'URO';
                    pc1 = UROProduct[0];
                    pc2 = UROProduct[1];
                    pc3 = UROProduct[2];
 
                    pc4_1 = Integer.valueOf(UROProduct[3]);
                    pc4_2 = Integer.valueOf(UROProduct[4]);
                    pc4_3 = Integer.valueOf(UROProduct[5]);
                    //aopp.Amount__c = Decimal.valueOf(UROAmount);
                } else if (recordType == 'Department_Class_GI') {
                    dc = 'GI';
                    pc1 = GIProduct[0];
                    pc2 = GIProduct[1];
                    pc3 = GIProduct[2];
 
                    pc4_1 = Integer.valueOf(GIProduct[3]);
                    pc4_2 = Integer.valueOf(GIProduct[4]);
                    pc4_3 = Integer.valueOf(GIProduct[5]);
                    returnContent += '  pc4_2 ='+pc4_2 + ' pc4_3 ='+pc4_3;
                    //aopp.Amount__c = Decimal.valueOf(GIAmount);
                } else if (recordType == 'Department_Class_ENT') {
                    dc = 'ENT';
                    pc1 = ENTProduct[0];
                    pc2 = ENTProduct[1];
                    pc3 = ENTProduct[2];
 
                    pc4_1 = Integer.valueOf(ENTProduct[3]);
                    pc4_2 = Integer.valueOf(ENTProduct[4]);
                    pc4_3 = Integer.valueOf(ENTProduct[5]);
                    //aopp.Amount__c = Decimal.valueOf(ENTAmount);
                } else if (recordType == 'Department_Class_ET') {
                    dc = 'ET';
                    pc1 = ETProduct[0];
                    pc2 = ETProduct[1];
                    pc3 = ETProduct[2];
 
                    pc4_1 = Integer.valueOf(ETProduct[3]);
                    pc4_2 = Integer.valueOf(ETProduct[4]);
                    pc4_3 = Integer.valueOf(ETProduct[5]);
                    //aopp.Amount__c = Decimal.valueOf(ETAmount);
                }
                //if (String.isBlank(aopp.Product_Category1__c)) {
                //    if (String.isBlank(aopp.Product_Category2__c)) {
                //        aopp.Product_Category1__c = aopp.Product_Category3__c;
                //    } else {
                //        aopp.Product_Category1__c = aopp.Product_Category2__c;
                //        aopp.Product_Category2__c = aopp.Product_Category3__c;
                //    }
                //    aopp.Product_Category3__c = null; 
                //}
                //aopp.Name = '目标::'+aopp.OCM_Term__c+':'+acc.Name;
                if (String.isNotBlank(pc1)) {
                    ProductTypes__c pt = productMap.get(pc1);
                    Agency_Opportunity__c aopp = setAgencyOpp(ahl.Hospital__c, acc.Id, rtId, OCMTerm, hospitalId, acc.Name);
                    aopp.Department_Cateogy__c = dc;
                    aopp.Product_Category__c = pc1;
                    aopp.TargetInputKey__c = hospitalId+':'+dc+':'+OCMTerm+':'+pt.Name2__c;
                    if (pc4_1 != 0) {
                        aopp.Aim_Price__c = pc4_1;
                    }
                    upsertList.add(aopp);
 
                    if (pt.OPD_Flg__c) {
                        Agency_Opportunity__c aopp_opd = setAgencyOpp(ahl.Hospital__c, acc.Id, rtId, OCMTerm, hospitalId, acc.Name+' OPD');
                        aopp_opd.Department_Cateogy__c = dc;
                        aopp_opd.Product_Category__c = pc1;
                        aopp_opd.TargetInputKey__c = hospitalId+':'+dc+':OPD:'+OCMTerm+':'+pt.Name2__c;
                        if (pc4_1 != 0) {
                            aopp_opd.Aim_Price__c = pc4_1;
                        }
                        upsertList.add(aopp_opd);
                    }
                    if (pt.SIS_Flg__c) {
                        Agency_Opportunity__c aopp_sis = setAgencyOpp(ahl.Hospital__c, acc.Id, rtId, OCMTerm, hospitalId, acc.Name+' SIS');
                        aopp_sis.Department_Cateogy__c = dc;
                        aopp_sis.Product_Category__c = pc1;
                        aopp_sis.TargetInputKey__c = hospitalId+':'+dc+':SIS:'+OCMTerm+':'+pt.Name2__c;
                        if (pc4_1 != 0) {
                            aopp_sis.Aim_Price__c = pc4_1;
                        }
                        upsertList.add(aopp_sis);
                    }
                }
                if (String.isNotBlank(pc2)) {
                    ProductTypes__c pt = productMap.get(pc2);
                    Agency_Opportunity__c aopp = setAgencyOpp(ahl.Hospital__c, acc.Id, rtId, OCMTerm, hospitalId, acc.Name);
                    aopp.Department_Cateogy__c = dc;
                    aopp.Product_Category__c = pc2;
                    aopp.TargetInputKey__c = hospitalId+':'+dc+':'+OCMTerm+':'+pt.Name2__c;
                    if (pc4_2 != 0) {
                        aopp.Aim_Price__c = pc4_2;
                    }
                    upsertList.add(aopp);
 
                    if (pt.OPD_Flg__c) {
                        Agency_Opportunity__c aopp_opd = setAgencyOpp(ahl.Hospital__c, acc.Id, rtId, OCMTerm, hospitalId, acc.Name+' OPD');
                        aopp_opd.Department_Cateogy__c = dc;
                        aopp_opd.Product_Category__c = pc2;
                        aopp_opd.TargetInputKey__c = hospitalId+':'+dc+':OPD:'+OCMTerm+':'+pt.Name2__c;
                        if (pc4_2 != 0) {
                            aopp_opd.Aim_Price__c = pc4_2;
                        }
                        upsertList.add(aopp_opd);
                    }
                    if (pt.SIS_Flg__c) {
                        Agency_Opportunity__c aopp_sis = setAgencyOpp(ahl.Hospital__c, acc.Id, rtId, OCMTerm, hospitalId, acc.Name+' SIS');
                        aopp_sis.Department_Cateogy__c = dc;
                        aopp_sis.Product_Category__c = pc2;
                        aopp_sis.TargetInputKey__c = hospitalId+':'+dc+':SIS:'+OCMTerm+':'+pt.Name2__c;
                        if (pc4_2 != 0) {
                            aopp_sis.Aim_Price__c = pc4_2;
                        }
                        upsertList.add(aopp_sis);
                    }
                }
                if (String.isNotBlank(pc3)) {
                    ProductTypes__c pt = productMap.get(pc3);
                    Agency_Opportunity__c aopp = setAgencyOpp(ahl.Hospital__c, acc.Id, rtId, OCMTerm, hospitalId, acc.Name);
                    aopp.Department_Cateogy__c = dc;
                    aopp.Product_Category__c = pc3;
                    aopp.TargetInputKey__c = hospitalId+':'+dc+':'+OCMTerm+':'+pt.Name2__c;
                    if (pc4_3 != 0) {
                        aopp.Aim_Price__c = pc4_3;
                    }
                    upsertList.add(aopp);
 
                    if (pt.OPD_Flg__c) {
                        Agency_Opportunity__c aopp_opd = setAgencyOpp(ahl.Hospital__c, acc.Id, rtId, OCMTerm, hospitalId, acc.Name+' OPD');
                        aopp_opd.Department_Cateogy__c = dc;
                        aopp_opd.Product_Category__c = pc3;
                        aopp_opd.TargetInputKey__c = hospitalId+':'+dc+':OPD:'+OCMTerm+':'+pt.Name2__c;
                        if (pc4_3 != 0) {
                            aopp_opd.Aim_Price__c = pc4_3;
                        }
                        upsertList.add(aopp_opd);
                    }
                    if (pt.SIS_Flg__c) {
                        Agency_Opportunity__c aopp_sis = setAgencyOpp(ahl.Hospital__c, acc.Id, rtId, OCMTerm, hospitalId, acc.Name+' SIS');
                        aopp_sis.Department_Cateogy__c = dc;
                        aopp_sis.Product_Category__c = pc3;
                        aopp_sis.TargetInputKey__c = hospitalId+':'+dc+':SIS:'+OCMTerm+':'+pt.Name2__c;
                        if (pc4_3 != 0) {
                            aopp_sis.Aim_Price__c = pc4_3;
                        }
                        upsertList.add(aopp_sis);
                    }
                }
                //upsertList.add(aopp);
            }
            Datetime nowDate = Datetime.now();
            if (upsertList.size() > 0) {
                LightningUtil.upsertAgencyOpportunity(upsertList);
            }
 
            List<Agency_Opportunity__c> delList = [select Id from Agency_Opportunity__c where RecordTypeId = :rtId and Agency_Hospital_Target__c = :hospitalId and LastModifiedDate < :nowDate];
            if (delList.size() > 0) {
                delete delList;
            }
        }
        return returnContent;
    }
 
    private static Agency_Opportunity__c setAgencyOpp(String hospitalTarget, String departmentClass, String rtId, String OCMTerm, String hospitalId, String accName){
        Agency_Opportunity__c aopp = new Agency_Opportunity__c();
        aopp.Hospital_Target__c = hospitalTarget;
        aopp.Department_Class__c = departmentClass;
        aopp.RecordTypeId = rtId;
        aopp.OCM_Term__c = OCMTerm;
        aopp.Agency_Hospital_Target__c = hospitalId;
        aopp.Name = '目标::'+OCMTerm+':'+accName;
        return aopp;
    }
/*
    private static Boolean isNumericOrDecimal(String s){
        Boolean ReturnValue;
        try{
            Decimal.valueOf(s);
            ReturnValue = TRUE; 
        } catch (Exception e) {
            ReturnValue = FALSE;
        }
        return ReturnValue;
    }
*/
}