liuyn
2024-03-11 a87f1c3df03078814ee97ad0c8ac200a232419e9
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
/*
 * Author: Zhang,Heyang
 * Created Date: 2023/08/07
 * Purpose: get page layout and record data
 * Test Class: LexNewAndEditRentalApplyPIPLTest
 *
 * */public with sharing class LexNewAndEditRentalApplyPIPLController {
 
    public static Map<String, Schema.SObjectType> schemaMap = Schema.getGlobalDescribe();
    // 字段信息
    public static Map<string, SObjectField> fieldMap = new Map<string, SObjectField>();
 
    @AuraEnabled
    public static ResponseBodyLWC initData(Id rid, String recordTypeId, String sobjectType) {
System.debug('enter LexNewAndEditRentalApplyPIPLController initData');
        System.debug('start recordTypeId = ' + recordTypeId);
        fieldMap = schemaMap.get(sobjectType).getDescribe().fields.getMap();
        ResponseBodyLWC res = new ResponseBodyLWC();
        Map<String, object> data = new Map<String, object>();
        res.entity = data;
        //deloitte-zhj 20231116 PIPL还原 start
        if (String.isBlank(recordTypeId)) {
            String recordTypeIdTemp =  LayoutDescriberHelper.getDefaultRecordType(sobjectType);
            System.debug('recordTypeIdTemp = ' + recordTypeIdTemp);
            if(recordTypeIdTemp != System.Label.target_customer){
                data.put('recordTypeId',recordTypeIdTemp);
                recordTypeId = recordTypeIdTemp;
            } else {
                data.put('recordTypeId',null);
                recordTypeId = null;
            }
        }else {
            data.put('recordTypeId',recordTypeId);
        }
        //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);
            }
        }
            //编辑
         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);
                }
            }
 
                User loginUser = [Select Id, Name, ProfileId From User where Id = :Userinfo.getUserId()];
                Boolean isEdit = false;
                Rental_Apply__c rentalApplyData = (Rental_Apply__c)leadData;
                System.debug('rentalApplyData = ' + rentalApplyData);
                if(rentalApplyData.status__c != '草案中' 
                && rentalApplyData.status__c != '填写完毕' 
                && rentalApplyData.status__c != '申请中'
                && rentalApplyData.status__c != '申请中(OPD未通过)'   //20231224 sx add  备品智能化添加状态 优化
                && rentalApplyData.Add_Approval_Status__c != '草案中'
                && rentalApplyData.Add_Approval_Status__c != '填写完毕'
                && rentalApplyData.Add_Approval_Status__c != '申请中'
                && !System.Label.ConsumApplyApprove.contains(loginUser.ProfileId) //zhj  2023-08-07 remove hardcode
                ){
                    isEdit = true;
                    data.put('isEdit', isEdit);
                }
                
            data.put('layout', Json.serialize(reviseMetaLayouts(layout)));
            data.put('recordTypeId', recordTypeId);
            System.debug('data.recordTypeId: ' + data.get('recordTypeId'));
            //获取值
            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);
                }
            }
            System.debug('leadPIPL还原 layout = ' + JSON.serialize(layout));
            data.put('layout', Json.serialize(reviseMetaLayouts(layout)));
            System.debug('leadPIPLreviseMetaLayouts还原 layout = ' + data.get('layout'));
        }
        //deloitte-zhj 20231116 PIPL还原 end
        res.status = 'Success';
        res.code = 200;
        res.msg = '';
        return res;
    }
    
    @AuraEnabled
    public static Rental_Apply__c getOrignalRentalApply(String raName){
        List<Rental_Apply__c> RentalApplys = [SELECT id, AWS_Data_Id__c, Routine_Check_Status__c FROM Rental_Apply__c where Name =:raName LIMIT 1];
        if(RentalApplys.size() > 0){
            return RentalApplys[0];
        }else{
            return null;
        }
}
 
    /**
     *@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') {
                        continue;
                    }
                    col.layoutItems.add(item);
                }
            }
        }
        return result;
    }
 
    private static Boolean isUpdateable(Schema.DescribeFieldResult dfr) {
        return (new List<String>{ 'Id', 'Name' }).contains(dfr.getName()) || dfr.isUpdateable();
    }
       
}