张宇恒
2022-12-23 ec47f5ab5923a315bf22e0df5af7371e9668589e
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
trigger SyncProduct2 on Product2 (after delete, after insert, after update) {
    if (StaticParameter.EscapeSyncProduct2Trigger) {
System.debug('Escape、EscapeSyncProduct2Trigger:::::' + StaticParameter.EscapeSyncProduct2Trigger);
        return;
    }
    List<Product2> pr2s = new List<Product2>();
    
    if (Trigger.isDelete) {
        for (Product2 pr2 : Trigger.old) {
            pr2s.add(pr2);
        }
    } else {
        for (Product2 pr2 : Trigger.new) {
            if (Trigger.isInsert
                    || Trigger.isUpdate
                            && (pr2.Name.equals(Trigger.oldMap.get(pr2.Id).get('Name'))
                                    || pr2.Asset_Model_No__c.equals(Trigger.oldMap.get(pr2.Id).get('Asset_Model_No__c'))
                                    || pr2.IsActive != Trigger.oldMap.get(pr2.Id).get('IsActive')
                                    || pr2.SFDA_Status__c != Trigger.oldMap.get(pr2.Id).get('SFDA_Status__c')))
            pr2s.add(pr2);
        }
    }
    
    Map<String, Product2__c> pr2Map = new Map<String, Product2__c>();
    if (pr2s.size() > 0) {
        List<Product2__c> pr2Cs = ControllerUtil.pr2SelectForSync(pr2s);
        for (Product2__c pr2 : pr2Cs) {
            pr2Map.put(pr2.Product2__c, pr2);
        }
    }
    
    List<Product2__c> insPr2 = new List<Product2__c>();
    List<Product2__c> updelPr2 = new List<Product2__c>();
    List<Product2__c> delPr2 = new List<Product2__c>();
    for (Product2 pr : pr2s) {
        // 見付かったら更新/削除
        if (pr2Map.containsKey(pr.Id)) {
            Product2__c pr2 = pr2Map.get(pr.Id);
            // 「準備中」「新製品申請中」は対象外
            if (pr.IsActive
                    && pr.SFDA_Status__c != '準備中' && pr.SFDA_Status__c != '新製品申請中') {
                pr2.Name = pr.Asset_Model_No__c != null ? pr.Asset_Model_No__c.mid(0, 80) : pr.Name.mid(0, 80);
                pr2.Name_Text__c = pr.Name;
                pr2.OT_CODE_Text__c = pr.ProductCode;
                pr2.CurrencyIsoCode = pr.CurrencyIsoCode;
                if (Trigger.isDelete) {
                    pr2.DeleteFlag__c = true;
                } else {
                    pr2.DeleteFlag__c = false;
                }
                updelPr2.add(pr2);
            } else {
                pr2.DeleteFlag__c = true;
                delPr2.add(pr2);
            }
        // 新規
        } else {
            if (pr.IsActive
                    && pr.SFDA_Status__c != '準備中' && pr.SFDA_Status__c != '新製品申請中') {
                Product2__c pr2 = new Product2__c(
                    Product2__c = pr.Id,
                    Name = pr.Asset_Model_No__c != null ? pr.Asset_Model_No__c.mid(0, 80) : pr.Name.mid(0, 80),
                    Name_Text__c = pr.Name,
                    OT_CODE_Text__c = pr.ProductCode,
                    CurrencyIsoCode = pr.CurrencyIsoCode
                );
                insPr2.add(pr2);
            }
        }
    }
    
    if (Trigger.isDelete) {
        //ControllerUtil.delPr2List(updelPr2);
        ControllerUtil.updelPr2List(updelPr2, null);
    }
    if (Trigger.isUpdate) {
        ControllerUtil.updelPr2List(updelPr2, null);
        ControllerUtil.updelPr2List(delPr2, null);
    }
    if (!Trigger.isDelete) ControllerUtil.insPr2List(insPr2);
 
    // HWAG-AWLAU6 2018/3/7 "是否多年保修"的值拷贝到"是否多年保修(文本)"中
    List<String> proId = new List<String>();
    List<String> conformChangeProId = new List<String>();
    if (Trigger.isInsert || Trigger.isUpdate) {
        for (Product2 pro : Trigger.new) {
            proId.add(pro.Id);
            //XHL -----20190523----UpdateStart--
            if (Trigger.isInsert){
                conformChangeProId.add(pro.Id);
            } else if (Trigger.isUpdate && (pro.Extend_new_product_gurantee__c != Trigger.oldMap.get(pro.Id).Extend_new_product_gurantee__c || 
                        pro.Extend_Gurantee_Start__c != Trigger.oldMap.get(pro.Id).Extend_Gurantee_Start__c || 
                        pro.Extend_Gurantee_End__c != Trigger.oldMap.get(pro.Id).Extend_Gurantee_End__c)){
                conformChangeProId.add(pro.Id);    
            }
            //XHL -----20190523----UpdateEnd
            
        }
    }
    if (conformChangeProId.size() > 0) {
        //List<Asset> assList = [select id, IS_Extend_Gurantee__c,GuaranteeFlag__c ,IS_Extend_Gurantee_Txt__c, Extend_Gurantee_Txt_Manual__c from Asset where Product2Id IN : proId AND GuaranteeFlag__c = true];
        List<Asset> assList = [select id, IS_Extend_Gurantee__c,GuaranteeFlag__c ,
                                    IS_Extend_Gurantee_Txt__c, Extend_Gurantee_Txt_Manual__c,
                                    Product2Id 
                                from Asset 
                                where Product2Id IN : conformChangeProId];
        if (assList != null && assList.size() > 0) {
            List<Asset> updAsset = new List<Asset>();
            for(Asset ass : assList) {
                //XHL -------------20190118----UpdateStart----------在保有设备中创建GuaranteeFlag__c字段----20190523将其还原
                if (ass.IS_Extend_Gurantee__c != ass.IS_Extend_Gurantee_Txt__c && ass.Extend_Gurantee_Txt_Manual__c == false) {
                    ass.IS_Extend_Gurantee_Txt__c = ass.IS_Extend_Gurantee__c;
                    updAsset.add(ass);
                }
                //XHL -------------20190118----UpdateEnd----------20190523将其还原
            }
            if (updAsset.size() > 0) {
                update updAsset;
            }
        }
    }
 
    // CHAN-AX9485 2018/3/27 关于产品配套有效无效的判断--产品变更时更新产品配套是否有效
    if (Trigger.isUpdate) {
        List<Product_Set_Detail__c> psdList = [select Id, Effective_Product_detail__c, EffectiveNo_Product_detail__c, Total_price_RMB__c,
                                            List_price_total_RMB_text__c from Product_Set_Detail__c where Product__c IN : proId];
        List<Product_Set_Detail__c> updPsdList = new List<Product_Set_Detail__c>();
        if (psdList != null && psdList.size() > 0) {
            for(Product_Set_Detail__c psd : psdList) {
                if (psd.Effective_Product_detail__c != psd.EffectiveNo_Product_detail__c
                    || psd.Total_price_RMB__c != psd.List_price_total_RMB_text__c) {
                    updPsdList.add(psd);
                }
            }
        }
        if (updPsdList.size() > 0) {
            update updPsdList;
        }
    }
}