GWY
2022-03-17 ae3dbaacd49b295337c5c53dbf5cf732898ed6f8
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
public without sharing class Product2TriggerHandler {
 
    public static void upsertProductSearch(List<Product2> newList, Map<Id, Product2> newMap, List<Product2> oldList, Map<Id, Product2> oldMap) 
    {
        if(Trigger.isAfter && (Trigger.isInsert || Trigger.isUpdate))
        {
            List<String> p2ids = new List<String>();
            for(Product2 p2 : newList)
            {
                p2ids.add(p2.id);
            }
            
            List<Product_Search__c> ps = [select Id,Name,CurrencyIsoCode,Product__c From Product_Search__c Where Product__c in: p2ids];
            Map <String,Product_Search__c> psmap = new Map<String,Product_Search__c>();
            List<Product_Search__c> pss = new List<Product_Search__c>();
            for(Product_Search__c p : ps)
            {
                psmap.put(p.Product__c,p);
            }
            
            for(Product2 p2 : newList)
            {
                Product_Search__c p2s;
                if(psmap.containsKey(p2.id))
                {
                    p2s = psmap.get(p2.id);
                }
                else
                {
                    p2s = new Product_Search__c();
                }
                
                p2s.Name =  p2.Name;
                p2s.Product__c = p2.id;
                p2s.CurrencyIsoCode = p2.CurrencyIsoCode;
                pss.add(p2s);
            }
            System.debug('psssize' + pss.size());
            upsert(pss);  
        }else if(Trigger.isDelete)
       {
           for(Product2 en : newList)
           {
                 en.addError('此数据不能删除');
           }
       }
    }
 
    public static void checkProduct2Level(List<Product2> newList, Map<Id, Product2> newMap, List<Product2> oldList, Map<Id, Product2> oldMap) {
        List<String> p2ids = new List<String>();
        for(Product2 p2 : newList)
        {
            p2ids.add(p2.id);
        }
 
        //存放查找的soql文
        List<Product_Register_Link__c> pro2List = new List<Product_Register_Link__c>();
        pro2List = [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,Product_Register__c,Product2__c 
        from Product_Register_Link__c 
        where Product2__r.Id in : p2ids];
        System.debug('pro2List='+pro2List);
        //存放产品的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 (pro2List.size()>0) {
            for (Product_Register_Link__c prl: pro2List) {
                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 && 
                    newMap.get(prl.Product2__r.Id).demoteer_Sap__c <= prl.Product_Register__r.ValidTo__c && newMap.get(prl.Product2__r.Id).demoteer_Sap__c >= prl.Product_Register__r.ValidFrom__c) {
                    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);
                    }
                }else {
                    if (!prSteMap.containsKey(prl.Product2__r.Id) || prSteMap.get(prl.Product2__r.Id) == null) {
                        prSteMap.put(prl.Product2__r.Id,null);
                    }  
                }
 
                if ((prl.Product_Register__r.MedPrdClass__c == '2' || prl.Product_Register__r.MedPrdClass__c == '3') && prl.Product_Register__r.Stelsedag__c <> null && 
                    newMap.get(prl.Product2__r.Id).demoteer_Sap__c <= prl.Product_Register__r.ValidTo__c && newMap.get(prl.Product2__r.Id).demoteer_Sap__c >= prl.Product_Register__r.ValidFrom__c) {
                    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);
                    }
                }else {
                    if (!prSte1Map.containsKey(prl.Product2__r.Id) || prSte1Map.get(prl.Product2__r.Id) == null) {
                        prSte1Map.put(prl.Product2__r.Id,null);
                    }  
                }
            }
        }
        
        //存放产品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>();
        System.debug('pro2Map='+pro2Map);
        System.debug('prSteMap='+prSteMap);
        System.debug('prSte1Map='+prSte1Map);
        Date stedate;
 
        //通过上面的循环,把关系表过获取的数据读出来,传给map
        if (pro2Map.size()>0) {
            for (Product_Register_Link__c pr: pro2List) {
                if (pro2Map.containsKey(pr.Product2__r.Id) && (prSteMap.containsKey(pr.Product2__r.Id) || prSte1Map.containsKey(pr.Product2__r.Id))) {
                    stedate = prSteMap.get(pr.Product2__r.Id)==null?(prSte1Map.get(pr.Product2__r.Id)==null?null:prSte1Map.get(pr.Product2__r.Id)):prSteMap.get(pr.Product2__r.Id);
                    if (stedate <> null) {
                        if (newMap.get(pr.Product2__r.Id).demoteer_Sap__c <> null && newMap.get(pr.Product2__r.Id).Diedatvanink__c <> null) {
                            if (pr.Product_Register__r.Stelsedag__c <> null) {
                                if (newMap.get(pr.Product2__r.Id).Diedatvanink__c <= stedate) {
                                   if (newMap.get(pr.Product2__r.Id).demoteer_Sap__c <= pr.Product_Register__r.ValidTo__c && newMap.get(pr.Product2__r.Id).demoteer_Sap__c >= pr.Product_Register__r.ValidFrom__c
                                        && pr.Product_Register__r.MedPrdClass__c == '2' || pr.Product_Register__r.MedPrdClass__c == '3') {
                                        System.debug('2');
                                        if (prlMap.get(pr.Product2__r.Id) == null || !prlMap.containsKey(pr.Product2__r.Id)) {
                                            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 (newMap.get(pr.Product2__r.Id).demoteer_Sap__c <= pr.Product_Register__r.ValidTo__c && newMap.get(pr.Product2__r.Id).demoteer_Sap__c >= pr.Product_Register__r.ValidFrom__c
                                        && (pr.Product_Register__r.MedPrdClass__c == '1')) {
                                        System.debug('1');
                                            if (prlMap.get(pr.Product2__r.Id) == null || !prlMap.containsKey(pr.Product2__r.Id)) {
                                                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 (newMap.get(pr.Product2__r.Id).Diedatvanink__c > stedate) {
                                    if (newMap.get(pr.Product2__r.Id).demoteer_Sap__c <= pr.Product_Register__r.ValidTo__c && newMap.get(pr.Product2__r.Id).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.containsKey(pr.Product2__r.Id)) {
                                            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 (newMap.get(pr.Product2__r.Id).demoteer_Sap__c <= pr.Product_Register__r.ValidTo__c && newMap.get(pr.Product2__r.Id).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.containsKey(pr.Product2__r.Id)) {
                                            prl1Map.put(pr.Product2__r.Id, '二类');
                                        }else {
                                            String Lins = prl1Map.get(pr.Product2__r.Id);
                                            Lins += '二类';
                                            prl1Map.put(pr.Product2__r.Id, Lins);
                                        }
                                    }
                                }
                            }else {
                                prl2Map.put(pr.Product2__r.Id, '空');
                            }
                        }
                    }else {
                        prl2Map.put(pr.Product2__r.Id, '空');
                    }
                }
            }
        }
                
                System.debug('prlMap='+prlMap);
                System.debug('prl1Map='+prl1Map);
                System.debug('prl2Map='+prl2Map);
        //给产品上的等级类别字段赋值
        for (Product2 pr2: newList) {
            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 = '二类';
                }else if (prlMap.get(pr2.Id).contains('一类') && !prlMap.get(pr2.Id).contains('二类')) {
                    pr2.Level_Category__c = '一类';
                }
            }else if (prl1Map.containsKey(pr2.Id)) {
                if (prl1Map.get(pr2.Id).contains('一类')) {
                    pr2.Level_Category__c = '一类';
                }else if (prl1Map.get(pr2.Id).contains('二类') && !prl1Map.get(pr2.Id).contains('一类')) {
                    pr2.Level_Category__c = '二类';
                }
            }else if (prl2Map.containsKey(pr2.Id)) {
                pr2.Level_Category__c = '非监管';
            }
            if (pr2.demoteer_Sap__c == null || pr2.Diedatvanink__c == null) {
                pr2.Level_Category__c = null;
            }
            System.debug('pr2.Level_Category__c='+pr2.Level_Category__c);
        }    
    }
}