public without sharing class AfterCreateHospitalTrigger {
    static List<RecordType> hRecs; 
    static List<RecordType> dcRecs;

    public static void setDefaultOwner(List<Account> newList, Map<Id, Account> newMap, List<Account> oldList, Map<Id, Account> oldMap) {
        for (Account a : newList) {
            if (a.RecordTypeId != '01210000000QemG') {
                continue;
            }
            // 病院の場合
            /*
            String ownerid = a.OwnerId;
            a.GI_Main__c = ownerid;
            a.BF_owner__c = ownerid;
            a.SP_Main__c = ownerid;
            a.ENT_owner_ID__c = ownerid;
            a.URO_owner_ID__c = ownerid;
            a.GYN_owner__c = ownerid;
            a.ET_owner__c = ownerid;
            */
        }
    }

    // 病院が新規されたら、自動で戦略科室を作成
    public static void generateDepartmentClass(List<Account> newList, Map<Id, Account> newMap, List<Account> oldList, Map<Id, Account> oldMap) {
        if (Trigger.isUpdate) {
            String[] departmentClassTypes = new String[] {'戦略科室分類 消化科', '戦略科室分類 耳鼻喉科', '戦略科室分類 泌尿科', '戦略科室分類 普外科', '戦略科室分類 婦人科', '戦略科室分類 呼吸科', '戦略科室分類 その他', '戦略科室分類ET'};
            String[] dcLabelNames = new String[] {'消化科', '耳鼻喉科', '泌尿科', '普外科', '妇科', '呼吸科', '其他', 'ET'};
            List<String> idList = new List<String>();
            Map<String,String> exsitMapAcc = new Map<String,String>();
            for(Account acc : newList){
                if (acc.RecordTypeId == '01210000000QemG' ) {
                     idList.add(acc.id);
                }
            }
            // 20210623 SQL优化 Start
            // List<Account> exsitAcc = [select id,Hospital_Department_Class__c,name from Account where Hospital_Department_Class__c in:idList];
            List<Account> exsitAcc = new List<Account>();
            if (idList.size() > 0) {
                exsitAcc = [select id,Hospital_Department_Class__c,name from Account where Hospital_Department_Class__c in:idList];
            }
            
            // 20210623 SQL优化 End
            for(Account acc: exsitAcc){
                exsitMapAcc.put(acc.Hospital_Department_Class__c, acc.name);
            }
            if (dcRecs == null) {
                dcRecs = [select Id, Name from RecordType where IsActive = true and SobjectType = 'Account' and Name IN :departmentClassTypes];
            }
            Map<String, String> rectMap = new Map<String, String>();
            for (RecordType rect : dcRecs) {
                rectMap.put(rect.Name, rect.Id);
            }

            List<Account> dcAccList = new List<Account>();
            for (Account a : newList) {
                if (a.RecordTypeId != '01210000000QemG' ) {
                    continue;
                }
                if(exsitMapAcc.containsKey(a.id)){
                    continue;
                }
                System.debug(a.Is_Active__c+'################'+oldMap.get(a.Id).Is_Active__c);
                if(a.Is_Active__c =='有効' && oldMap.get(a.Id).Is_Active__c =='申请中'){

                // 病院の場合
                //调一次反应权限
                    for (Integer i = 0; i < departmentClassTypes.size(); i++) {
                        String dcLabel = departmentClassTypes[i];
                        String owneridLine = '';
                        if(dcLabel == '戦略科室分類ET'){
                            //戦略科室分類ET
                            owneridLine = a.ET_owner__c;                
                        }else if(dcLabel == '戦略科室分類 消化科'){
                            //戦略科室分類 消化科
                            owneridLine = a.GI_Main__c;
                        }else if(dcLabel == '戦略科室分類 泌尿科'){
                            //戦略科室分類 泌尿科
                            owneridLine = a.URO_owner_ID__c;
                        }else if(dcLabel == '戦略科室分類 普外科'
                                || dcLabel == '戦略科室分類 その他'){
                            //戦略科室分類 普外科&戦略科室分類 その他
                            System.debug('SP_Main__c===='+a.SP_Main__c);
                            owneridLine = a.SP_Main__c;
                        }else if(dcLabel == '戦略科室分類 婦人科'){
                            //戦略科室分類 婦人科
                            owneridLine = a.GYN_owner__c;
                        }else if(dcLabel == '戦略科室分類 呼吸科'){
                            //戦略科室分類 呼吸科
                            owneridLine = a.BF_owner__c;
                        }else if(dcLabel == '戦略科室分類 耳鼻喉科'){
                            //戦略科室分類 耳鼻喉科
                            owneridLine = a.ENT_owner_ID__c;
                        }
                        Account dc = new Account(
                                RecordTypeId = rectMap.get(dcLabel),
                                Department_Class_Label__c = dcLabelNames[i],
                                Name = '*',
                                Hospital_Department_Class__c = a.Id,
                                ParentId = a.Id,
                                OCM_Category__c = a.OCM_Category__c,
                                ownerid = owneridLine
                        );
                        dcAccList.add(dc);
                    }
                }
                if (System.Test.isRunningTest()) {
                    for (Integer i = 0; i < departmentClassTypes.size(); i++) {
                        String dcLabel = departmentClassTypes[i];
                        Account dc = new Account(
                                RecordTypeId = rectMap.get(dcLabel),
                                Department_Class_Label__c = dcLabelNames[i],
                                Name = '*',
                                Hospital_Department_Class__c = a.Id,
                                ParentId = a.Id,
                                OCM_Category__c = a.OCM_Category__c,
                                ownerid = a.ownerid
                        );
                        dcAccList.add(dc);
                    }
                }
                //a.Account_Set_Compelet__c = true;
            }
            if (dcAccList.size() > 0) {
                insert dcAccList;
                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++;
            }
        }
    }
}