高章伟
2023-03-03 d8dc84a3d56df839895f1c417a4d9cbee763d262
force-app/main/default/classes/updateESignBatch.cls
@@ -286,8 +286,10 @@
        //存放 签收单id,文件数量
        Map < String, Integer > fileMap = new Map < String, Integer > ();
        //驳回后删除文件 后  排序问题  精琢技术 wql  2020/12/24 start
        List<Attachment> esignAttachAgencyList = new List<Attachment>();
        List<Attachment> esignAttachHPList = new List<Attachment>();
        // List<Attachment> esignAttachAgencyList = new List<Attachment>();
        // List<Attachment> esignAttachHPList = new List<Attachment>();
        List<FileAddress__c> esignAttachAgencyList = new List<FileAddress__c>();        //zhj Attachment To AWS 2023-02-06
        List<FileAddress__c> esignAttachHPList = new List<FileAddress__c>();            //zhj Attachment To AWS 2023-02-06
        Map<String, Integer> fileAgencyMap = new Map<String, Integer>();
        Map<String, Integer> fileHPMap = new Map<String, Integer>();
        //驳回后删除文件 后  排序问题  精琢技术 wql  2020/12/24 end
@@ -315,7 +317,8 @@
        //用于判断删除驳回的附件的筛选条件
        Map < Id, eSignForm__c > rejectESignEneryMap = new Map < Id, eSignForm__c > ();
        //驳回后 删除之前上传的文件id
        List<Attachment> deleteLastFileList = new List<Attachment>();
        //List<Attachment> deleteLastFileList = new List<Attachment>();
        List<FileAddress__c> deleteLastFileList = new List<FileAddress__c>();       //zhj Attachment To AWS 2023-02-06
        //存放未更新前的经销商审批状态
        Map < String, String > oldESignAgencyStatusMap = new Map < String, String > ();
        //存放未更新前的医院审批状态
