@isTest
private class SolutionProjectRequirementsTriggerTest {

	private static User getUser() {
        String timenow = Datetime.now().format('yyyyMMddHHmmss');
        User user1 = new User(Test_staff__c = true, LastName = 'TestMao', FirstName = 'TestMaoF',
                Alias = 'hp', CommunityNickname = 'TestMao', Email = 'Test@sunbridge.com',
                Username = 'Test' + timenow + '@sunbridge.com', IsActive = true, EmailEncodingKey = 'ISO-2022-JP',
                TimeZoneSidKey = 'Asia/Tokyo', LocaleSidKey = 'ja_JP', LanguageLocaleKey = 'ja',
                ProfileId = System.Label.ProfileId_SystemAdmin,
                Dept__c = '医疗华北营业本部', Job_Category__c = '销售服务', Province__c = '北京');

        List<Profile> p = [Select Id From Profile Where Name = '2S1_销售医院担当'];
        System.assertEquals(p.size(), 1);
        System.runAs(new User(Id = Userinfo.getUserId())) {
            insert user1;
        }
        return user1;
    }

    static testMethod void testMethod2(){

        User user = getUser();

        

        Address_Level__c al = new Address_Level__c();
        al.Name = '東京';
        al.Level1_Code__c = 'CN-99';
        al.Level1_Sys_No__c = '999999';
        insert al;
        
        Address_Level2__c al2 = new Address_Level2__c();
        al2.Level1_Code__c = 'CN-99';
        al2.Level1_Sys_No__c = '999999';
        al2.Level1_Name__c = '東京';
        al2.Name = '渋谷区';
        al2.Level2_Code__c = 'CN-9999';
        al2.Level2_Sys_No__c = '9999999';
        al2.Address_Level__c = al.id;
        insert al2;

        List<RecordType> rectHp = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and DeveloperName = 'HP'];
        if (rectHp.size() == 0) {
            return;
        }
        List<RecordType> rectSct = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and DeveloperName = 'Department_Class_GI'];
        if (rectSct.size() == 0) {
            return;
        }
        List<RecordType> rectDpt = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and DeveloperName = 'Department_GI'];
        if (rectDpt.size() == 0) {
            return;
        }

        Profile p = [select Id from Profile where id =:System.Label.ProfileId_SystemAdmin];
        // ユーザー作成
        User hpOwner = new User(Test_staff__c = true, LastName = 'hp', FirstName = 'owner', Alias = 'hp', CommunityNickname = 'hpOwner', Email = 'olympus_hpowner@sunbridge.com', Username = 'olympus_hpowner@sunbridge.com', IsActive = true, EmailEncodingKey = 'ISO-2022-JP', TimeZoneSidKey = 'Asia/Tokyo', LocaleSidKey = 'ja_JP', LanguageLocaleKey = 'ja', ProfileId = p.id);
        insert hpOwner;
        User hpOwner2 = new User(Test_staff__c = true, LastName = 'hp2', FirstName = 'owner', Alias = 'hp2', CommunityNickname = 'hpOwner2', Email = 'olympus_hpowner@sunbridge.com', Username = 'olympus_hpowner2@sunbridge.com', IsActive = true, EmailEncodingKey = 'ISO-2022-JP', TimeZoneSidKey = 'Asia/Tokyo', LocaleSidKey = 'ja_JP', LanguageLocaleKey = 'ja', ProfileId = p.id);
        insert hpOwner2;

        // 病院作成
        Account hp = new Account(RecordTypeId = rectHp[0].Id, Name = 'hp', OwnerId = hpOwner.Id, GI_Product_Leader__c = System.UserInfo.getName().replace(' ', ''));
        insert hp;

        Account dc = [select Id, Name, RecordType_DeveloperName__c, Account2__c from Account where ParentId = :hp.Id and RecordType_DeveloperName__c = 'Department_Class_GI'];
        Account dc2 = [select Id, Name, RecordType_DeveloperName__c, Account2__c from Account where ParentId = :hp.Id and RecordType_DeveloperName__c = 'Department_Class_BF'];

        // 診療科を作る
        NFM001Controller.isRunning = false;
        Account dpt = new Account(RecordTypeId = rectDpt[0].Id);
        dpt.Name         = '*';
        dpt.Department_Name__c  = 'TestDepart';
        dpt.ParentId            = dc.Id;
        dpt.Department_Class__c = dc.Id;
        dpt.Hospital__c         = hp.Id;
        insert dpt;

        SolutionProjectRequirements__c solProjecthb = new SolutionProjectRequirements__c();
    	solProjecthb.Name = 'Test';
    	solProjecthb.HospitalOwner__c = user.Id;
    	solProjecthb.Sales_Assistant__c = user.Id;
    	solProjecthb.GIR_Assistant__c = user.Id;
    	solProjecthb.Project_Type__c = '新建大楼';
    	solProjecthb.Purpose_Proposal__c = '促进销售商谈';
        solProjecthb.Hospital__c = hp.Id;
        solProjecthb.Department_Class__c = dpt.Id;
        insert solProjecthb;

   
    }

    static testMethod void testMethod1() {
    	User user = getUser();
    	Datetime ocsmDate = Datetime.now();
    	String ocsmPeriod = '';
    	if(ocsmDate.month() == 1 || ocsmDate.month() == 2 || ocsmDate.month() == 3){
            ocsmPeriod = ocsmPeriod = ocsmDate.year()-1868+'P';
        }else{
            ocsmPeriod = ocsmDate.year()-1867+'P';
        }
        GIRSolutionAreaNumber__c girsolution = new GIRSolutionAreaNumber__c();
        girsolution.Name = ocsmPeriod;
        girsolution.GIRSolutionXN__c = 1;
        girsolution.GIRSolutionXB__c = 1;
        girsolution.GIRSolutionDB__c = 1;
        girsolution.GIRSolutionHN__c = 1;
        girsolution.GIRSolutionHB__c = 1;
        girsolution.GIRSolutionHD__c = 1;
        // girsolution.Name = 'daqu';
        insert girsolution;

        List<RecordType> rectCo = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '病院'];
        if (rectCo.size() == 0) {
            return;
        }
        List<RecordType> rectSct = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '戦略科室分類 呼吸科'];
        if (rectSct.size() == 0) {
            return;
        }
        List<RecordType> rectDpt = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '診療科 消化科'];
        if (rectDpt.size() == 0) {
            return;
        }

        List<Address_Level__c> addrressList = new List<Address_Level__c>();
        Address_Level__c addresshb = new Address_Level__c();
        addresshb.Level1_Code__c = 'CN-01';
        addrressList.add(addresshb);
        Address_Level__c addressxb = new Address_Level__c();
        addressxb.Level1_Code__c = 'CN-03';
        addrressList.add(addressxb);
        Address_Level__c addressdb = new Address_Level__c();
        addressdb.Level1_Code__c = 'CN-13';
        addrressList.add(addressdb);
        Address_Level__c addresshd = new Address_Level__c();
        addresshd.Level1_Code__c = 'CN-16';
        addrressList.add(addresshd);
        Address_Level__c addresshn = new Address_Level__c();
        addresshn.Level1_Code__c = 'CN-22';
        addrressList.add(addresshn);
        Address_Level__c addressxn = new Address_Level__c();
        addressxn.Level1_Code__c = 'CN-25';
        addrressList.add(addressxn);
        insert addrressList;

        List<Account> acclist = new List<Account>();
        Account companyHb = new Account();
        companyHb.RecordTypeId = rectCo[0].Id;
        companyHb.AgentCode_Ext__c = '99999991';
        companyHb.Name         = 'GIRTestCompany';
        companyHb.State_Master__c = addresshb.Id;
        acclist.add(companyHb);
        Account companyxb = new Account();
        companyxb.RecordTypeId = rectCo[0].Id;
        companyxb.AgentCode_Ext__c = '99999992';
        companyxb.Name         = 'GIRTestCompany';
        companyxb.State_Master__c = addressxb.Id;
        acclist.add(companyxb);
        Account companydb = new Account();
        companydb.RecordTypeId = rectCo[0].Id;
        companydb.AgentCode_Ext__c = '99999993';
        companydb.Name         = 'GIRTestCompany';
        companydb.State_Master__c = addressdb.Id;
        acclist.add(companydb);
        Account companyhd = new Account();
        companyhd.RecordTypeId = rectCo[0].Id;
        companyhd.AgentCode_Ext__c = '99999994';
        companyhd.Name         = 'GIRTestCompany';
        companyhd.State_Master__c = addresshd.Id;
        acclist.add(companyhd);
        Account companyHn = new Account();
        companyHn.RecordTypeId = rectCo[0].Id;
        companyHn.AgentCode_Ext__c = '99999995';
        companyHn.Name         = 'GIRTestCompany';
        companyHn.State_Master__c = addresshn.Id;
        acclist.add(companyHn);
        Account companyxn = new Account();
        companyxn.RecordTypeId = rectCo[0].Id;
        companyxn.AgentCode_Ext__c = '99999996';
        companyxn.Name         = 'GIRTestCompany';
        companyxn.State_Master__c = addressxn.Id;
        acclist.add(companyxn);
        insert acclist;

        List<SolutionProjectRequirements__c> solproList  = new List<SolutionProjectRequirements__c>();
    	SolutionProjectRequirements__c solProjecthb = new SolutionProjectRequirements__c();
    	solProjecthb.Name = 'Test';
    	solProjecthb.HospitalOwner__c = user.Id;
    	solProjecthb.Sales_Assistant__c = user.Id;
    	solProjecthb.GIR_Assistant__c = user.Id;
    	solProjecthb.Project_Type__c = '新建大楼';
    	solProjecthb.Purpose_Proposal__c = '促进销售商谈';
    	solProjecthb.Hospital__c = companyHb.Id;
    	solproList.add(solProjecthb);
        
        SolutionProjectRequirements__c solProjectxb = new SolutionProjectRequirements__c();
    	solProjectxb.Name = 'Test';
    	solProjectxb.HospitalOwner__c = user.Id;
    	solProjectxb.Sales_Assistant__c = user.Id;
    	solProjectxb.GIR_Assistant__c = user.Id;
    	solProjectxb.Project_Type__c = '新建大楼';
    	solProjectxb.Purpose_Proposal__c = '促进销售商谈';
    	solProjectxb.Hospital__c = companyxb.Id;
    	solproList.add(solProjectxb);

    	SolutionProjectRequirements__c solProjectdb = new SolutionProjectRequirements__c();
    	solProjectdb.Name = 'Test';
    	solProjectdb.HospitalOwner__c = user.Id;
    	solProjectdb.Sales_Assistant__c = user.Id;
    	solProjectdb.GIR_Assistant__c = user.Id;
    	solProjectdb.Project_Type__c = '新建大楼';
    	solProjectdb.Purpose_Proposal__c = '促进销售商谈';
    	solProjectdb.Hospital__c = companydb.Id;
    	solproList.add(solProjectdb);

    	SolutionProjectRequirements__c solProjecthd = new SolutionProjectRequirements__c();
    	solProjecthd.Name = 'Test';
    	solProjecthd.HospitalOwner__c = user.Id;
    	solProjecthd.Sales_Assistant__c = user.Id;
    	solProjecthd.GIR_Assistant__c = user.Id;
    	solProjecthd.Project_Type__c = '新建大楼';
    	solProjecthd.Purpose_Proposal__c = '促进销售商谈';
    	solProjecthd.Hospital__c = companyhd.Id;
    	solproList.add(solProjecthd);

    	SolutionProjectRequirements__c solProjecthn = new SolutionProjectRequirements__c();
    	solProjecthn.Name = 'Test';
    	solProjecthn.HospitalOwner__c = user.Id;
    	solProjecthn.Sales_Assistant__c = user.Id;
    	solProjecthn.GIR_Assistant__c = user.Id;
    	solProjecthn.Project_Type__c = '新建大楼';
    	solProjecthn.Purpose_Proposal__c = '促进销售商谈';
    	solProjecthn.Hospital__c = companyHn.Id;
    	solproList.add(solProjecthn);

    	SolutionProjectRequirements__c solProjectxn = new SolutionProjectRequirements__c();
    	solProjectxn.Name = 'Test';
    	solProjectxn.HospitalOwner__c = user.Id;
    	solProjectxn.Sales_Assistant__c = user.Id;
    	solProjectxn.GIR_Assistant__c = user.Id;
    	solProjectxn.Project_Type__c = '新建大楼';
    	solProjectxn.Purpose_Proposal__c = '促进销售商谈';
    	solProjectxn.Hospital__c = companyxn.Id;
    	solproList.add(solProjectxn);
    	insert solproList;

    	girsolution.GIRSolutionXN__c = 100;
        girsolution.GIRSolutionXB__c = 100;
        girsolution.GIRSolutionDB__c = 100;
        girsolution.GIRSolutionHN__c = 100;
        girsolution.GIRSolutionHB__c = 100;
        girsolution.GIRSolutionHD__c = 100;
        girsolution.Name = ocsmPeriod;
        update girsolution;

        for(SolutionProjectRequirements__c sol : solproList){
        	sol.Area_Number__c = '';
        }
        
        update solproList;

    }
}