@isTest
public with sharing class CreateOpportunityTaskBatchTest {

    @TestSetup
    static void makeData(){
        User user = new User(Test_staff__c = true);
        user.LastName = '_サンブリッジ';
        user.FirstName = 'う';
        user.Alias = 'う';
        user.Email = 'olympusTest03@sunbridge.com';
        user.Username = 'olympusTest03@sunbridge.com';
        user.CommunityNickname = 'う';
        user.IsActive = true;
        user.EmailEncodingKey = 'ISO-2022-JP';
        user.TimeZoneSidKey = 'Asia/Tokyo';
        user.LocaleSidKey = 'ja_JP';
        user.LanguageLocaleKey = 'ja';
        user.ProfileId = System.Label.ProfileId_SystemAdmin;
        user.Job_Category__c = '销售推广';
        user.Province__c = '上海市';
        user.Use_Start_Date__c = Date.today().addMonths(-6);
        user.SalesManager__c = UserInfo.getUserId();
        user.BuchangApprovalManagerSales__c = UserInfo.getUserId();
        user.JingliApprovalManager__c = UserInfo.getUserId();
        user.BuchangApprovalManager__c = UserInfo.getUserId();
        user.ZongjianApprovalManager__c = UserInfo.getUserId();

        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;
        }


        StaticParameter.EscapeOpportunityBefUpdTrigger = true;
        StaticParameter.EscapeSyncOpportunityTrigger = true;
        StaticParameter.EscapeNFM007Trigger = true;
        StaticParameter.EscapeOpportunityHpDeptUpdTrigger = true;
        StaticParameter.EscapeSyncOpportunityTrigger = true;

        System.runAs(new User(Id = Userinfo.getUserId())) {
            insert user;
            // テストデータ
            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;
            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 opp = new Opportunity();
            opp.AccountId           = depart.Id;
            opp.Department_Class__c = section.Id;
            opp.Hospital__c         = company.Id;
            opp.SAP_Send_OK__c      = false;
            opp.Name                = 'GZ-SP-NFM007_1';
            opp.Trade__c            = '内貿';
            opp.StageName           = '引合';
            opp.CloseDate           = date.newinstance(2022, 11, 30);
            opp.Stock_apply_status__c = '申请中';
            opp.IsNextMonthOfVisit__c = false;

            opp.ET_SP_Consumption__c = false;
            opp.Opp_order_Type__c = '';
            opp.ThisPhase_Ship_Forecast__c  = false;
            opp.Equipment_Order_Flg__c  = false;
            opp.old_Oppo_No__c = '';
            opp.If_Account_Change__c  = false;
            // opp.ForecastAccuracyObject__c = true;

            // 待解决设置值后查询时为空的问题(batch中测试程序会跳过该条件)
            opp.OCM_man_province_cus_txt__c = '北京';
            opp.OCM_man_province_txt__c = '北京';
            // opp.SalesdepartmentForecast__c = '1.华北';
            insert opp;
        }        
    }

    @IsTest
    static void myTest(){
        
        Test.startTest();
            List<Opportunity> opps = [select id,name,StageName,ForecastAccuracyObject__c,SalesdepartmentForecast__c,
                                            OwnerId,Department_Name__c,AccountId,OCM_man_province_txt__c,OCM_man_province_cus_txt__c 
                                            from Opportunity];
            List<Id> testId = new List<Id>();
            testId.add(opps.get(0).Id);
            DataBase.executeBatch(new CreateOpportunityTaskBatch(testId));
        Test.stopTest();
        task__c myTask = [select id,Name from task__c];
        System.assertEquals('用户拜访:NFM007TestCompany 消化科 NFM007TestDepart', myTask.Name);   
    }


    @IsTest
    static void myTest2(){
        
        Test.startTest();
             DataBase.executeBatch(new CreateOpportunityTaskBatch());
        Test.stopTest();
        
    }
}