Li Jun
2022-03-28 f5a94e721ae5a26f817f0df75065b64f1f192eb3
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
/**
 * 调用地址:
 * ContactTriggerHandler.sendToComPlat 直接调用 接口
 */
public without sharing class NFM606Controller {
 
    public static String logstr;
    public static String status;  
    //add aws respnse sushanhu 20220228 start
    public static String responseBody;  
    //add aws respnse sushanhu 20220228 end
    private static final String LOG_TYPE = 'NFM606';
    private static final String API = '/admin/api/user/save';
    public class NFM606 {
        public GeDatas GeDatas;
    }
    public Class GeDatas {
        public NFMUtil.MonitoringToComPlat Monitoring;
        public GeData[] GeData;
 
    }
    public Class GeData {
        public String ContactId; //智慧医疗ID
        public String ServiceUserId; //服务平台用户ID
        public String Mobile; //手机号码
        public String PersonManagementCode; //人员管理编码
        public Boolean AgentFlag; //经销商标识 True:是经销商;False:是医院
        public String State; //省
        public String City; //市
        public String SalesBusinessDivision; //销售本部
        public Boolean Status; //状态
        public String Hospital; //医院编码
        public String Department; //科室编码
        public String Name; //姓名
        public String Account; //经销商名称
        public String AgentCode; //经销商编码
        public String AgentUserType; //人员类型
        public String Email; //电子邮件
        //add aws pi start sushanhu 20220228
        public String DataId;//aws 存储凭据
        //add aws pi end sushanhu 20220228
 
    }
 
    @future(callout = true)
    public static void callout(String iflog_Id, List < String > conIdList) {
        executeNotFuture(iflog_Id, conIdList);
    }
    public static void executeNotFuture(String iflog_Id, List < String > conIdList) {
        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 = '';
 
        NFMUtil.MonitoringToComPlat me = new NFMUtil.MonitoringToComPlat();
        me.Tag                  = 'MSGH';
        me.Sender               = 'SFDC';
        me.Receiver             = '智慧医疗/服务新系统';
        me.MessageType          = LOG_TYPE;
        me.MessageGroupNumber   = nowStr;
        if (String.isNotBlank(iflog.Name)) {
            me.MessageGroupNumber = iflog.Name;
        }
        me.TransmissionDateTime = nowStr;
        me.Text = '';
        String randomstr = NFMUtil.randomUUID(16);
        Long timestamp = DateTime.now().getTime();
        String timestampStr = String.valueOf(timestamp);
        String getToken = NFMUtil.getToken(randomstr, timestamp);
        me.API_RANDOM_STR = randomstr;
        me.API_TIME = timestampStr;
        me.API_TOKEN = getToken;
 
 
        
        GeDatas gds = new GeDatas();
        gds.GeData = new List < GeData > ();
        BatchIF_Log__c rowData = null;
 
        // 排除 记录类型为 社内员工的联系人
        List < Contact > conList = [select id, UnifiedI_Contact_ID__c, MobilePhone, 
            Salesdepartment_Text__c,
            Account.Management_Code__c,
            Account.Hospital__r.State_Master__r.Name,
            Account.Hospital__r.City_Master__r.Name,
            Account.Hospital__r.State_Master__r.Level1_Code__c,
            Account.Hospital__r.City_Master__r.Level2_Code__c,
            Isactive__c,
            ContactType__c,
            Account.Name,
            CManageCode__c,
            Account.Salesdepartment_Dept__c,
            Account.Hospital__r.Salesdepartment_Dept__c,
            Account.Hospital_Department_Class__r.Salesdepartment_Dept__c,
            Agency_User__c,
            RecordTypeId,
            ServicePlatformCode__c,
            Account.Parent_Management_Code__c,
            Account.State_Master__r.Level1_Code__c,
            Account.City_Master__r.Level2_Code__c,
            Account.Hospital_Department_Class__r.State_Master__r.Level1_Code__c,
            Account.Hospital_Department_Class__r.City_Master__r.Level2_Code__c,
            Account.ParentId,
            Account.Parent.ParentId,
            UniqueNumber__c,
            AWS_Data_Id__c,
            Name, Email from Contact where id in: conIdList AND RecordTypeId != '01210000000QtkyAAC'
        ];
        String logstr = iflog.Log__c + ' ' + 'NumberOfRecord=' + conList.size() + '\n';
        try {
            for (Contact con: conList) {
                String flag = '';
                GeData ged = new GeData();
                ged.ContactId = String.isBlank(con.UnifiedI_Contact_ID__c) ? '':con.UnifiedI_Contact_ID__c; //智慧医疗ID
                //update to AWS_UnifiedI_Contact_ID__c sushanhu 20220228 start
                //ged.ContactId = String.isBlank(con.) ? '':con.AWS_UnifiedI_Contact_ID__c; //AWS 智慧医疗ID
                ////update to AWS_UnifiedI_Contact_ID__c sushanhu 20220228 end
                ged.ServiceUserId = String.isBlank(con.ServicePlatformCode__c) ? '':con.ServicePlatformCode__c; //服务平台用户ID???
                ged.Mobile = String.isNotBlank(con.UniqueNumber__c) ? con.UniqueNumber__c:con.MobilePhone;//手机
                ged.PersonManagementCode = con.CManageCode__c; //人员管理编码
                ged.Status = '有效'.equals(con.Isactive__c) ? true:false ;//状态
                ged.Name = con.Name;//姓名
                ged.Email = '';
                ged.Hospital =''; //医院编码
                ged.Department = ''; //科室编码
                ged.Account = '';//经销商名
                ged.AgentCode = '';//经销商编码
                ged.AgentUserType =''; //人员类型
                ged.AgentFlag = false;//经销商标识
                if (!ged.Status) {
                    ged.Mobile = con.MobilePhone;
                }
                // 医师
                if ('01210000000QfWdAAK'.equals(con.RecordTypeId)) {
                    ged.Hospital = con.Account.Parent_Management_Code__c;//医院编码
                    ged.Department = con.Account.Management_Code__c; //科室编码
                    ged.AgentFlag = false;//医院
 
                }
                // 经销商
                if ('01210000000QfWiAAK'.equals(con.RecordTypeId)) {
                    //ged.Name = con.Name;//姓名
                    ged.Account = con.Account.Name;//经销商名
                    ged.AgentCode = con.Account.Management_Code__c;//经销商编码
                    ged.AgentUserType = String.isBlank(con.ContactType__c) ? '': con.ContactType__c.replaceAll(';',',');//人员类型
                    ged.AgentFlag = true;//经销商
                    ged.Email = con.Email;
                }
                ged.DataId =con.AWS_Data_Id__c;//add aws 存储凭据
                ged = getStateAndCity(ged,con);
                gds.GeData.add(ged);
 
            }
            logstr += '\nend';
            if (gds.GeData.size() > 0) {
                me.NumberOfRecord       = '' + gds.GeData.size();
                gds.Monitoring = me;
 
 
                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.MessageType          = gds.Monitoring.MessageType;
                Monitoring.MessageGroupNumber   = gds.Monitoring.MessageGroupNumber;
                Monitoring.NumberOfRecord       = gds.Monitoring.NumberOfRecord;
                Monitoring.TransmissionDateTime = gds.Monitoring.TransmissionDateTime;
                Monitoring.Text = '';
 
 
                NFM606 nfm606 = new NFM606();
                nfm606.GeDatas = new GeDatas();
                nfm606.GeDatas = gds;
 
                rowData = NFMUtil.makeRowData(Monitoring, LOG_TYPE, nfm606);
                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;
        }
        iflog.Log__c = iflog.Log__c + logstr;
        upsert iflog;
 
    }
 
    private static GeData getStateAndCity(GeData ged,Contact con){
        String state = '';
        String city = '';
        String salesdepartment_Dept = '';
        if (String.isBlank(con.Account.ParentId)) {//医院/经销商下的联系人,获取医院/经销商的省和市的编码;销售本部
            state =  con.Account.State_Master__r.Level1_Code__c;
            city =  con.Account.City_Master__r.Level2_Code__c;
            salesdepartment_Dept =  con.Account.Salesdepartment_Dept__c; //销售本部             
        } else if(String.isNotBlank(con.Account.Parent.ParentId)){//科室下的联系人, 获取医院的省和市的编码;销售本部
            state = con.Account.Hospital__r.State_Master__r.Level1_Code__c;//省
            city = con.Account.Hospital__r.City_Master__r.Level2_Code__c;//市
            salesdepartment_Dept = con.Account.Hospital__r.Salesdepartment_Dept__c;//销售本部
        } else {//战略科室下的联系人,获取医院的省和市的编码;销售本部
            state = con.Account.Hospital_Department_Class__r.State_Master__r.Level1_Code__c;//省
            city = con.Account.Hospital_Department_Class__r.City_Master__r.Level2_Code__c;//市
            salesdepartment_Dept = con.Account.Hospital_Department_Class__r.Salesdepartment_Dept__c;//销售本部
        }
        ged.State = state;
        ged.City = city;
        ged.SalesBusinessDivision = salesdepartment_Dept;
 
        return ged;
    }
 
 
    /**
     * 接口发送失败 根据BatchLogId进行手动发送
     * @Author   XHL
     * @DateTime 2021-07-22
     * @param    rowDataId  [BatchLogId]
     */
    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);
    }
 
    /**
     * 逻辑正常执行发送
     * @Author   XHL
     * @DateTime 2021-07-22
     * @param    rowDataSFDC [BatchLogId]
     * @param    iflog       [执行情况记录]
     */
    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 {
            
            
            // // 发往PO 
            // status = NFMUtil.sendToSapRet(rowDataStr, NFMUtil.NFM606_ENDPOINT);
            // System.debug('NFM606Log--status->'+ status);
            // // status = ''
            // if ('Accepted'.equals(status)) {
            //     logstr += status+'\n';
            //     rowDataSFDC.retry_cnt__c = 0;
            // } 
            // else {
            //     rowDataSFDC = NFMUtil.LogAutoSend(rowDataSFDC, null, status);
            // }
            //update to aws 20220228 sushanhu satrt 
            PIHelper.PIIntegration pi =PIHelper.getPIIntegrationInfo('NFM606');
            NFMUtil.response result =NFMUtil.sendToPiAWS(rowDataStr,pi.newUrl,pi.token);
            system.debug('aws result---'+result);
            responseBody=result.responseBody;
            //Map<String, Object> res = (Map<String, Object>)JSON.deserializeUntyped(responseBody);
            //status=(String)res.get(status);
            status =result.status;
            system.debug('stadtucode--'+result.status);
            if ('202'.equals(status)) {
                logstr += status+'\n';
                     rowDataSFDC.retry_cnt__c = 0;
            }
            else {
                logstr+=responseBody+'\n';
                rowDataSFDC = NFMUtil.LogAutoSend(rowDataSFDC, null, status);
            }
            //update to aws 20220228 sushanhu end
        } catch (Exception ex) {
            // TODO IOException
             // 异常重发
            // Callout from triggers are currently not supported.
            if (!String.valueOf(ex.getMessage()).contains('Callout from triggers')) {
                logstr += ex.getMessage();
                iflog.ErrorLog__c += ex.getMessage() + '\n';
                iflog.ErrorLog__c += ex.getStackTraceString() + '\n';
            }
            rowDataSFDC = NFMUtil.LogAutoSend(rowDataSFDC, null, status);
        }
        iflog.Log__c = iflog.Log__c + logstr;
        upsert iflog;
        upsert rowDataSFDC;
    }
}