public without sharing class RentalApplyFaultHandler extends Oly_TriggerHandler {
    private Map<Id, Rental_Apply_Fault__c> newMap;
    private Map<Id, Rental_Apply_Fault__c> oldMap;
    private List<Rental_Apply_Fault__c> newList;
    private List<Rental_Apply_Fault__c> oldList;

    public RentalApplyFaultHandler(){
        this.newMap =  (Map<Id, Rental_Apply_Fault__c>) Trigger.newMap;
        this.oldMap =  (Map<Id, Rental_Apply_Fault__c>) Trigger.oldMap;
        this.newList = (List<Rental_Apply_Fault__c>) Trigger.new;
        this.oldList = (List<Rental_Apply_Fault__c>) Trigger.old;

        
    }


    protected override void beforeInsert() {

        //List 备品借出申请
        List<String> raListIds = new List<String>();
        Set<String> radListIds = new Set<String>();
        //申请单Id, List<故障报告>
        Map<Id, List<Rental_Apply_Fault__c>> raMap = new Map<Id, List<Rental_Apply_Fault__c>>();

        //Name 规则 ： 备品借出申请单 + 001 ~999
        for(Rental_Apply_Fault__c raf: newList){
            //获取所有的申请单Id
            radListIds.add(raf.Rental_Apply_Equipment_Set_Detail__c);
        }

        
        // 获取申请单下所有明细的Id
        List<Rental_Apply_Equipment_Set_Detail__c> raesdList = [SELECT Id,Rental_Apply__c from Rental_Apply_Equipment_Set_Detail__c WHERE Id in :radListIds];
        for(Rental_Apply_Equipment_Set_Detail__c rae : raesdList){
            raListIds.add(rae.Rental_Apply__c);
        }

        raesdList = [SELECT Id,Rental_Apply__c from Rental_Apply_Equipment_Set_Detail__c WHERE Rental_Apply__c in :raListIds];
        for(Rental_Apply_Equipment_Set_Detail__c rea : raesdList){
            radListIds.add(rea.Id);
        }

        system.debug('radListIds===='+radListIds);
        
        //查出 申请单下全部的检测报告
        List<Rental_Apply_Fault__c> raList = [SELECT Id, Rental_Apply_Equipment_Set_Detail__c, Rental_Apply_Equipment_Set_Detail__r.Rental_Apply__r.Id,Rental_Apply_Equipment_Set_Detail__r.Rental_Apply__r.Name from Rental_Apply_Fault__c where Rental_Apply_Equipment_Set_Detail__c in :radListIds];

        //查出 所有申请单明细
        Map<Id,Rental_Apply_Equipment_Set_Detail__c> radMap = new Map<Id,Rental_Apply_Equipment_Set_Detail__c>([
                Select Id,Rental_Apply__r.Name, Rental_Apply__r.Id, Rental_Apply__r.Person_In_Charge__c, Rental_Apply__r.demo_purpose2__c,
                        Rental_Apply__r.Rental_Assistant__c, Rental_Apply__r.Rental_Assistant2__c, Rental_Apply__r.Loaner_Storage_mail_address__c,
                        Rental_Apply__r.ZongjianApprovalManager__c, Rental_Apply__r.BuchangApprovalManagerSales__c, SerialNumber_F__c,
                        Rental_Apply__r.SalesManager__c, Rental_Apply__r.Loaner_centre_mail_address__c, Rental_Apply__r.Hospital__r.Name,
                        Fixture_Model_No__c, Rental_Apply__r.OCM_dept_category__c, Rental_Apply__r.WorkPlace__c, (select id from Rental_Apply_Equipment_Set_Detail_Fault__r)
                from Rental_Apply_Equipment_Set_Detail__c 
                where id in :radListIds]);

        system.debug('radMap===='+radMap);
        for (Rental_Apply_Fault__c raf : raList) {
            system.debug('raf.Rental_Apply_Equipment_Set_Detail__c==='+raf.Rental_Apply_Equipment_Set_Detail__c);
            system.debug('radMap.get(raf.Rental_Apply_Equipment_Set_Detail__c).Rental_Apply__r.Id===' + radMap.get(raf.Rental_Apply_Equipment_Set_Detail__c).Rental_Apply__r.Id);
            if(raMap.containsKey(radMap.get(raf.Rental_Apply_Equipment_Set_Detail__c).Rental_Apply__r.Id)){
                raMap.get(radMap.get(raf.Rental_Apply_Equipment_Set_Detail__c).Rental_Apply__r.Id).add(raf);
            } else {
                List<Rental_Apply_Fault__c> tempList = new List<Rental_Apply_Fault__c>();
                tempList.add(raf);
                raMap.put(radMap.get(raf.Rental_Apply_Equipment_Set_Detail__c).Rental_Apply__r.Id,tempList);
            }
        }

        system.debug('raMap===='+raMap);

        for(Rental_Apply_Fault__c raf : newList){

            //当前借出明细Id
            String radId = raf.Rental_Apply_Equipment_Set_Detail__c;
            //当前申请单Id
            String raId = radMap.get(radId).Rental_Apply__r.Id;

            if( radMap.containsKey(raf.Rental_Apply_Equipment_Set_Detail__c) && radMap.get(raf.Rental_Apply_Equipment_Set_Detail__c).Rental_Apply_Equipment_Set_Detail_Fault__r.size() >= 1 ){
                raf.addError('已存在检测分析报告');
            }

            if(raMap.containsKey(raId)){
                
                Integer rafSize = raMap.get(raId).size() + 1;
                system.debug('rafSize===='+rafSize);
                String rafSizeString = '000' + rafSize;
                String cutString = rafSizeString.substring(rafSizeString.length()-3, rafSizeString.length());

                if(rafSize == 0) {
                    raf.Name = radMap.get(radId).Rental_Apply__r.Name + '-JCBG-001';
                }else{
                    raf.Name = radMap.get(radId).Rental_Apply__r.Name + '-JCBG-' + cutString;
                }
            }else{
                raf.Name = radMap.get(radId).Rental_Apply__r.Name + '-JCBG-001';
            }

            raf.RentalApplyNo__c = radMap.get(radId).Rental_Apply__r.Name;
            raf.demo_purpose2__c = radMap.get(radId).Rental_Apply__r.demo_purpose2__c;
            raf.HospitalText__c = radMap.get(radId).Rental_Apply__r.Hospital__r.Name;
            raf.Repair_product_body_number__c = radMap.get(radId).SerialNumber_F__c;
            raf.Fixture_Model_No__c = radMap.get(radId).Fixture_Model_No__c;
            raf.OCM_dept_category__c = radMap.get(radId).Rental_Apply__r.OCM_dept_category__c;
            raf.WorkPlace__c = radMap.get(radId).Rental_Apply__r.WorkPlace__c;
            raf.Person_In_Charge_User__c = radMap.get(radId).Rental_Apply__r.Person_In_Charge__c;
            raf.Rental_Assistant__c = radMap.get(radId).Rental_Apply__r.Rental_Assistant__c;
            raf.Rental_Assistant2__c = radMap.get(radId).Rental_Apply__r.Rental_Assistant2__c;
            raf.ZongjianApprovalManager__c = radMap.get(radId).Rental_Apply__r.ZongjianApprovalManager__c;
            raf.BuchangApprovalManagerSales__c = radMap.get(radId).Rental_Apply__r.BuchangApprovalManagerSales__c;
            raf.SalesManager__c = radMap.get(radId).Rental_Apply__r.SalesManager__c;
            raf.Loaner_centre_mail_address__c = radMap.get(radId).Rental_Apply__r.Loaner_centre_mail_address__c;
            raf.Loaner_Storage_mail_address__c = radMap.get(radId).Rental_Apply__r.Loaner_Storage_mail_address__c;
            
            raf.status__c = '草案中';
        }
    }

    protected override void afterInsert() {
        // 给备品出借担当增加读写权限
        List<Rental_Apply_Fault__Share> rafShareList = new List<Rental_Apply_Fault__Share>();
        for (Rental_Apply_Fault__c raf: newList) {
            // 备品出借担当
            if (raf.Person_In_Charge_User__c != null) {
                Rental_Apply_Fault__Share rafShare = new Rental_Apply_Fault__Share();
                rafShare.AccessLevel = 'Edit';
                rafShare.UserOrGroupId = raf.Person_In_Charge_User__c;
                rafShare.ParentID = raf.Id;
                rafShareList.add(rafShare);
            }

            Rental_Apply_Fault__c tempRaf = [select id, ZongjianApprovalManager__r.isActive, BuchangApprovalManagerSales__r.isActive, SalesManager__r.isActive from Rental_Apply_Fault__c where Id =:raf.id ];

            System.debug('-------------temp.ZongjianApprovalManager__r.isActive-------------' + tempRaf.ZongjianApprovalManager__r.isActive);
            System.debug('-------------raf.ZongjianApprovalManager__r.isActive--------------' + raf.ZongjianApprovalManager__r.isActive);

            // CL4 高级总监
            if (raf.ZongjianApprovalManager__c != null && tempRaf.ZongjianApprovalManager__r.isActive) {
                Rental_Apply_Fault__Share rafShare = new Rental_Apply_Fault__Share();
                rafShare.AccessLevel = 'READ';
                rafShare.UserOrGroupId = raf.ZongjianApprovalManager__c;
                rafShare.ParentID = raf.Id;
                rafShareList.add(rafShare);
            }

            // CL5 总监级
            if (raf.BuchangApprovalManagerSales__c != null && tempRaf.BuchangApprovalManagerSales__r.isActive) {
                Rental_Apply_Fault__Share rafShare = new Rental_Apply_Fault__Share();
                rafShare.AccessLevel = 'READ';
                rafShare.UserOrGroupId = raf.BuchangApprovalManagerSales__c;
                rafShare.ParentID = raf.Id;
                rafShareList.add(rafShare);
            }

            // CL6 经理级
            if (raf.SalesManager__c != null && tempRaf.SalesManager__r.isActive) {
                Rental_Apply_Fault__Share rafShare = new Rental_Apply_Fault__Share();
                rafShare.AccessLevel = 'READ';
                rafShare.UserOrGroupId = raf.SalesManager__c;
                rafShare.ParentID = raf.Id;
                rafShareList.add(rafShare);
            }
        }

        if (!rafShareList.isEmpty()) {
            insert rafShareList;
        }
        
    }

    protected override void beforeUpdate() {
        Date today = Date.today();
        List<Rental_Apply_Fault__c> deleteAttachmentIds = new List<Rental_Apply_Fault__c>();
        Set<Id> deleteIds = new Set<Id>();
        for(Rental_Apply_Fault__c raf: newList){
            Rental_Apply_Fault__c oldRaf = oldMap.get(raf.Id);

            if (raf.status__c == '已发送' && oldRaf.status__c != '已发送') {
                raf.SendDate__c = today;
            }

            if (raf.status__c == '已反馈' && oldRaf.status__c != '已反馈') {
                raf.LoanerFeedback_date__c = today;

                if (raf.UseExplain__c == null) {
                    raf.adderror('【备品使用情况说明】不能为空');
                    return;
                }
            }

            if ((String.isBlank(raf.AttachmentId1__c) && !String.isBlank(oldRaf.AttachmentId1__c)) 
                || (String.isBlank(raf.AttachmentId2__c) && !String.isBlank(oldRaf.AttachmentId2__c))) {
                deleteAttachmentIds.add(raf);
                if (String.isBlank(raf.AttachmentId1__c) && !String.isBlank(oldRaf.AttachmentId1__c)) {
                    deleteIds.add(oldRaf.AttachmentId1__c);
                }
                if (String.isBlank(raf.AttachmentId2__c) && !String.isBlank(oldRaf.AttachmentId2__c)) {
                    deleteIds.add(oldRaf.AttachmentId2__c);
                }
            }
        }


        if (!deleteAttachmentIds.isEmpty()) {
            // List<Attachment> attList = [SELECT Id,ParentId FROM Attachment WHERE ParentId IN: deleteAttachmentIds Order by ParentId,Id];
            // Map<String, List<Attachment>> parentMap = new Map<String, List<Attachment>>();
            // for (Attachment att : attList) {
            //     if (!deleteIds.contains(att.Id)) {
            //         if (!parentMap.isEmpty() && parentMap.containskey(att.ParentId)) {
            //             List<Attachment> attTemp = parentMap.get(att.ParentId);
            //             attTemp.add(att);
            //             parentMap.put(att.ParentId, attTemp);
            //         } else {
            //             List<Attachment> attTemp = new List<Attachment>();
            //             attTemp.add(att);
            //             parentMap.put(att.ParentId, attTemp);
            //         }
            //     }
            // }
            List<Id> reList = new List<Id>();
            for(Rental_Apply_Fault__c  re: deleteAttachmentIds){
                reList.add(re.Id);
            }
            List<ContentDocumentLink> linkId = [SELECT ContentDocumentId,LinkedEntityId FROM ContentDocumentLink WHERE LinkedEntityId IN: reList Order by LinkedEntityId];
            List<Id> idList = new List<Id>();
            for (ContentDocumentLink link : linkId) {
                idList.add(link.ContentDocumentId);
            }
            Map<String, List<ContentVersion>> parentMap = new Map<String, List<ContentVersion>>();
            List<ContentVersion> versionList = [SELECT ContentDocumentId,Id FROM ContentVersion where ContentDocumentId in: idList];
            Map<String,ContentVersion> shareMap = new Map<String,ContentVersion>();
            for (ContentVersion version : versionList) {
                for(ContentDocumentLink link: linkId){
                    System.debug('conId  ' + link.ContentDocumentId);
                    System.debug('verId  ' + version.ContentDocumentId);
                    if(link.ContentDocumentId == version.ContentDocumentId){
                        shareMap.put(link.ContentDocumentId,version);
                        
                    }
                }
            }
            for (ContentDocumentLink link : linkId) {
                if (!deleteIds.contains(link.ContentDocumentId)) {
                    if (!parentMap.isEmpty() && parentMap.containskey(link.LinkedEntityId)) {
                        List<ContentVersion> linkTemp = parentMap.get(link.LinkedEntityId);
                        linkTemp.add(shareMap.get(link.ContentDocumentId));
                        parentMap.put(link.LinkedEntityId, linkTemp);
                    } else {
                        List<ContentVersion> linkTemp = new List<ContentVersion>();
                        linkTemp.add(shareMap.get(link.ContentDocumentId));
                        parentMap.put(link.LinkedEntityId, linkTemp);
                    }
                }
            }


            for (Rental_Apply_Fault__c raf : deleteAttachmentIds) {
                if (!parentMap.isEmpty() && parentMap.containskey(raf.Id)) {
                    Rental_Apply_Fault__c oldObj = oldMap.get(raf.Id);
                    System.debug('oldObj' + oldObj);
                    // List<Attachment> attTemp = parentMap.get(raf.Id);
                    List<ContentVersion> cVersionTemp = parentMap.get(raf.Id);
                    System.debug('cVersionTemp' + cVersionTemp);
                    for(Integer i = 0;i < cVersionTemp.size();i++){
                        System.debug('attId1:' + raf.AttachmentId1__c);
                        System.debug('attId2:' + raf.AttachmentId2__c);
                        System.debug('oldattId1:' + oldObj.AttachmentId1__c);
                        System.debug('oldattId2:' + oldObj.AttachmentId2__c);
                        if(oldObj.AttachmentId1__c == cVersionTemp[i].Id && raf.AttachmentId1__c == null){
                            System.debug('123456:' + cVersionTemp[i].Id);
                            cVersionTemp.remove(i);
                        }else if(oldObj.AttachmentId2__c == cVersionTemp[i].Id && raf.AttachmentId2__c == null){
                            System.debug('123456:' + cVersionTemp[i].Id);
                            cVersionTemp.remove(i);
                        }

                    }
                    System.debug('========================================' + cVersionTemp);
                    if(cVersionTemp.size() == 0){
                    }else if (cVersionTemp.size() == 1) {
                        // raf.AttachmentId1__c = attTemp[0].Id;
                        raf.AttachmentId1__c = cVersionTemp[0].Id;
                        raf.AttachmentId2__c = null;
                    } else {
                        // raf.AttachmentId1__c = attTemp[0].Id;
                        // raf.AttachmentId2__c = attTemp[1].Id;
                        raf.AttachmentId1__c = cVersionTemp[0].Id;
                        raf.AttachmentId2__c = cVersionTemp[1].Id;
                    }
                    
                }
            }
        }
    }

    protected override void afterUpdate() {
        Set<Id> feedbackIds = new Set<Id>();
        Set<Id> sendReportIds = new Set<Id>();
        for(Rental_Apply_Fault__c raf: newList){
            Rental_Apply_Fault__c oldRaf = oldMap.get(raf.Id);

            if (raf.status__c == '已发送' && oldRaf.status__c != '已发送') {
                sendReportIds.add(raf.Id);
            }

            if (raf.status__c == '已反馈' && oldRaf.status__c != '已反馈') {
                feedbackIds.add(raf.Id);
            }
        }

        if (!sendReportIds.isEmpty()) {
            //备品中心推送邮件给现场
            sendEmail(sendReportIds, '1');
        }
        
        if (!feedbackIds.isEmpty()) {
            //现场回复后推送的邮件
            sendEmail(feedbackIds, '2');
        }
    }

    //推送邮件处理
    public void sendEmail(Set<Id> rafIds, String flag) {
        List<Rental_Apply_Fault__c> rafList = [
                SELECT Id, Person_In_Charge_User__r.Email, Owner.Email, Rental_Assistant__r.Email, Rental_Assistant2__r.Email,
                        Loaner_centre_mail_address__c, Loaner_Storage_mail_address__c, ZongjianApprovalManager__r.Email,
                        BuchangApprovalManagerSales__r.Email, SalesManager__r.Email, Salesdept__c, StorageCheckEmail__c
                FROM Rental_Apply_Fault__c 
                WHERE Id IN: rafIds];

        EmailTemplate et = null;
        if (flag == '1') {
            et = [Select id from EmailTemplate where name = '备品检测分析报告' limit 1];
        } else if (flag == '2') {
            et = [Select id from EmailTemplate where name = '备品检测分析报告操作不当提醒已回复' limit 1];
        } else {
            return;
        }
        
        List<Messaging.SingleEmailMessage> emails = new List<Messaging.SingleEmailMessage>();

        for (Rental_Apply_Fault__c raf : rafList) {
            String[] toAddresses = new String[]{};
            String[] ccAddresses = new String[]{};

            if (flag == '1') {
                //7月31日演示注释 start 
                //To 申请人
                //toAddresses.add(raf.Person_In_Charge_User__r.Email);
                //To 备品跟进助理
                // if(raf.Rental_Assistant__r.Email != null) {
                //     toAddresses.add(raf.Rental_Assistant__r.Email);
                // }
                // if(raf.Rental_Assistant2__r.Email != null){
                //     toAddresses.add(raf.Rental_Assistant2__r.Email);
                // }

                //CC   部外 备品审批流人员及所属总监---cl4总监邮箱 备品总窗口
                //部内 白总、古部长，林部长   白科，古芳丽，林芳
                //     ocmasset公共邮箱
                //     检测仓库公共邮箱  
                 
                //ccAddresses = System.Label.RentalApplyFaultReplyToBuNei.split(';');

                //cc 部外 备品审批流人员及所属总监---cl4总监邮箱  备品总窗口  
                //setEmailAddress(raf, ccAddresses);
                //7月31日演示注释 end
                if(raf.Loaner_Storage_mail_address__c!=null){
                    ccAddresses.add(raf.Loaner_Storage_mail_address__c);
                }
                ccAddresses.add(raf.Loaner_centre_mail_address__c);
                ccAddresses.add('sunxia@prec-tech.com');
                toAddresses.add('lijinghuan@prec-tech.com');
                //ccAddresses.add(raf.StorageCheckEmail__c);

            } else if (flag == '2') {
                //To ocmasset公共邮箱 检测仓库公共邮箱 检测担当
                //7月31日演示注释 start
                // toAddresses.add(raf.Loaner_centre_mail_address__c);
                // toAddresses.add(raf.Loaner_Storage_mail_address__c);
                // //toAddresses.add(raf.StorageCheckEmail__c);
                // toAddresses.add(raf.Owner.Email);

                //部内  白总、古部长，林部长
                
                //ccAddresses = System.Label.RentalApplyFaultReplyToBuNei.split(';');

                //cc 部外 备品审批流人员及所属总监---cl4总监邮箱  备品总窗口  
                //setEmailAddress(raf, ccAddresses);
                //7月31日演示注释 end
                ccAddresses.add('sunxia@prec-tech.com');
                toAddresses.add('lijinghuan@prec-tech.com');

                //Cc 备品跟进助理
                /*if(raf.Rental_Assistant__r.Email != null) {
                    ccAddresses.add(raf.Rental_Assistant__r.Email);
                }
                if(raf.Rental_Assistant2__r.Email != null){
                    ccAddresses.add(raf.Rental_Assistant2__r.Email);
                }*/
            }
            
            Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
            mail = Messaging.renderStoredEmailTemplate(et.Id, null, raf.Id);
            mail.setSaveAsActivity(false);
            mail.setToAddresses(toAddresses);
            System.debug('=============' + ccAddresses);
            mail.setCcAddresses(ccAddresses);
            emails.add(mail);
        }

        Messaging.sendEmail(emails);
    }

    public void setEmailAddress(Rental_Apply_Fault__c rafObj, String[] ccAddr) {
        //本部                窗口    CL5总监（备品审批） CL4高级总监（备品审批）   建议抄送人1  建议抄送人2  备注
        //华北                赵靖苹  是                  否                      慕海霞       李鸷
        //东北                关贝妮  是                  否                      赵志成       李鸷 
        //西北                张媛媛  是                  否                      颜大庆       宋琰
        //西南                赵雯雯  是                  是
        //华东                徐明楠  是                  否                      宋真         杨剑清  
        //华南                夏俊洁  是                  是                      俞澜         严蕊芳  
        //MA                  无     是                  否                       陆吉       
        //SOLUTION            殷佳骊  是                  否
        //医疗服务本部         无      是                                         陆吉（理论上没有）
        //外科事业本部         李海红  是                  否 
        //市场企划本部         无      是                                         陆吉（理论上没有） 
        //消化.呼吸内镜事业本部 无      是                 否                      陆吉  
        //消化.呼吸领域解决方案本部   无   是                                      陆吉（理论上没有）  
        if (rafObj.Salesdept__c == '医疗华北营业本部') {
            if (rafObj.BuchangApprovalManagerSales__c != null) {
                ccAddr.add(rafObj.BuchangApprovalManagerSales__r.Email);
            }
            if (rafObj.SalesManager__c != null) {
                ccAddr.add(rafObj.SalesManager__r.Email);
            }
            if (!String.isBlank(System.Label.RentalApplyFaultReplyTo_Huabei) && System.Label.RentalApplyFaultReplyTo_Huabei != 'null') {
                ccAddr.addAll(System.Label.RentalApplyFaultReplyTo_Huabei.split(';'));
            }
        } else if (rafObj.Salesdept__c == '医疗东北营业本部') {
            if (rafObj.BuchangApprovalManagerSales__c != null) {
                ccAddr.add(rafObj.BuchangApprovalManagerSales__r.Email);
            }
            if (rafObj.SalesManager__c != null) {
                ccAddr.add(rafObj.SalesManager__r.Email);
            }
            if (!String.isBlank(System.Label.RentalApplyFaultReplyTo_Dongbei) && System.Label.RentalApplyFaultReplyTo_Dongbei != 'null') {
                ccAddr.addAll(System.Label.RentalApplyFaultReplyTo_Dongbei.split(';'));
            }
        } else if (rafObj.Salesdept__c == '医疗西北营业本部') {
            if (rafObj.BuchangApprovalManagerSales__c != null) {
                ccAddr.add(rafObj.BuchangApprovalManagerSales__r.Email);
            }
            if (rafObj.SalesManager__c != null) {
                ccAddr.add(rafObj.SalesManager__r.Email);
            }
            if (!String.isBlank(System.Label.RentalApplyFaultReplyTo_Xibei) && System.Label.RentalApplyFaultReplyTo_Xibei != 'null') {
                ccAddr.addAll(System.Label.RentalApplyFaultReplyTo_Xibei.split(';'));
            }
        } else if (rafObj.Salesdept__c == '医疗西南营业本部') {
            if (rafObj.BuchangApprovalManagerSales__c != null) {
                ccAddr.add(rafObj.BuchangApprovalManagerSales__r.Email);
            }
            if (rafObj.SalesManager__c != null) {
                ccAddr.add(rafObj.SalesManager__r.Email);
            }
            if (rafObj.ZongjianApprovalManager__c != null) {
                ccAddr.add(rafObj.ZongjianApprovalManager__r.Email);
            }
            if (!String.isBlank(System.Label.RentalApplyFaultReplyTo_Xinan) && System.Label.RentalApplyFaultReplyTo_Xinan != 'null') {
                ccAddr.addAll(System.Label.RentalApplyFaultReplyTo_Xinan.split(';'));
            }
        } else if (rafObj.Salesdept__c == '医疗华东营业本部') {
            if (rafObj.BuchangApprovalManagerSales__c != null) {
                ccAddr.add(rafObj.BuchangApprovalManagerSales__r.Email);
            }
            if (rafObj.SalesManager__c != null) {
                ccAddr.add(rafObj.SalesManager__r.Email);
            }
            if (!String.isBlank(System.Label.RentalApplyFaultReplyTo_Huadong) && System.Label.RentalApplyFaultReplyTo_Huadong != 'null') {
                ccAddr.addAll(System.Label.RentalApplyFaultReplyTo_Huadong.split(';'));
            }
        } else if (rafObj.Salesdept__c == '医疗华南营业本部') {
            if (rafObj.BuchangApprovalManagerSales__c != null) {
                ccAddr.add(rafObj.BuchangApprovalManagerSales__r.Email);
            }
            if (rafObj.SalesManager__c != null) {
                ccAddr.add(rafObj.SalesManager__r.Email);
            }
            if (rafObj.ZongjianApprovalManager__c != null) {
                ccAddr.add(rafObj.ZongjianApprovalManager__r.Email);
            }
            if (!String.isBlank(System.Label.RentalApplyFaultReplyTo_Huanan) && System.Label.RentalApplyFaultReplyTo_Huanan != 'null') {
                ccAddr.addAll(System.Label.RentalApplyFaultReplyTo_Huanan.split(';'));
            }
        } else if (rafObj.Salesdept__c == 'MA本部') {
            if (rafObj.BuchangApprovalManagerSales__c != null) {
                ccAddr.add(rafObj.BuchangApprovalManagerSales__r.Email);
            }
            if (rafObj.SalesManager__c != null) {
                ccAddr.add(rafObj.SalesManager__r.Email);
            }
            if (!String.isBlank(System.Label.RentalApplyFaultReplyTo_MA) && System.Label.RentalApplyFaultReplyTo_MA != 'null') {
                ccAddr.addAll(System.Label.RentalApplyFaultReplyTo_MA.split(';'));
            }
        } else if (rafObj.Salesdept__c == 'Solution本部') {
            if (rafObj.BuchangApprovalManagerSales__c != null) {
                ccAddr.add(rafObj.BuchangApprovalManagerSales__r.Email);
            }
            if (rafObj.SalesManager__c != null) {
                ccAddr.add(rafObj.SalesManager__r.Email);
            }
            if (!String.isBlank(System.Label.RentalApplyFaultReplyTo_SOLUTION) && System.Label.RentalApplyFaultReplyTo_SOLUTION != 'null') {
                ccAddr.addAll(System.Label.RentalApplyFaultReplyTo_SOLUTION.split(';'));
            }
        } else if (rafObj.Salesdept__c == '医疗服务本部') {
            if (rafObj.BuchangApprovalManagerSales__c != null) {
                ccAddr.add(rafObj.BuchangApprovalManagerSales__r.Email);
            }
            if (rafObj.SalesManager__c != null) {
                ccAddr.add(rafObj.SalesManager__r.Email);
            }
            if (!String.isBlank(System.Label.RentalApplyFaultReplyTo_Yiliao) && System.Label.RentalApplyFaultReplyTo_Yiliao != 'null') {
                ccAddr.addAll(System.Label.RentalApplyFaultReplyTo_Yiliao.split(';'));
            }
        } else if (rafObj.Salesdept__c == '外科事业本部') {
            if (rafObj.BuchangApprovalManagerSales__c != null) {
                ccAddr.add(rafObj.BuchangApprovalManagerSales__r.Email);
            }
            if (rafObj.SalesManager__c != null) {
                ccAddr.add(rafObj.SalesManager__r.Email);
            }
            if (!String.isBlank(System.Label.RentalApplyFaultReplyTo_Waike) && System.Label.RentalApplyFaultReplyTo_Waike != 'null') {
                ccAddr.addAll(System.Label.RentalApplyFaultReplyTo_Waike.split(';'));
            }
        } else if (rafObj.Salesdept__c == '市场企划本部') {
            if (rafObj.BuchangApprovalManagerSales__c != null) {
                ccAddr.add(rafObj.BuchangApprovalManagerSales__r.Email);
            }
            if (rafObj.SalesManager__c != null) {
                ccAddr.add(rafObj.SalesManager__r.Email);
            }
            if (!String.isBlank(System.Label.RentalApplyFaultReplyTo_Shichang) && System.Label.RentalApplyFaultReplyTo_Shichang != 'null') {
                ccAddr.addAll(System.Label.RentalApplyFaultReplyTo_Shichang.split(';'));
            }
        } else if (rafObj.Salesdept__c == '消化·呼吸内镜事业本部') {
            if (rafObj.BuchangApprovalManagerSales__c != null) {
                ccAddr.add(rafObj.BuchangApprovalManagerSales__r.Email);
            }
            if (rafObj.SalesManager__c != null) {
                ccAddr.add(rafObj.SalesManager__r.Email);
            }
            if (!String.isBlank(System.Label.RentalApplyFaultReplyTo_HXNJ) && System.Label.RentalApplyFaultReplyTo_HXNJ != 'null') {
                ccAddr.addAll(System.Label.RentalApplyFaultReplyTo_HXNJ.split(';'));
            }
        } else if (rafObj.Salesdept__c == '消化·呼吸领域解决方案本部') {
            if (rafObj.BuchangApprovalManagerSales__c != null) {
                ccAddr.add(rafObj.BuchangApprovalManagerSales__r.Email);
            }
            if (rafObj.SalesManager__c != null) {
                ccAddr.add(rafObj.SalesManager__r.Email);
            }
            if (!String.isBlank(System.Label.RentalApplyFaultReplyTo_HXLY) && System.Label.RentalApplyFaultReplyTo_HXLY != 'null') {
                ccAddr.addAll(System.Label.RentalApplyFaultReplyTo_HXLY.split(';'));
            }
        }
    }
}