/**
 * This class contains unit tests for validating the behavior of Apex classes
 * and triggers.
 *
 * Unit tests are class methods that verify whether a particular piece
 * of code is working properly. Unit test methods take no arguments,
 * commit no data to the database, and are flagged with the testMethod
 * keyword in the method definition.
 *
 * All test methods in an org are executed whenever Apex code is deployed
 * to a production org to confirm correctness, ensure code
 * coverage, and prevent regressions. All Apex classes are
 * required to have at least 75% code coverage in order to be deployed
 * to a production org. In addition, all triggers must have some code coverage.
 * 
 * The @isTest class annotation indicates this class only contains test
 * methods. Classes defined with the @isTest annotation do not count against
 * the org size limit for all Apex scripts.
 *
 * See the Apex Language Reference for more information about Testing and Code Coverage.
 */
@isTest
private class OPDAutomaticSortBeforeBatchTest {

    static testMethod void testMethod1() {
        StaticParameter.EscapeNFM001AgencyContractTrigger = true;
        StaticParameter.EscapeNFM001Trigger = true;
        Oly_TriggerHandler.bypass(ContactTriggerHandler.class.getName());
        Oly_TriggerHandler.bypass(AgencyHospitalHandler.class.getName());
        Oly_TriggerHandler.bypass(AssetHandler.class.getName());
        Date toDate = Date.today();
        Boolean userProfileCheck = false;
        // 获取从本月14日至次月13日的日期
        Date startDate = toDate.toStartOfMonth().addDays(13);
        Date endDate = toDate.addMonths(1).toStartOfMonth().addDays(12);
        
        opp2AuxiliarySort__c oas1 = new opp2AuxiliarySort__c();
        oas1.Name = '北京市';
        oas1.type__c = 1;
        insert oas1;
        // opp2AuxiliarySort__c oas2 = new opp2AuxiliarySort__c();
        // oas2.Name = '山东';
        // oas2.type__c = 1;
        // insert oas2;
        // opp2AuxiliarySort__c oas3 = new opp2AuxiliarySort__c();
        // oas3.Name = '河北';
        // oas3.type__c = 1;
        // insert oas3;
        // opp2AuxiliarySort__c oas4 = new opp2AuxiliarySort__c();
        // oas4.Name = '上海';
        // oas4.type__c = 1;
        // insert oas4;

        // opp2AuxiliarySort__c oas5 = new opp2AuxiliarySort__c();
        // oas5.Name = '1.华北';
        // oas5.type__c = 2;
        // insert oas5;
        Opportunity opp = new Opportunity(
        Name='Opp',
        StageName='引合',
        CloseDate=Date.today().addDays(10),
        Close_Forecasted_Date__c=Date.today(),
            CurrencyIsoCode = 'CNY'
        );
        insert opp;
        Statu_Achievements__c Sac = new Statu_Achievements__c(
            name = 'zhucan_one',
            Opportunity__c = opp.id,
            DeliveryDate__c = Date.today(),
            ContractNO__c = 'ContractNO1',
            ContractAmount__c = 1000
        );
        insert Sac;
        
        RecordType rectCam =
        [SELECT Id FROM RecordType
         WHERE IsActive = true AND SobjectType = 'Campaign'
                          AND DeveloperName = 'ServiceEngineerTraining'];
        Campaign cam = new Campaign();
        cam.Name = 'cam';
        cam.Name2__c = '1234';
        cam.RecordTypeId = rectCam.Id;
        cam.StartDate = startDate.addDays(10);
        cam.EndDate = startDate.addDays(15);
        cam.Mailflg_after45__c = true;
        cam.Mailflg_cancel__c = true;
        cam.Mailflg_before15__c = true;
        cam.Mailflg_before7__c = true;
        cam.Mailflg_after3__c = true;
        insert cam;


        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;

        Contact contact2 = new Contact();
        contact2.AccountId = dep.Id;
        contact2.RecordTypeId = Schema.SObjectType.Contact.getRecordTypeInfosByDeveloperName().get('Agency').getRecordTypeId();
        contact2.FirstName = '責任者';
        contact2.LastName = 'test1经销商';
        contact2.Agency_User__c = true;
        insert contact2;
        // システム管理者
        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.Dept__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();

        // MIXED_DML_OPERATION, DML operation on setup object is not permitted Error
        System.runAs(new User(Id = Userinfo.getUserId())) {
            Date nowday = Date.today();
            OPDPlan__c oPDPlan = new OPDPlan__c();
            oPDPlan.Status__c = '计划中';
            oPDPlan.OPDPlan_ImplementDate__c =  Date.today().addMonths(1).toStartOfMonth().addDays(20);
            oPDPlan.OPDType__c = '询价';
            oPDPlan.Related_Opportunity1_ID__c = opp.Id;
            oPDPlan.NoOpp_Reason__c = null;
            // oPDPlan.OPDPlan_ImplementDate__c = startDate.addDays(3);
            oPDPlan.Campaign__c = cam.Id;
            oPDPlan.PlanProdDetail__c = '1000*1;';
            oPDPlan.If_AutoSort__c = 0;
            oPDPlan.ApprovePassed__c = nowday;
            insert oPDPlan;

            OPDPlan__c oPDPlan2 = new OPDPlan__c();
            oPDPlan2.Status__c = '计划中';
            oPDPlan2.OPDPlan_ImplementDate__c =  Date.today().addMonths(1).toStartOfMonth().addDays(15);

            oPDPlan2.OPDType__c = '询价';
            oPDPlan2.Related_Opportunity1_ID__c = opp.Id;
            oPDPlan2.NoOpp_Reason__c = null;
            // oPDPlan2.OPDPlan_ImplementDate__c = startDate;
            oPDPlan2.PlanProdDetail__c = '1000*1;';
            oPDPlan2.If_AutoSort__c = 0;
            oPDPlan2.ApprovePassed__c = nowday;
            insert oPDPlan2;

            OPDPlan__c oPDPlan3 = new OPDPlan__c();
            oPDPlan3.Status__c = '计划中';
            oPDPlan3.OPDPlan_ImplementDate__c =  Date.today().addMonths(1).toStartOfMonth().addDays(22);
            oPDPlan3.OPDType__c = '询价';
            oPDPlan3.Related_Opportunity1_ID__c = opp.Id;
            oPDPlan3.NoOpp_Reason__c = null;
            // oPDPlan3.OPDPlan_ImplementDate__c = startDate;
            oPDPlan3.PlanProdDetail__c = '1000*1;';
            oPDPlan3.If_AutoSort__c = 0;
            oPDPlan3.ApprovePassed__c = nowday;

            insert oPDPlan3;
            Database.executeBatch(new OPDAutomaticSortBeforeBatch(true),1);
            Database.executeBatch(new OPDAutomaticSortBeforeBatch(Date.today().addDays(-1)),1);
            List<String> ids = new List<String>();
            ids.add(oPDPlan.id);
            ids.add(oPDPlan2.id);
            ids.add(oPDPlan3.id);
            Database.executeBatch(new OPDAutomaticSortBeforeBatch(ids),1);
            Database.executeBatch(new OPDAutomaticSortBeforeBatch(true,Date.today().addDays(-1)),1);
        }
    }
}