trigger ContentDocumentTrigger on ContentDocument (before delete) {
|
if((!Test.isRunningTest())&&UserInfo.getUserId()==System.Label.ByPassTrigger){
|
return;
|
}
|
System.debug('enter ContentDocumentTrigger');
|
List<String> rentaldelIds = new List<String>();
|
List<String> attdelIds = new List<String>();
|
// LHJ CHAN-BCLD4P 20190618 Start
|
List<String> specDelIds = new List<String>();
|
// LHJ CHAN-BCLD4P 20190618 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>();
|
|
List<String> oFOrderIds = new List<String>();
|
Map<String,Boolean> oFOrderIdMap = new Map<String,Boolean>();
|
|
// sx 20230210start 备品借出申请故障报告附件提取
|
List<String> rentaldeleteApplyFaultIds = new List<String>();
|
Map<String,List<Id>> deleteImages = new Map<String,List<Id>>();
|
// sx 20230210end 备品借出申请故障报告附件提取
|
|
|
//deloitte-zhj 发票附件删除 20230901 start
|
String invoiceStart = System.Label.invoiceStart;
|
List<String> rentaldelIdsZhj = new List<String>();
|
List<String> attdelIdsZhj = new List<String>();
|
|
if (Trigger.isBefore && Trigger.isDelete) {
|
for (ContentDocument cd : Trigger.old) {
|
System.debug('cd.Id = ' + cd.Id);
|
List<ContentDocumentLink> cdlList = [SELECT id, LinkedEntityId FROM ContentDocumentLink WHERE ContentDocumentId = :cd.Id];
|
System.debug('cdlList = ' + cdlList);
|
System.debug('cd.Id = ' + cd.Id);
|
for (ContentDocumentLink cdl : cdlList) {
|
if (String.valueOf(cdl.LinkedEntityId).startsWith(invoiceStart)) {
|
System.debug('String.valueOf(cdl.LinkedEntityId).startsWith(invoiceStart)');
|
rentaldelIdsZhj.add(cdl.LinkedEntityId);
|
attdelIdsZhj.add(cd.Id);
|
}
|
}
|
}
|
}
|
if (rentaldelIdsZhj.size() > 0) {
|
System.debug('rentaldelIdsZhj = ' + rentaldelIdsZhj);
|
System.debug('attdelIdsZhj = ' + attdelIdsZhj);
|
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 :rentaldelIdsZhj AND Attachment_ID__c IN :attdelIdsZhj
|
]) {
|
rentaldelList.add(ra);
|
}
|
System.debug('rentaldelList = ' + rentaldelList);
|
ControllerUtil.delConsumableAccessoriesInvoiceSet(rentaldelList);
|
}
|
//deloitte-zhj 发票附件删除 20230901 end
|
|
//CHAN-BCNCRB ---XHL----End-----
|
|
if (Trigger.isBefore && Trigger.isDelete) {
|
// List<Attachment> attDeleteList = new List<Attachment>();
|
List<ContentDocumentLink> conDeleteList = new List<ContentDocumentLink>();
|
List<Id> idList = new List<Id>();
|
for (ContentDocument doc : Trigger.old) {
|
idList.add(doc.Id);
|
}
|
List<ContentDocumentLink> link = [select LinkedEntityId from ContentDocumentLink where ContentDocumentId in: idList limit 1];
|
List<ContentVersion> version = [select Id from ContentVersion where ContentDocumentId in: idList];
|
for (ContentDocument doc : Trigger.old) {
|
//if (att.Name.toUpperCase() == '合同书.PDF' && String.valueOf(att.ParentId).startsWith('a0t')) {
|
// 添付した契約書のファイル名は指定できない
|
if (String.valueOf(link[0].LinkedEntityId).startsWith('a2K')) {
|
rentaldelIds.add(link[0].LinkedEntityId);
|
attdelIds.add(version[0].Id);
|
}
|
if(String.valueOf(link[0].LinkedEntityId).startsWith(System.Label.Oppor_sepKey)){
|
OpporFileIds.add(link[0].LinkedEntityId);
|
}
|
// LHJ CHAN-BCLD4P 20190618 Start
|
if(String.valueOf(link[0].LinkedEntityId).startsWith(System.Label.OppSpecialApply)){
|
specDelIds.add(link[0].LinkedEntityId);
|
}
|
// LHJ CHAN-BCLD4P 20190618 End
|
if(String.valueOf(link[0].LinkedEntityId).startsWith('a3Z')){
|
oFOrderIds.add(link[0].LinkedEntityId);
|
oFOrderIdMap.put(link[0].LinkedEntityId,false);
|
}
|
//2023-2-10 sx备品 备品检测分析报告 start
|
Id cid = link[0].LinkedEntityId;
|
String parentLabel = cid.getSObjectType().getDescribe().getName();
|
if(parentLabel.equals('Rental_Apply_Fault__c')){
|
rentaldeleteApplyFaultIds.add(link[0].LinkedEntityId);
|
conDeleteList.add(link[0]);
|
if(deleteImages.containskey(version[0].Id)){
|
deleteImages.get(link[0].LinkedEntityId).add(version[0].Id);
|
}else{
|
List<Id> temp = new List<Id>();
|
temp.add(version[0].Id);
|
deleteImages.put(link[0].LinkedEntityId,temp);
|
}
|
}
|
//2023-2-10 sx备品 备品检测分析报告 end
|
}
|
//2023-2-10 sx备品 备品检测分析报告 start
|
List<Rental_Apply_Fault__c> rafList = [SELECT Id, Name FROM Rental_Apply_Fault__c WHERE Id IN: rentaldeleteApplyFaultIds AND (status__c = '已发送' OR status__c = '已反馈')];
|
for (Rental_Apply_Fault__c raf : rafList) {
|
for (ContentDocumentLink link1 : conDeleteList) {
|
if (link1.LinkedEntityId == raf.Id) {
|
// ContentDocument doc = [select Id from ContentDocument where Id =: link.ContentDocumentId];
|
link1.addError('非草案中的检测分析报告不允许删除已上传的故障图片');
|
}
|
}
|
}
|
//2023-2-10 sx备品 备品检测分析报告 end
|
}
|
|
|
|
//20221207 lt 医院审批优化 start
|
//2023-2-10 sx备品 备品检测分析报告 start
|
|
if(rentaldeleteApplyFaultIds.size() > 0){
|
List<Rental_Apply_Fault__c> renList = [SELECT Id,AttachmentId1__c,AttachmentId2__c from Rental_Apply_Fault__c where Id in :rentaldeleteApplyFaultIds];
|
|
system.debug('renList====='+renList);
|
for(Rental_Apply_Fault__c ren : renList){
|
if(deleteImages.containskey(ren.Id)){
|
|
if(deleteImages.get(ren.Id).contains(ren.AttachmentId1__c)){
|
ren.AttachmentId1__c = '';
|
}
|
if(deleteImages.get(ren.Id).contains(ren.AttachmentId2__c)){
|
ren.AttachmentId2__c = '';
|
}
|
}
|
}
|
system.debug('renList====='+renList);
|
UPDATE renList;
|
}
|
//2023-2-10 sx备品 备品检测分析报告 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(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');
|
update ofc;
|
}else{
|
|
OpporFileUndeleteMap.put(ofc.id,ofc.id);
|
System.debug(ofc+'23333444444');
|
}
|
}
|
}
|
}
|
System.debug(Trigger.isDelete+'Trigger.isDelete');
|
System.debug(Trigger.isBefore+'Trigger.isBefore');
|
|
|
|
if(Trigger.isDelete && Trigger.isBefore){
|
List<Id> idList = new List<Id>();
|
for(ContentDocument doc : Trigger.old){
|
idList.add(doc.Id);
|
}
|
List<ContentDocumentLink> link = [select LinkedEntityId from ContentDocumentLink where ContentDocumentId in: idList];
|
for(ContentDocument doc : Trigger.old){
|
if(OpporFileUndeleteMap.containsKey(link[0].LinkedEntityId)){
|
doc.addError('备货已审批,不可删除附件');
|
return;
|
}else{
|
}
|
}
|
}
|
if(Trigger.isDelete && Trigger.isAfter){
|
List<Id> idList = new List<Id>();
|
for(ContentDocument doc : Trigger.old){
|
idList.add(doc.Id);
|
}
|
List<ContentDocumentLink> link = [select LinkedEntityId from ContentDocumentLink where ContentDocumentId in: idList];
|
for(ContentDocument doc : Trigger.old){
|
// List<ContentDocumentLink> link = [select LinkedEntityId from ContentDocumentLink where ContentDocumentId =: doc.Id];
|
if(OpporFileUndeleteMap.containsKey(link[0].LinkedEntityId)){
|
link[0].addError('备货已审批,不可删除附件');
|
return;
|
}else{
|
DeleteReIds.add(link[0].LinkedEntityId);
|
}
|
}
|
}
|
|
|
// List<Attachment> atcList = [select id,ParentId,createddate,createdbyid from Attachment where ParentId in:DeleteReIds];
|
if (DeleteReIds.size() > 0) {
|
List<ContentDocumentLink> linkList = [select ContentDocumentId from ContentDocumentLink where LinkedEntityId =:DeleteReIds];
|
List<Id> idList = new List<Id>();
|
for (ContentDocumentLink link : linkList) {
|
idList.add(link.ContentDocumentId);
|
}
|
List<ContentDocument> docList = [select Id,ArchivedDate,ArchivedById from ContentDocument where Id =: idList];
|
System.debug(DeleteReIds+'222222222222');
|
if(docList.size()>0){
|
for(String ids : DeleteReIds){
|
for(ContentDocument doc : docList){
|
if(OppordeleteReTimeMap.containsKey(ids)){
|
if(doc.ArchivedDate > OppordeleteReTimeMap.get(ids)){
|
OppordeleteReTimeMap.put(ids,doc.ArchivedDate);
|
OppordeleteReUserMap.put(ids,doc.ArchivedById);
|
}else{
|
continue;
|
}
|
}else{
|
OppordeleteReTimeMap.put(ids,doc.ArchivedDate);
|
OppordeleteReUserMap.put(ids,doc.ArchivedById);
|
}
|
}
|
}
|
|
|
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;
|
}
|
}
|
}
|
|
|
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;
|
|
}
|
}
|
|
}
|