@isTest
private class OPDLendSortCopyToSortBeforeBatchTest {
    static testMethod void testMethod1() {

        String timenow = Datetime.now().format('yyyyMMddHHmmss');

        // カスタム設定を定義
        SS_Batch_Column_Mapping__c opdMapping = new SS_Batch_Column_Mapping__c(Name = 'OPDPlan__c', SS_TableName__c='SS_OPDPlan__c');
        opdMapping.From_Column_001__c = 'Id';
        opdMapping.SS_Column_001__c = 'OPDPlan__c.Name';
        opdMapping.From_Column_002__c = 'Name';
        opdMapping.SS_Column_002__c = 'Name';
        insert new SS_Batch_Column_Mapping__c[] {opdMapping};

        SS_BatchColumnMapping__c opdMapping2 = new SS_BatchColumnMapping__c(Name = 'OPDPlan__c', SS_TableName__c='SS_OPDPlan__c');
        opdMapping2.From_Column_001__c = 'Id';
        opdMapping2.SS_Column_001__c = 'OPDPlan__c.Name';
        opdMapping2.From_Column_002__c = 'Name';
        opdMapping2.SS_Column_002__c = 'Name';
        insert new SS_BatchColumnMapping__c[] {opdMapping2};


        User hpOwner = new User(Test_staff__c = true, LastName = 'TestMaoTestMaoF', 
                                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,
                                Stay_or_not__c = '已离职');
        // Insert hpOwner;

        if (Test.isRunningTest()) {
            System.runAs(new User(Id = UserInfo.getUserId())){
                Insert hpOwner;
            }
        }

        // 省
        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;

        // 病院を作る
        Account hospital = new Account();
        hospital.recordtypeId = [Select Id FROM RecordType WHERE IsActive = true and SobjectType = 'Account' and DeveloperName = 'HP'].id;
        hospital.Name = 'test hospital';
        hospital.Is_Active__c = '有効';
        hospital.Attribute_Type__c = '卫生部';
        hospital.Speciality_Type__c = '综合医院';
        hospital.Grade__c = '一级';
        hospital.OCM_Category__c = 'SLTV';
        hospital.Is_Medical__c = '医疗机构';
        hospital.State_Master__c = al.id;
        hospital.City_Master__c = al2.id;
        hospital.Town__c = '东京';
        insert hospital;

        // 戦略科室を得る
        Account[] strategicDep = [SELECT ID, Name FROM Account WHERE parentId = :hospital.Id AND recordType.DeveloperName = 'Department_Class_OTH'];

        // 診療科を作る
        Account dep = new Account();
        dep.recordtypeId = [Select Id FROM RecordType WHERE IsActive = true and SobjectType = 'Account' and DeveloperName = 'Department_OTH'].id;
        dep.Name = 'test dep';
        dep.AgentCode_Ext__c = '9999998';
        dep.ParentId = strategicDep[0].Id;
        dep.Department_Class__c = strategicDep[0].Id;
        dep.Hospital__c = hospital.Id;
        insert dep;

        //目标客户
        Account_Number_of_target__c anto1 = new Account_Number_of_target__c();
        //战略科室名
        anto1.Account__c = strategicDep[0].Id;
        anto1.OCM_Period__c = '153P';
        anto1.OPD_PlanMonth_190Main__c = '11月';
        anto1.OPD_PlanMonth_290Miro__c = '6月';
        anto1.OPD_PlanMonth_CV170__c = '1月';
        anto1.OPD_amount_CV190__c = 0;
        anto1.OPD_amount_290MiroGI__c = 0;
        anto1.OPD_amount_CV170__c = 0;
        insert anto1;
        Account_Number_of_target__c[] an = [select id, name , Account__c , Account__r.name, Account__r.ownerid, OCM_Period__c from Account_Number_of_target__c where OCM_Period__c = '153P'];
        OPDPlan__c opdp2 = new OPDPlan__c();
        opdp2.Account_Laboratory__c = dep.Id;

        
        opdp2.OPDPlan_ImplementDate__c = Date.today()+1;
        
        opdp2.Status__c = '计划中';
        opdp2.if_HaveOpportunity__c = 1;
        opdp2.NoOpp_Reason__c = 'HCP对应';
        opdp2.OPD_Customers_Target__c = an[0].Id;
        opdp2.OwnerId = hpOwner.id;
        opdp2.OPDLendSortBefore__c = 1;
        opdp2.SystemDelayMark__c = false;
        opdp2.OPDType__c = '事件';
        insert opdp2;

        OPDPlan__c opdp3 = new OPDPlan__c();
        opdp3.Account_Laboratory__c = dep.Id;

        
        opdp3.OPDPlan_ImplementDate__c = Date.today()+1;
        
        opdp3.Status__c = '计划中';
        opdp3.if_HaveOpportunity__c = 1;
        opdp3.NoOpp_Reason__c = 'HCP对应';
        opdp3.OPD_Customers_Target__c = an[0].Id;
        opdp3.OwnerId = hpOwner.id;
        opdp3.OPDLendSortBefore__c = 1;
        opdp3.SystemDelayMark__c = false;
        opdp3.OPDLendSort__c = 2;
        opdp3.OPDType__c = '事件';
        insert opdp3;

        OPDPlan__c opdp4 = new OPDPlan__c();
        opdp4.Account_Laboratory__c = dep.Id;

        
        opdp4.OPDPlan_ImplementDate__c = Date.today()+1;
        
        opdp4.Status__c = '计划中';
        opdp4.if_HaveOpportunity__c = 1;
        opdp4.NoOpp_Reason__c = 'HCP对应';
        opdp4.OPD_Customers_Target__c = an[0].Id;
        opdp4.OwnerId = hpOwner.id;
        opdp4.OPDLendSortBefore__c = 1;
        opdp4.SystemDelayMark__c = false;
        opdp4.OPDLendSortDraft__c = 3;
        opdp4.OPDType__c = '事件';
        insert opdp4;

        System.Test.StartTest();

        Database.executeBatch(new OPDLendSortCopyToSortBeforeBatch(),100);
        List<String> opdIdList = new List<String>();
        opdIdList.add(opdp2.Id);
        Database.executeBatch(new OPDLendSortCopyToSortBeforeBatch(opdIdList),100);

        System.Test.StopTest();
        
    }
}