1
chenjingwu
2024-05-24 35910c355f74e736e2068a0ffb7f39ae9615d8e1
force-app/main/default/triggers/ContentDocumentLink.trigger
@@ -1,4 +1,33 @@
trigger ContentDocumentLink on ContentDocumentLink (before insert,after insert,after delete, before update, after update) {
trigger ContentDocumentLink on ContentDocumentLink (before insert,after insert,before delete) {
    // 20231103 陈京武 Lightning文件修改 Start
    if(StaticParameter.ContentDocumentLink == false){
        return;
    }
    if((!Test.isRunningTest())&&UserInfo.getUserId()==System.Label.ByPassTrigger){
        return;
    }
//     if (Trigger.isAfter && Trigger.isInsert) {
//         Set<Id> Ids = new Set<id>();
//         Map<Id,ContentDocumentLink> mapcvwithid = new Map<Id,ContentDocumentLink>();
//         for(ContentDocumentLink c:Trigger.new){
//         ids.add(c.ContentDocumentId);
//         mapcvwithid.put(c.ContentDocumentId,c);
//         }
//     if(ids.size() >  0){
//         for(ContentDocument ContentDocumentObj : [select id,FileType from ContentDocument where id in: ids]){
//             if(mapcvwithid.get(ContentDocumentObj.Id).LinkedEntityId != null && ContentDocumentObj.FileType != null){
//                 String ObjectName = mapcvwithid.get(ContentDocumentObj.Id).LinkedEntityId.getSObjectType().getDescribe().getName();
//                 String filetype = ContentDocumentObj.FileType.toLowerCase();
//                 String CsName = ObjectName+'-'+filetype;
//                     ContentDocumentLink ContentDocumentLinkObj = mapcvwithid.get(ContentDocumentObj.Id);
//                     ContentDocumentLinkObj.addError('Testing error');
//                     return;
//             }
//         }
//     }
// }
    List<String> rentalIds = new List<String>();
    List<String> attIds = new List<String>();
    List<String> rentalApplyIds = new List<String>();
@@ -33,6 +62,8 @@
    List<String> rentaldeleteApplyFaultIds = new List<String>();
    Map<String,List<Id>> deleteImages = new Map<String,List<Id>>();
    // sx 20230210end 备品借出申请故障报告附件提取
    Set<Id> docidList = new Set<Id>();
    Set<Id> delDocidList = new Set<Id>();
    if (Trigger.isBefore && Trigger.isInsert) {
        for (ContentDocumentLink link : Trigger.new) {
            if(String.valueOf(link.LinkedEntityId).startsWith(System.Label.Oppor_sepKey)){
@@ -42,9 +73,48 @@
            if(String.valueOf(link.LinkedEntityId).startsWith('a3Z')){
                opportunityFileOrderIds.add(link.LinkedEntityId);
            }
            docidList.add(link.ContentDocumentId);
        }
    }
    if (Trigger.isAfter && Trigger.isInsert) {
        for (ContentDocumentLink link : Trigger.new) {
            docidList.add(link.ContentDocumentId);
        }
    }
    if(Trigger.isDelete && Trigger.isBefore){
        for (ContentDocumentLink link : Trigger.old) {
            docidList.add(link.ContentDocumentId);
            delDocidList.add(link.ContentDocumentId);
        }
    }
    ContentVersion ver = new ContentVersion();
    System.debug('docidList.size=>' + docidList.size());
    if(docidList.size() > 0){
        ver = [select FileType,TextPreview from ContentVersion where ContentDocumentId in: docidList limit 1];
        System.debug('TextPreview=>' + ver.TextPreview);
        System.debug('FileType=>' + ver.TextPreview);
        if(ver.FileType == 'SNOTE'){
            System.debug('可恶的备注!!!');
            return;
        }
    }
    if (Trigger.isBefore && Trigger.isDelete) {
        for (ContentDocumentLink link : Trigger.old) {
            if (String.valueOf(link.LinkedEntityId).startsWith('001')) {
                accIds.add(link.LinkedEntityId);
            }
            // LHJ CHAN-BCLD4P 20190618 Start
            if(String.valueOf(link.LinkedEntityId).startsWith(System.Label.OppSpecialApply)){
                specIds.add(link.LinkedEntityId);
            }
            // LHJ CHAN-BCLD4P 20190618 End
            //20221014 yc 医院审批优化 start
            if (String.valueOf(link.LinkedEntityId).startsWith('a5C') || String.valueOf(link.LinkedEntityId).startsWith('a5A')) {
                accAppIds.add(link.LinkedEntityId);
            }
            //20221014 yc 医院审批优化 end
        }
    }
    //CHAN-BCNCRB ---XHL----End-----
    if (Trigger.isAfter && Trigger.isInsert) {
        List<Id> idList = new List<Id>();
@@ -109,23 +179,6 @@
        }
    }
    if (Trigger.isAfter && Trigger.isDelete) {
        for (ContentDocumentLink link : Trigger.old) {
            if (String.valueOf(link.LinkedEntityId).startsWith('001')) {
                accIds.add(link.LinkedEntityId);
            }
            // LHJ CHAN-BCLD4P 20190618 Start
            if(String.valueOf(link.LinkedEntityId).startsWith(System.Label.OppSpecialApply)){
                specIds.add(link.LinkedEntityId);
            }
            // LHJ CHAN-BCLD4P 20190618 End
            //20221014 yc 医院审批优化 start
            if (String.valueOf(link.LinkedEntityId).startsWith('a5C') || String.valueOf(link.LinkedEntityId).startsWith('a5A')) {
                accAppIds.add(link.LinkedEntityId);
            }
            //20221014 yc 医院审批优化 end
        }
    }
    
    //2023-2-10  sx备品 备品检测分析报告 start
@@ -192,10 +245,14 @@
    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];
        List<ContentDocumentLink> linkList = [select LinkedEntityId from ContentDocumentLink where LinkedEntityId =: accIds order by LinkedEntityId];
        List<ContentDocumentLink> linkList = [select ContentDocumentId,LinkedEntityId from ContentDocumentLink where ContentDocument.FileType != 'SNOTE' and LinkedEntityId =: accIds order by LinkedEntityId];
        System.debug('客户Link的条数:' + linkList.size());
        for (ContentDocumentLink link : linkList) {
            attMap.put(link.LinkedEntityId, link.LinkedEntityId);
            if(!delDocidList.contains(link.ContentDocumentId)){
                attMap.put(link.LinkedEntityId, link.LinkedEntityId);
            }
        }
        System.debug('attMap的条数:' + attMap);
        List<Account> updAccList = new List<Account>();
        //20221207 lt 医院审批优化 start
@@ -209,10 +266,20 @@
            if(accList.size() > 0){
                // Account acc = new Account(Id = accid);
                //20221207 lt 医院审批优化 start
                for(Account acc : accList){
                    if(Trigger.isDelete && (Trigger.isAfter)){
                for(Account acc : accList){
                    if(Trigger.isUpdate && Trigger.isAfter){
                        for(ContentDocumentLink link : Trigger.New){
                            if(attMap.containsKey(link.LinkedEntityId) &&acc.Is_Active__c != '草案中'){
                                System.debug('lt123---草案中---');
                                link.addError('非草案中,附件不允许编辑。');
                                return;
                            }
                        }
                    }
                    if(Trigger.isDelete && (Trigger.isBefore)){
                        for(ContentDocumentLink link : Trigger.old){
                            // if(attMap.containsKey(att.ParentID) && acc.Is_Active__c != '草案中'){
                            if(acc.Is_Active__c != '草案中'){
                                link.addError('非草案中,附件不允许删除。');
                
@@ -221,6 +288,7 @@
                        }
                    }
                    acc.Is_upload_file__c = attMap.containsKey(accid);
                    System.debug('是否上传附件:' + attMap.containsKey(accid));
                    updAccList.add(acc);
                }
            }
@@ -232,16 +300,21 @@
        //     acc.Is_upload_file__c = attMap.containsKey(accid);
        //     updAccList.add(acc);
        // }
        if (updAccList.size() > 0) update updAccList;
        if (updAccList.size() > 0){
            StaticParameter.ContentDocumentTrigger = false;
            update updAccList;
        }
    }
    //20221014 yc 医院审批优化 start
    if (accAppIds.size() > 0) {
        Map<String, String> attMap = new Map<String, String>();
        // List<Attachment> attList = [select Id, ParentId from Attachment where ParentId = :accAppIds order by ParentId];
        List<ContentDocumentLink> linkList = [select LinkedEntityId from ContentDocumentLink where LinkedEntityId =: accAppIds order by LinkedEntityId];
        List<ContentDocumentLink> linkList = [select ContentDocumentId,LinkedEntityId from ContentDocumentLink where  ContentDocument.FileType != 'SNOTE' and  LinkedEntityId =: accAppIds order by LinkedEntityId];
        for (ContentDocumentLink link : linkList) {
            attMap.put(link.LinkedEntityId, link.LinkedEntityId);
            if(!delDocidList.contains(link.ContentDocumentId)){
                attMap.put(link.LinkedEntityId, link.LinkedEntityId);
            }
        }
        List<Account_Delay_Apply__c> updAccAppList = new List<Account_Delay_Apply__c>();
        
@@ -268,7 +341,11 @@
            
        }
        try {
            if (updAccAppList.size() > 0) update updAccAppList;
            System.debug('客户变更申请');
            if (updAccAppList.size() > 0){
                StaticParameter.ContentDocumentTrigger = false;
                update updAccAppList;
            }
        } catch (Exception e) {
            linkList[0].addError(e.getMessage());
        }
@@ -281,7 +358,7 @@
        
        Map<String, String> attMap = new Map<String, String>();
        // List<Attachment> attList = [select Id, ParentId from Attachment where ParentId = :specIds order by ParentId];
        List<ContentDocumentLink> linkList = [select LinkedEntityId from ContentDocumentLink where LinkedEntityId =: specIds order by LinkedEntityId];
        List<ContentDocumentLink> linkList = [select LinkedEntityId from ContentDocumentLink where  ContentDocument.FileType != 'SNOTE' and  LinkedEntityId =: specIds order by LinkedEntityId];
        for (ContentDocumentLink link : linkList) {
            attMap.put(link.LinkedEntityId, link.LinkedEntityId);
        }
@@ -328,18 +405,19 @@
                    ofc.Last_upload_user__c = UserInfo.getUserId();
                    ofc.Last_upload_time__c = System.now();
                    System.debug(ofc+'23333');
                    update OpporList;
                }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.isAfter){
    if(Trigger.isDelete && Trigger.isBefore){
        for(ContentDocumentLink link : Trigger.old){
            if(OpporFileUndeleteMap.containsKey(link.LinkedEntityId)){
                link.addError('备货已审批,不可删除附件');
@@ -360,73 +438,83 @@
                System.debug(Trigger.isBefore+'陈京武');
                return;
            }else{
                System.debug(Trigger.isBefore+'武');
                DeleteReIds.add(link.LinkedEntityId);
                if(link.LinkedEntityId.getSObjectType().getDescribe().getName() != 'User'){
                    DeleteReIds.add(link.LinkedEntityId);
                }
                // System.debug(Trigger.isBefore+'武');
                // DeleteReIds.add(link.LinkedEntityId);
            }
        }
    }
    // List<Attachment> atcList = [select id,ParentId,createddate,createdbyid from Attachment where ParentId in:DeleteReIds];
    List<ContentDocumentLink> linkList = [select ContentDocumentId from ContentDocumentLink where LinkedEntityId in: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);
    if(DeleteReIds.size() > 0){
        // List<Attachment> atcList = [select id,ParentId,createddate,createdbyid from Attachment where ParentId in:DeleteReIds];
        List<ContentDocumentLink> linkList = [select LinkedEntityId,ContentDocumentId from ContentDocumentLink where LinkedEntityId in:DeleteReIds];
        List<Id> idList = new List<Id>();
        for (ContentDocumentLink link : linkList) {
            if(link.LinkedEntityId.getSObjectType().getDescribe().getName() != 'User'){
                idList.add(link.ContentDocumentId);
            }
        }
        List<ContentDocument> docList = [select Id,CreatedDate,CreatedById from ContentDocument where Id =: idList];
        System.debug(DeleteReIds+'222222222222');
        System.debug(docList + 'doclist');
        if(docList.size()>0){
            for(String ids : DeleteReIds){
                for(ContentDocument doc : docList){
                    if(OppordeleteReTimeMap.containsKey(ids)){
                        if(doc.CreatedDate > OppordeleteReTimeMap.get(ids)){
                            OppordeleteReTimeMap.put(ids,doc.CreatedDate);
                            OppordeleteReUserMap.put(ids,doc.CreatedById);
                        }else{
                            continue;
                        }
                    }else{
                        continue;
                        OppordeleteReTimeMap.put(ids,doc.CreatedDate);
                        OppordeleteReUserMap.put(ids,doc.CreatedById);
                    }
                }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');
            }
            if(Trigger.isInsert && Trigger.isAfter){
                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){
                if (!ofc.Is_Locked__c) {
                    ofc.Last_upload_time__c = null;
                    ofc.Last_upload_user__c = null;
            System.debug(OppordeleteReTimeMap + 'OppordeleteReTimeMap');
            System.debug(OppordeleteReUserMap + 'OppordeleteReUserMap');
            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');
                }
                if(Trigger.isInsert && Trigger.isAfter){
                    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){
                    if (!ofc.Is_Locked__c) {
                        ofc.Last_upload_time__c = null;
                        ofc.Last_upload_user__c = null;
                        update reDateList;
                    }
                }
            }
        }
    }
    //CHAN-BCNCRB ---XHL----Start-----
    if (opportunity_FileIds.size() > 0) {
@@ -502,7 +590,7 @@
        if (oFOrderList.size() > 0 ) {
            for (OpportunityFileOrder__c oFOrder :oFOrderList) {
                System.debug(oFOrderIdMap.get(oFOrder.Id));
                if (oFOrderIdMap.get(oFOrder.Id)) {
                    oFOrder.LatestUploadUser__c = UserInfo.getUserId();
                    oFOrder.LatestUploadTime__c = System.now();
@@ -524,6 +612,6 @@
    // DB202304087063 SFDC系统附件上传自动通知功能开发需求 end
// 20231103 陈京武 Lightning文件修改 End
    
}