高章伟
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
public with sharing class ProRegisterLinkHandler extends Oly_TriggerHandler {
    /* 2018-08-31 更新产品内最新产品在注册证
     1.根据产品-注册证关系找出所有对应的产品
     2.根据产品找出其所需最新产品注册证
    */
    private Map<Id, Product_Register_Link__c> newMap;
    private Map<Id, Product_Register_Link__c> oldMap;
    private List<Product_Register_Link__c> newList;
    private List<Product_Register_Link__c> oldList;
 
    public ProRegisterLinkHandler() {
        this.newMap = (Map<Id, Product_Register_Link__c>) Trigger.newMap;
        this.oldMap = (Map<Id, Product_Register_Link__c>) Trigger.oldMap;
        this.newList = (List<Product_Register_Link__c>) Trigger.new;
        this.oldList = (List<Product_Register_Link__c>) Trigger.old;
    }
 
    protected override void afterUpdate() {
        updateProduct('1');
    }
 
    protected override void afterInsert() {
        updateProduct('1');
    }
 
    protected override void afterDelete() {
        updateProduct('2');
    }
 
 
    // 新建和更新产品-注册证关系时使用
    private void updateProduct(string HandleType) {
 
        // 产品-注册证关系 中所有 产品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>();
 
        List<Product_Register_Link__c> currentList = new List<Product_Register_Link__c>();
 
        if ('1'.equals(HandleType)) {
            currentList = newList;
        } else {
            currentList = oldList;
        }
 
        for (Product_Register_Link__c PRL : currentList) {
            Product2_ID_list.add(PRL.Product2__c);
        }
 
        Exist_ProductMap = new map<ID, Product2>([select ID, SFDA_Status__c from Product2 where id in: Product2_ID_list]);
        List<Product_Register_Link__c> allPRLList = new List<Product_Register_Link__c>();
 
        allPRLList = [select  Product2__c,
                              Product_Register__c,
                              Product_Register__r.PrdCompanyAddr__c,
                              Product_Register__r.PrdCompanyAddr2__c,
                              Product_Register__r.PrdCompanyAddr3__c,
                              Product_Register__r.ValidFrom__c,
                              Product_Register__r.ValidTo__c,
                              Product_Register__r.name,
                              Product_Register__r.BusinessScopeKey__c,
                              Product_Register__r.RegisterNoStatus__c
                         from Product_Register_Link__c
                        where Product2__c  in : Product2_ID_list
                          //and If_Delete__c = false
                     order by Product2__c,
                              Product_Register__r.RegisterNoStatusCode__c,
                              LastModifiedDate desc
                     ];
 
        String tmpProductID = '';
        Map<ID, Product2> Upd_ProductMap = new Map<ID, Product2>();
        Product2 temProduct = new Product2();
        String temAllProdStatus = '';
        String temAllProdStatus2 = '';
        String temAllProdStatusAll = '';
        String temAllScopeKey = '';
 
        for (String ProId : Product2_ID_list) {
            temProduct = Exist_ProductMap.get(ProId);
            temProduct.Register_Latest__c = null;               // 最新注册证ID
            temProduct.SFDA_Approbation_No__c = null;           // CFDA注册号
            temProduct.SFDA_Approbated_Date__c = null;          // CFDA注册日
            temProduct.SFDA_Expiration_Date__c = null;          // CFDA有效期限
 
            //if (temProduct.SFDA_Status__c != '不要') {
                //temProduct.ProduceCompany__c = null;                // 生产企业
            //}
            temProduct.SFDA_Approbated_Status__c = null;        // CFDA注册证状态
            temProduct.RegisterNo_ALL__c  = null;
            temProduct.RegisterNo_ALL2__c  = null;
            // 20190401 CHAN-BAPCE6 LHJ Start
            temProduct.RegScopeKeyAll__c  = null;
            // 20190401 CHAN-BAPCE6 LHJ End
 
            Upd_ProductMap.put(ProId, temProduct);
        }
 
        for (Product_Register_Link__c temPRL : allPRLList) {
            if (!tmpProductID.equals(temPRL.Product2__c)) {
                temAllProdStatus = '';
                temAllProdStatus2 = '';
                temAllProdStatusAll = '';
                temAllScopeKey = '';
                tmpProductID = temPRL.Product2__c;
                temProduct = Upd_ProductMap.get(tmpProductID);
                temProduct.Register_Latest__c = temPRL.Product_Register__c;                    // 最新注册证ID
                temProduct.SFDA_Approbation_No__c = temPRL.Product_Register__r.name;           // CFDA注册号
                temProduct.SFDA_Approbated_Date__c = temPRL.Product_Register__r.ValidFrom__c;  // CFDA注册日
                temProduct.SFDA_Expiration_Date__c = temPRL.Product_Register__r.ValidTo__c;    // CFDA有效期限
                if(temPRL.Product_Register__r.Name != 'FYL') {
                    temProduct.ProduceCompany__c = temPRL.Product_Register__r.PrdCompanyAddr__c;   // 生产企业
                    //CHAN-C4X63A 【委托】NFM204字段“生产企业地址”优化 XHL 20210716 Start
                    temProduct.ProduceCompany2__c = temPRL.Product_Register__r.PrdCompanyAddr2__c;
                    temProduct.ProduceCompany3__c = temPRL.Product_Register__r.PrdCompanyAddr3__c;
                    //CHAN-C4X63A 【委托】NFM204字段“生产企业地址”优化 XHL 20210716 End
                }
                temProduct.SFDA_Approbated_Status__c = temPRL.Product_Register__r.RegisterNoStatus__c;    // CFDA注册证状态;
            }
            //// 如果注册证状态有效,记录有效注册证号
            if (temPRL.Product_Register__r.RegisterNoStatus__c == '有效') {
                //temAllProdStatus += ';' + temPRL.Product_Register__r.name;
                String productRegisterName = temPRL.Product_Register__r.Name;
                if (temAllProdStatus.length() + productRegisterName.length() <= 240) {
                    if (!temAllProdStatusAll.contains(productRegisterName)) {
                        temAllProdStatus += ';' + productRegisterName;
                        temAllProdStatusAll += ';' + productRegisterName;
                    }
                    //temAllProdStatus += ';' + productRegisterName;
                } else {
                    if (!temAllProdStatusAll.contains(productRegisterName)) {
                        temAllProdStatus2 += ';' + productRegisterName;
                        temAllProdStatusAll += ';' + productRegisterName;
                    }
                    //temAllProdStatus2 += ';' + productRegisterName;
                }
                // 20190401 CHAN-BAPCE6 LHJ Start
                //if (String.isNotBlank(temPRL.Product_Register__r.BusinessScopeKey__c)  && 
                //        (!temAllScopeKey.contains(temPRL.Product_Register__r.BusinessScopeKey__c))) {
                //    temAllScopeKey += ';' + temPRL.Product_Register__r.BusinessScopeKey__c;
                //}
                String  businessScopeKey = temPRL.Product_Register__r.BusinessScopeKey__c;
                if (String.isNotBlank(businessScopeKey)) {
                    for(String scopeKey:businessScopeKey.split(';')){
                        if (!temAllScopeKey.contains(scopeKey)) {
                            temAllScopeKey += ';' + scopeKey;
                        }
                    }
                    
                    
                }
                // 20190401 CHAN-BAPCE6 LHJ End
            }
            if (temAllProdStatus != '') {
                temProduct.RegisterNo_ALL__c = temAllProdStatus.substring(1, temAllProdStatus.length());
            } else {
                temProduct.RegisterNo_ALL__c = '';
            }
            if (temAllProdStatus2 != '') {
                temProduct.RegisterNo_ALL2__c = temAllProdStatus2.substring(1, temAllProdStatus2.length());
            } else {
                temProduct.RegisterNo_ALL2__c = '';
            }
            // 20190401 CHAN-BAPCE6 LHJ Start
            if (temAllScopeKey != '') {
                temProduct.RegScopeKeyAll__c = temAllScopeKey.substring(1, temAllScopeKey.length());
            } else {
                temProduct.RegScopeKeyAll__c = '';
            }
            // 20190401 CHAN-BAPCE6 LHJ End
 
            Upd_ProductMap.put(tmpProductID, temProduct);
        }
 
        if (Upd_ProductMap.keySet().size() > 0)  {
            update Upd_ProductMap.values();
        }
    }
}