111
沙世明
2022-11-22 928399eceec50e3d37ea08669a12789a9410a9d2
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
public without sharing class NewAgencyOpportunityController {
    
    static string sobjectType = 'Agency_Opportunity__c';
 
    @AuraEnabled
    public static ControllerResponse Init(string rid, String pid, string record_type_id){
        system.debug('rid='+rid+',length='+(rid==null?'null':rid.length()+''));
        system.debug('record_type_id='+record_type_id+',length='+(record_type_id==null?'null':record_type_id.length()+''));
        
        ControllerResponse res = new ControllerResponse();
        Map<string,object> data = new Map<string,object>();
        res.Data = data;
        
        Agency_Opportunity__c ao = null;
        List<Metadata.LayoutSection> layout = null;
        if(string.isBlank(rid)){
            
            layout = MetaDataUtility.GetRecordTypePageLayout(record_type_id, sobjectType);
            data.put('layout', Json.serialize(layout));
            
        }else{
            ao = [select RecordTypeId from Agency_Opportunity__c where id = :rid];
            if(ao == null){
                res.Message = 'id不存在';
                return res;
            }
            record_type_id = ao.RecordTypeId;
            system.debug('record_type_id is fresh ='+ao.RecordTypeId);
            
            layout = MetaDataUtility.GetRecordTypePageLayout(record_type_id, sobjectType);
            data.put('layout', Json.serialize(layout));
            
            List<String> fieldApiList = new List<String>(); 
            /*
            for (LayoutDescriberHelper.LayoutSection ls : layout.layoutSections) {
                for (LayoutDescriberHelper.LayoutField lf : ls.layoutFields) {
                    if (lf.fieldAPI != '') {
                        System.debug('lf.fieldAPI='+lf.fieldAPI+' fieldType='+lf.fieldType);
                        fieldApiList.add(lf.fieldAPI);
                    }
                }
            }
            */
            for( Metadata.LayoutSection s: layout){
               system.debug('s = ' + s);
                for( Metadata.LayoutColumn c: s.layoutColumns){
                   system.debug(c);
                    if(c.layoutItems != null){
                        for( Metadata.layoutItem item: c.layoutItems){
                           system.debug(item);
                            fieldApiList.add(item.field);
                        }
                    }
                    
                }
            }
            system.debug('layout = ' + layout);
            system.debug(fieldApiList);
            ao = database.query(SoqlHelper.DistinctQueryFields('select id, Agency_Contact__r.AWS_Data_Id__c, ' + string.join(fieldApiList, ',') + ' from ' + sobjectType + ' where id = :rid'));
            System.debug('ao: ' + ao);
            System.debug('ao.Agency_Contact__r.AWS_Data_Id__c' + ao.Agency_Contact__r.AWS_Data_Id__c);
            
            data.put('data', ao);
        }
        if(!string.isBlank(pid) && !pid.contains('__c')){
            Id parentId = pid;
            data.put('pidType', parentId.getSObjectType().getDescribe().getName());
        }
        data.put('fields', SObjectHelper.GetFieldInfos(sobjectType));
        data.put('staticResource', Json.serialize(PIHelper.getPIIntegrationInfo('Agency_Contact__c')));
        res.IsSuccess = true;
        return res;
    }
 
    @AuraEnabled
    public static ControllerResponse Save(Map<string,object> data,string transId,String recordTypeId){
        system.debug('data='+data);
        system.debug(!data.containsKey('Id') );
        system.debug( data.get('Id') == null);
        //NewAndEditBaseController.Response response = NewAndEditBaseController.save(new Agency_Contact__c(),Json.serialize(data),transId, !data.containsKey('Id') || data.get('Id') == null );
        //ControllerResponse r = new ControllerResponse();
        
        Sobject sobj = new Agency_Opportunity__c();
        ControllerResponse r = SaveCore(sobj, data, transId,recordTypeId);
        if (r.IsSuccess) {
            r.Data = new Map<string,object>{
                'recordId'=> sobj.Id
            };
        }
        return r;
    }
    
 
    public static ControllerResponse SaveCore(Sobject sobj, Map<string,object> data,string transId,String recordTypeId ) {
        Integer index = 0;
        string sobjectTypeValue = sobj.getSObjectType().getDescribe().getName();
        System.debug('sobjectTypeValue:'+sobjectTypeValue+' Info:' + JSON.serialize(data));
        
        //1. Prepare the payload for  opportunity
        Map<String, Schema.SObjectField> fieldAPIToTypeMap = SobjectHelper.GetFieldMap(sobjectTypeValue);
        System.debug('fieldAPIToTypeMap = ' + fieldAPIToTypeMap);
        ControllerResponse r = new ControllerResponse();
        
        //2. Save Record Process
        Savepoint sp = Database.setSavepoint();
        try{
            for(string fieldAPI : fieldAPIToTypeMap.keySet()){
                if(data.containsKey(fieldAPI)){
                    Schema.DisplayType fielddataType = fieldAPIToTypeMap.get(fieldAPI).getDescribe().getType();  
                    String fieldValue = String.valueOf(data.get(fieldAPI)); 
                    if(String.valueOf(fielddataType)=='DATE'){
                        sobj.put(fieldAPI,(String.isBlank(fieldValue)||String.isEmpty(fieldValue))? null:Date.valueOf(fieldValue.replace('/', '-')));
                    }else if(String.valueOf(fielddataType)=='DATETIME'){
                        if(String.isNotBlank(fieldValue)&&fieldValue.contains('T')){
                            fieldValue = fieldValue.replace('T',' ');
                            sobj.put(fieldAPI, Datetime.valueOfGmt(fieldValue));
                        }else if(String.isNotBlank(fieldValue))  {
                            fieldValue = fieldValue.replace('/', '-') + ':00';
                            sobj.put(fieldAPI, Datetime.valueOf(fieldValue));
                        }else{
                            sobj.put(fieldAPI, null);
                        }
                    }else if(String.valueof(fielddataType)=='CURRENCY'|| String.valueof(fielddataType)=='PERCENT'||String.valueOf(fielddataType)=='Number'||String.valueOf(fielddataType)=='DOUBLE' ){
                        sobj.put(fieldAPI, (String.isBlank(fieldValue)||String.isEmpty(fieldValue))?null:Decimal.valueOf(fieldValue.replace(',', ''))); 
                    }else {
                        sobj.put(fieldAPI, fieldValue);
                    }
                }
            }
            if (recordTypeId != null) {
                sobj.put('RecordTypeId',recordTypeId);
            }
            System.debug('sobj = ' + sobj);
            if(!Test.isRunningTest()){
                upsert sobj;
            }
            //System.debug('respzhj = ' + resp);
            r.IsSuccess = true;
            return r;
 
        }catch(DmlException e) {
            
            System.debug(e.getNumDml());
            System.debug(e.getDmlFields(index));
            System.debug(e.getDmlId(index));
            System.debug(e.getDmlIndex(index));
            System.debug(e.getDmlMessage(index));
            System.debug(e.getDmlStatusCode(index));
            System.debug(e.getDmlType(index));
            system.debug(e.getMessage());
            system.debug(e.getStackTraceString());
 
            System.debug('into catch'+e.getMessage());
            Database.rollback(sp);
            r.IsSuccess = false;
            r.message ='保存失败,原因:'+ e.getDmlMessage(index);
            PIHelper.saveTransLog(sobjectTypeValue,'awsDataId',sobj.Id,transId, Json.serialize(data) ,'failed',r.message);
            return r;
            
        }catch(Exception e) {
            System.debug('into catch'+e.getMessage());
            Database.rollback(sp);
            r.IsSuccess = false;
            r.message = e.getMessage()+e.getStackTraceString();
            PIHelper.saveTransLog(sobjectTypeValue,'awsDataId',sobj.Id,transId, Json.serialize(data) ,'failed',r.message);
            return r;
        }
    }
    @AuraEnabled
    public static ControllerResponse getAWSDataIds(String agencyHospitalId, String accountId){
        system.debug('agencyHospitalId = ' + agencyHospitalId + 'accountId = ' + accountId);
        ControllerResponse r = new ControllerResponse();
        List<String> conAWSIds = new List<String>();
        List<Agency_Contact__c> agencyContact = [select Id, AWS_Data_Id__c from Agency_Contact__c  where (Department_Class__r.ParentId =:accountId or Agency_Hospital__c =:agencyHospitalId)  and AWS_Data_Id__c!='']; 
        System.debug('agencyContact = ' + Json.serialize(agencyContact));
        for(Agency_Contact__c ac : agencyContact){
            conAWSIds.add(ac.AWS_Data_Id__c);
        }
        r.Data = conAWSIds;
        r.IsSuccess = true;
        return r;
    }
 
    @AuraEnabled
    public static ControllerResponse searchAgencyContacts(String awsAgencyContactIds,String agencyHospitalId,String accountId){
        system.debug('awsAgencyContactIds = ' + awsAgencyContactIds + 'agencyHospitalId = ' + agencyHospitalId + 'accountId = ' + accountId);
        ControllerResponse resp = new ControllerResponse();
        resp.IsSuccess = false;
        Map<String,Agency_Contact__c> awsIdToContactMapTemp = new Map<String,Agency_Contact__c>();
        if(!checkNullString(awsAgencyContactIds)){
            List<String> awsDataIds = (List<String>) JSON.deserialize(awsAgencyContactIds, List<String>.class);
            System.debug('awsDataIds = ' + awsDataIds.size());
            List<Agency_Contact__c> conListTemp = new List<Agency_Contact__c>();
            String accId = '';
            String accHospitalId = '';
            if(!checkNullString(agencyHospitalId) || !checkNullString(accountId)){
                List<Agency_Hospital_Link__c> actList = [select id from Agency_Hospital_Link__c where id = :agencyHospitalId];
                List<Account> accList = [select Id, parentId from Account where id = :accountId];
                if (actList.size() > 0) {
                    accHospitalId = actList[0].Id;
                }
                if (accList.size() > 0) {
                    accId = accList[0].Id;
                }
                conListTemp = new List<Agency_Contact__c>([select Id,AWS_Data_Id__c, Agency_Hospital__r.Name, Department_Class__r.Parent.Name from Agency_Contact__c  where (Department_Class__r.ParentId =:accId or Agency_Hospital__c =: accHospitalId) and AWS_Data_Id__c in:awsDataIds]);
            }else {
                conListTemp = new List<Agency_Contact__c>([select Id,AWS_Data_Id__c, Agency_Hospital__r.Name, Department_Class__r.Name from Agency_Contact__c where AWS_Data_Id__c in:awsDataIds]);
            }
            for(Agency_Contact__c con:conListTemp){
                awsIdToContactMapTemp.put(con.AWS_Data_Id__c,con);
            }
        }
        System.debug('awsIdToContactMapTemp = ' + awsIdToContactMapTemp);
        if(awsIdToContactMapTemp.keySet().size()>0){
            resp.IsSuccess = true;
            resp.Data = JSON.serialize(awsIdToContactMapTemp);// PI contact info
        }
        return resp;
    }
 
    public static Boolean checkNullString(String inputString){
        if(String.isEmpty(inputString)||String.isBlank(inputString)){
            return true;
        }
        return false;
    }
}