11
GWY
2022-03-10 0fdc2c45a4b4f8199da04611433e93b6d9e969ae
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
public without sharing class ProRegisterHandler {
 
    public static void updateProduct(List<Product_Register__c> newList, Map<Id, Product_Register__c> newMap, List<Product_Register__c> oldList, Map<Id, Product_Register__c> oldMap) {
 
        // 产品-注册证关系 中所有 产品ID
        List<ID> Product2_ID_list = new list<ID>();
        // 现有产品 map
        Map<ID, Product2> Exist_ProductMap = new map<ID, Product2>();
        // 需要更新产品 list
        List<Product2> UpdateProductlist = new list<Product2>();
        // 根据变更的证ID找到关联的所有的产品
        list<Product_Register_Link__c> ExistPRLList = [select Product2__c,
                                       Product_Register__c
                                       from Product_Register_Link__c
                                       where Product_Register__c in: newList
                                                      ];
 
        for (Product_Register_Link__c PRL : ExistPRLList) {
            // 产品ID的LIST
            Product2_ID_list.add(PRL.Product2__c);
        }
 
        // 通过产品ID找到该产品最新注册证ID
        Exist_ProductMap = new Map<ID, Product2> ([select ID, Register_Latest__c, registrationCode__c, clinical_product_code__c
                from Product2
                where id in: Product2_ID_list
                                                  ]);
 
        // 通过产品ID找到所有涉及的有效关系
        List<Product_Register_Link__c> allPRLList = [select Product2__c,
                                       Product_Register__c,
                                       Product_Register__r.PrdCompanyAddr__c,
                                       Product_Register__r.ValidFrom__c,
                                       Product_Register__r.ValidTo__c,
                                       Product_Register__r.Name,
                                       Product_Register__r.ValidProductRegister__c,
                                       Product_Register__r.ClinicalProductCode__c
                                       from Product_Register_Link__c
                                       where Product2__c in : Product2_ID_list
                                       order by Product2__c,
                                       LastModifiedDate desc
                                                    ];
 
        String tmpProductID = '';
        Product2 temProduct = new Product2();
        Map<Id, Product2> UpdProMap = new Map<Id, Product2>();
 
        for (Product_Register_Link__c temPRL : allPRLList) {
 
            if (!tmpProductID.equals(temPRL.Product2__c)) {
 
                tmpProductID = temPRL.Product2__c;
                temProduct = Exist_ProductMap.get(tmpProductID);
                temProduct.Register_Latest__c = temPRL.Product_Register__c;                    // 最新注册证ID
                temProduct.registrationCode__c = null;
                temProduct.clinical_product_code__c = null;
            }
 
            // 如果注册证状态有效,记录有效注册证号
            if (temPRL.Product_Register__r.ValidProductRegister__c ) {
                if (String.isBlank(temProduct.registrationCode__c)) {
                    temProduct.registrationCode__c = temPRL.Product_Register__r.Name + '\n';
                } else {
                    if (String.isNotBlank(temPRL.Product_Register__r.Name)) {
                        if (temProduct.registrationCode__c.indexOf(temPRL.Product_Register__r.Name) == -1) {
                            temProduct.registrationCode__c += temPRL.Product_Register__r.Name + '\n';
                        }
                    }
                }
 
                String clinicalCode = temPRL.Product_Register__r.ClinicalProductCode__c;
                if (String.isBlank(temProduct.clinical_product_code__c)) {
                    temProduct.clinical_product_code__c = clinicalCode;//temPRL.Product_Register__r.ClinicalProductCode__c;
                } else {
 
                    if (String.isNotBlank(clinicalCode)) {
 
                        Map<String, String> codeMap = new Map<String, String>();
                        for (String code : temProduct.clinical_product_code__c.split('/')) {
                            codeMap.put(code, code);
                        }
                        codeMap.put(clinicalCode, clinicalCode);
                        temProduct.clinical_product_code__c = '';
                        for (String code : codeMap.values()) {
                            if (String.isBlank(temProduct.clinical_product_code__c)) {
                                temProduct.clinical_product_code__c = code;
                            } else {
                                temProduct.clinical_product_code__c += '/' + code;
                            }
                        }
                        
                    }
 
                }
            }
            UpdProMap.put(tmpProductID, temProduct);
        }
        if (UpdProMap.keySet().size() > 0)  update UpdProMap.values();
    }
 
    //产品降类,只要注册证更新了,就检查一遍,重新给产品上的等级类别字段赋值。
    //可以找出需要的Id,然后调用方法。
    public static void checkRegistervervaldatum(List<Product_Register__c> newList, Map<Id, Product_Register__c> newMap, List<Product_Register__c> oldList, Map<Id, Product_Register__c> oldMap) {
        List<String> prcList = new List<String>();
        for (Product_Register__c pr : newList) {
            Product_Register__c oldpr = oldMap.get(pr.Id);
            //有效期的始与终变了,就把这个产品注册证的id放入一个List集合里面
            //if (pr.ValidTo__c != oldpr.ValidTo__c || pr.ValidFrom__c != oldpr.ValidFrom__c) {
                prcList.add(pr.id);
            //}
        }
        //放到Map里,用于注册证更新产品
        Map<Id,Product2> prt2Map = new Map<Id,Product2>();
        //通过之前存储的Id用于关系表的查询。
        List<Product_Register_Link__c> prlList = new List<Product_Register_Link__c>();
        prlList = [select Product2__r.id,Product2__r.demoteer_Sap__c,Product2__r.Diedatvanink__c,Product_Register__r.MedPrdClass__c,Product_Register__r.Stelsedag__c,Product_Register__r.ValidTo__c,Product_Register__r.ValidFrom__c 
        from Product_Register_Link__c 
        where Product_Register__r.Id in : prcList];
        //查询出产品ID
        List<Id> pro2IdList = new  List<Id>();
        for (Product_Register_Link__c prl1: prlList) {
            pro2IdList.add(prl1.Product2__r.id);
        }
 
        List<Product_Register_Link__c> prl1List = new List<Product_Register_Link__c>();
        prl1List = [select Product2__r.id,Product2__r.demoteer_Sap__c,Product2__r.Diedatvanink__c,Product_Register__r.Stelsedag__c,Product_Register__r.MedPrdClass__c,Product_Register__r.ValidTo__c,Product_Register__r.ValidFrom__c 
        from Product_Register_Link__c 
        where Product2__r.Id in : pro2IdList];
 
        //根据Id存放产品所属的所有注册证下的器械分类。
        Map<Id,List<String>> pro2Map = new Map<Id,List<String>>();
        //存放注册证上最前的维护日
        Map<Id,Date> prSteMap = new Map<Id,Date>();
        Map<Id,Date> prSte1Map = new Map<Id,Date>();
        if (prl1List.size()>0) {
            for (Product_Register_Link__c prl: prl1List) {
                if (pro2Map.containsKey(prl.Product2__r.Id)) {
                    pro2Map.get(prl.Product2__r.Id).add(prl.Product_Register__r.MedPrdClass__c);
                }else {
                    pro2Map.put(prl.Product2__r.Id, new List<String>());
                    pro2Map.get(prl.Product2__r.Id).add(prl.Product_Register__r.MedPrdClass__c);
                }
 
                if (prl.Product_Register__r.MedPrdClass__c == '1' && prl.Product_Register__r.Stelsedag__c <> null) {
                    if (prSteMap.containsKey(prl.Product2__r.Id)) {
                        if (prSteMap.get(prl.Product2__r.Id) > prl.Product_Register__r.Stelsedag__c) {
                            prSteMap.put(prl.Product2__r.Id,prl.Product_Register__r.Stelsedag__c);
                        }
                    }else {
                        prSteMap.put(prl.Product2__r.Id,prl.Product_Register__r.Stelsedag__c);
                    }
                }
 
                if ((prl.Product_Register__r.MedPrdClass__c == '2' || prl.Product_Register__r.MedPrdClass__c == '2') && prl.Product_Register__r.Stelsedag__c <> null) {
                    if (prSte1Map.containsKey(prl.Product2__r.Id)) {
                        if (prSte1Map.get(prl.Product2__r.Id) > prl.Product_Register__r.Stelsedag__c) {
                            prSte1Map.put(prl.Product2__r.Id,prl.Product_Register__r.Stelsedag__c);
                        }
                    }else {
                        prSte1Map.put(prl.Product2__r.Id,prl.Product_Register__r.Stelsedag__c);
                    }
                }
            }
        }
        System.debug('pro2Map='+pro2Map);
        System.debug('prSteMap='+prSteMap);
        System.debug('prSte1Map='+prSte1Map);
        //产品Id与等级类别
        Map<Id,String> prlMap = new Map<Id,String>();
        Map<Id,String> prl1Map = new Map<Id,String>();
        Map<Id,String> prl2Map = new Map<Id,String>();
        List<ID> pidList = new List<ID>();
        if (pro2Map.size()>0) {
            for (Product_Register_Link__c pr: prl1List) {
                if (pro2Map.containsKey(pr.Product2__r.Id) && (prSteMap.containsKey(pr.Product2__r.Id) || prSte1Map.containsKey(pr.Product2__r.Id))) {
                    Date stedate = prSteMap.get(pr.Product2__r.Id)==null?prSte1Map.get(pr.Product2__r.Id)==null?null:prSte1Map.get(pr.Product2__r.Id):prSte1Map.get(pr.Product2__r.Id);
                    pidList.add(pr.Product2__r.Id);
                    if (pr.Product2__r.demoteer_Sap__c <> null && pr.Product2__r.Diedatvanink__c <> null) {
                        if (pr.Product_Register__r.Stelsedag__c <> null) {
                            if (pr.Product2__r.Diedatvanink__c <= stedate) {
                               if (pr.Product2__r.demoteer_Sap__c <= pr.Product_Register__r.ValidTo__c && pr.Product2__r.demoteer_Sap__c >= pr.Product_Register__r.ValidFrom__c
                                    && pr.Product_Register__r.MedPrdClass__c == '2' || pr.Product_Register__r.MedPrdClass__c == '3') {
                                    if (prlMap.get(pr.Product2__r.Id) == null) {
                                        prlMap.put(pr.Product2__r.Id, '二类');
                                    }else {
                                        String Lins = prlMap.get(pr.Product2__r.Id);
                                        Lins += '二类';
                                        prlMap.put(pr.Product2__r.Id, Lins);
                                    }
                                }
                                else if (pr.Product2__r.demoteer_Sap__c <= pr.Product_Register__r.ValidTo__c && pr.Product2__r.demoteer_Sap__c >= pr.Product_Register__r.ValidFrom__c
                                    && (pr.Product_Register__r.MedPrdClass__c == '1')) {
                                        if (prlMap.get(pr.Product2__r.Id) == null) {
                                            prlMap.put(pr.Product2__r.Id, '一类');
                                        }else {
                                            String Lins = prlMap.get(pr.Product2__r.Id);
                                        Lins += '一类';
                                        prlMap.put(pr.Product2__r.Id, Lins);
                                        }
                                }else {
                                    if (prlMap.get(pr.Product2__r.Id) == null) {
                                        prlMap.put(pr.Product2__r.Id, '非监管');
                                    }else {
                                        String Lins = prlMap.get(pr.Product2__r.Id);
                                        Lins += '非监管';
                                        prlMap.put(pr.Product2__r.Id, Lins);
                                    }
                                }
                            }else if (pr.Product2__r.Diedatvanink__c > stedate) {
                                if (pr.Product2__r.demoteer_Sap__c <= pr.Product_Register__r.ValidTo__c && pr.Product2__r.demoteer_Sap__c >= pr.Product_Register__r.ValidFrom__c
                                    && pr.Product_Register__r.MedPrdClass__c == '1') {
                                    if (prl1Map.get(pr.Product2__r.Id) == null) {
                                        prl1Map.put(pr.Product2__r.Id, '一类');
                                    }else {
                                        String Lins = prl1Map.get(pr.Product2__r.Id);
                                        Lins += '一类';
                                        prl1Map.put(pr.Product2__r.Id, Lins);
                                    }
                                }else if (pr.Product2__r.demoteer_Sap__c <= pr.Product_Register__r.ValidTo__c && pr.Product2__r.demoteer_Sap__c >= pr.Product_Register__r.ValidFrom__c
                                    && pr.Product_Register__r.MedPrdClass__c == '2' || pr.Product_Register__r.MedPrdClass__c == '3') {
                                    if (prl1Map.get(pr.Product2__r.Id) == null) {
                                        prl1Map.put(pr.Product2__r.Id, '二类');
                                    }else {
                                        String Lins = prl1Map.get(pr.Product2__r.Id);
                                        Lins += '二类';
                                        prl1Map.put(pr.Product2__r.Id, Lins);
                                    }
                                }else {
                                    if (prlMap.get(pr.Product2__r.Id) == null) {
                                        prlMap.put(pr.Product2__r.Id, '非监管');
                                    }else {
                                        String Lins = prlMap.get(pr.Product2__r.Id);
                                        Lins += '非监管';
                                        prlMap.put(pr.Product2__r.Id, Lins);
                                    }
                                }
                            }
                        }else {
                            prl2Map.put(pr.Product2__r.Id, '空');
                        }
                    }
                }
            }
        }
        System.debug('prlMap='+prlMap);
        System.debug('prl1Map='+prl1Map);
        System.debug('prl2Map='+prl2Map);
 
        List<Product2> por2Lsit = new List<Product2>();
        //查出产品的id和等级类别的字段 
        por2Lsit = [select Id,Level_Category__c,demoteer_Sap__c,Diedatvanink__c from Product2 where Id in : pidList];
        if (por2Lsit.size() > 0) {
            for (Product2 pr2: por2Lsit) {
                if (prlMap.containsKey(pr2.Id)) {
                    if ((prlMap.get(pr2.Id).contains('二类') && prlMap.get(pr2.Id).contains('一类')) || 
                        (!prlMap.get(pr2.Id).contains('一类') && prlMap.get(pr2.Id).contains('二类'))){
                        pr2.Level_Category__c = '二类';
                        prt2Map.put(pr2.Id, pr2);
                    }else if (prlMap.get(pr2.Id).contains('一类') && !prlMap.get(pr2.Id).contains('二类')) {
                        pr2.Level_Category__c = '一类';
                        prt2Map.put(pr2.Id, pr2);
                    }else if (prlMap.get(pr2.Id).contains('非监管') && !prl2Map.containsKey(pr2.Id)) {
                        pr2.Level_Category__c = '非监管';
                        prt2Map.put(pr2.Id, pr2);
                    }else {
                        pr2.Level_Category__c = null;
                        prt2Map.put(pr2.Id, pr2);
                    }
                }
                if (prl1Map.containsKey(pr2.Id)) {
                    if (prl1Map.get(pr2.Id).contains('一类')) {
                        pr2.Level_Category__c = '一类';
                        prt2Map.put(pr2.Id, pr2);
                    }else if (prl1Map.get(pr2.Id).contains('二类') && !prl1Map.get(pr2.Id).contains('一类')) {
                        pr2.Level_Category__c = '二类';
                        prt2Map.put(pr2.Id, pr2);
                    }else if (prl1Map.get(pr2.Id).contains('非监管') && !prl2Map.containsKey(pr2.Id)) {
                        pr2.Level_Category__c = '非监管';
                        prt2Map.put(pr2.Id, pr2);
                    }else {
                        pr2.Level_Category__c = null;
                        prt2Map.put(pr2.Id, pr2);
                    }
                }
                if (pr2.demoteer_Sap__c == null || pr2.Diedatvanink__c == null) {
                    pr2.Level_Category__c = null;
                    prt2Map.put(pr2.Id, pr2);
                }
            }
        }
        
        System.debug('prt2Map='+prt2Map);
        if (prt2Map.size() > 0) {
           update prt2Map.values(); 
        } 
    }
}