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
/*
 * Author:     Zhang,Huajian
 * Created Date: 06/01/2023
 * Purpose: apex Class
 *
 * */
public without sharing class LexNewAndEditBasePIPLController {
    public static Map<String, Schema.SObjectType> schemaMap = Schema.getGlobalDescribe();
    // 字段信息
    public static Map<string, SObjectField> fieldMap = new Map<string, SObjectField>();
 
    public static ResponseBodyLWC initData(Id rid, String recordTypeId, String sobjectType) {
        try {
            System.debug('rid = ' + rid);
            System.debug('recordTypeId_20230711 = ' + recordTypeId);
            System.debug('sobjectType_20230711 =' + sobjectType);
            fieldMap = schemaMap.get(sobjectType).getDescribe().fields.getMap();
            System.debug('fieldMap = ' + fieldMap);
            System.debug('fieldMap.containsKey(Postal_Code__c): ' + fieldMap.containsKey('Postal_Code__c'));
            ResponseBodyLWC res = new ResponseBodyLWC();
            Map<String, object> data = new Map<String, object>();
            res.entity = data;
 
            //add by Link 2023-10-18:add Approval chenck
            List<Profile> pList = [SELECT Id FROM Profile WHERE Name = '系统管理员' OR Name = 'System Administrator'];
            User u = [SELECT Profile.Id FROM User WHERE Id =:UserInfo.getUserId()];
            if(u.Profile.Id != pList[0].Id){
                if(String.isNotBlank(rid)){
                    Boolean isApproval = Approval.isLocked(rid);
                    if(isApproval){
                        return new ResponseBodyLWC('Error', 500, '数据审批中,不能编辑', '');
                    }
                }
            }
            if (String.isBlank(recordTypeId)) {
                String recordTypeIdTemp =  LayoutDescriberHelper.getDefaultRecordType(sobjectType);
                System.debug('recordTypeIdTemp = ' + recordTypeIdTemp);
                if(recordTypeIdTemp != System.Label.target_customer){ //niwu change
                    data.put('recordTypeId',recordTypeIdTemp);
                    recordTypeId = recordTypeIdTemp;
                } else {
                    data.put('recordTypeId',null);
                    recordTypeId = null;
                }
            }
            //deloitte-zhj 2023/07/31 自定义元数据配置titleMap start
            Map<String, Map<String, String>> titleMap = new Map<String, Map<String, String>>();
            List<LEX_PIPL_Layout_Config__mdt> piLayoutConfigList = [SELECT id,DeveloperName, Layout_Header__c, Sobject_Type__c FROM LEX_PIPL_Layout_Config__mdt];
            if(piLayoutConfigList.size() > 0){
                for(LEX_PIPL_Layout_Config__mdt piLayoutConfig : piLayoutConfigList){
                    //titleMap.put(piLayoutConfig.Sobject_Type__c,JSON.deserialize(piLayoutConfig.Layout_Header__c, Map<String, String>.class));
                    Map<String, Object> tempMap = (Map<String, Object>)JSON.deserializeUntyped(piLayoutConfig.Layout_Header__c);
                    Map<String, String> innerMap = new Map<String, String>();
                    for (String key : tempMap.keySet()) {
                        innerMap.put(key, (String)tempMap.get(key));
                    }
                    titleMap.put(piLayoutConfig.Sobject_Type__c, innerMap);
                }
            }
            //deloitte-zhj 2023/07/31 自定义元数据配置titleMap end
            //编辑
            if(!String.isBlank(rid)){
                //获取对应对象的字段                List<Sobject> lso = Database.query('select id from RecordType where SobjectType = :sobjectType');
                String sql = 'select ';
                DescribeSObjectResult objectType = rid.getSobjectType().getDescribe();
                List<String> objectFields = new List<String>(objectType.fields.getMap().keySet());
                sql += String.join(objectFields, ',') +' from '+sobjectType+' where id =\''+rid+'\' limit 1';
                System.debug('sql = ' + sql);
                Sobject leadData = Database.query(sql);
                System.debug('leadData_20230711 = ' + leadData);
                if(leadData == null){
                    return new ResponseBodyLWC('Error',500, 'id不存在', '');
                }
                if(objectFields.contains('recordtypeid')){
                    recordTypeId = (String)leadData.get('RecordTypeId');
                    System.debug('recordTypeId_20230711_Debug = ' + recordTypeId);
                }
                List<Metadata.LayoutSection> layout = MetaDataUtility.GetRecordTypePageLayout(recordTypeId, sobjectType);
                System.debug('layout=' + layout);
                for (Metadata.LayoutSection section : layout) {
                    if(titleMap.containsKey(sobjectType) && titleMap.get(sobjectType).containsKey(section.label)){
                        section.label = titleMap.get(sobjectType).get(section.label);
                    }
                }
                data.put('layout', Json.serialize(reviseMetaLayouts(layout)));
                data.put('recordTypeId', recordTypeId);
                System.debug('data.recordTypeId: ' + data.get('recordTypeId'));
                data.put('AWSDataId', (String)leadData.get('AWS_Data_Id__c'));
                System.debug('(String)leadData.get(AWS_Data_Id__c): ' + (String)leadData.get('AWS_Data_Id__c'));
                //获取值
                System.debug('leadData = ' + JSON.serialize(leadData));
                data.put('data', leadData);
            }else {
                List<Metadata.LayoutSection> layout = MetaDataUtility.GetRecordTypePageLayout(recordTypeId, sobjectType);
                System.debug('layout = ' + JSON.serialize(layout));
                for (Metadata.LayoutSection section : layout) {
                    if(titleMap.containsKey(sobjectType) && titleMap.get(sobjectType).containsKey(section.label)){
                        section.label = titleMap.get(sobjectType).get(section.label);
                    }
                }
                data.put('layout', Json.serialize(reviseMetaLayouts(layout)));
            }
            //获取PI字段
            PIHelper.PIIntegration piIntegration = PIHelper.getPIIntegrationInfo(sobjectType);
            System.debug('piIntegration.PIDetails = ' + piIntegration.PIDetails);
            Map<String, String> AWSToSobjectNonEncryptedMap = new Map<String, String>();
            List<String> AWSToSobjectNonEncryptedMapKeySet = new List<String>();
            for (PI_Field_Policy_Detail__c PIDetail : piIntegration.PIDetails) {
                AWSToSobjectNonEncryptedMap.put(PIDetail.AWS_Field_API__c, PIDetail.SF_Field_API_Name__c);
                AWSToSobjectNonEncryptedMapKeySet.add(PIDetail.AWS_Field_API__c);
            }
            System.debug('AWSToSobjectNonEncryptedMap = ' + AWSToSobjectNonEncryptedMap);
            System.debug('AWSToSobjectNonEncryptedMapKeySet = ' + AWSToSobjectNonEncryptedMapKeySet);
            System.debug('Json.serialize(PIHelper.getPIIntegrationInfo(sobjectType)) = ' + Json.serialize(PIHelper.getPIIntegrationInfo(sobjectType)));
            System.debug('Json.serialize(PIHelper.getPIIntegrationInfo(sobjectTypeV2)) = ' + Json.serialize(PIHelper.getPIIntegrationInfo(sobjectType + 'V2')));
            data.put('AWSToSobjectNonEncryptedMap', AWSToSobjectNonEncryptedMap);
            data.put('AWSToSobjectNonEncryptedMapKeySet', AWSToSobjectNonEncryptedMapKeySet);
            data.put('staticResource', Json.serialize(PIHelper.getPIIntegrationInfo(sobjectType)));
            data.put('staticResourceV2', Json.serialize(PIHelper.getPIIntegrationInfo(sobjectType + 'V2')));
            data.put('titleMap', Json.serialize(titleMap));
            res.status = 'Success';
            res.code = 200;
            res.msg = '';
            return res;
        } catch (Exception e) {
            System.debug('error = ' + e.getMessage() + ' line = ' + e.getLineNumber()+e.getStackTraceString());
            return new ResponseBodyLWC('Error', 500, e.getMessage() + ' ' + e.getLineNumber(), '');
        }
    }
 
    public static ResponseBodyLWC queryAccount(String accountTypes, String accountId) {
        ResponseBodyLWC res = new ResponseBodyLWC();
        Map<String, object> data = new Map<String, object>();
        res.entity = data;
        System.debug('accountType = ' + accountTypes);
        System.debug('accountId = ' + accountId);
        try {
            List<Object> types = (List<Object>) JSON.deserializeUntyped(accountTypes);
            System.debug('types=' + types);
            String soql = 'select Id,Name,';
            for (Object t : types) {
                soql += (String) t + ',';
            }
            soql = soql.substring(0, soql.length() - 1);
            soql += ' from Account where id=\'' + accountId + '\'';
            System.debug('soql=' + soql);
            Sobject account = new Account();
            if (!Test.isRunningTest()) {
                account = Database.query(soql);
            } else {
                account.put('Id', System.Label.Agency_ID_c);
            }
            Map<String, Map<String, String>> m = new Map<String, Map<String, String>>();
            System.debug('account=' + account);
            for (Object ty : types) {
                String t = (String) ty;
                if (account.get(t) != null || Test.isRunningTest()) {
                    Sobject acc = new Account();
                    if (Test.isRunningTest()) {
                        acc.put('Id', System.Label.Agency_ID_c);
                        acc.put('Name', 'Name');
                    } else {
                        acc = Database.query('select Id,Name from Account where id=\'' + account.get(t) + '\'');
                    }
                    Map<String, String> n = new Map<String, String>();
                    n.put('Id', (String) acc.get('Id'));
                    n.put('Name', (String) acc.get('Name'));
                    m.put(t, n);
                }
            }
            System.debug('m=' + m);
            data.put('m', m);
            data.put('account', account);
            res.status = 'Success';
            res.code = 200;
            res.msg = '';
            return res;
        } catch (Exception e) {
            return new ResponseBodyLWC('Error', 500, e.getMessage() + ' ' + e.getLineNumber(), '');
        }
    }
 
    public static ResponseBodyLWC searchContactInit(String accountId, String searchKeyWord) {
        ResponseBodyLWC res = new ResponseBodyLWC();
        Map<String, object> data = new Map<String, object>();
        res.entity = data;
        System.debug('accountId = ' + accountId);
        System.debug('searchKeyWord = ' + searchKeyWord);
        try {
            List<Contact> conList = new List<Contact>();
            List<Contact> noPIContactList = new List<Contact>();
            if (checkNullString(accountId) && checkNullString(searchKeyWord)) {
                //conList = new List<Contact>();
                conList = LexAWSServicePIPLDao.searchContactInitNoAccountId();
            } else {
                if (checkNullString(accountId)) {
                    //conList = new List<Contact>();
                    conList = LexAWSServicePIPLDao.searchContactInitNoAccountId();
                } else {
                    //2022-5-12 yjk 将科室匹配改为医院匹配查询联系人 statt
                    Account act = [SELECT id, Hospital__c FROM Account WHERE id = :accountId];
                    // conList = new List<Contact>(
                    //     [
                    //         SELECT Id, AWS_Data_Id__c, Account.Name
                    //         FROM Contact
                    //         WHERE Account.Hospital__c = :act.Hospital__c AND AWS_Data_Id__c != ''
                    //     ]
                    // );
                    conList = LexAWSServicePIPLDao.searchContactInit(act.Hospital__c);
                    //noPIContactList = AWSServiceTool.getNoPIContact(searchKeyWord, accountId);
                    noPIContactList = LexAWSServicePIPLDao.getNoPIContact(searchKeyWord, accountId);
                    //2022-5-12 yjk 将科室匹配改为医院匹配查询联系人 end
                }
            }
            Map<String, Contact> awsIdToContactMap = new Map<String, Contact>();
            List<String> conAWSIds = new List<String>();
            for (Contact con : conList) {
                conAWSIds.add(con.AWS_Data_Id__c);
                awsIdToContactMap.put(con.AWS_Data_Id__c, con);
            }
            data.put('awsIdToContactMap', awsIdToContactMap);
            data.put('conAWSIds', conAWSIds);
            data.put('noPIContactList', noPIContactList);
            data.put('contactStaticResource', JSON.serialize(PIHelper.getPIIntegrationInfo('Contact')));
            res.status = 'Success';
            res.code = 200;
            res.msg = '';
            return res;
        } catch (Exception e) {
            return new ResponseBodyLWC('Error', 500, e.getMessage() + ' ' + e.getLineNumber(), '');
        }
    }
 
    public static Boolean checkNullString(String inputString) {
        if (String.isEmpty(inputString) || String.isBlank(inputString)) {
            return true;
        }
        return false;
    }
 
    /**
     *@description 转换layout
     *@param    sections                        默认metalayout
     *@return    List<Metadata.LayoutSection>    标准metalayout
     */
    public static List<Metadata.LayoutSection> reviseMetaLayouts(List<Metadata.LayoutSection> sections) {
        List<Metadata.LayoutSection> result = new List<Metadata.LayoutSection>();
        if (sections == null) {
            return null;
        }
        for (Metadata.LayoutSection s : sections) {
            Metadata.LayoutSection section = new Metadata.LayoutSection();
            section.customLabel = s.customLabel;
            section.detailHeading = s.detailHeading;
            section.editHeading = s.editHeading;
            section.label = s.label;
            section.style = s.style;
            result.add(section);
            for (Metadata.LayoutColumn c : s.layoutColumns) {
                if (c.layoutItems == null) {
                    break;
                }
                Metadata.LayoutColumn col = new Metadata.LayoutColumn();
                col.reserved = col.reserved;
                section.layoutColumns.add(col);
                for (Metadata.layoutItem item : c.layoutItems) {
                    if(item.field == 'Consum_Apply_Equipment_Set__c'){
                        col.layoutItems.add(item);
                    }
                    if (item.emptySpace != true && (!fieldMap.containsKey(item.field) || !isUpdateable(fieldMap.get(item.field).getDescribe()))  && item.field != 'IsJump_Rental__c' && item.field != 'JumpCause_Rental__c') {
                        // System.debug('item.emptySpace = ' + item.emptySpace);
                        // System.debug('!fieldMap.containsKey(item.field) = ' + fieldMap.containsKey(item.field));
                        // System.debug('isUpdateable(fieldMap.get(item.field).getDescribe()) = ' + isUpdateable(fieldMap.get(item.field).getDescribe()));
                        // System.debug('continue filed = ' + item.field);
                        continue;
                    }
                    // if(!isUpdateable(fieldMap.get(item.field).getDescribe())){
                    //     item.behavior = 'Readonly';
                    // }
                    col.layoutItems.add(item);
                }
            }
        }
        return result;
    }
 
    private static Boolean isUpdateable(Schema.DescribeFieldResult dfr) {
        return (new List<String>{ 'Id', 'Name' }).contains(dfr.getName()) || dfr.isUpdateable();
    }
 
    public static ResponseBodyLWC searchContactInitV2(String accountId, List<String> conAWSIds, String searchKeyWord) {
        ResponseBodyLWC res = new ResponseBodyLWC();
        Map<String, object> data = new Map<String, object>();
        res.entity = data;
        System.debug('accountId = ' + accountId);
        System.debug('conAWSIds = ' + conAWSIds);
        System.debug('searchKeyWord = ' + searchKeyWord);
        try {
            List<Contact> conList = new List<Contact>();
            List<Contact> noPIContactList = new List<Contact>();
            //通过awsIds查询联系人
            if(conAWSIds.size() != 0){
                conList = LexAWSServicePIPLDao.searchContactByAWSIds(conAWSIds);
            }
            noPIContactList = LexAWSServicePIPLDao.getNoPIContact(searchKeyWord, accountId);
            data.put('conList', conList);
            data.put('noPIContactList', noPIContactList);
            res.status = 'Success';
            res.code = 200;
            res.msg = '';
            return res;
        } catch (Exception e) {
            return new ResponseBodyLWC('Error', 500, e.getMessage() + ' ' + e.getLineNumber(), '');
        }
    }
 
    public static ResponseBodyLWC getNoPIContact(String searchContactName,String accountId) {
        ResponseBodyLWC res = new ResponseBodyLWC();
        Map<String, object> data = new Map<String, object>();
        res.entity = data;
        try {
            List<Contact> noPIContactList = new List<Contact>();
            noPIContactList = LexAWSServicePIPLDao.getNoPIContact(searchContactName, accountId);
            data.put('noPIContactList', noPIContactList);
            res.status = 'Success';
            res.code = 200;
            res.msg = '';
            return res;
        } catch (Exception e) {
            return new ResponseBodyLWC('Error', 500, e.getMessage() + ' ' + e.getLineNumber(), '');
        }
    }
}