D C
2023-05-26 9a0ef802a678ffc421fc1d416f7f867e89e5536a
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
trigger OpporFileAllEvent on Opportunity_File__c (
    before insert, 
    before update, 
    before delete, 
    after insert, 
    after update, 
    after delete, 
    after undelete) {
    List<String> opsidList = new List<String>();
    Map<String,Datetime> OpporMaptoOFC = new Map<String,Datetime>();
    Map<Datetime,String> OFCMaptoDateTime = new Map<Datetime,String>();
    List<String> idNowList  = new List<String>();
        if (Trigger.isBefore) {
            //call your handler.before method
            if(Trigger.isDelete){
                for(Opportunity_File__c ofc : Trigger.old){
                    if(ofc.Is_Locked__c){
                        ofc.addError('备货已确认,不能删除询价文件');
                        return;
                    }
                    idNowList.add(ofc.id);
                    opsidList.add(ofc.Opportunity__c);
                }
            }
            if (Trigger.isUpdate) {
                for(Opportunity_File__c ofc : Trigger.old){
                    Opportunity_File__c ofcn = Trigger.newMap.get(ofc.id);
                    if(ofc.Is_Locked__c && ofcn.Is_Locked__c){
                        ofcn.addError('备货已确认,不能修改询价文件');
                        return;
                    }
                }
            }
            if(Trigger.isUpdate||Trigger.isInsert){
                for(Opportunity_File__c ofc : Trigger.new){
                    if(Trigger.isUpdate){
                        if(ofc.Last_upload_time__c != Trigger.oldMap.get(ofc.id).Last_upload_time__c){
                            opsidList.add(ofc.Opportunity__c);
                        }
                    }else{
                        opsidList.add(ofc.Opportunity__c);
                    }
 
                    idNowList.add(ofc.id);
                    if(ofc.Last_upload_time__c!=null){
                        OFCMaptoDateTime.put(ofc.Last_upload_time__c, ofc.id);
                        OpporMaptoOFC.put(ofc.Opportunity__c,ofc.Last_upload_time__c);
                    }
                    system.debug('开始结果========='+OpporMaptoOFC);
                    system.debug('开始结果========='+OFCMaptoDateTime);
                }
            }
                List<Opportunity_File__c> allFileDate = [select id, name,Opportunity__c,Last_upload_time__c 
                                                            from Opportunity_File__c 
                                                            where Opportunity__r.id in:opsidList 
                                                            and id not in:idNowList
                                                            and Last_upload_time__c !=null];
 
                if(allFileDate.size()>0){
                    for(Opportunity_File__c ofc : allFileDate){
 
                        OFCMaptoDateTime.put(ofc.Last_upload_time__c,ofc.id);
 
                        if(OpporMaptoOFC.containsKey(ofc.Opportunity__c)){
 
                            if(OpporMaptoOFC.get(ofc.Opportunity__c) < ofc.Last_upload_time__c){
 
                                OpporMaptoOFC.put(ofc.Opportunity__c, ofc.Last_upload_time__c);
 
                            }
                        }else{
                            OpporMaptoOFC.put(ofc.Opportunity__c,ofc.Last_upload_time__c);
                        }
                    }
                    system.debug('最后结果========='+OpporMaptoOFC);
                    system.debug('最后结果========='+OFCMaptoDateTime);
                    List<Opportunity> updateDateTimeList = new List<Opportunity>();
                    for(String Oppids : OpporMaptoOFC.keySet()){
 
                        Opportunity ops = new Opportunity(
                                id = Oppids,
                                Last_opportunity_file__c = OFCMaptoDateTime.get(OpporMaptoOFC.get(Oppids))
                            );
 
                        updateDateTimeList.add(ops);
                    }
 
                    if(updateDateTimeList.size()>0){
                        update updateDateTimeList;
                    }
                }else{
 
                    List<Opportunity> updateDateTimeList = new List<Opportunity>();
                    for(String opsid : opsidList){
                        Opportunity ops = new Opportunity(
                                id = opsid,
                                Last_opportunity_file__c = OFCMaptoDateTime.get(OpporMaptoOFC.get(opsid))
                            );
                        updateDateTimeList.add(ops);
                    }
                    if(updateDateTimeList.size()>0){
                        update updateDateTimeList;
                    }
 
                }
        } else if (Trigger.isAfter) {
            //call handler.after method
 
        }
}