@isTest
private class NextMonthVisitTwoBatchTest {
    //private static Event e{get;set;}
    private static String loginId {get; set;}
    private static User u1 {get; set;}
    private static User u2 {get; set;}
    private static User u3 {get; set;}
    private static Account accHP {get; set;}
    private static Account accDepClass {get; set;}
    private static Account accDep {get; set;}
    private static Contact con1 {get; set;}
    private static Contact con2 {get; set;}
    private static Contact con3 {get; set;}
    private static Contact con4 {get; set;}
    private static Contact con5 {get; set;}
    private static Opportunity opp1 {get; set;}
    private static Opportunity opp2 {get; set;}
    private static Opportunity opp3 {get; set;}
    private static Opportunity opp4 {get; set;}
    private static Opportunity opp5 {get; set;}
    private static Maintenance_Contract__c mc1 {get; set;}
    private static Maintenance_Contract__c mc2 {get; set;}
    private static String accDepId {get; set;}
    private static String accDepName {get; set;}
    private static void init() {
        loginId = UserInfo.getUserId();
        id pid = UserInfo.getProfileId();
        //Profile p = [select id from Profile where id = :System.Label.ProfileId_SystemAdmin];
        System.Test.startTest();
        u3 = new User(Test_staff__c = true);
        u3.LastName = '_サンブリッジ';
        u3.Batch_User__c = true;
        u3.FirstName = 'う';
        u3.Alias = 'う';
        u3.Email = 'olympusTest03@sunbridge.com';
        u3.Username = 'olympusTest03@sunbridge.com';
        u3.CommunityNickname = 'う';
        u3.IsActive = true;
        u3.EmailEncodingKey = 'ISO-2022-JP';
        u3.TimeZoneSidKey = 'Asia/Tokyo';
        u3.LocaleSidKey = 'ja_JP';
        u3.LanguageLocaleKey = 'ja';
        u3.ProfileId = pid;
        u3.Job_Category__c = '支援';
        u3.Province__c = '東京';


        u1 = new User(Test_staff__c = true);
        u1.LastName = '_サンブリッジ';
        u1.FirstName = 'あ';
        u1.Batch_User__c = true;
        u1.Alias = 'あ';
        u1.Email = 'olympusTest01@sunbridge.com';
        u1.Username = 'olympusTest10@sunbridge.com';
        u1.CommunityNickname = 'あ1';
        u1.IsActive = true;
        u1.EmailEncodingKey = 'ISO-2022-JP';
        u1.TimeZoneSidKey = 'Asia/Tokyo';
        u1.LocaleSidKey = 'ja_JP';
        u1.LanguageLocaleKey = 'ja';
        u1.ProfileId = pid;
        u1.Job_Category__c = '销售服务';
        u1.Province__c = '東京';
        u1.ManagerId = u3.id;


        u2 = new User(Test_staff__c = true);
        u2.LastName = '_サンブリッジ';
        u2.FirstName = 'い';
        u2.Batch_User__c = true;
        u2.Alias = 'い';
        u2.Email = 'olympusTest02@sunbridge.com';
        u2.Username = 'olympusTest02@sunbridge.com';
        u2.CommunityNickname = 'い';
        u2.IsActive = true;
        u2.EmailEncodingKey = 'ISO-2022-JP';
        u2.TimeZoneSidKey = 'Asia/Tokyo';
        u2.LocaleSidKey = 'ja_JP';
        u2.LanguageLocaleKey = 'ja';
        u2.ProfileId = pid;
        u2.Job_Category__c = '销售推广';
        u2.Province__c = '東京';
        insert New List<user>{u1,u2,u3};


        //RecordType rtDepClass = [select id from RecordType where IsActive = true and SobjectType = 'Account' and DeveloperName =:'Department_Class_ENT'];           // 耳鼻喉科

        //Address_Level__c al = [select id from Address_Level__c where Level1_Code__c =:'CN-01'];
        //Address_Level2__c al2 = [select id from Address_Level2__c where Name =:'延庆县'];
        // system.runAs(u2) {
            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;

            accHP = new Account();
            accHP.Name = '病院1';
            accHP.Grade__c = '一般';
            accHP.OCM_Category__c = '一般';
            accHP.Attribute_Type__c = '保険省';
            accHP.Speciality_Type__c = '総合病院';
            accHP.Is_Active__c = '有効';
            accHP.State_Master__c = al.id;
            accHP.City_Master__c = al2.id;
            accHP.RecordTypeId = '01210000000QemG';
            accHP.Valid_To__c = Date.today() + 2;
            accHP.FSE_GI_Main_Leader__c = UserInfo.getUserId();
            accHP.FSE_SP_Main_Leader__c = UserInfo.getUserId();
            insert accHP;
            /*
            accDepClass = new Account();
            accDepClass.Name = '戦略科室分類1';
            accDepClass.Department_Class_Label__c = '耳鼻喉科';
            accDepClass.Hospital_Department_Class__c = accHP.id;
            accDepClass.ParentId = accHP.id;
            accDepClass.RecordTypeId = rtDepClass.id;
            insert accDepClass;
            */
            accDepClass = [select Id from Account where ParentId = :accHP.Id and Department_Class_Label__c = '耳鼻喉科'];

            accDep = new Account();
            accDep.Name = '診療科1';
            accDep.Department_Class_Label__c = '診療科1';
            accDep.Hospital__c = accHP.id;
            accDep.ParentId = accDepClass.id;
            accDep.Department_Class__c = accDepClass.id;
            accDep.Department_Name__c = '診療科1';
            accDep.CurrencyIsoCode = 'CNY';
            accDep.RecordTypeId = '01210000000Qfm2';
            accDep.FSE_GI_Main_Leader__c = UserInfo.getUserId();
            accDep.FSE_SP_Main_Leader__c = UserInfo.getUserId();

            insert accDep;
            accDepId = accDep.Id;
            accDep = [select Name from Account where Id = :accDep.Id];
            accDepName = accDep.Name;
        // 取引先責任者の住所を数式項目に変更の為、当該項目を削除
        // 省の参照先は病院の住所になったため、カバレッジテストを行うには
        // 所属する病院の賞を変更して実施してください。by 宗像(真)
            List<Contact> conList = new List<Contact>();
            con1 = new Contact();
            con1.Firstname = 'ZZ1';
            con1.LastName = '取引先責任者1';
            con1.RecordTypeId = '01210000000QfWd';
            con1.AccountId = accDep.Id;
        //        con1.State__c = '北京市';
            con2 = new Contact();
            con2.Firstname = 'ZZ2';
            con2.LastName = '取引先責任者2';
            con2.RecordTypeId = '01210000000QfWd';
            con2.AccountId = accDep.Id;
        //        con2.State__c = '北京市';
            con3 = new Contact();
            con3.Firstname = 'ZZ3';
            con3.LastName = '取引先責任者3';
            con3.RecordTypeId = '01210000000QfWd';
            con3.AccountId = accDep.Id;
        //        con3.State__c = '北京市';
            con4 = new Contact();
            con4.Firstname = 'ZZ4';
            con4.LastName = '取引先責任者4';
            con4.RecordTypeId = '01210000000QfWd';
            con4.AccountId = accDep.Id;
        //        con4.State__c = '北京市';
            con5 = new Contact();
            con5.Firstname = 'ZZ5';
            con5.LastName = '取引先責任者5';
            con5.RecordTypeId = '01210000000QfWd';
            con5.AccountId = accDep.Id;
        //        con5.State__c = '北京市';
            conList.add(con1);
            conList.add(con2);
            conList.add(con3);
            conList.add(con4);
            conList.add(con5);
            insert conList;
            System.Test.stopTest();
            List<Opportunity> oppList = new List<Opportunity>();
            opp1 = new Opportunity();
            opp1.Name = '引合1';
            opp1.AccountId = accDep.Id;
            opp1.Opportunity_Category__c = 'ENT';
            opp1.Trade__c = '外貿';
            opp1.StageName = '引合';
            opp1.CurrencyIsoCode = 'CNY';
            opp1.Close_Forecasted_Date__c = date.today().addMonths(1);
            opp1.CloseDate = date.today().addMonths(1);
            opp1.Opportunity_stage__c = '确定了对手参数';
            opp1.Competitor__c = 'E';
            opp1.Sales_Root__c = 'OCM直接販売';
            opp1.Hospital__c = accHP.id;
            opp1.Department_Class__c = accDepClass.id;
            opp1.StageName = '引合';
            opp2 = new Opportunity();
            opp2.Name = '引合2';
            opp2.AccountId = accDep.Id;
            opp2.Opportunity_Category__c = 'ENT';
            opp2.Trade__c = '外貿';
            opp2.StageName = '引合';
            opp2.Opportunity_stage__c = '确定了对手参数';
            opp2.CurrencyIsoCode = 'CNY';
            opp2.Close_Forecasted_Date__c = date.today().addMonths(1);
            opp2.CloseDate = date.today().addMonths(1);
            opp2.Competitor__c = 'E';
            opp2.Sales_Root__c = 'OCM直接販売';
            opp2.Hospital__c = accHP.id;
            opp2.Department_Class__c = accDepClass.id;
            opp2.StageName = '引合';
            opp3 = new Opportunity();
            opp3.Name = '引合3';
            opp3.AccountId = accDep.Id;
            opp3.Opportunity_Category__c = 'ENT';
            opp3.Trade__c = '外貿';
            opp3.StageName = '引合';
            opp3.Opportunity_stage__c = '确定了对手参数';
            opp3.CurrencyIsoCode = 'CNY';
            opp3.Close_Forecasted_Date__c = date.today().addMonths(1);
            opp3.CloseDate = date.today().addMonths(1);
            opp3.Competitor__c = 'E';
            opp3.Sales_Root__c = 'OCM直接販売';
            opp3.Hospital__c = accHP.id;
            opp3.Department_Class__c = accDepClass.id;
            opp3.StageName = '引合';
            opp4 = new Opportunity();
            opp4.Name = '引合4';
            opp4.AccountId = accDep.Id;
            opp4.Opportunity_Category__c = 'ENT';
            opp4.Trade__c = '外貿';
            opp4.StageName = '引合';
            opp4.CurrencyIsoCode = 'CNY';
            opp4.Opportunity_stage__c = '确定了对手参数';
            opp4.Close_Forecasted_Date__c = date.today().addMonths(1);
            opp4.CloseDate = date.today().addMonths(1);
            opp4.Competitor__c = 'E';
            opp4.Sales_Root__c = 'OCM直接販売';
            opp4.Hospital__c = accHP.id;
            opp4.Department_Class__c = accDepClass.id;
            opp4.StageName = '引合';
            opp5 = new Opportunity();
            opp5.Name = '引合5';
            opp5.AccountId = accDep.Id;
            opp5.Opportunity_Category__c = 'ENT';
            opp5.Trade__c = '外貿';
            opp5.StageName = '引合';
            opp5.CurrencyIsoCode = 'CNY';
            opp5.Close_Forecasted_Date__c = date.today().addMonths(1);
            opp5.CloseDate = date.today().addMonths(1);
            opp5.Competitor__c = 'E';
            opp5.Sales_Root__c = 'OCM直接販売';
            opp5.Opportunity_stage__c = '确定了对手参数';
            opp5.Hospital__c = accHP.id;
            opp5.Department_Class__c = accDepClass.id;
            opp5.StageName = '引合';
            oppList.add(opp1);
            oppList.add(opp2);
            oppList.add(opp3);
            oppList.add(opp4);
            oppList.add(opp5);
            insert oppList;
            System.debug('opp1.Name:' + opp1.Name);

            accompanying_report__c ar = new accompanying_report__c();
            ar.Name = 'システム管理者';
            ar.Profile_ID_1__c = pid;
            //Database.insert(ar, false);
            insert ar;

            report_report__c rr = new report_report__c();
            rr.Name = 'システム管理者';
            rr.Profile_ID_1__c = pid;
            //Database.insert(rr, false);
            insert rr;
    }
    // static testMethod void testMethod1() {
    //     init();
    //      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;
    //     }
    //     Account company = new Account();
    //     company.RecordTypeId = rectCo[0].Id;
    //     company.Name         = 'NFM007TestCompany';
    //     upsert company;
    //     Account section = new Account();
    //     section.RecordTypeId = rectSct[0].Id;
    //     section.Name         = '*';
    //     section.Department_Class_Label__c = '消化科';
    //     section.ParentId                  = company.Id;
    //     section.Hospital_Department_Class__c = company.Id;
    //     upsert section;
    //     // System.Test.startTest();
    //     Account depart = new Account();
    //     depart.RecordTypeId = rectDpt[0].Id;
    //     depart.Name         = '*';
    //     depart.Department_Name__c  = 'NFM007TestDepart';
    //     depart.ParentId            = section.Id;
    //     depart.Department_Class__c = section.Id;
    //     depart.Hospital__c         = company.Id;
    //     upsert depart;

