Li Jun
2022-04-06 fb04e7c01d119c60632b4298d18fd93f3ccb3d79
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
global with sharing class NFM702Controller {
 
    private static final String LOG_TYPE = 'NFM702';
 
    public static String logstr;
    public static String status;   
    public static String ResponseBody;
    public static String Result;
 
    public NFM702Controller() {
 
    }
 
    public class Response {
        public Result Result;
    }
 
    public Class Result{
        public String Result;
        public String Message;
    }
 
    public class NFM702 {
        public NFMUtil.Monitoring Monitoring;
        public GeData[] GeData;
    }
 
    // public Class GeDatas {
    //     public NFMUtil.Monitoring Monitoring;
    //     public GeData[] GeData;
    // }
 
    public Class GeData {
        public String StaffMCode;   //人员管理编码
        public String HospitalName;     //医院名称
        public String OfficeName;   //科室名称
        public String Name;    //姓名
        public String Mobile;   //电话
        public String HcpNo;    //HPC编码
        public Boolean IsActive;    //是否有效
        public String UpdateStatus;     //操作类型
        // add by sushanhu 20220304 for pi satrt
        public String DataId;           //aws 存储凭据 
        // add by sushanhu 20220304 for pi end
    }
 
    @future(callout = true)
    public static void callout( String iflog_Id, List < String > accIdList ){
        executeNotFuture(iflog_Id, accIdList);
    }
 
    public static void executeNotFuture(String iflog_Id, List < String > accIdList) {
        
        Datetime nowDT = Datetime.now();
        String nowStr = nowDT.format('yyyyMMddHHmmss');
        BatchIF_Log__c iflog = new BatchIF_Log__c();
        if (string.isNotBlank(iflog_Id)) {
            iflog = [Select Id, Name, Log__c, ErrorLog__c,MessageGroupNumber__c from BatchIF_Log__c where Id =: iflog_Id];
        }
        iflog.MessageGroupNumber__c = String.isBlank(iflog.Id)?nowStr:iflog.MessageGroupNumber__c;
        iflog.Log__c = 'callout start\n';
        iflog.Type__c = LOG_TYPE;
        iflog.ErrorLog__c = '';
 
        NFM702 gds = new NFM702();
        gds.GeData = new List < GeData >();
 
        BatchIF_Log__c rowData = null;
 
        List<Contact> conList = [select Id,
                                        CManageCode__c,     //人员管理编码
                                        Account.Hospital_Name__c,       //医院名称
                                        Account.Department_Name__c,       //科室名称
                                        Name,       //姓名
                                        Phone,      //电话
                                        HcpNo__c,       //HCP编码
                                        Isactive__c,    //是否有效
                                        // UpdateStatus__c,            //操作类型
                                        AWS_Data_Id__c              //aws 存储凭据 add bysushanhu 20220304 for Pi
                                        from Contact WHERE Id IN:accIdList order by Id];  //AccountId
        String logstr = iflog.Log__c + ' ' + 'NumberOfRecord=' + conList.size() + '\n';
 
        NFMUtil.Monitoring mon = new NFMUtil.Monitoring();
        mon.Tag                = 'MSGH';
        mon.Sender                  = 'SFDC';
        mon.Receiver              = 'OBPM';
        mon.MessageType          = 'NFM702';
        mon.MessageGroupNumber      = nowStr;
        mon.NumberOfRecord          = '' + accIdList.size();
        mon.TransmissionDateTime = nowStr;
        mon.Text = '';
 
 
        try{
            for( Contact con : conList ){
                GeData ged = new GeData();
                ged.StaffMCode = con.CManageCode__c;
                ged.HospitalName = con.Account.Hospital_Name__c;
                ged.OfficeName = con.Account.Department_Name__c;
                ged.Name = con.Name;
                ged.Mobile = con.Phone;
                ged.HcpNo = con.HcpNo__c;
                ged.IsActive = '有效'.equals(con.Isactive__c)? true:false;
                // ged.UpdateStatus = con.UpdateStatus__c;
                ged.UpdateStatus = String.isBlank(con.HcpNo__c) ? 'I' : 'U';
                ged.DataId  =   con.AWS_Data_Id__c;//add by sushanhu  for pi 20220304
                gds.GeData.add(ged);
            }
 
            logstr += '\nend';
 
            if(gds.GeData.size()>0){
 
                mon.NumberOfRecord = '' + gds.GeData.size();
                gds.Monitoring = mon;
 
                logstr = iflog.Log__c + '\nNumberOfRecord=' + gds.GeData.size();
                NFMUtil.Monitoring Monitoring = new NFMUtil.Monitoring();
                Monitoring.Tag                  = gds.Monitoring.Tag;
                Monitoring.Sender               = gds.Monitoring.Sender;
                Monitoring.Receiver             = gds.Monitoring.Receiver;
                Monitoring.MessageGroupNumber   = gds.Monitoring.MessageGroupNumber;
                Monitoring.MessageType          = gds.Monitoring.MessageType;
                Monitoring.NumberOfRecord       = gds.Monitoring.NumberOfRecord;
                Monitoring.TransmissionDateTime = gds.Monitoring.TransmissionDateTime;
                Monitoring.Text = '';
 
                NFM702 nfm702 = new NFM702();
                nfm702.GeData = new List <GeData>();
                nfm702.GeData = gds.GeData;
                nfm702.Monitoring = new NFMUtil.Monitoring();
                nfm702.Monitoring = Monitoring;
 
                rowData = NFMUtil.makeRowData(iflog, LOG_TYPE, nfm702);
                execute(rowData, iflog);
            }
 
        }catch(Exception e){
            //发生错误时
            System.debug(Logginglevel.ERROR, LOG_TYPE + iflog.Name + ':' + e.getMessage());
            System.debug(Logginglevel.ERROR, LOG_TYPE + iflog.Name + ':' + e.getStackTraceString());
            logstr += e.getMessage();
            iflog.ErrorLog__c += e.getMessage() + '\n';
            iflog.ErrorLog__c += e.getStackTraceString() + '\n';
            iflog.Log__c = iflog.Log__c + logstr;
        }
 
        if (rowData != null) {
            upsert rowData;
        }
 
        System.debug(Logginglevel.DEBUG, 'NFM702_' + iflog.Name + ' end');
        iflog.Log__c = iflog.Log__c + logstr;
 
        upsert iflog;
    }
 
    public static void ManualExecute(String rowDataId) {
        List < BatchIF_Log__c > row = [select id, name, MessageGroupNumber__c, retry_cnt__c,
            RowDataFlg__c, ErrorLog__c, Type__c,
            Log__c, Log2__c, Log3__c, Log4__c, Log5__c, Log6__c,
            Log7__c, Log8__c, Log9__c, Log10__c, Log11__c, Log12__c
            from BatchIF_Log__c
            where id =: rowDataId
        ];
        if (row.size() > 0) execute(row[0], null);
    }
 
    public static void execute(BatchIF_Log__c rowDataSFDC, BatchIF_Log__c iflog) {
        Integer batch_retry_max_cnt = Integer.valueOf(System.Label.batch_retry_max_cnt);
        String rowDataStr = NFMUtil.getRowDataStr(rowDataSFDC);
        String logstr = rowDataSFDC.MessageGroupNumber__c + ' start\n';
 
        if (iflog == null) {
            iflog = new BatchIF_Log__c();
            iflog.Type__c = LOG_TYPE;
            iflog.MessageGroupNumber__c = rowDataSFDC.MessageGroupNumber__c;
            iflog.Log__c = logstr;
            iflog.ErrorLog__c = '';
        } else {
            iflog.Type__c = LOG_TYPE;
            iflog.MessageGroupNumber__c = rowDataSFDC.MessageGroupNumber__c;
            logstr = iflog.Log__c;
        }
        try {
 
            // NFMUtil.response response = NFMUtil.sendToSapStatusAndBody(rowDataStr, NFMUtil.NFM702_ENDPOINT);
            // System.debug('NFM702Log--status->'+ status);
 
            // status = response.status;
            // ResponseBody = response.responseBody;
            // Response resultList = ( Response ) JSON.deserializeStrict(ResponseBody, Response.class);
            // System.debug('NFM702Log--status->'+ ResponseBody);
 
            // Result = resultList.Result.Result;
 
            // if ('00'.equals(Result)) {
            //     logstr += ResponseBody+'\n';
            //     rowDataSFDC.retry_cnt__c = 0;
            //     //iflog.Log__c = 'ok';
            // } 
            // else {
            //     //iflog.Log__c = 'ok2';
            //     rowDataSFDC = NFMUtil.LogAutoSend(rowDataSFDC, null, status);
            // }
            //update to AWS sushanhu FOR PI start 20220304 
            PIHelper.PIIntegration pi =PIHelper.getPIIntegrationInfo('NFM702');
            NFMUtil.response response = NFMUtil.sendToPiAWS(rowDataStr, pi.newUrl,pi.token);
            
            status = response.status;
            System.debug('NFM702Log--status->'+ status);
           
            ResponseBody = response.responseBody;
            System.debug('NFM702Log--responsebody->'+ ResponseBody);
            if ('200'.equals(status)) {
                //aws 正确响应
                // Response resultList = ( Response ) JSON.deserializeStrict(ResponseBody, Response.class);
                Map<String, Object> result = (Map<String, Object>)JSON.deserializeUntyped(ResponseBody);
                String statusCode =(String)result.get('status');
    
                if ('0'.equals(statusCode)) {
                    logstr += ResponseBody+'\n';
                    rowDataSFDC.retry_cnt__c = 0;
                    //iflog.Log__c = 'ok';
                } else {
                    //iflog.Log__c = 'ok2';
                rowDataSFDC = NFMUtil.LogAutoSend(rowDataSFDC, null, status);
                }
            }
 
            else {
                
                logstr=ResponseBody;
                iflog.ErrorLog__c=ResponseBody;
            }
            //update to AWS sushanhu FOR PI  20220304 end
        }catch ( Exception ex ) {
           //打印错误日志
           System.debug(Logginglevel.ERROR, LOG_TYPE + iflog.Name + ':' + ex.getMessage());
           System.debug(Logginglevel.ERROR, LOG_TYPE + iflog.Name + ':' + ex.getStackTraceString());
 
           logstr = ex.getMessage();
           iflog.ErrorLog__c += ex.getMessage() + '\n';
           iflog.ErrorLog__c += ex.getStackTraceString() + '\n';
           if(!Test.isRunningTest()){
                //rowDataSFDC = NFMUtil.LogAutoSend(rowDataSFDC, null, status);
           }
        }
 
        iflog.Log__c = iflog.Log__c + logstr;
        upsert iflog;
        upsert rowDataSFDC;
    }
}