GWY
2022-04-06 7560140a14a60e949e6130d98225297e84f0a198
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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
@RestResource(urlMapping = '/SBG204/*')
global with sharing class SBG204Rest {
 
    global class GeDatas {
        public NFMUtil.Monitoring Monitoring;
        public GeData[] GeData;
    }
 
    global class GeData {
 
        public String MaterialNo;         //物料号
 
        public String RegisterNo;         //注册证号
        public String RegisterNoClass_New; //注册证号经营范围编号(新)
        public String RegisterNoClass_Old; //注册证号经营范围编号(旧)
 
        public String ValidFrom;          //注册证号效期从
        public String ValidTo;            //注册证号效期至
        public String MedPrdClass;        //医疗器械分类
        public String EffectDate;         //系统维护日
 
        public String Model;              //产品型号
        public String ProductName;        //产品名称(委托产品名称)
        public String PrdCompanyLicense;  //生产企业许可证号/备案凭证号
        public String PrdCompanyAddr;     //生产企业地址
        public String ClinicalCode;       //型号规格
        public String REG_Name;           // 注册人名称
        public String REG_ADD;            // 注册人住所
 
 
 
        public String AuthorizedCompany;  //委托方企业名称
 
        public String ApprovedDate;       //委托批准日期
        public String Z3PLAuthorized;    //是否3PL委托产品
        public String AuthorizedCompany_SH;
        public String ApprovedDate_SH;
        public String YXQX;
 
        public String Other1;
        public String Other2;
        public String Other3;
 
        public String MaterialGroup;      //物料组
        public String MaterialStatus;     //物料状态
        public String RegisterNoStatus;   //注册证号状态
 
        public String Unit;               //单位(基本单位)
        public String StroageCondition;   //储存条件
        public String TransCondition;     //运输条件
        public String Z3PLAuthorizedNo;   //3PL业务委托协议号
        public String PrdValidFrom;       //委托产品效期从
        public String PrdValidTo;         //委托产品效期至
        public String BusinessScope;      //经营范围
 
        public String MaterialStatusType;  // 状态 1:只更新物料状态
 
 
        public String APP_NAME_BJ;   //  北京首营审核人
        public String APP_DATE_BJ;   //  北京审核日期
        public String APP_RSLT_BJ;   //  北京首营审核结果
        public String APP_NAME_SH;   //  上海首营审核人
        public String APP_DATE_SH;   //  上海审核日期
        public String APP_RSLT_SH;   //  上海首营审核结果
        public String APP_NAME_GZ;   //  广州首营审核人
        public String APP_DATE_GZ;   //  广州审核日期
        public String APP_RSLT_GZ;   //  广州首营审核结果
        public String APP_NAME_OSH;   //  OSH首营审核人
        public String APP_DATE_OSH;   //  OSH审核日期
        public String APP_RSLT_OSH;   //  OSH首营审核结果
        public String PROD_ADD;       // 生产地址
        public String STRU_COM;      // 结构及组成
        public String ADOPT_RANGE;    // 适用范围
 
 
    }
 
    @HttpPost
    global static void execute() {
 
        // 取得接口传输内容
        String strData = RestContext.request.requestBody.toString();
        GeDatas ges = (GeDatas) JSON.deserializeStrict(strData, GeDatas.class);
 
        if (ges == null ) {
            return;
        }
 
        NFMUtil.Monitoring Monitoring = ges.Monitoring;
        if (Monitoring == null) {
            return;
        }
 
        BatchIF_Log__c rowData = NFMUtil.saveRowData(Monitoring, 'SBG204', ges.GeData);
        if (String.isBlank(rowData.Log__c) == false) {
            executefuture(rowData.Id);
        }
 
        // JSONを戻す
        RestResponse res = RestContext.response;
        res.addHeader('Content-Type', 'application/json');
        res.statusCode = 200;
        String jsonResponse = '{"status": "0", "Message":""}';
        res.responseBody = blob.valueOf(jsonResponse);
        return;
    }
 
    @future
    global static void executefuture(String rowData_Id) {
        main(rowData_Id);
    }
 
    global static void main (String rowData_Id) {
        Integer batch_retry_max_cnt = Integer.valueOf(System.Label.batch_retry_max_cnt);
        BatchIF_Log__c rowData = [Select Id, Name, Log__c, ErrorLog__c, Log2__c, Log3__c, Log4__c, Log5__c, Log6__c, Log7__c, Log8__c, Log9__c, Log10__c, Log11__c, Log12__c, MessageGroupNumber__c, retry_cnt__c from BatchIF_Log__c where RowDataFlg__c = true and Id = :rowData_Id];
        String logstr = rowData.MessageGroupNumber__c + ' start\n';
        BatchIF_Log__c iflog = new BatchIF_Log__c();
        iflog.Type__c = 'SBG204';
        iflog.MessageGroupNumber__c = rowData.MessageGroupNumber__c;
        iflog.Log__c = logstr;
        iflog.ErrorLog__c = '';
        insert iflog;
        String groupNumber = rowData.MessageGroupNumber__c;
        String rowDataStr = NFMUtil.getRowDataStr(rowData);
        List<GeData> GeDataList = (List<GeData>) JSON.deserialize(rowDataStr, List<GeData>.class);
 
        if (GeDataList == null || GeDataList.size() == 0) {
            return;
        }
 
        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 = 'Product_Register__c'
                                                  or Table__c = 'Product2')];
        for (BatchIF_Transfer__c t : transferList) {
            transferMap.put(t.Column__c + t.External_value__c, t.Internal_value__c);
        }
 
        Savepoint sp = Database.setSavepoint();
        try {
 
            List<String> productNoList = new List<String>();
            List<String> mD5KeyList = new List<String>();
            List<String> registerNoDelList = new List<String>();
            List<GeData> satisfyGeData = new List<GeData>();
            for (GeData data : GeDataList) {
                //作为产品注册证的唯一标识必须都有值才算正确的产品注册证
                if (String.isNotBlank(data.RegisterNo) && String.isNotBlank(data.ValidFrom)) {
                    mD5KeyList.add(MD5Encrypt(data.RegisterNo,data.ValidFrom));
                } else {
                    iflog.ErrorLog__c += 'RegisterNo/ValidFrom is required,This data is skipped.\n';
                    continue;
                }
 
                //产品Code的List
                if (String.isNotBlank(data.MaterialNo)) {
                    Pattern pattern = Pattern.compile('^[0-9]*$');  
                    Matcher isNum = pattern.matcher(data.MaterialNo); 
                    if (isNum.matches()) {
                        data.MaterialNo = data.MaterialNo.leftPad(18, '0');
                    }
                    productNoList.add(data.MaterialNo);
                } else {
                    iflog.ErrorLog__c += 'RegisterNo[ ' + data.RegisterNo + ' ] of MaterialNo is required,This data is skipped.\n';
                    continue;
                }
 
                // 删除注册证关系List
                if (String.isNotBlank(data.RegisterNo)) {
                    registerNoDelList.add(MD5Encrypt(data.RegisterNo,data.ValidFrom));
                }
 
                satisfyGeData.add(data);
 
            }
 
            Map<String, Product2> productMap = new Map<String, Product2>();
            Map<String, Product_Register__c> registerMap = new Map<String, Product_Register__c>();
 
            // 产品
            List<Product2> productList = [select id, ProductCode from Product2 where ProductCode = :productNoList];
            for (Product2 product : productList) {
                productMap.put(product.ProductCode, product);
            }
 
            List<Product_Register__c> registerList2 = [select id, Name, MD5EncryptionKey__c from Product_Register__c where MD5EncryptionKey__c = :mD5KeyList];
            for (Product_Register__c register : registerList2) {
                registerMap.put(register.MD5EncryptionKey__c, register);
            }
            List<Product_Register_Link__c> prlinkList = new List<Product_Register_Link__c>();
 
            prlinkList = [select id,
                          Product2__c,
                          Product2__r.ProductCode,
                          Product_Register__c,
                          Product_Register__r.Name
                          from Product_Register_Link__c
                          where Product_Register__r.MD5EncryptionKey__c in :registerNoDelList];
 
            Map<String, Product2> upd_productMap = new Map<String, Product2>();
            Map<String, Product_Register__c> ups_registerMap = new Map<String, Product_Register__c>();
            Map<String, Product_Register_Link__c> ins_linkMap = new Map<String, Product_Register_Link__c>();
 
 
            for (GeData data : satisfyGeData) {
 
                Product2 prd = new Product2();
                Product_Register__c pr = new Product_Register__c();
 
 
                if (String.isNotBlank(data.MaterialNo)) {
 
                    if (productMap.containsKey(data.MaterialNo)) {
 
                        prd = productMap.get(data.MaterialNo);
                        prd.Product_CCode__c = data.Model;
                        String prdCompanyAddr = data.REG_Name;
 
                        if (String.isNotBlank(prdCompanyAddr)) {
                            if (prdCompanyAddr.indexOf(';') != -1) {
                                String splitPrdCompanyAddr =  prdCompanyAddr.split(';')[0];
                                prd.Factory__c = splitPrdCompanyAddr.indexOf(':') == -1 ?  splitPrdCompanyAddr : splitPrdCompanyAddr.split(':')[1];
                            } else {
                                prd.Factory__c = prdCompanyAddr;
                            }
                        } else {
                            prd.Factory__c = prdCompanyAddr;
                        }
                    } else {
 
                        iflog.ErrorLog__c += 'OTCode[' + data.MaterialNo + ']不存在\n';
                        continue;
                    }
 
                    upd_productMap.put(data.MaterialNo, prd);
 
                    logstr += 'Product:' + data.MaterialNo;
                }
                if (String.isNotBlank(data.RegisterNo) ) {
                    String mD5key = MD5Encrypt(data.RegisterNo,data.ValidFrom);
                    if (registerMap.containsKey(mD5key)) {
                        pr = registerMap.get(mD5key);
                    } 
                    pr.MD5EncryptionKey__c    = mD5key;                  //产品注册证号的唯一标识
                    pr.Name                   = data.RegisterNo;         //注册证号
                    pr.IsFromSPO__c           = true;
                    pr.RegisterNoClass_New__c = data.RegisterNoClass_New; //注册证号分类编号(新)
                    pr.RegisterNoClass_Old__c = data.RegisterNoClass_Old; //注册证号分类编号(旧)
                    pr.ValidFrom__c           = NFMUtil.parseStr2Date(data.ValidFrom);          //注册证号效期从
                    pr.ValidTo__c             = NFMUtil.parseStr2Date(data.ValidTo);            //注册证号效期至
                    pr.Stelsedag__c           = NFMUtil.parseStr2Date(data.EffectDate);         //系统维护日
                    pr.MedPrdClass__c         = data.MedPrdClass;        //医疗器械分类
                    pr.PrdCompanyLicense__c   = data.PrdCompanyLicense;  //生产企业许可证号/备案凭证号
                    pr.PrdCompanyAddr__c      = data.PrdCompanyAddr;     //生产企业地址
                    pr.ClinicalProductCode__c = data.ClinicalCode;       //型号规格
                    pr.RegistrantName__c = data.REG_Name ;               // 注册人名称
                    pr.RegistrantResidence__c = data.REG_ADD ;           // 注册人住所
                    ups_registerMap.put(data.RegisterNo, pr); 
                    logstr += '  Register:' + data.RegisterNo + '\n';
                }
            }
            
            if (upd_productMap.keySet().size() > 0) update upd_productMap.values();
            if (ups_registerMap.keySet().size() > 0) upsert ups_registerMap.values();
 
            // 删除注册证号下所有物料
            List<Product_Register_Link__c> delList = new List<Product_Register_Link__c>();
            for (Product_Register_Link__c prlc : prlinkList) {
 
                delList.add(prlc);
 
            }
 
            if (delList.size() > 0 )    delete delList;
 
 
            // 取得所有注册证ID
            registerList2 = [select id, Name, MD5EncryptionKey__c from Product_Register__c where MD5EncryptionKey__c = :mD5KeyList];
            for (Product_Register__c register : registerList2) {
                registerMap.put(register.MD5EncryptionKey__c, register);
            }
 
            // 产品和注册证关系表
            for (GeData data : satisfyGeData) {
                if (!productMap.containsKey(data.MaterialNo)) {
                    // 产品不存在的场合
                    continue;
                }
 
                String prlink = data.MaterialNo + '_' + data.RegisterNo;
                Product_Register_Link__c prl = new Product_Register_Link__c();
                prl.Product2__c = productMap.get(data.MaterialNo).id;
                prl.Product_Register__c = registerMap.get(MD5Encrypt(data.RegisterNo,data.ValidFrom)).id;
                prl.If_Delete__c = false;
                ins_linkMap.put(prlink, prl);
 
            }
 
            if (ins_linkMap.keySet().size() > 0) insert ins_linkMap.values();
 
            logstr += '\nend';
            rowData.retry_cnt__c = 0;
 
        } catch (Exception ex) {
            // エラーが発生した場合
            Database.rollback(sp);
            System.debug(Logginglevel.ERROR, 'SBG204_' + rowData.MessageGroupNumber__c + ':' + ex.getMessage());
            System.debug(Logginglevel.ERROR, 'SBG204_' + rowData.MessageGroupNumber__c + ':' + ex.getStackTraceString());
            logstr += '\n' + ex.getMessage();
            iflog.ErrorLog__c = ex.getMessage() + '\n' + ex.getStackTraceString() + '\n' + iflog.ErrorLog__c;
            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 + '错误次数已经超过自动收信设定的最大次数,请手动收信';
            }
        }
        update rowData;
        iflog.Log__c = logstr;
        if (iflog.Log__c.length() > 131072) {
            iflog.Log__c = iflog.Log__c.subString(0, 131065) + ' ...';
        }
        if (iflog.ErrorLog__c.length() > 32768) {
            iflog.ErrorLog__c = iflog.ErrorLog__c.subString(0, 32760) + ' ...';
        }
        update iflog;
    }
    //将"产品注册证号"与"注册证号效期从"进行MD5加密作为产品注册证的唯一标识
    public static String MD5Encrypt(String RegisterNo,String ValidFrom){
        if (String.isBlank(RegisterNo) || String.isBlank(ValidFrom)) {
            return null;
        }
        String result = '';
        //注册证号效期从
        Date  startOfDay = NFMUtil.parseStr2Date(ValidFrom);
        //时间戳
        Long timestamp = Datetime.newInstanceGmt(startOfDay.year(), startOfDay.Month(),startOfDay.Day(), 0, 0, 0).getTime();
        //MD5转化
        String mD5EncryptionKey = EncodingUtil.convertToHex(Crypto.generateDigest('MD5', Blob.valueOf(RegisterNo+timestamp))).toUpperCase();
        result = mD5EncryptionKey;
        return result;
    }
 
}