public without sharing class RentalHpDeptUpdHandler  extends Oly_TriggerHandler {
    
    private Map<Id, Rental_Apply__c> newMap;
    private Map<Id, Rental_Apply__c> oldMap;
    private List<Rental_Apply__c> newList;
    private List<Rental_Apply__c> oldList;
    public RentalHpDeptUpdHandler() {
        this.newMap = (Map<Id, Rental_Apply__c>) Trigger.newMap;
        this.oldMap = (Map<Id, Rental_Apply__c>) Trigger.oldMap;
        this.newList = (List<Rental_Apply__c>) Trigger.new;
        this.oldList = (List<Rental_Apply__c>) Trigger.old;
    }

    protected override void afterInsert() {
        afterInsertRental_Apply();
    }

    protected override void beforeUpdate() {
        ChangeRental_Apply();
        ChangeOPDPlan();
        afterInsertRental_Apply();//20210104 you 增加手动 添加opd计划，从无到有
    }

    private void afterInsertRental_Apply(){
        //update by rentx 2020-11-18  SWAG-BVF8SP start
        // List<OPDPlan__c> oPDPlans = new List<OPDPlan__c>();
        
        // for(Rental_Apply__c rental : newList) {
            
     //        OPDPlan__c opdplan = new OPDPlan__c();
     //        if (String.isNotBlank(rental.OPDPlan__c)) {
     //            opdplan.Id = rental.OPDPlan__c;
     //            opdplan.Rental_Apply2__c = rental.Id;
                
     //            oPDPlans.add(opdplan);
     //        }
     //    }
     //    if (oPDPlans.size() > 0) {
            
     //        update oPDPlans;
     //    }
        Map<String,String> orMap = new Map<String,String>();
        for(Rental_Apply__c rental : newList) {//20210104 you 增加手动 添加opd计划，从无到有 加了update得id条件
            if (String.isNotBlank(rental.OPDPlan__c) && (trigger.isInsert || (Trigger.isUpdate && string.isBlank(oldMap.get(rental.Id).OPDPlan__c)))) {
                orMap.put(rental.OPDPlan__c,rental.Id );
            }
        }

        if (orMap.size()>0) {
            List<OPDPlan__c> opdList = [select Rental_Apply2__c,Rental_Apply1__c,Rental_Apply3__c,Rental_Apply4__c,Rental_Apply5__c
                                            from OPDPlan__c where id in :orMap.keySet()];
            if (opdList!= null && opdList.size() > 0) {
                for(OPDPlan__c opd : opdList){  
                    if (opd.Rental_Apply2__c == null) {
                        opd.Rental_Apply2__c = orMap.get(opd.Id);
                    }else if (opd.Rental_Apply1__c == null) {
                        opd.Rental_Apply1__c = orMap.get(opd.Id);
                    }else if (opd.Rental_Apply3__c == null) {
                        opd.Rental_Apply3__c = orMap.get(opd.Id);
                    }else if (opd.Rental_Apply4__c == null) {
                        opd.Rental_Apply4__c = orMap.get(opd.Id);
                    }else if (opd.Rental_Apply5__c == null) {
                        opd.Rental_Apply5__c = orMap.get(opd.Id);
                    }
                }
                update opdList;
            }
        }
        

        //update by rentx 2020-18-11 SWAG-BVF8SP end

    }
    private void ChangeOPDPlan(){
        //update by rentx 2020-11-18 SWAG-BVF8SP start
        //暂时不考虑OPD变化,之前的代码是备品上的OPD发生改变或者备品上的opd被删除,因此暂时注释掉了
        /*
        //List<OPDPlan__c> oPDPlanList = new List<OPDPlan__c>();
        Map<String,OPDPlan__c> oPDPlanMap = new Map<String,OPDPlan__c>();
        List<String> oPDPlanIds = new List<String>();
        for(Rental_Apply__c rental : newList) {
            String opdplanId = oldMap.get(rental.Id).OPDPlan__c;
            OPDPlan__c opdplan = new OPDPlan__c();
            if (opdplanId != rental.OPDPlan__c && String.isNotBlank( rental.OPDPlan__c)) {
                                
                opdplan.Id = rental.OPDPlan__c;
                opdplan.Rental_Apply2__c = rental.Id;
                //oPDPlanList.add(opdplan);
                oPDPlanMap.put(rental.Id,opdplan );
            } else if (opdplanId != rental.OPDPlan__c && String.isBlank( rental.OPDPlan__c)) {
                   
                oPDPlanIds.add(opdplanId);
                opdplan.Id = opdplanId;
                //opdplan.Rental_Apply2__c = null;
                //rental.OPDPlanNewestRental__c = null;
                //oPDPlanList.add(opdplan);
                oPDPlanMap.put(rental.Id,opdplan );
                
            }
        }

        if (oPDPlanIds.size() > 0) {
            List<OPDPlan__c> oPDPlans = [select Id,Name,Rental_Apply2__c from OPDPlan__c where Id in :oPDPlanIds];
            
            for (OPDPlan__c OPDPlan :oPDPlans) {
                if (oPDPlanMap.containsKey(OPDPlan.Rental_Apply2__c)) {
                    oPDPlanMap.get(OPDPlan.Rental_Apply2__c).Rental_Apply2__c = null;
                }
            }

        }
        
        if ( oPDPlanMap.size() > 0) {
            update oPDPlanMap.values();
        }

        */

        //设置备品2,1,3,4,5的状态
        Map<Id,Rental_Apply__c> orMap = new Map<Id,Rental_Apply__c>();
        //2021-11-22  mzy SWAG-C8E6LE 【委托】OPD计划状态调整改善 start
        Map<String,Rental_Apply__c> orInstall = new Map<String,Rental_Apply__c>();
        //2021-11-22  mzy SWAG-C8E6LE 【委托】OPD计划状态调整改善 end
        for(Rental_Apply__c rental : newList) {
            //如果opd备品上的opd计划不为空 并且是updat操作 更新对应的opd计划中的备品状态为 已出库
            if (String.isNotBlank(rental.OPDPlan__c) && ((oldMap.get(rental.Id).Shippment_loaner_time__c == null && rental.Shippment_loaner_time__c != null) )) {
            //20210119 youchang start   SWAG-BX7F9W  拿掉条件|| (!oldMap.get(rental.Id).RA_Status_Text__c.equals('取消') && rental.RA_Status_Text__c.equals('取消'))
                orMap.put(rental.OPDPlan__c,rental );
            }
            //2021-11-22  mzy SWAG-C8E6LE 【委托】OPD计划状态调整改善 start
            //备品申请的状态为医院已装机确认,OPD计划的出借目的为OPD,OPD未交报告或未进行无报告申请,则将OPD的状态改为 7.完毕未报告
            //医院已装机确认的逻辑:用户装机日有值,备品中心确认为true
            if(String.isNotBlank(rental.OPDPlan__c)&&(rental.HP_received_sign_day__c!=null&&(oldMap.get(rental.Id).AssetManageConfirm__c==false &&rental.AssetManageConfirm__c))){
                orInstall.put(rental.OPDPlan__c,rental );
            }
            //如果备品借出申请上的OPD计划不为空,备品的回寄时间(Ok)不为空时
            //if(String.isNotBlank(rental.OPDPlan__c)&&((oldMap.get(rental.Id).Asset_return_time_OK__c==null)&&rental.Asset_return_time_OK__c != null)){
            //    orInstall.put(rental.OPDPlan__c,rental);
            //}
            //2021-11-22  mzy SWAG-C8E6LE 【委托】OPD计划状态调整改善 end

        }
        if (orMap.size() > 0) {
            List<OPDPlan__c> opdList = [select Rental_Apply2__c,Rental_Apply1__c,Rental_Apply3__c,Rental_Apply4__c,Rental_Apply5__c,
                                            Rental_Apply2_Status__c,Rental_Apply1_Status__c,Rental_Apply3_Status__c,Rental_Apply4_Status__c,Rental_Apply5_Status__c 
                                            from OPDPlan__c where id in :orMap.keySet()];
            if (opdList!= null && opdList.size() > 0) {
                for(OPDPlan__c opd : opdList){  
                    if (opd.Rental_Apply2__c == orMap.get(opd.Id).Id) {
                        opd.Rental_Apply2_Status__c = 'OVER';
                    }
                    //2021-11-22   mzy  SWAG-C8E6LE 【委托】OPD计划状态调整改善  start
                    // else if (opd.Rental_Apply1__c == orMap.get(opd.Id).Id) {
                    //     opd.Rental_Apply1_Status__c = 'OVER';
                    // }else if (opd.Rental_Apply3__c == orMap.get(opd.Id).Id) {
                    //     opd.Rental_Apply3_Status__c = 'OVER';
                    // }else if (opd.Rental_Apply4__c == orMap.get(opd.Id).Id) {
                    //     opd.Rental_Apply4_Status__c = 'OVER';
                    // }else if (opd.Rental_Apply5__c == orMap.get(opd.Id).Id) {
                    //     opd.Rental_Apply5_Status__c = 'OVER';
                    // }
                    //2021-11-22   mzy  SWAG-C8E6LE 【委托】OPD计划状态调整改善  end
                }
                update opdList;
            }
        }
        //2021-11-22   mzy  SWAG-C8E6LE 【委托】OPD计划状态调整改善  start
        if(orInstall.size()>0){
            //备品申请的状态为医院已装机确认,OPD计划的出借目的为OPD,
            //OPD未交报告及未进行无报告申请,则将OPD的状态改为 7.完毕未报告
            List<OPDPlan__c> opdList2 = [select id,Rental_Apply2__c from OPDPlan__c 
            where RentalReson__c = 'OPD' 
            AND (
            (OPDPlan_Report__c = null ) 
            OR (OPDPlan_Report__c != null and OPDPlan_Report__r.Status__c = '草案中' )
            )
            AND noReportStatus__c = null  
            AND id in :orInstall.keySet()];
            
            if(opdList2!=null&&opdList2.size()>0){
                for(OPDPlan__c tempOPD :opdList2){
                    if (tempOPD.Rental_Apply2__c == orInstall.get(tempOPD.Id).Id) {
                        tempOPD.Status__c = '待提交报告';
                    }
                }

                update opdList2;
            }
        }
        //2021-11-22   mzy  SWAG-C8E6LE 【委托】OPD计划状态调整改善  end

        //update by rentx 2020-11-18 SWAG-BVF8SP end
    }
    private void ChangeRental_Apply(){
        List<String> accIds = new List<String>();
    
    for(Rental_Apply__c a : newList) {
        if (oldMap.get(a.Id).get('Account__c') != a.Account__c
                || a.AccountParentId__c != a.Strategic_dept__c
                || a.AccountParentParentId__c != a.Hospital__c) {
            if (!String.isBlank(a.Account__c)) {
                accIds.add(a.Account__c);
            }
        }
    }
    
    if (accIds.size() > 0) {
        // 診療科レコードタイプ
        String[] deptTypes = new String[] {'病院', '診療科 その他', '診療科 呼吸科', '診療科 婦人科', '診療科 普外科', '診療科 泌尿科', '診療科 消化科', '診療科 耳鼻喉科'};
        List<RecordType> deptRects = [select Id, Name from RecordType where IsActive = true and SobjectType = 'Account' and Name IN :deptTypes];
        Map<String, String> deptRectMap = new Map<String, String>();
        for (RecordType rect : deptRects) {
            deptRectMap.put(rect.Id, rect.Name);
        }
        
        Map<Id, Account> accMap = new Map<Id, Account>();
        List<Account> accs = ControllerUtil.selectAccountForTrigger(accIds);
        for(Account acc : accs) {
            accMap.put(acc.Id, acc);
        }
        
        for(Rental_Apply__c a : newList) {
            // 「診療科」に診療科を選択する場合
            Account acc = accMap.get(a.Account__c);
            if (acc != null
                    && deptRectMap.get(acc.RecordTypeId) != null) {
                if (deptRectMap.get(acc.RecordTypeId) == '病院') {
                    a.Strategic_dept__c = null;
                    a.Hospital__c = acc.Id;
                } else {
                    a.Strategic_dept__c = acc.ParentId;
                    a.Hospital__c = acc.Parent.ParentId;
                }
            }
        }
    }
    }

  
}