19626
2023-10-07 988f9735377909b6310301e582c15804e004783f
force-app/main/default/triggers/ContentDocumentLink.trigger
@@ -1,4 +1,31 @@
trigger ContentDocumentLink on ContentDocumentLink (before insert,after insert,after delete, before update, after update) {
trigger ContentDocumentLink on ContentDocumentLink (before insert,after insert,before delete) {
    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>();
@@ -44,7 +71,23 @@
            }
        }
    }
    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 +152,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
@@ -209,10 +235,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('非草案中,附件不允许删除。');
                
@@ -232,7 +268,10 @@
        //     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
@@ -268,6 +307,7 @@
            
        }
        try {
            System.debug('客户变更申请');
            if (updAccAppList.size() > 0) update updAccAppList; 
        } catch (Exception e) {
            linkList[0].addError(e.getMessage());
@@ -328,18 +368,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('备货已审批,不可删除附件');
@@ -365,68 +406,75 @@
            }
        }
    }
    // 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 +550,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();