    //     Opportunity opp1 = new Opportunity();
    //     opp1.AccountId           = depart.Id;
    //     opp1.Department_Class__c = section.Id;
    //     opp1.Hospital__c         = company.Id;
    //     opp1.SAP_Send_OK__c      = false;
    //     opp1.Name                = 'GZ-SP-NFM007_1';
    //     opp1.Trade__c            = '内貿';
    //     opp1.StageName           = '引合';
    //     opp1.CloseDate           = date.newinstance(2022, 11, 30);
    //     opp1.Stock_apply_status__c = '申请中';
    //     opp1.IsNextMonthOfVisit__c = true;
    //     insert opp1;
        
        
    //     Daily_Report__c dr = new Daily_Report__c();
    //     dr.Reported_Date__c = Date.newInstance(2020, 5, 1);
    //     dr.Status__c = '作成中';
    //     dr.Reporter__c = u1.Id;
    //     dr.OwnerId = u1.Id;
    //     insert dr;

    //     Event__c ec = new Event__c(
    //         Subject__c = 'test',
    //         StartDateTime__c = Datetime.newInstance(2020, 5, 1, 1, 0, 0),
    //         DurationInMinutes__c = 60,
    //         EndDateTime__c = Datetime.newInstance(2020, 5, 1, 2, 0, 0),
    //         ActivityDate__c = Date.today(),
    //         Daily_Report__c = dr.Id
    //     );
    //     insert ec;

