public without sharing class LightningUtil {
    public LightningUtil() {
        
    }
    public static List<Agency_Contact__c> selectOCMAgencyContact(Set<String> hospitalSet, Set<String> ahospitalSet) {
        return [select Id, Agency_Hospital__c, IsOlympusContact__c, Name, Doctor_Division1__c, Type__c, Hospital_DC_Name__c,Department_Cateogy_F__c,Hospital_Name__c from Agency_Contact__c 
                    where Department_Class__r.Hospital_Department_Class__c in :hospitalSet or (Department_Class__c = null and Agency_Hospital__c in :aHospitalSet) order by Name];
    }
    public static List<Agency_Contact__c> searchOCMAgencyContact(Set<String> hospitalSet, Set<String> aHospitalSet, String hosStr, String conStr) {
        return [select Id, Agency_Hospital__c, IsOlympusContact__c, Name, Doctor_Division1__c, Type__c, Hospital_DC_Name__c,Department_Cateogy_F__c,Hospital_Name__c from Agency_Contact__c 
                    where (Department_Class__r.Hospital_Department_Class__c in :hospitalSet or (Department_Class__c = null and Agency_Hospital__c in :aHospitalSet)) and Name like :conStr and Hospital_DC_Name__c like :hosStr order by Name];
    }
    public static List<Contact> selectContact(String cId) {
        return [select Id, AccountId, Name from Contact where id=:cId];
    }

    public static List<Agency_Contact__c> selectAgencyContactList(String hospital_id) {
        //return [select id,Name,Doctor_Division1__c FROM Contact WHERE Strategic_dept_Class__c in (select Id From Account where Hospital_Department_Class__c=:hospital_id and RecordTypeId=:record_type_id)];
        Agency_Hospital_Link__c ahl = [select Hospital__c from Agency_Hospital_Link__c where id = :hospital_id];
        return [select id,Name,Doctor_Division1__c,Type__c,Agency_Hospital__c FROM Agency_Contact__c WHERE Hospital_ID18__c=:ahl.Hospital__c];
    }

    public static List<contact> selectAgencyPerson() {
        String login_user_id = UserInfo.getUserId();
        return [select id, Name, Agency_User__c from contact where Agency_User__c = true and Isactive__c = '有效' and AccountId in (select AccountId from User where id=:login_user_id)];
    }
    
    public static void updateAccMaxActivityDate(String accId, Date week) {
        Agency_Hospital_Link__c account = [select Id,MaxActivityDate__c from Agency_Hospital_Link__c where id=:accId];
        account.MaxActivityDate__c = week;
        update account;
    }
    
    public static Agency_Report_Header__c upsertAgencyReportHeader(Agency_Report_Header__c data) {
        upsert data Agency_Report_Header__c.HeaderInputKey__c;
        return data;
    }

    public static void upsertMAgencyReportHeader(List<Agency_Report_Header__c> data) {
        upsert data Agency_Report_Header__c.HeaderInputKey__c;
        // return data;
    }

    public static Agency_Report__c insertAgencyReport(Agency_Report__c data) {
        if (String.isBlank(data.Hospital__c)) {
            List<Agency_Hospital_Link__c> agency_hospital_link = [select Id, Hospital__c from Agency_Hospital_Link__c where Id=:data.Agency_Hospital__c];
            data.Hospital__c = agency_hospital_link[0].Hospital__c;
        }
        if (String.isBlank(data.Department_Class__c)) {
            String record_type_id = getRecordTypeId(data.Department_Cateogy__c);
            List<Account> account = [select Id, RecordTypeId from account where Hospital_Department_Class__c=:data.Hospital__c and RecordTypeId=:record_type_id];
            data.Department_Class__c = account[0].Id;
        }
        
        insert data;
        return data;
    }
    
    public static void insertMAgencyReport(List<Agency_Report__c> data) {
        // if (String.isBlank(data.Hospital__c)) {
        //     List<Agency_Hospital_Link__c> agency_hospital_link = [select Id, Hospital__c from Agency_Hospital_Link__c where Id=:data.Agency_Hospital__c];
        //     data.Hospital__c = agency_hospital_link[0].Hospital__c;
        // }
        // if (String.isBlank(data.Department_Class__c)) {
        //     String record_type_id = getRecordTypeId(data.Department_Cateogy__c);
        //     List<Account> account = [select Id, RecordTypeId from account where Hospital_Department_Class__c=:data.Hospital__c and RecordTypeId=:record_type_id];
        //     data.Department_Class__c = account[0].Id;
        // }
        
        insert data;
        // return data;
    }

    public static Agency_Report__c updateAgencyReport(Agency_Report__c data) {
        if (String.isBlank(data.Hospital__c)) {
            List<Agency_Hospital_Link__c> agency_hospital_link = [select Id, Hospital__c from Agency_Hospital_Link__c where Id=:data.Agency_Hospital__c];
            data.Hospital__c = agency_hospital_link[0].Hospital__c;
        }
        if (String.isBlank(data.Department_Class__c)) {
            String record_type_id = getRecordTypeId(data.Department_Cateogy__c);
            List<Account> account = [select Id, RecordTypeId from account where Hospital_Department_Class__c=:data.Hospital__c and RecordTypeId=:record_type_id];
            data.Department_Class__c = account[0].Id;
        }
        
        update data;
        return data;
    }

    public static List<Account> getDeptListByRecordType(String hospital_id, Set<String> recordIds) {
        return [select Id, RecordTypeId, Name from account where Hospital_Department_Class__c = :hospital_id and RecordTypeId in :recordIds];
    }

    public static List<RecordType> getAccRecordType(String[] deptTypes) {
        return [select Id, Name, DeveloperName from RecordType where IsActive = true and SobjectType = 'Account' and DeveloperName IN :deptTypes];
    }

    public static void upsertAgencyOpportunity(List<Agency_Opportunity__c> upsertList) {
        upsert upsertList Agency_Opportunity__c.TargetInputKey__c;
    }
    
    public static List<Agency_Report__c> selectAgencyReport(Date week, String person_str) {
        if (String.isBlank(person_str)) {
            List<Contact> person_list = selectAgencyPerson();
            return [Select Id, Name, Report_Date__c, Product_Category1__r.Name, Product_Category2__r.Name, Product_Category3__r.Name, Product_Category1__c, Product_Category2__c, Product_Category3__c, Department_Cateogy__c, Purpose_Type__c, Agency_Report_Header__c, Agency_Hospital__r.Name, Agency_Hospital__r.Hospital__c, OppName__c,
                            Person_In_Charge2__c, Person_In_Charge2__r.Name, doctor2__c, doctor2__r.Name, Submit_date__c,
                            Product_Category__c, Product_Category__r.Name, Result__c, visitor_title__c, Opportunity__c, Opportunity__r.Name
                            From Agency_Report__c
                            where Submit_date__c=:week and Person_In_Charge2__c in :person_list order by LastModifiedDate desc];
        } else {
            return [Select Id, Name, Report_Date__c, Product_Category1__r.Name, Product_Category2__r.Name, Product_Category3__r.Name, Product_Category1__c, Product_Category2__c, Product_Category3__c, Department_Cateogy__c, Purpose_Type__c, Agency_Report_Header__c, Agency_Hospital__r.Name, Agency_Hospital__r.Hospital__c, OppName__c,
                            Person_In_Charge2__c, Person_In_Charge2__r.Name, doctor2__c, doctor2__r.Name, Submit_date__c,
                            Product_Category__c, Product_Category__r.Name, Result__c, visitor_title__c, Opportunity__c, Opportunity__r.Name
                            From Agency_Report__c
                            where Submit_date__c=:week and Person_In_Charge2__c=:person_str order by LastModifiedDate desc];
        }
    }
    
    public static List<Agency_Report__c> selectMAgencyReport(Date date1, Date date2 ,List<Contact> conMList) {
        List<Agency_Report__c> reportlist = [Select Id, Name, Report_Date__c, Product_Category1__r.Name, Product_Category2__r.Name, Product_Category3__r.Name, Product_Category1__c, Product_Category2__c, Product_Category3__c, Department_Cateogy__c, Purpose_Type__c, Agency_Report_Header__c, Agency_Hospital__r.Name, Agency_Hospital__r.Hospital__c, OppName__c,
                            Person_In_Charge2__c, Person_In_Charge2__r.Name, doctor2__c, doctor2__r.Name, Submit_date__c,
                            Product_Category__c, Product_Category__r.Name, Result__c, visitor_title__c, Opportunity__c, Opportunity__r.Name
                            From Agency_Report__c
                            where Person_In_Charge2__c in :conMList and Report_Date__c >= :date1 and Report_Date__c <= :date2 ];
        return reportlist;
    }

    public static List<Agency_Report__c> selectAgencyReportById(String report_id) {
        return [Select Id, Name, Department_Cateogy__c, Purpose_Type__c, Agency_Report_Header__c, Agency_Hospital__r.Name, Agency_Hospital__r.Hospital__c, OppName__c,
                            Person_In_Charge2__c, Person_In_Charge2__r.Name, doctor2__c, doctor2__r.Name, Submit_date__c,
                            Product_Category__c, Product_Category__r.Name, Result__c, visitor_title__c, Opportunity__c, Opportunity__r.Name
                            From Agency_Report__c
                            where id=:report_id];
    }
    
    public static List<Agency_Opportunity__c> selectOpportunityByIdAndHospitalLinkId(String opportunity_id, String agency_hospital_link_id) {
        return [select Id, Name, StageName__c, StageName2__c, RecordTypeId, Amount__c, OCMSale_Price__c, Close_Forecasted_Date__c, TargetInputKey__c, Agency_Hospital__c, Agency_Hospital__r.Name, Department_Cateogy__c, Product_Category__c, Product_Category__r.Name
                from Agency_Opportunity__c where id=:opportunity_id and Agency_Hospital__c=:agency_hospital_link_id];
    }
    
    public static String getRecordTypeId(String department_str) {
        String record_type_id = '';
        if (department_str == 'GI') {
            // 消化科
            record_type_id = '01210000000QemLAAS';
        } else if(department_str == 'BF') {
            // 呼吸科
            record_type_id = '01210000000QezZAAS';
        } else if(department_str == 'GYN') {
            // 婦人科
            record_type_id = '01210000000QezoAAC';
        } else if(department_str == 'GS') {
            // 普外科
            record_type_id = '01210000000QezeAAC';
        } else if(department_str == 'URO') {
            // 泌尿科
            record_type_id = '01210000000QezjAAC';
        } else if(department_str == 'ENT') {
            // 耳鼻喉科
            record_type_id = '01210000000QeztAAC';
        } else if(department_str == 'ET') {
            record_type_id = '01210000000QemQAAS';
        } else if(department_str == 'OTH') {
            // その他
            record_type_id = '01210000000QezyAAC';
        }
        
        return record_type_id;
    }

    public static void updateAgencyOpportunity(Agency_Opportunity__c updaterecord) {
        upsert updaterecord;
    }
    
}