高章伟
2022-02-18 8b5f4c6c281cfa548f92de52c8021e37aa81901e
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
/**
 * NFM201.trigger触发
 */
public without sharing class NFM201Controller {
    public static boolean isRunning = false;
    public static String debug_msg = '';
    public static String status;
 
    public class Endusers_element {
        public NFMUtil.Monitoring Monitoring;
        public Gedata[] GeData;
    }
 
    public class GeData {
        public String ManageCode;
        public String HospitalName;
        public String HospitalAlias;
        public String AttributeType;
        public String Grade;
        public String SpecialityType;
        public String GovermentKeyCustomer;
        public String State;
        public String City;
        public String Town;
        public String Telehone;
        public String Fax;
        public String ZIPCode;
        public String Address;
        public String Category;
        public String TermDate;
        public String PurposeOfAdvice;
        public String SectionCategory;
        public String SectionName;
        public String SectionCode;
        public Boolean NeedQuolified;
    }
 
    /**
     * NFM201の送信処理
     *
     * @param iflog_Id           ログテーブルのId
     * @param accIds             送信対象取引先
     * @param purposeOfAdviceMap 1:Delete 2:Add(Insert) 3:Change(Update)
     * @param needSendRectMap    送信対象のレコードタイプのmap
     */
    @future (callout=true)
    public static void callout(String iflog_Id, List<String> accIds, Map<String, String> purposeOfAdviceMap, Map<String, String> needSendRectMap, Map<String, String> needQuolifiedMap) {
        if (accIds == null || accIds.size() == 0) {
            return;
        }
 
        // MessageGroupNumber の取得
        List<BatchIF_Log__c> iflogList = [Select Id, Name, Log__c, ErrorLog__c from BatchIF_Log__c where Id = :iflog_Id];
        
        BatchIF_Log__c iflog = null;
        if (iflogList.size() > 0) {
            iflog = iflogList.get(0);
            iflog.ErrorLog__c = '';
        } else {
            // データ取れってないとは、rollbackされていることです
            return;
        }
        String logstr = iflog.Log__c + ' ' + 'NumberOfRecord=' + accIds.size() + '\n';
 
        // Monitoringの設定
        Datetime nowDT = Datetime.now();
        String nowStr = nowDT.format('yyyyMMddHHmm');
        Endusers_element endusers = new Endusers_element();
        endusers.Monitoring = new NFMUtil.Monitoring();
        endusers.Monitoring.Tag                  = 'MSGH';
        endusers.Monitoring.Sender               = 'SFDC';
        endusers.Monitoring.Receiver             = 'SPO';
        endusers.Monitoring.MessageType          = 'NFM201';
        endusers.Monitoring.MessageGroupNumber   = iflog.Name;
        endusers.Monitoring.NumberOfRecord       = '' + accIds.size();
        endusers.Monitoring.TransmissionDateTime = nowStr;
        endusers.Monitoring.Text = '';
 
        BatchIF_Log__c rowData = null;
        // BatchIF転送表 から、コード変換のMapを作成
        Map<String, String> transferMap = new Map<String, String>();
        List<BatchIF_Transfer__c> transferList = [select Table__c,
                                                         Column__c,
                                                         External_value__c,
                                                         Internal_value__c
                                                    from BatchIF_Transfer__c
                                                   where Dropped_Flag__c = false
                                                     and Table__c = 'Account'];
        for (BatchIF_Transfer__c t : transferList) {
            transferMap.put(t.Column__c + t.Internal_value__c, t.External_value__c);
        }
 
        try {
            // 転送データを取得、参照先のデータがあるので、ここで検索必要です。
            List<Account> accList = [select Id, RecordTypeId,
                                            Management_Code__c,
                                            Name,
                                            site,
                                            Alias_Name2__c,
                                            Attribute_Type__c,
                                            Grade__c,
                                            Speciality_Type__c,
                                            Goverment_key_customer__c,
                                            State_Text__c,
                                            City_Master__r.name,
                                            //City_Master__r.Level2_Sys_No__c,
                                            Town__c,
                                            Department_Name__c,
                                            Phone,
                                            Fax,
                                            Postal_Code__c,
                                            Street__c,
                                            OCM_Category__c,
                                            Ban_On_Use_Date__c,
                                            //If_Need_Quolified__c, 
                                            Department_Class__r.Department_Class_Label__c,
                                            Hospital__r.Name,
                                            Hospital__r.Management_Code__c,
                                            Hospital__r.Alias_Name2__c,
                                            Hospital__r.site,
                                            Hospital__r.Attribute_Type__c,
                                            Hospital__r.Grade__c,
                                            Hospital__r.Speciality_Type__c,
                                            Hospital__r.Goverment_key_customer__c,
                                            Hospital__r.State_Text__c,
                                            //Hospital__r.City_Master__r.Level2_Sys_No__c,
                                            Hospital__r.City_Master__r.name,
                                            Hospital__r.Town__c,
                                            Hospital__r.Postal_Code__c,
                                            Hospital__r.Street__c,
                                            Hospital__r.OCM_Category__c,
                                            //Hospital__r.If_Need_Quolified__c, 
                                            Hospital__r.Ban_On_Use_Date__c
                                       from Account where Id IN :accIds All ROWS];
            System.debug(Logginglevel.DEBUG, 'NFM201_ accList.size()=' + accList.size());
 
            // Enduserのデータの設定
            endusers.GeData = new List<GeData>();
            for (Account acc : accList) {
                GeData endUser = new GeData();
                endusers.GeData.add(endUser);
                if (needSendRectMap.get(acc.RecordTypeId) == '病院') {
                    endUser.ManageCode            = acc.Management_Code__c;
                    endUser.HospitalName          = acc.Name;
                    endUser.HospitalAlias         = ((acc.site == null) ? '' : acc.site + ';')  
                                                    + ((acc.Alias_Name2__c == null) ? '' : acc.Alias_Name2__c + ';');
                    endUser.AttributeType         = acc.Attribute_Type__c;
                    endUser.Grade                 = acc.Grade__c;
                    endUser.SpecialityType        = acc.Speciality_Type__c;
                    endUser.GovermentKeyCustomer  = acc.Goverment_key_customer__c;
                    endUser.State                 = acc.State_Text__c;
                    //endUser.City                  = acc.City_Master__r.Level2_Sys_No__c;
                    endUser.City                  = acc.City_Master__r.name;
                    endUser.Town                  = acc.Town__c;
                    endUser.ZIPCode               = acc.Postal_Code__c;
                    endUser.Address               = acc.Street__c;
                    endUser.Category              = NFMUtil.getMapValue(transferMap, 'OCM_Category__c', acc.OCM_Category__c, iflog);
                    endUser.TermDate              = NFMUtil.formatDate2StrSpo(acc.Ban_On_Use_Date__c);
                    endUser.PurposeOfAdvice       = purposeOfAdviceMap.get(acc.Id);
                    endUser.SectionCategory       = '';
                    endUser.SectionName           = acc.Name;
                    endUser.SectionCode           = acc.Management_Code__c;
                    //endUser.NeedQuolified         = acc.If_Need_Quolified__c;
                    if (needQuolifiedMap.containsKey(acc.Id)) {
                        endUser.NeedQuolified         = true;
                    } else {
                        endUser.NeedQuolified         = false;
                    }
                } else {
                    endUser.ManageCode            = acc.Hospital__r.Management_Code__c;
                    endUser.HospitalName          = acc.Hospital__r.Name;
                    //endUser.HospitalAlias         = acc.Hospital__r.site + ';' + acc.Hospital__r.Alias_Name2__c;
                    endUser.HospitalAlias         = ((acc.Hospital__r.site == null) ? '' : acc.Hospital__r.site + ';')  
                                                    + ((acc.Hospital__r.Alias_Name2__c == null) ? '' : acc.Hospital__r.Alias_Name2__c + ';');
                    endUser.AttributeType         = acc.Hospital__r.Attribute_Type__c;
                    endUser.Grade                 = acc.Hospital__r.Grade__c;
                    endUser.SpecialityType        = acc.Hospital__r.Speciality_Type__c;
                    endUser.GovermentKeyCustomer  = acc.Hospital__r.Goverment_key_customer__c;
                    endUser.State                 = acc.Hospital__r.State_Text__c;
                    //endUser.City                  = acc.Hospital__r.City_Master__r.Level2_Sys_No__c;
                    endUser.City                  = acc.Hospital__r.City_Master__r.Name;
                    endUser.Town                  = acc.Hospital__r.Town__c;
                    endUser.ZIPCode               = acc.Hospital__r.Postal_Code__c;
                    endUser.Address               = acc.Hospital__r.Street__c;
                    endUser.Category              = NFMUtil.getMapValue(transferMap, 'OCM_Category__c', acc.Hospital__r.OCM_Category__c, iflog);
                    endUser.TermDate              = NFMUtil.formatDate2StrSpo(acc.Hospital__r.Ban_On_Use_Date__c);
                    endUser.PurposeOfAdvice       = purposeOfAdviceMap.get(acc.Id);
                    endUser.SectionCategory       = NFMUtil.getMapValue(transferMap, 'Department_Class_Label__c', acc.Department_Class__r.Department_Class_Label__c, iflog);
                    //endUser.SectionName           = acc.Department_Name__c;
                    endUser.SectionName           = acc.name;
                    endUser.SectionCode           = acc.Management_Code__c;
                    //endUser.NeedQuolified         = acc.Hospital__r.If_Need_Quolified__c;
                    endUser.NeedQuolified         = false;
                }
                endUser.Telehone                  = acc.Phone;
                endUser.Fax                       = acc.Fax;
 
                logstr += endUser.SectionCode + '(' + endUser.PurposeOfAdvice + ')' + ' ';
            }
 
 
            if (endusers.GeData.size() > 0) {
 
                NFMUtil.Monitoring Monitoring   = new NFMUtil.Monitoring();
                Monitoring.Tag                  = endusers.Monitoring.Tag;
                Monitoring.Sender               = endusers.Monitoring.Sender;
                Monitoring.Receiver             = endusers.Monitoring.Receiver;
                Monitoring.MessageType          = endusers.Monitoring.MessageType;
                Monitoring.MessageGroupNumber   = endusers.Monitoring.MessageGroupNumber;
                Monitoring.NumberOfRecord       = endusers.Monitoring.NumberOfRecord;
                Monitoring.TransmissionDateTime = endusers.Monitoring.TransmissionDateTime;
                Monitoring.Text = '';
                rowData = NFMUtil.makeRowData(Monitoring, 'NFM201', endusers);
                execute(rowData, iflog);
 
            }
            logstr +='\nStatus:' + status;
            logstr += '\nend';
 
        } catch (Exception ex) {
            // エラーが発生した場合
            System.debug(Logginglevel.ERROR, 'NFM201_' + iflog.Name + ':' + ex.getMessage());
            System.debug(Logginglevel.ERROR, 'NFM201_' + iflog.Name + ':' + ex.getStackTraceString());
            logstr += ex.getMessage();
            iflog.ErrorLog__c += ex.getMessage() + '\n';
            iflog.ErrorLog__c += ex.getStackTraceString() + '\n';
        }
        if (rowData != null) {
            insert rowData;
        }
 
        iflog.Log__c = logstr;
        update iflog;
    }
 
    public static void execute(BatchIF_Log__c rowData, BatchIF_Log__c iflog) {
 
        Integer batch_retry_max_cnt = Integer.valueOf(System.Label.batch_retry_max_cnt);
        String rowDataStr = NFMUtil.getRowDataStr(rowData);
        Endusers_element endusers = (Endusers_element) JSON.deserialize(rowDataStr, Endusers_element.class);
 
        String logstr = endusers.Monitoring.MessageGroupNumber + ' start\n';
        Boolean needUpdateIflog = false;
        if  (iflog == null) {
            needUpdateIflog = true;
            iflog = new BatchIF_Log__c();
            iflog.Type__c = 'NFM201';
            iflog.MessageGroupNumber__c = endusers.Monitoring.MessageGroupNumber;
            iflog.Log__c = logstr;
            iflog.ErrorLog__c = '';
            //insert iflog;
            //iflog = [Select Id, Name, Log__c, ErrorLog__c from BatchIF_Log__c where Id = :iflog.Id];
        } else {
            //iflog = [Select Id, Name, Log__c, ErrorLog__c from BatchIF_Log__c where Id = :iflog.Id];
            logstr = iflog.Log__c;
        }
 
        try{
            // 向SPO送信
           // NFMUtil.sendToSpo(rowDataStr, NFMUtil.NFM201_ENDPOINT);
           status = NFMUtil.sendToSpoRet(rowDataStr, NFMUtil.NFM201_ENDPOINT);
           if (status == 'OK') {
                rowData.retry_cnt__c=0;
            } else {
                if (rowData.retry_cnt__c == null) rowData.retry_cnt__c = 0;
                if (rowData.retry_cnt__c < batch_retry_max_cnt) {
                    rowData.retry_cnt__c++;
                    LogAutoSendSchedule.assignOneMinute();
                }
                if (rowData.retry_cnt__c >= batch_retry_max_cnt) {
                    rowData.ErrorLog__c = status+'错误次数已经超过自动送信设定的最大次数,请手动送信';
                }
            }
 
        } catch (Exception ex) {
            // TODO IOException
            // エラーが発生した場合
            System.debug(Logginglevel.ERROR, 'NFM201_' + iflog.Name + ':' + ex.getMessage());
            System.debug(Logginglevel.ERROR, 'NFM201_' + iflog.Name + ':' + ex.getStackTraceString());
            logstr += ex.getMessage();
            iflog.ErrorLog__c += ex.getMessage() + '\n';
            iflog.ErrorLog__c += ex.getStackTraceString() + '\n';
 
            if (rowData.retry_cnt__c == null) rowData.retry_cnt__c = 0;
            if (rowData.retry_cnt__c < batch_retry_max_cnt) {
                rowData.retry_cnt__c++;
                LogAutoSendSchedule.assignOneMinute();
            }
            if (rowData.retry_cnt__c >= batch_retry_max_cnt) {
                rowData.ErrorLog__c = ex.getMessage() + '\n' + ex.getStackTraceString() + '\n' + rowData.ErrorLog__c+'错误次数已经超过自动送信设定的最大次数,请手动送信';
            }
        }
        logstr +='\nStatus:' + status;
        iflog.Log__c = logstr;
 
        if (needUpdateIflog) {
            upsert iflog;
            update rowData;
        }
    }
}