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 
 | 
  
 | 
        } 
 | 
} 
 |