    //     Event e = new Event();
    //     e.ActivityDateTime = Datetime.newInstance(2020, 5, 1, 1, 0, 0);
    //     e.ActivityDate =  Date.newInstance(2020, 5, 1);
    //     e.DurationInMinutes = 0;
    //     e.OwnerId = u1.id;
    //     e.IsScheduled__c = true;
    //     e.whatid__c = accDep.Id;
    //     e.NextEventC_ID__c = ec.Id;
    //     e.Related_Opportunity1_ID__c  =opp1.Id;
    //     insert e;
        

    //     Database.executeBatch(new NextMonthVisitTwoBatch(), 1);
    //     // System.Test.stopTest();
    // }

   
    static testMethod void testMethod2() {
        init();
         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;
        }
        Account company = new Account();
        company.RecordTypeId = rectCo[0].Id;
        company.Name         = 'NFM007TestCompany';
        upsert company;
        Account section = new Account();
        section.RecordTypeId = rectSct[0].Id;
        section.Name         = '*';
        section.Department_Class_Label__c = '消化科';
        section.ParentId                  = company.Id;
        section.Hospital_Department_Class__c = company.Id;
        upsert section;
        // System.Test.startTest();
        Account depart = new Account();
        depart.RecordTypeId = rectDpt[0].Id;
        depart.Name         = '*';
        depart.Department_Name__c  = 'NFM007TestDepart';
        depart.ParentId            = section.Id;
        depart.Department_Class__c = section.Id;
        depart.Hospital__c         = company.Id;
        upsert depart;

