public without sharing class LightningUtil {
    public LightningUtil() {
        
    }
    // 精琢科技  zxk 查询用户的用户产品区分字段  start
    public static User loginUserId(){
        system.debug('UserInfo.getUserId()================>'+UserInfo.getUserId());
        return [select UserPro_Type__c from User where id =: UserInfo.getUserId()];
    }
    // 精琢科技  zxk 查询用户的用户产品区分字段  end
    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,
                AWS_Data_Id__c  // 20220222 PI改造 by Bright
                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];
    }

    // 20220222 PI改造 by Bright--start
    public static List<Agency_Contact__c> searchOCMAgencyContact(Set<String> hospitalSet, Set<String> aHospitalSet, String hosStr, List<string> awsids) {

        string sql = 'select Id, Agency_Hospital__c, IsOlympusContact__c, Name, Doctor_Division1__c, Type__c, Hospital_DC_Name__c,Department_Cateogy_F__c,Hospital_Name__c,AWS_Data_Id__c from Agency_Contact__c';
        sql += ' where (Department_Class__r.Hospital_Department_Class__c in :hospitalSet or (Department_Class__c = null and Agency_Hospital__c in :aHospitalSet)) and Hospital_DC_Name__c like :hosStr';
 
        if (awsids!=null && awsids.size()>0) {
            sql += ' and AWS_Data_Id__c in :awsids ';
        }
        sql += ' order by Hospital_DC_Name__c';
        system.debug('sql='+sql);
        return Database.query(sql);
    }
    // 20220222 PI改造 by Bright--end
    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();
        // PIPL update Yin Mingjie 21/02/2022 start
        // 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)];
        return [select id, Name, Agency_User__c, AWS_Data_Id__c from contact where  Agency_User__c = true and  Isactive__c = '有效' and AccountId in 
        (select AccountId from User where id=:login_user_id)];
        // PIPL update Yin Mingjie 21/02/2022 end
    }
    public static List<contact> selectAgencyPerson01() {
        String login_user_id = UserInfo.getUserId();
        system.debug('login_user_id=============>'+login_user_id);
        return [select id, Name,Agency_User__c from contact where  Agency_User__c = false and  Isactive__c = '有效'  and id in 
        (select ContactId from User where id =: login_user_id)];
    }

    public static List<contact> selectAgencyPerson02() {
        String login_user_id = UserInfo.getUserId();
        return [select id, Name, Agency_User__c from contact where Isactive__c = '有效' and AccountId in 
        (select AccountId from User where id=:login_user_id)];
    }

    public static List<contact> selectAgencyPerson03() {
        String login_user_id = UserInfo.getUserId();
        system.debug('login_user_id=============>'+login_user_id);
        return [select id, Name,Agency_User__c from contact where   Isactive__c = '有效'  and id in 
        (select ContactId 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;
    }

    // PIPL update Yin Mingjie 21/02/2022 start
    public static Map<String, String> insertAgencyContact(Agency_Contact__c data) {
        Map<String, String> acmap = new Map<String, String>();
        acmap.put('AgencyContactId', '');
        acmap.put('errormsg', '');
        try {
            insert data;
            acmap.put('AgencyContactId', data.Id);
        } catch (Exception e) {
            acmap.put('errormsg', e.getMessage());
        }
        return acmap;
    }
    // PIPL update Yin Mingjie 21/02/2022 end

    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) {
        insert 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,ConsumptionOfConsumables__c, WorkMark__c,warlocksNumber__c,Department_Cateogy__c,ProductClassification__c,WarlockClassification__c,ProductCcategory__c, productCategories__c,DealerPersonnel__c,WorkRecord__c,Purpose_Type__c, Agency_Report_Header__c, Agency_Hospital__r.Name, Agency_Hospital__r.Hospital__c, OppName__c,
                            Person_In_Charge2__c,DealerPersonnel__r.Name, Person_In_Charge2__r.Name, doctor2__c, doctor2__r.Name, Submit_date__c,
                            doctor2__r.AWS_Data_Id__c,Person_In_Charge2__r.AWS_Data_Id__c,// 20220222 PI改造 by Bright
                            Product_Category__c, Product_Category__r.Name, Result__c, visitor_title__c, Opportunity__c, Opportunity__r.Name,EffectProgress__c,
                            UseProduct1__c,UseProduct2__c,UseProduct3__c,UseProduct1__r.Name,UseProduct2__r.Name,UseProduct3__r.Name    
                            From Agency_Report__c
                            where Submit_date__c=:week and Person_In_Charge2__c in :person_list and WeeklyReportClassification__c = ''  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,ConsumptionOfConsumables__c, WorkMark__c,warlocksNumber__c,Department_Cateogy__c,DealerPersonnel__c,ProductClassification__c,WarlockClassification__c,ProductCcategory__c,productCategories__c,WorkRecord__c,Purpose_Type__c, Agency_Report_Header__c, Agency_Hospital__r.Name, Agency_Hospital__r.Hospital__c, OppName__c,
                            Person_In_Charge2__c,DealerPersonnel__r.Name, Person_In_Charge2__r.Name, doctor2__c, doctor2__r.Name, Submit_date__c,
                            doctor2__r.AWS_Data_Id__c,Person_In_Charge2__r.AWS_Data_Id__c,// 20220222 PI改造 by Bright
                            Product_Category__c, Product_Category__r.Name, Result__c, visitor_title__c, Opportunity__c, Opportunity__r.Name,EffectProgress__c,
                            UseProduct1__c,UseProduct2__c,UseProduct3__c,UseProduct1__r.Name,UseProduct2__r.Name,UseProduct3__r.Name   
                            From Agency_Report__c
                            where Submit_date__c=:week and Person_In_Charge2__c=:person_str and WeeklyReportClassification__c = ''  order by LastModifiedDate desc];
        }
    }
    //追溯系统
    public static List<Agency_Report__c> selectAgencyReport01(Date week, String person_str) {
        system.debug('person_strperson_str=================>'+person_str);
        if (String.isBlank(person_str)) {
            List<Contact> person_list = selectAgencyPerson01();
            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,ConsumptionOfConsumables__c, WorkMark__c,warlocksNumber__c,Department_Cateogy__c,ProductClassification__c,WarlockClassification__c,ProductCcategory__c,productCategories__c,  DealerPersonnel__c,WorkRecord__c, Agency_Report_Header__c, Agency_Hospital__r.Name, Agency_Hospital__r.Hospital__c, OppName__c,
                            Person_In_Charge2__c,DealerPersonnel__r.Name, 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,EffectProgress__c,
                            doctor2__r.AWS_Data_Id__c,// 20220222 PI改造 by Bright
                            UseProduct1__c,UseProduct2__c,UseProduct3__c,UseProduct1__r.Name,UseProduct2__r.Name,UseProduct3__r.Name    
                            From Agency_Report__c
                            where Submit_date__c=:week and Person_In_Charge2__c in :person_list and WeeklyReportClassification__c != '' 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,ConsumptionOfConsumables__c, WorkMark__c,warlocksNumber__c,Department_Cateogy__c,DealerPersonnel__c,ProductClassification__c,WarlockClassification__c,ProductCcategory__c,productCategories__c, WorkRecord__c, Agency_Report_Header__c, Agency_Hospital__r.Name, Agency_Hospital__r.Hospital__c, OppName__c,
                            Person_In_Charge2__c,DealerPersonnel__r.Name, Person_In_Charge2__r.Name, doctor2__c, doctor2__r.Name, Submit_date__c,
                            doctor2__r.AWS_Data_Id__c,// 20220222 PI改造 by Bright
                            Product_Category__c, Product_Category__r.Name, Result__c, visitor_title__c, Opportunity__c, Opportunity__r.Name,EffectProgress__c,
                            UseProduct1__c,UseProduct2__c,UseProduct3__c,UseProduct1__r.Name,UseProduct2__r.Name,UseProduct3__r.Name   
                            From Agency_Report__c
                            where Submit_date__c=:week and Person_In_Charge2__c=:person_str and WeeklyReportClassification__c != '' 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,ConsumptionOfConsumables__c,warlocksNumber__c,WorkMark__c,Department_Cateogy__c, DealerPersonnel__c,WorkRecord__c, ProductClassification__c,WarlockClassification__c,ProductCcategory__c,productCategories__c,Purpose_Type__c, Agency_Report_Header__c, Agency_Hospital__r.Name, Agency_Hospital__r.Hospital__c, OppName__c,
                            Person_In_Charge2__c,DealerPersonnel__r.Name,Person_In_Charge2__r.Name, doctor2__c, doctor2__r.Name, Submit_date__c,//支援需求SupportNeeds__c
                            doctor2__r.AWS_Data_Id__c,// 20220222 PI改造 by Bright
                            Product_Category__c, Product_Category__r.Name, Result__c, visitor_title__c, Opportunity__c, Opportunity__r.Name,EffectProgress__c,SupportNeeds__c,
                            UseProduct1__c,UseProduct2__c,UseProduct3__c,UseProduct1__r.Name,UseProduct2__r.Name,UseProduct3__r.Name  
                            From Agency_Report__c
                            where Person_In_Charge2__c in :conMList and Report_Date__c >= :date1 and Report_Date__c <= :date2 and WeeklyReportClassification__c = '' ];
        return reportlist;
    }
    //追溯系统
    public static List<Agency_Report__c> selectMAgencyReport01(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,ConsumptionOfConsumables__c,warlocksNumber__c,WorkMark__c,Department_Cateogy__c, DealerPersonnel__c,WorkRecord__c, ProductClassification__c,WarlockClassification__c,ProductCcategory__c,productCategories__c,Agency_Report_Header__c, Agency_Hospital__r.Name, Agency_Hospital__r.Hospital__c, OppName__c,
                            Person_In_Charge2__c,DealerPersonnel__r.Name,Person_In_Charge2__r.Name, doctor2__c, doctor2__r.Name, Submit_date__c,
                            doctor2__r.AWS_Data_Id__c,// 20220222 PI改造 by Bright
                            Product_Category__c, Product_Category__r.Name, Result__c, visitor_title__c, Opportunity__c, Opportunity__r.Name,EffectProgress__c,
                            UseProduct1__c,UseProduct2__c,UseProduct3__c,UseProduct1__r.Name,UseProduct2__r.Name,UseProduct3__r.Name  
                            From Agency_Report__c
                            where Person_In_Charge2__c in :conMList and Report_Date__c >= :date1 and Report_Date__c <= :date2 and WeeklyReportClassification__c != '' ];
        return reportlist;
    }
    //经销商系统的
    public static List<Agency_Report__c> selectAgencyReportById(String report_id) {
        return [Select Id, Name, Department_Cateogy__c,Purpose_Type__c, ConsumptionOfConsumables__c,warlocksNumber__c,WorkMark__c,WorkRecord__c,EffectProgress__c,ProductClassification__c,ProductCcategory__c,productCategories__c, Agency_Report_Header__c, Agency_Hospital__r.Name, Agency_Hospital__r.Hospital__c, OppName__c,
                            Person_In_Charge2__c,DealerPersonnel__c,DealerPersonnel__r.Name, Person_In_Charge2__r.Name, doctor2__c, doctor2__r.Name, Submit_date__c,
                            doctor2__r.AWS_Data_Id__c,// 20220222 PI改造 by Bright
                            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_Report__c> selectAgencyReportById01(String report_id) {
        return [Select Id, Name, Department_Cateogy__c, ConsumptionOfConsumables__c,warlocksNumber__c,WorkMark__c,WorkRecord__c,EffectProgress__c,ProductClassification__c,ProductCcategory__c,productCategories__c, Agency_Report_Header__c, Agency_Hospital__r.Name, Agency_Hospital__r.Hospital__c, OppName__c,
                            Person_In_Charge2__c,DealerPersonnel__c,DealerPersonnel__r.Name, Person_In_Charge2__r.Name, doctor2__c, doctor2__r.Name, Submit_date__c,
                            doctor2__r.AWS_Data_Id__c,// 20220222 PI改造 by Bright
                            Product_Category__c, Product_Category__r.Name, Result__c, visitor_title__c 
                            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;
    }

    
}