| trigger AttachmentTrigger on Attachment (before insert,after insert,before delete,after delete) { | 
|     List<String> rentalIds = new List<String>(); | 
|     List<String> attIds = new List<String>(); | 
|     List<String> rentaldelIds = new List<String>(); | 
|     List<String> attdelIds = new List<String>(); | 
|     List<String> rentalApplyIds = new List<String>(); | 
|     List<String> accIds = new List<String>(); | 
|     // LHJ CHAN-BCLD4P 20190618 Start | 
|     List<String> specIds = new List<String>(); | 
|     List<String> specDelIds = new List<String>(); | 
|     // LHJ CHAN-BCLD4P 20190618 End | 
|   | 
|     // MZY SWAG-BXXBKA  2021-02-19 start | 
|     List<String> opdIds = new List<String>(); | 
|     Map<String,String> opdMap = new Map<String,String>(); | 
|     // MZY SWAG-BXXBKA  2021-02-19 end | 
|   | 
|     List<String> OpporFileIds = new List<String>(); | 
|     Map<String,String> OpporFileUndeleteMap = new Map<String,String>(); | 
|     List<String> DeleteReIds = new List<String>(); | 
| Map<String,String> OppordeleteReUserMap = new Map<String,String>(); | 
|     Map<String,Datetime> OppordeleteReTimeMap = new Map<String,Datetime>(); | 
|     //CHAN-BCNCRB ---XHL----Start----- | 
|     List<String> opportunity_FileIds = new List<String>(); | 
|   | 
|     List<String> opportunityFileOrderIds = new List<String>(); | 
|     List<String> oFOrderIds = new List<String>(); | 
|     Map<String,Boolean> oFOrderIdMap = new Map<String,Boolean>(); | 
|     if (Trigger.isBefore && Trigger.isInsert) { | 
|         for (Attachment att : Trigger.new) { | 
|             if(String.valueOf(att.ParentId).startsWith(System.Label.Oppor_sepKey)){ | 
|                 opportunity_FileIds.add(att.ParentId); | 
|             } | 
|   | 
|             if(String.valueOf(att.ParentId).startsWith('a3Z')){ | 
|                 opportunityFileOrderIds.add(att.ParentId); | 
|             } | 
|         } | 
|     } | 
|     //CHAN-BCNCRB ---XHL----End----- | 
|     if (Trigger.isAfter && Trigger.isInsert) { | 
|         for (Attachment att : Trigger.new) { | 
|             //if (att.Name.toUpperCase() == '合同书.PDF' && String.valueOf(att.ParentId).startsWith('a0t')) { | 
|             // 添付した契約書のファイル名は指定できない | 
|             if (String.valueOf(att.ParentId).startsWith('a2K')) { | 
|                 rentalIds.add(att.ParentId); | 
|                 attIds.add(att.Id); | 
|             } | 
|             if (String.valueOf(att.ParentId).startsWith('a0t')) { | 
|                 rentalApplyIds.add(att.ParentId);  | 
|             } | 
|             if (String.valueOf(att.ParentId).startsWith('001')) { | 
|                 accIds.add(att.ParentId); | 
|             } | 
|             if(String.valueOf(att.ParentId).startsWith(System.Label.Oppor_sepKey)){ | 
|                 OpporFileIds.add(att.ParentId); | 
|             } | 
|             // LHJ CHAN-BCLD4P 20190618 Start | 
|             if(String.valueOf(att.ParentId).startsWith(System.Label.OppSpecialApply)){ | 
|                 specIds.add(att.ParentId); | 
|             } | 
|             // LHJ CHAN-BCLD4P 20190618 End | 
|   | 
|             // MZY SWAG-BXXBKA  2021-02-19 start  | 
|             if(String.valueOf(att.ParentId).startsWith('a3E')){ | 
|                 opdIds.add(att.ParentId); | 
|                 opdMap.put(att.ParentId,att.Name); | 
|              } | 
|             // MZY SWAG-BXXBKA  2021-02-19 end | 
|   | 
|             if(String.valueOf(att.ParentId).startsWith('a3Z')){ | 
|                 oFOrderIds.add(att.ParentId); | 
|                 oFOrderIdMap.put(att.ParentId,true); | 
|             } | 
|         } | 
|     } | 
|     if (Trigger.isBefore && Trigger.isDelete) { | 
|         for (Attachment att : Trigger.old) { | 
|             //if (att.Name.toUpperCase() == '合同书.PDF' && String.valueOf(att.ParentId).startsWith('a0t')) { | 
|             // 添付した契約書のファイル名は指定できない | 
|             if (String.valueOf(att.ParentId).startsWith('a2K')) { | 
|                 rentaldelIds.add(att.ParentId); | 
|                 attdelIds.add(att.Id); | 
|             } | 
|             if(String.valueOf(att.ParentId).startsWith(System.Label.Oppor_sepKey)){ | 
|                 OpporFileIds.add(att.ParentId); | 
|             } | 
|             // LHJ CHAN-BCLD4P 20190618 Start | 
|             if(String.valueOf(att.ParentId).startsWith(System.Label.OppSpecialApply)){ | 
|                 specDelIds.add(att.ParentId); | 
|             } | 
|             // LHJ CHAN-BCLD4P 20190618 End | 
|             if(String.valueOf(att.ParentId).startsWith('a3Z')){ | 
|                 oFOrderIds.add(att.ParentId); | 
|                 oFOrderIdMap.put(att.ParentId,false); | 
|             } | 
|         } | 
|     } | 
|     if (Trigger.isAfter && Trigger.isDelete) { | 
|         for (Attachment att : Trigger.old) { | 
|             if (String.valueOf(att.ParentId).startsWith('001')) { | 
|                 accIds.add(att.ParentId); | 
|             } | 
|             // LHJ CHAN-BCLD4P 20190618 Start | 
|             if(String.valueOf(att.ParentId).startsWith(System.Label.OppSpecialApply)){ | 
|                 specIds.add(att.ParentId); | 
|             } | 
|             // LHJ CHAN-BCLD4P 20190618 End | 
|         } | 
|     } | 
|   | 
|      | 
|     if (rentaldelIds.size() > 0) { | 
|         List<Consumable_accessories_invoice__c> rentaldelList = new List<Consumable_accessories_invoice__c>(); | 
|         for (Consumable_accessories_invoice__c ra : [select Id from Consumable_accessories_invoice__c where Invoice_code__c in :rentaldelIds and Attachment_ID__c in :attdelIds]) { | 
|             rentaldelList.add(ra); | 
|         } | 
|         ControllerUtil.delConsumableAccessoriesInvoiceSet(rentaldelList); | 
|     } | 
|      | 
|     if (rentalIds.size() > 0) { | 
|         List<Consumable_order__c> rentalList = new List<Consumable_order__c>(); | 
|         for (Consumable_order__c ra : [select Id from Consumable_order__c where Id in :rentalIds]) { | 
|             ra.Consumable_pdf_insert_day__c = Date.today(); | 
|             ra.Order_Attachment__c = attIds[0]; | 
|             //ra.Order_status__c = '录入完成'; | 
|             //ra.Contract_pdf_updated__c = true; | 
|             rentalList.add(ra); | 
|         } | 
|         ControllerUtil.updRentalApplyList(rentalList); | 
|     } | 
|      if (rentalApplyIds.size() > 0) { | 
|         List<Rental_Apply__c> rentalApplyList = new List<Rental_Apply__c>(); | 
|         for (Rental_Apply__c ra : [select Id from Rental_Apply__c where Id in :rentalApplyIds]) { | 
|             ra.Contract_pdf_update_day__c = Date.today(); | 
|             ra.Contract_pdf_updated__c = true; | 
|             rentalApplyList.add(ra); | 
|         } | 
|         ControllerUtil.updRentalApplyList(rentalApplyList); | 
|     } | 
|   | 
|     if (accIds.size() > 0) { | 
|         Map<String, String> attMap = new Map<String, String>(); | 
|         List<Attachment> attList = [select Id, ParentId from Attachment where ParentId = :accIds order by ParentId]; | 
|         for (Attachment att : attList) { | 
|             attMap.put(att.ParentId, att.ParentId); | 
|         } | 
|         List<Account> updAccList = new List<Account>(); | 
|         for (String accid : accIds) { | 
|             Account acc = new Account(Id = accid); | 
|             acc.Is_upload_file__c = attMap.containsKey(accid); | 
|             updAccList.add(acc); | 
|         } | 
|         if (updAccList.size() > 0) update updAccList; | 
|     } | 
|     // LHJ CHAN-BCLD4P 20190618 Start | 
|     if (specIds.size() > 0) { | 
|          | 
|         Map<String, String> attMap = new Map<String, String>(); | 
|         List<Attachment> attList = [select Id, ParentId from Attachment where ParentId = :specIds order by ParentId]; | 
|         for (Attachment att : attList) { | 
|             attMap.put(att.ParentId, att.ParentId); | 
|         } | 
|         List<OpportunitySpecialApply__c> updSpecList = new List<OpportunitySpecialApply__c>(); | 
|         for (String specId : specIds) { | 
|             OpportunitySpecialApply__c spec = new OpportunitySpecialApply__c(Id = specId); | 
|             spec.Is_upload_file__c = attMap.containsKey(specId); | 
|             updSpecList.add(spec); | 
|         } | 
|         if (updSpecList.size() > 0) update updSpecList; | 
|     } | 
|     // LHJ CHAN-BCLD4P 20190618 End | 
|      | 
|     // MZY SWAG-BXXBKA  2021-02-19 start | 
|     System.debug('SWAG-BXXBKA start'); | 
|     if (opdIds.size()>0) { | 
|         List<OPDPlan__c> updateOPDs = new List<OPDPlan__c>(); | 
|   | 
|         OPDPlan__c tempOPD = new OPDPlan__c(); | 
|         for (String i :opdIds ) { | 
|             tempOPD.id = i; | 
|             tempOPD.AttachmentCertificate__c = opdMap.get(i); | 
|             updateOPDs.add(tempOPD); | 
|         } | 
|         System.debug('临时OPD :'+tempOPD); | 
|         if (updateOPDs.size()>0){ | 
|             update updateOPDs; | 
|         } | 
|     } | 
|     System.debug('SWAG-BXXBKA end'); | 
|     // MZY SWAG-BXXBKA  2021-02-19 end | 
|   | 
|     if(OpporFileIds.size()>0){ | 
|         List<Opportunity_File__c> OpporList = [select id,Last_upload_time__c, | 
|                                                             Is_Locked__c, | 
|                                                             Last_upload_user__c, | 
|                                                             Stock_apply_time__c | 
|                                                         from Opportunity_File__c  | 
|                                                         where id  | 
|                                                         in:OpporFileIds ]; | 
|         if(OpporList.size()>0){ | 
|             for(Opportunity_File__c ofc : OpporList){ | 
|                 if(!ofc.Is_Locked__c){ | 
|                     ofc.Last_upload_user__c = UserInfo.getUserId(); | 
|                     ofc.Last_upload_time__c = System.now(); | 
|                     System.debug(ofc+'23333'); | 
|                 }else{ | 
|                     OpporFileUndeleteMap.put(ofc.id,ofc.id); | 
|                     System.debug(ofc+'23333444444'); | 
|                 } | 
|             } | 
|             update OpporList; | 
|         } | 
|     } | 
|     System.debug(Trigger.isDelete+'Trigger.isDelete'); | 
|     System.debug(Trigger.isBefore+'Trigger.isBefore'); | 
|     | 
|   | 
|   | 
|     if(Trigger.isDelete && Trigger.isBefore){ | 
|         for(Attachment att : Trigger.old){ | 
|             if(OpporFileUndeleteMap.containsKey(att.ParentID)){ | 
|                 att.addError('备货已审批,不可删除附件'); | 
|   | 
|                 return; | 
|             }else{ | 
|             } | 
|         } | 
|     } | 
|     if(Trigger.isDelete && Trigger.isAfter){ | 
|         for(Attachment att : Trigger.old){ | 
|             if(OpporFileUndeleteMap.containsKey(att.ParentID)){ | 
|                 att.addError('备货已审批,不可删除附件'); | 
|   | 
|                 return; | 
|             }else{ | 
|                 DeleteReIds.add(att.ParentId); | 
|             } | 
|         } | 
|     } | 
|      | 
|   | 
|     if(Trigger.isInsert){ | 
|         for(Attachment att : Trigger.new){ | 
|             if(OpporFileUndeleteMap.containsKey(att.ParentID)){ | 
|                 att.addError('备货已审批,不可删除附件'); | 
|   | 
|                 return; | 
|             }else{ | 
|                 DeleteReIds.add(att.ParentId); | 
|             } | 
|         } | 
|     } | 
|     List<Attachment> atcList = [select id,ParentId,createddate,createdbyid from Attachment where ParentId in:DeleteReIds]; | 
|     System.debug(DeleteReIds+'222222222222'); | 
|     if(atcList.size()>0){ | 
|         for(String ids : DeleteReIds){ | 
|             for(Attachment atc : atcList){ | 
|                 if(OppordeleteReTimeMap.containsKey(ids)){ | 
|                     if(atc.createddate > OppordeleteReTimeMap.get(ids)){ | 
|                         OppordeleteReTimeMap.put(ids,atc.createddate); | 
|                         OppordeleteReUserMap.put(ids,atc.createdbyid); | 
|                     }else{ | 
|                         continue; | 
|                     } | 
|                 }else{ | 
|                     OppordeleteReTimeMap.put(ids,atc.createddate); | 
|                     OppordeleteReUserMap.put(ids,atc.createdbyid); | 
|                 } | 
|             } | 
|         } | 
|   | 
|   | 
|         List<Opportunity_File__c> reDateList = [select id,Last_upload_time__c, | 
|                                                                 Is_Locked__c, | 
|                                                                 Last_upload_user__c | 
|                                                             from Opportunity_File__c  | 
|                                                             where id  | 
|                                                             in:DeleteReIds ]; | 
|         if(reDateList.size()>0){ | 
|             for(Opportunity_File__c ofc : reDateList){ | 
|                 ofc.Last_upload_time__c = OppordeleteReTimeMap.get(ofc.id); | 
|                 ofc.Last_upload_user__c = OppordeleteReUserMap.get(ofc.id); | 
|                 System.debug(ofc+'233333333'); | 
|             } | 
|             update reDateList; | 
|         } | 
|     }else{ | 
|         List<Opportunity_File__c> reDateList = [select id,Last_upload_time__c, | 
|                                                                 Is_Locked__c, | 
|                                                                 Last_upload_user__c | 
|                                                             from Opportunity_File__c  | 
|                                                             where id  | 
|                                                             in:DeleteReIds ]; | 
|         if(reDateList.size()>0){ | 
|             for(Opportunity_File__c ofc : reDateList){ | 
|                 ofc.Last_upload_time__c = null; | 
|                 ofc.Last_upload_user__c = null; | 
|             } | 
|             update reDateList; | 
|         } | 
|     } | 
|     //CHAN-BCNCRB ---XHL----Start----- | 
|     if (opportunity_FileIds.size() > 0) { | 
|         List<Opportunity_File__c> opporList = [select id,Last_upload_time__c, | 
|                                                             Is_Locked__c, | 
|                                                             Last_upload_user__c, | 
|                                                             Stock_apply_time__c, | 
|                                                             Oppor_File_Stage__c | 
|                                                         from Opportunity_File__c  | 
|                                                         where id in:opportunity_FileIds And Oppor_File_Stage__c = 'G']; | 
|         List<String>  ofIds = new  List<String>();                                               | 
|         if (opporList.size() > 0) { | 
|   | 
|             for (Opportunity_File__c oppf : opporList) { | 
|                 ofIds.add(oppf.Id); | 
|             } | 
|   | 
|             if (ofIds.size() > 0 ) { | 
|                  List<Attachment> atcList = [select id,ParentId,createddate,createdbyid from Attachment where ParentId in:ofIds]; | 
|              | 
|                 if (atcList.size() > 0) { | 
|                     delete atcList; | 
|                 } | 
|             } | 
|             | 
|              | 
|         } | 
|     } | 
|     //CHAN-BCNCRB ---XHL----End----- | 
|      | 
|   | 
|     if (opportunityFileOrderIds.size() > 0) { | 
|         List<OpportunityFileOrder__c> ofoList = [select id, | 
|                                                             OpporFileStage__c | 
|                                                         from OpportunityFileOrder__c  | 
|                                                         where Id in:opportunityFileOrderIds And OpporFileStage__c = 'G']; | 
|         List<String>  ofoIds = new  List<String>();                                               | 
|         if (ofoList.size() > 0) { | 
|   | 
|             for (OpportunityFileOrder__c ofo : ofoList) { | 
|                 ofoIds.add(ofo.Id); | 
|             } | 
|   | 
|             if (ofoIds.size() > 0 ) { | 
|                  List<Attachment> atcList = [select id,ParentId,createddate,createdbyid from Attachment where ParentId in:ofoIds]; | 
|              | 
|                 if (atcList.size() > 0) { | 
|                     delete atcList; | 
|                 } | 
|             } | 
|             | 
|              | 
|         } | 
|     } | 
|   | 
|     if (oFOrderIdMap.size() > 0) { | 
|   | 
|         List<OpportunityFileOrder__c> oFOrderList = [select Id,Name,LatestUploadUser__c,LatestUploadTime__c | 
|                                                     from OpportunityFileOrder__c  | 
|                                                     where Id In : oFOrderIdMap.keySet()]; | 
|         if (oFOrderList.size() > 0 ) { | 
|   | 
|             for (OpportunityFileOrder__c oFOrder :oFOrderList) { | 
|   | 
|                 if (oFOrderIdMap.get(oFOrder.Id)) { | 
|                     oFOrder.LatestUploadUser__c = UserInfo.getUserId(); | 
|                     oFOrder.LatestUploadTime__c = System.now(); | 
|                 } else if (oFOrderIdMap.get(oFOrder.Id) == false){ | 
|                     oFOrder.LatestUploadUser__c = null; | 
|                     oFOrder.LatestUploadTime__c = null; | 
|                 } | 
|             } | 
|   | 
|             update oFOrderList; | 
|   | 
|         }                                             | 
|     } | 
| } |