        Opportunity opp1 = new Opportunity();
        opp1.AccountId           = depart.Id;
        opp1.Department_Class__c = section.Id;
        opp1.Hospital__c         = company.Id;
        opp1.SAP_Send_OK__c      = false;
        opp1.Name                = 'GZ-SP-NFM007_1';
        opp1.Trade__c            = '内貿';
        opp1.StageName           = '引合';
        opp1.CloseDate           = date.newinstance(2022, 11, 30);
        opp1.Stock_apply_status__c = '申请中';
        opp1.IsNextMonthOfVisit__c = true;
        insert opp1;

        Daily_Report__c dr = new Daily_Report__c();
        dr.Reported_Date__c = Date.newInstance(2020, 5, 1);
        dr.Status__c = '作成中';
        dr.Reporter__c = u1.Id;
        dr.OwnerId = u1.Id;
        insert dr;

        Event__c ec = new Event__c(
            Subject__c = 'test',
            StartDateTime__c = Datetime.newInstance(2020, 5, 1, 1, 0, 0),
            DurationInMinutes__c = 60,
            EndDateTime__c = Datetime.newInstance(2020, 5, 1, 2, 0, 0),
            ActivityDate__c = Date.today(),
            Daily_Report__c = dr.Id
        );
        insert ec;

