liuyn
2024-03-11 a87f1c3df03078814ee97ad0c8ac200a232419e9
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
// FixtureSetHandlerTest
public without sharing class FixtureSetHandler extends Oly_TriggerHandler {
    private Map<Id, Fixture_Set__c> newMap;
    private Map<Id, Fixture_Set__c> oldMap;
    private List<Fixture_Set__c> newList;
    private List<Fixture_Set__c> oldList;
 
    public FixtureSetHandler() {
        this.newMap = (Map<Id, Fixture_Set__c>) Trigger.newMap;
        this.oldMap = (Map<Id, Fixture_Set__c>) Trigger.oldMap;
        this.newList = (List<Fixture_Set__c>) Trigger.new;
        this.oldList = (List<Fixture_Set__c>) Trigger.old;
    }
 
    protected override void beforeInsert() {
        beforeSetValue();
    }
    protected override void beforeUpdate() {
        beforeSetValue();
        upToAsset();// 20230719 ljh DB202306212211 p9
    }
    private void beforeSetValue() {
        for (Fixture_Set__c nObj : newList) {
            nObj.Loaner_code__c = nObj.Name;
        }
    }
    /**
     * @Author: [lijinhuan]
     * @description: 备品类型I和备品类型II放到保有设备上显示
     * @return {*}
     */
    private void upToAsset() {
        Map<String,Fixture_Set__c> fsMap= new Map<String,Fixture_Set__c>();
        // Set<String> ModelNoSet = new  Set<String>();
        for (Fixture_Set__c nObj : newList) {
            Fixture_Set__c oObj;
            if (Trigger.isUpdate) {
                oObj = oldMap.get(nObj.Id);
            }
            // 发生改变
            if( (String.isNotBlank(nObj.Loaner_categoryI__c) && nObj.Loaner_categoryI__c != oObj.Loaner_categoryI__c)
                || (String.isBlank(nObj.Loaner_categoryI__c) && String.isNotBlank(oObj.Loaner_categoryI__c))
                || (String.isNotBlank(nObj.Loaner_categoryII__c) && nObj.Loaner_categoryII__c != oObj.Loaner_categoryII__c)
                || (String.isBlank(nObj.Loaner_categoryII__c) && String.isNotBlank(oObj.Loaner_categoryII__c))
                // 借用机会可视化UATbug25 zyh 20231218 start
                || (String.isBlank(nObj.Product_Type__c) && String.isNotBlank(oObj.Product_Type__c))
                || (String.isNotBlank(nObj.Product_Type__c) && nObj.Product_Type__c != oObj.Product_Type__c)
                // 借用机会可视化UATbug25 zyh 20231218 end
            ){
                if(String.isNotBlank(nObj.Fixture_Set_Body_Model_No__c)){
                    String model = nObj.Fixture_Set_Body_Model_No__c.toUpperCase();
                    fsMap.Put(model,nObj);
                }
                
            }
            // if(Trigger.isDelete && String.isNotBlank(oObj.Fixture_Set_Body_Model_No__c ) && (String.isNotBlank(oObj.Loaner_categoryI__c)||String.isNotBlank(oObj.Loaner_categoryII__c))){
            //     ModelNoSet.add(oObj.Fixture_Set_Body_Model_No__c);
            // }
        }
        // if(fsMap.size() > 0 || ModelNoSet.size() > 0){
        if(fsMap.size() > 0){
            // 查询这个配套的产品,赋值给产品
            List<Product2>  Product2List = [SELECT Id,Fixture_Model_No_T__c,Loaner_categoryI__c,Loaner_categoryII__c ,Product_Type__c
                                                 FROM Product2 
                                                // WHERE Fixture_Model_No_T__c IN :fsMap.keySet() OR Fixture_Model_No_T__c IN :ModelNoSet ];
                                                WHERE Fixture_Model_No_T__c IN :fsMap.keySet() ];
            if(Product2List.size() > 0){
                Map<String,Product2> updateMap = new Map<String,Product2>();
                for(Product2 pro:Product2List){
                    Product2 p2 = new Product2();
                    p2.Id = pro.Id;
                    p2.Loaner_categoryI__c = fsMap.get(pro.Fixture_Model_No_T__c.toUpperCase()).Loaner_categoryI__c;
                    p2.Loaner_categoryII__c = fsMap.get(pro.Fixture_Model_No_T__c.toUpperCase()).Loaner_categoryII__c;
                    // 借用机会可视化UATbug25 zyh 20231218 start
                    p2.Product_Type__c = fsMap.get(pro.Fixture_Model_No_T__c.toUpperCase()).Product_Type__c;
                    // 借用机会可视化UATbug25 zyh 20231218 end
                    if(((String.isNotBlank(p2.Loaner_categoryI__c) && p2.Loaner_categoryI__c.equals(pro.Loaner_categoryI__c)) || (String.isBlank(p2.Loaner_categoryI__c)&&String.isBlank(pro.Loaner_categoryI__c)))
                        && ((String.isNotBlank(p2.Loaner_categoryII__c) && p2.Loaner_categoryII__c.equals(pro.Loaner_categoryII__c)) || (String.isBlank(p2.Loaner_categoryII__c)&&String.isBlank(pro.Loaner_categoryII__c)))
                        // 借用机会可视化UATbug25 zyh 20231218 start
                        && ((String.isNotBlank(p2.Product_Type__c) && p2.Product_Type__c.equals(pro.Product_Type__c)) || (String.isBlank(p2.Product_Type__c)&&String.isBlank(pro.Product_Type__c)))){
                        // 借用机会可视化UATbug25 zyh 20231218 end
                        continue;
                    }
                    updateMap.put(pro.Id,p2);
                }
                if(updateMap.size() > 0){
                    update updateMap.values();
                }
            }
        }
    }
    
}