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

    public AccountDelayApplyHandler() {
        this.newMap = (Map<Id, Account_Delay_Apply__c>) Trigger.newMap;
        this.oldMap = (Map<Id, Account_Delay_Apply__c>) Trigger.oldMap;
        this.newList = (List<Account_Delay_Apply__c>) Trigger.new;
        this.oldList = (List<Account_Delay_Apply__c>) Trigger.old;
    }
    
    protected override void beforeInsert(){
        // OpenWindow();
        OpenWindow1();
    }
    protected override void afterInsert(){
        MultiOption();
    }
    protected override void beforeUpdate(){
        MultiOption();
        setColumn();
        // OpenWindow();
        CustomizePageJudge();
    }
    protected override void afterUpdate(){
        MultiOption();
        setAccColumn();
    }

   //多选项数据统计
    private void MultiOption(){
        //创建参数 key:Sobject对象 value:发生改变的选项列表字段集合 (哪个选项列表发生变化就把哪个当做参数)
        Map<SObject,List<String>> data = new Map<SObject,List<String>>();
        for (Account_Delay_Apply__c ada : newList) {
            List<String> columsList = new List<String>();
            Account_Delay_Apply__c oldada = null;
            if (Trigger.isUpdate) {
                oldada = oldMap.get(ada.Id);
            }

            //变更理由(可多选)
            if (oldada == null || oldada.ChangeReason__c != ada.ChangeReason__c) {
                columsList.add('ChangeReason__c');
            }

            data.put(ada, columsList);
        }

        SplitOptionListUtil util = new SplitOptionListUtil();
        util.makeObject(data);

    }

    // //营业窗口
    // private void OpenWindow(){
    //     Map<String, OCM_Management_Province__c> mpMap = new Map<String, OCM_Management_Province__c>();
    //     Set<String> accOCSMset = new Set<String>();

    //     for(Account_Delay_Apply__c ada1 : newList){
    //         Account_Delay_Apply__c oldada1 = null;
    //         if(Trigger.isUpdate){

    //             oldada1 = oldMap.get(ada1.Id);

    //             if(ada1.OCM_man_province_HP__c != oldada1.OCM_man_province_HP__c){
    //                accOCSMset.add(ada1.OCM_man_province_HP__c);
    //             }
    //        }
    //         if(Trigger.isInsert){
    //             accOCSMset.add(ada1.OCM_man_province_HP__c);
    //         }

    //     }
        
    //    if(null !=accOCSMset){
    //         List<OCM_Management_Province__c> mpList = 
    //                 [select id, Name, Window1__c from OCM_Management_Province__c where Name =: accOCSMset];

    //         for (OCM_Management_Province__c mp : mpList) {
    //             mpMap.put(mp.Name, mp);
    //         }
    //     }

    //     for(Account_Delay_Apply__c ada2 : newList){
    //         if(Trigger.isInsert || Trigger.isUpdate){
    //             if(null!=mpMap && mpMap.containsKey(ada2.OCM_man_province_HP__c)){
    //                 OCM_Management_Province__c omp = mpMap.get(ada2.OCM_man_province_HP__c);
    //                  if (omp != null) {
    //                      ada2.OpenWindow__c = omp.Window1__c;
    //                  } 
    //              }
    //         }
            
    //     }
        
    // }

    //营业窗口
    private void OpenWindow1(){
        Map<String, OCM_Management_Province__c> mpMap = new Map<String, OCM_Management_Province__c>();
        List<OCM_Management_Province__c> mpList = new List<OCM_Management_Province__c>();
        Set<String> accId = new Set<String>();
        Set<String> accOCSMset = new Set<String>();

        for(Account_Delay_Apply__c ada1 : newList){
            Account_Delay_Apply__c oldada1 = null;
            if(Trigger.isInsert){
                accId.add(ada1.Hospital__c);
            }
            
        }

        if(accId.size() > 0){
            List<Account> accList = [SELECT Id, Name, OCM_man_province_HP__c
                                     FROM Account 
                                     WHERE id in :accId];
            for(Account acc : accList){
                accOCSMset.add(acc.OCM_man_province_HP__c);
            }
            
        }
      
        if(null !=accOCSMset){
             mpList = [select id, Name, Window1__c from OCM_Management_Province__c where Name =: accOCSMset];
        }

        for(Account_Delay_Apply__c ada2 : newList){
            for(OCM_Management_Province__c omp : mpList){
                if(Trigger.isInsert){
                    ada2.OpenWindow__c = omp.Window1__c;
                }
            }
        }
        
    }

    //提交时设置old字段的值
    private void setColumn(){

        List<String> accIds = new List<String>();
        List<Account_Delay_Apply__c> alist = new List<Account_Delay_Apply__c>();
        
        for (Account_Delay_Apply__c ada :newList ) {
            //判断更新前后的状态(是否有效)是否不同 若不同,则判断更新后的状态是否为'审批中'
            if ((ada.Is_Active__c != oldMap.get(ada.Id).Is_Active__c) && ada.Is_Active__c == '申请中' ) {
                alist.add(ada);
                accIds.add(ada.Hospital__c);
            }
        }
        if (accIds.size() == 0) {
            return;
        }else{
                Map<String,Account> accMap = new Map<String,Account>();
                List<Account> statusList = [SELECT Id, Name, Website, Site,
                                                   Is_Active__c, Feature__c, Attribute_Type__c, Speciality_Type__c, 
                                                   Abbreviation__c, Street__c, City_Master__c, Field3_companyname__c, 
                                                   State_Master__c, Grade__c, Address__c, Alias_Name2__c, 
                                                   Town__c, Postal_Code__c
                                            FROM Account 
                                            WHERE id in :accIds];

                for (Account acc :statusList ) {
                    accMap.put(acc.Id, acc);
                }

                for (Account_Delay_Apply__c ada : alist) {
                    ada.Feature_old__c = accMap.get(ada.Hospital__c).Feature__c;
                    ada.Attribute_Type_old__c = accMap.get(ada.Hospital__c).Attribute_Type__c;
                    ada.Speciality_Type_old__c = accMap.get(ada.Hospital__c).Speciality_Type__c;
                    ada.Abbreviation_old__c = accMap.get(ada.Hospital__c).Abbreviation__c;
                    ada.Street_old__c = accMap.get(ada.Hospital__c).Street__c;
                    ada.HospitalName_old__c = accMap.get(ada.Hospital__c).Name;
                    ada.City_Master_old__c = accMap.get(ada.Hospital__c).City_Master__c;
                    ada.Field3_companyname_old__c = accMap.get(ada.Hospital__c).Field3_companyname__c;
                    ada.State_Master_old__c = accMap.get(ada.Hospital__c).State_Master__c;
                    ada.Grade_old__c = accMap.get(ada.Hospital__c).Grade__c;
                    //ada.Address_Together_old__c = accMap.get(ada.Hospital__c).Address__c;
                    ada.Website_old__c = accMap.get(ada.Hospital__c).Website;
                    ada.Site_old__c = accMap.get(ada.Hospital__c).Site;
                    ada.Alias_Name2_old__c = accMap.get(ada.Hospital__c).Alias_Name2__c;
                    ada.Town_old__c = accMap.get(ada.Hospital__c).Town__c;
                    ada.Postal_Code_old__c = accMap.get(ada.Hospital__c).Postal_Code__c;

                }

                // for (Account acc : statusList) {
                //     acc.Status = '申请中';
                // }
            update statusList;   
        }

    }

    //变更有效时反写客户
    private void setAccColumn(){

        List<String> accIds = new List<String>();
        List<String> adaIds = new List<String>();
        // List<Account_Delay_Apply__c> alist = new List<Account_Delay_Apply__c>();
        // List<Account> acclist = new List<Account>();
        List<Account> updacclist = new List<Account>();
        
        for (Account_Delay_Apply__c ada :newList ) {
            //判断更新前后的状态(是否有效)是否不同 若不同,则判断更新后的状态是否为'审批中'
            if ((ada.Is_Active__c != oldMap.get(ada.Id).Is_Active__c) && ada.Is_Active__c == '审批通过' ) {
                // alist.add(ada);
                adaIds.add(ada.Id);
                accIds.add(ada.Hospital__c);
            }
        }
        if (accIds.size() == 0) {
            return;
        }else{
                Map<String,Account> accMap = new Map<String,Account>();
                List<Account> statusList = [SELECT Id, Name, Website, Site,
                                                   Is_Active__c, Feature__c, Attribute_Type__c, Speciality_Type__c, 
                                                   Abbreviation__c, Street__c, City_Master__c, Field3_companyname__c, 
                                                   State_Master__c, Grade__c, Address__c, Alias_Name2__c, 
                                                   Town__c, Postal_Code__c, HospitalType__c, InstitutionalType__c,WhetherRiskPassing__c,IsUnlock__c
                                            FROM Account 
                                            WHERE id in :accIds];
                
                List<Account_Delay_Apply__c> adastatusList = [SELECT Id, Name, Website__c, Site__c,
                                                    Is_Active__c, Feature__c, Attribute_Type__c, Speciality_Type__c, 
                                                    Abbreviation__c, Street__c, City_Master__c, Field3_companyname__c, 
                                                    State_Master__c, Grade__c, Alias_Name2__c, 
                                                    Town__c, Postal_Code__c, HospitalName__c, Hospital__c,
                                                    HospitalType__c, InstitutionalType__c,WhetherRiskPassing__c
                                            FROM Account_Delay_Apply__c 
                                            WHERE id in :adaIds];                           

                for (Account acc :statusList ) {
                    accMap.put(acc.Id, acc);
                }

                for (Account_Delay_Apply__c ada : adastatusList) {
                    accMap.get(ada.Hospital__c).Feature__c = ada.Feature__c;
                    accMap.get(ada.Hospital__c).Attribute_Type__c = ada.Attribute_Type__c;
                    accMap.get(ada.Hospital__c).Speciality_Type__c = ada.Speciality_Type__c;
                    accMap.get(ada.Hospital__c).Abbreviation__c = ada.Abbreviation__c;
                    accMap.get(ada.Hospital__c).Street__c = ada.Street__c;
                    accMap.get(ada.Hospital__c).Name = ada.HospitalName__c;
                    accMap.get(ada.Hospital__c).City_Master__c = ada.City_Master__c;
                    accMap.get(ada.Hospital__c).Field3_companyname__c = ada.Field3_companyname__c;
                    accMap.get(ada.Hospital__c).State_Master__c = ada.State_Master__c;
                    accMap.get(ada.Hospital__c).Grade__c = ada.Grade__c; 
                    // accMap.get(ada.Hospital__c).Address__c = ada.Address_Together__c;
                    accMap.get(ada.Hospital__c).Website = ada.Website__c;
                    accMap.get(ada.Hospital__c).Site = ada.Site__c;
                    accMap.get(ada.Hospital__c).Alias_Name2__c = ada.Alias_Name2__c;
                    accMap.get(ada.Hospital__c).Town__c = ada.Town__c;
                    accMap.get(ada.Hospital__c).Postal_Code__c = ada.Postal_Code__c;

                    accMap.get(ada.Hospital__c).HospitalType__c = ada.HospitalType__c;
                    accMap.get(ada.Hospital__c).InstitutionalType__c = ada.InstitutionalType__c;
                    accMap.get(ada.Hospital__c).WhetherRiskPassing__c= ada.WhetherRiskPassing__c;
                    accMap.get(ada.Hospital__c).IsUnlock__c= true; //跳过验证规则
                    system.debug('原数据==='+accMap.get(ada.Hospital__c).IsUnlock__c);
                    updacclist.add(accMap.get(ada.Hospital__c));

                }
            //if(updacclist.size() > 0){
            //    update updacclist;
            //}
            if(updacclist.size()>0){
            //一个客户更新失败
            List<String> failedaccList = new List<String>();
            Database.SaveResult[] saveAccResults = Database.update(updacclist,false);
            for(Integer i = 0;i<saveAccResults.size();i++) {
                if(!saveAccResults.get(i).isSuccess() ){ 
                    //将更新失败的客户添加到集合中
                    failedaccList.add(String.valueOf( updacclist.get(i).id ).substring(0,15));
                }
            }
            
            //更新成功后,清除客户上的 是否解锁
            List<Account> successAccList = new List<Account>();
            for(Account statusacc:statusList){
                if(failedaccList.contains( String.valueOf( statusacc.id ).substring(0,15)  )){
                    system.debug('更新失败=='+saveAccResults);
                    //更新失败,则不清除标识
                }else {
                    system.debug('清除标识==='+statusacc.IsUnlock__c);
                    //更新成功,清除标识
                    statusacc.IsUnlock__c = false;
                    successAccList.add(statusacc);
                }
            }

            //清除标识
            if(successAccList.size()>0){
                update successAccList;
            }
            
        }
               
        }

    }

    public void CustomizePageJudge(){

        for (Account_Delay_Apply__c ada : newList){          
            System.debug('lt123-----new审批步骤-----'+ada.ApprovalSteps__c);
            System.debug('lt123-----old审批步骤-----'+oldMap.get(ada.Id).ApprovalSteps__c);     
            if(ada.ApprovalSteps__c != oldMap.get(ada.Id).ApprovalSteps__c){
                System.debug('lt123-----审批步骤改变-----');
                if(ada.CustomizePageFlg__c == true){
                    System.debug('lt123-----ada.CustomizePageFlg__c-----'+ada.CustomizePageFlg__c);
                    ada.CustomizePageFlg__c = false;
                }else{
                    System.debug('lt123-----else,ada.CustomizePageFlg__c-----'+ada.CustomizePageFlg__c);
                     ada.addError('请点击链接(相关项)确认具体信息后审批。');
                }
            }
        }
    }


}