        Event e = new Event();
        Date today  = date.today();
        e.ActivityDateTime = today.addMonths(1).toStartOfMonth();
        e.ActivityDate =  today.addMonths(1).toStartOfMonth();
        e.DurationInMinutes = 0;
        e.OwnerId = u1.id;
        e.IsScheduled__c = true;
        e.whatid__c = accDep.Id;
        e.NextEventC_ID__c = ec.Id;
        e.Related_Opportunity1_ID__c  =opp1.Id;
        e.Related_Opportunity2_ID__c  =opp2.Id;
        e.Related_Opportunity3_ID__c  =opp3.Id;
        e.Related_Opportunity4_ID__c  =opp4.Id;
        e.Related_Opportunity5_ID__c  =opp5.Id;
        insert e;
        

        Database.executeBatch(new NextMonthVisitTwoBatch(), 20);
        // System.Test.stopTest();
    }

    static testMethod void testMethod3() {
        init();
         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;
        }
        Account company = new Account();
        company.RecordTypeId = rectCo[0].Id;
        company.Name         = 'NFM007TestCompany';
        upsert company;
        Account section = new Account();
        section.RecordTypeId = rectSct[0].Id;
        section.Name         = '*';
        section.Department_Class_Label__c = '消化科';
        section.ParentId                  = company.Id;
        section.Hospital_Department_Class__c = company.Id;
        upsert section;
        // System.Test.startTest();
        Account depart = new Account();
        depart.RecordTypeId = rectDpt[0].Id;
        depart.Name         = '*';
        depart.Department_Name__c  = 'NFM007TestDepart';
        depart.ParentId            = section.Id;
        depart.Department_Class__c = section.Id;
        depart.Hospital__c         = company.Id;
        upsert depart;

        Opportunity opp1 = new Opportunity();
        opp1.AccountId           = depart.Id;
        opp1.Department_Class__c = section.Id;
        opp1.Hospital__c         = company.Id;
        opp1.SAP_Send_OK__c      = false;
        opp1.Name                = 'GZ-SP-NFM007_1';
        opp1.Trade__c            = '内貿';
        opp1.StageName           = '引合';
        opp1.CloseDate           = date.newinstance(2022, 11, 30);
        opp1.Stock_apply_status__c = '申请中';
        opp1.IsNextMonthOfVisit__c = true;
        insert opp1;

        Daily_Report__c dr = new Daily_Report__c();
        dr.Reported_Date__c = Date.newInstance(2020, 5, 1);
        dr.Status__c = '作成中';
        dr.Reporter__c = u1.Id;
        dr.OwnerId = u1.Id;
        insert dr;

        Event__c ec = new Event__c(
            Subject__c = 'test',
            StartDateTime__c = Datetime.newInstance(2020, 5, 1, 1, 0, 0),
            DurationInMinutes__c = 60,
            EndDateTime__c = Datetime.newInstance(2020, 5, 1, 2, 0, 0),
            ActivityDate__c = Date.today(),
            Daily_Report__c = dr.Id
        );
        insert ec;

        Event e = new Event();
        Date today  = date.today();
        e.ActivityDateTime = today;
        e.ActivityDate =  today;
        e.DurationInMinutes = 0;
        e.OwnerId = u1.id;
        e.IsScheduled__c = true;
        e.whatid__c = accDep.Id;
        e.NextEventC_ID__c = ec.Id;
        e.Related_Opportunity1_ID__c  =opp1.Id;
        e.Related_Opportunity2_ID__c  =opp2.Id;
        e.Related_Opportunity3_ID__c  =opp3.Id;
        e.Related_Opportunity4_ID__c  =opp4.Id;
        e.Related_Opportunity5_ID__c  =opp5.Id;
        insert e;
        

        Database.executeBatch(new NextMonthVisitTwoBatch(true), 20);
        // System.Test.stopTest();
    }
}