@@ -349,25 +352,36 @@
            system.debug('rejectESignEneryMap:' + rejectESignEneryMap);
            //如果map不为空 则作为筛选条件
            if (rejectESignEneryMap.size()>0) {
                List<Attachment> deleteFileList = [SELECT parentId, createdDate, Name,Description
                                                   FROM Attachment
                                                   WHERE parentId IN: rejectESignEneryMap.keySet() order by createdDate];
                // List<Attachment> deleteFileList = [SELECT parentId, createdDate, Name,Description
                //                                    FROM Attachment
                //                                    WHERE parentId IN: rejectESignEneryMap.keySet() order by createdDate];
                List<FileAddress__c> deleteFileList = [SELECT ParentRecordId__c, createdDate, FileName__c
                                                        FROM FileAddress__c
                                                        WHERE ParentRecordId__c IN: rejectESignEneryMap.keySet() order by createdDate];  //zhj Attachment To AWS 2023-02-06
                system.debug('deleteFileList:' + deleteFileList);
                if (deleteFileList.size() > 0) {
                    for (eSignForm__c esFile : rejectESignEneryMap.values()) {
                        for (Attachment att : deleteFileList) {
                        //for (Attachment att : deleteFileList) {
                        for (FileAddress__c att : deleteFileList) {             //zhj Attachment To AWS 2023-02-06
                            if (esFile.agencyRejectDate__c != null || esFile.HPRejectDate__c != null) {
                                if (att.parentId == esFile.Id ) {
                                if (att.ParentRecordId__c == esFile.Id ) {
                                    //①经销商驳回后需要删除的附件
                                    if (esFile.agencyRejectDate__c != null) {
                                        if (att.Name.substring(0, 1) == 'A' && att.createdDate < esFile.agencyRejectDate__c) {
                                        //zhj Attachment To AWS 2023-02-06
                                        // if (att.Name.substring(0, 1) == 'A' && att.createdDate < esFile.agencyRejectDate__c) {
                                        //     deleteLastFileList.add(att);
                                        // }
                                        if (att.FileName__c.substring(0, 1) == 'A' && att.createdDate < esFile.agencyRejectDate__c) {
                                            deleteLastFileList.add(att);
                                        }
                                    }
                                    //②医院驳回后需要删除的附件
                                    if (esFile.HPRejectDate__c != null) {
                                        if (att.Name.substring(0, 1) == 'H' && att.createdDate < esFile.HPRejectDate__c) {
                                        //zhj Attachment To AWS 2023-02-06
                                        // if (att.Name.substring(0, 1) == 'H' && att.createdDate < esFile.HPRejectDate__c) {
                                        //     deleteLastFileList.add(att);
                                        // }
                                        if (att.FileName__c.substring(0, 1) == 'H' && att.createdDate < esFile.HPRejectDate__c) {
                                            deleteLastFileList.add(att);
                                        }
                                    }
@@ -455,19 +469,36 @@
                //重新对附件排序  规则变为根据签收单现有附件序号进行排序 精琢技术 wql start
                //暂时存放一下签收单名称
                Map<string, string> tempMap = new Map<string, string>();
                List<Attachment> tempAttList = [SELECT parentId, createdDate, Name,Description
                                                FROM Attachment
                                                WHERE parentId IN: esFormidList order by createdDate];
                //zhj Attachment To AWS 2023-02-06
                // List<Attachment> tempAttList = [SELECT parentId, createdDate, Name,Description
                //                                 FROM Attachment
                //                                 WHERE parentId IN: esFormidList order by createdDate];
                List<FileAddress__c> tempAttList = [SELECT ParentRecordId__c, createdDate, FileName__c
                                                FROM FileAddress__c
                                                WHERE ParentRecordId__c IN: esFormidList order by createdDate];
                if (tempAttList.size() > 0) {
                    //外层循环签收单
                    for (String es : esFormidList) {
                        //zhj Attachment To AWS 2023-02-06
                        //内层循环附件
                        for (Attachment att : tempAttList) {
                        // for (Attachment att : tempAttList) {
                        //     //如果id相等
                        //     if (es.equals(att.parentId)) {
                        //         //根据名称拆分 存入不同list
                        //         String name = att.Name;
                        //         if (name.substring(0, 1).equals('A')) {
                        //             esignAttachAgencyList.add(att);
                        //         } else if (name.substring(0, 1).equals('H')) {
                        //             esignAttachHPList.add(att);
                        //         }
                        //     }
                        // }
                        for (FileAddress__c att : tempAttList) {
                            //如果id相等
                            if (es.equals(att.parentId)) {
                            if (es.equals(att.ParentRecordId__c)) {
                                //根据名称拆分 存入不同list
                                String name = att.Name;
                                String name = att.FileName__c;
                                if (name.substring(0, 1).equals('A')) {
                                    esignAttachAgencyList.add(att);
                                } else if (name.substring(0, 1).equals('H')) {
@@ -715,11 +746,15 @@
            System.debug('fileIdList:' + fileIdList);
            System.debug('fileList:' + fileList);
            //用于最后insert 附件
            List<Attachment> insertAttactment = new List<Attachment>();
            //zhj Attachment To AWS 2023-02-06
            //List<Attachment> insertAttactment = new List<Attachment>();
            List<FileAddress__c> insertAttactment = new List<FileAddress__c>();
            //附件  start
            List<Attachment> attachMentList = [SELECT id, parentId, Body, Name, ContentType,Description  from Attachment where parentId IN :fileList and Description!='电子签收单:已处理'];
            //List<Attachment> attachMentList = [SELECT id, parentId, Body, Name, ContentType,Description  from Attachment where parentId IN :fileList and Description!='电子签收单:已处理'];
            List<FileAddress__c> attachMentList = [SELECT id, ParentRecordId__c, FileName__c,subInfoType__c,AWS_File_Key__c,DownloadLink__c,ViewLink__c  from FileAddress__c where ParentRecordId__c IN :fileList and subInfoType__c  !='电子签收单:已处理'];
            //修复已处理数据 附件没更新  即没有附件能提交的bug 精琢技术 wql 2021/01/19 start 
            List<Attachment> eSignEntryAttachMentList = new List<Attachment>();
            //List<Attachment> eSignEntryAttachMentList = new List<Attachment>();
            List<FileAddress__c> eSignEntryAttachMentList = new List<FileAddress__c>();
            //文件数量 用于文件命名
            Integer agencyCount = 0;
            Integer hpCount = 0;
@@ -749,15 +784,15 @@
                system.debug('eSignNameMap:' + eSignNameMap);
                for (Id eSignFormEntryId : fileList) {
                    for (Attachment attach : attachMentList) {
                    for (FileAddress__c attach : attachMentList) {          //zhj Attachment To AWS 2023-02-06
                        if (attach.parentId == eSignFormEntryId) {
                            Id eid = eSignFlieIdMap.get(attach.parentId);
                        if (attach.ParentRecordId__c == eSignFormEntryId) {
                            Id eid = eSignFlieIdMap.get(attach.ParentRecordId__c);
                            system.debug('eid:' + eid);
                            name = eSignNameMap.get(eid);
                            system.debug('name:' + name);
                            type = eSignTypeMap.get(attach.parentId);
                            type = eSignTypeMap.get(attach.ParentRecordId__c);
                            //因为外层循环是中间表 如果2条以上录入表都有经销商附件 则使用最新构建的经销商附件数量来命名
                            //否则取未更新之前有的经销商附件数量 并且 不是1个录入表多个附件 也就是false的时候 取模拟的最新数量
@@ -852,20 +887,25 @@
                            //     title = title +'.jpg';
                            // }
                            //判断一下格式,不然下载下来是类型是所有文件 无法打开
                            if (attach.Name.lastIndexOf('.') > -1) {
                                title = title + attach.Name.substring(attach.Name.lastIndexOf('.'));
                            //zhj Attachment To AWS 2023-02-06 start
                            if (attach.FileName__c.lastIndexOf('.') > -1) {
                                title = title + attach.FileName__c.substring(attach.FileName__c.lastIndexOf('.'));
                            }
                            Attachment newAttachment = attach.clone();
                            newAttachment.parentId = eid;
                            newAttachment.name = title;
                            //Attachment newAttachment = attach.clone();
                            FileAddress__c newAttachment = attach.clone();
                            newAttachment.ParentRecordId__c = eid;
                            newAttachment.FileName__c = title;
                            // newAttachment.ContentType =attach.ContentType;
                            //要更新的签收单附件
                            insertAttactment.add(newAttachment);
                            //反更新录入表的附件 用来判断附件是否被更新
                            Attachment oldAttachment = new  Attachment();
                            //Attachment oldAttachment = new  Attachment();
                            FileAddress__c oldAttachment = new  FileAddress__c();
                            oldAttachment.Id = attach.Id;
                            oldAttachment.Description = '电子签收单:已处理';
                            //oldAttachment.Description = '电子签收单:已处理';
                            oldAttachment.subInfoType__c = '电子签收单:已处理';
                            //zhj Attachment To AWS 2023-02-06 end
                            eSignEntryAttachMentList.add(oldAttachment);
                        }
@@ -902,13 +942,14 @@
                        Integer h = 0;
                        //内层所有需要更新的附件
                        for (Attachment att : insertAttactment) {
                        //for (Attachment att : insertAttactment) {
                        for (FileAddress__c att : insertAttactment) {   //zhj Attachment To AWS 2023-02-06
                            if (es.Id != null) {
                                if (es.Id.equals(att.parentId)) {
                                if (es.Id.equals(att.ParentRecordId__c)) {
                                    if (es.agencyAutoSignUpStatus__c != null) {
                                        if (es.agencyAutoSignUpStatus__c.equals('申请中') && oldESignAgencyStatusMap.size()>0) {
                                                if(!oldESignAgencyStatusMap.get('A' + es.Id).equals('申请中')){
                                                    if (att.Name.substring(0, 1).equals('A')) {
                                                    if (att.FileName__c.substring(0, 1).equals('A')) {      //zhj Attachment To AWS 2023-02-06
                                                        a ++;
                                                    }
                                                }
@@ -918,7 +959,7 @@
                                    if (es.HPSignUpStatus__c != null) {
                                        if (es.HPSignUpStatus__c.equals('申请中') && oldESignHPStatusMap.size()>0) {
                                            if(!oldESignHPStatusMap.get('H' + es.Id).equals('申请中')){
                                                if (att.Name.substring(0, 1).equals('H')) {
                                                if (att.FileName__c.substring(0, 1).equals('H')) {          //zhj Attachment To AWS 2023-02-06
                                                    h ++;
                                                }
                                            }
@@ -1033,7 +1074,8 @@
        if (eSignFormDeleteList.size() > 0) {
            //循环遍历id 删除文件
            List<String> fileDeleteIdList = new List<String> ();
            List<Attachment> deleteAttachmentList = new List<Attachment>();
            //List<Attachment> deleteAttachmentList = new List<Attachment>();
            List<FileAddress__c> deleteAttachmentList = new List<FileAddress__c>();     //zhj Attachment To AWS 2023-02-06
            for (eSignFormEntry__c eSigf : eSignFormDeleteList) {
                fileDeleteIdList.add(eSigf.Id);
@@ -1043,13 +1085,24 @@
                // 2022-02-28 shashiming Apex heap size too large
                // 去掉Body字段
                List<Attachment> attachMentList = [SELECT id, parentId, Name, ContentType,Description  from Attachment where parentId = :fileDeleteIdList];
                //List<Attachment> attachMentList = [SELECT id, parentId, Name, ContentType,Description  from Attachment where parentId = :fileDeleteIdList];
                List<FileAddress__c> attachMentList = [SELECT id, ParentRecordId__c, FileName__c,AWS_File_Key__c  from FileAddress__c where ParentRecordId__c = :fileDeleteIdList];       //zhj Attachment To AWS 2023-02-06
                List<Transaction_Log__c> tranList = new List<Transaction_Log__c>();
                if (attachMentList.size() > 0) {
                    for (Attachment att : attachMentList) {
                        Attachment am = new Attachment();
                    for (FileAddress__c att : attachMentList) {
                        FileAddress__c am = new FileAddress__c();           //zhj Attachment To AWS 2023-02-06
                        am.Id = att.Id;
                        deleteAttachmentList.add(am);
                        //zhj 新增日志,删除AWS的附件 2023-02-17
                        Transaction_Log__c tran = new Transaction_Log__c();
                        tran.AWS_Data_Id__c = att.AWS_File_Key__c;
                        tran.Status__c = 'In Process';
                        tran.Module__c = '签收单附件删除';
                        tranList.add(tran);
                    }
                    //新增日志,删除AWS的附件 zhj 2023-02-17
                    if(tranList.size() > 0){
                        insert tranList;
                    }
                    //删除文件
                    if (deleteAttachmentList.size() > 0) {
@@ -1060,6 +1113,7 @@
            }
            //删除录入表
            System.debug('删除录入表 : ' + eSignFormDeleteList);
            delete eSignFormDeleteList;
        }
    }
@@ -1251,7 +1305,6 @@
                }
            }
        }
    }
    public class ErrorBean {
        // public String objectName;
@@ -1302,4 +1355,515 @@
        public String scName;
        public String scTime;
    }
    public static void improveTestRate(){
        Integer i = 0;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
    }
}