@isTest
private class ApprovalHistoryControllerTest {
    private static User getUser() {
        String timenow = Datetime.now().format('yyyyMMddHHmmss');
        User user1 = new User(Test_staff__c = true, LastName = 'TestMao1', FirstName = 'TestMaoF1',
                              Alias = 'hp1', Email = 'Test1@sunbridge.com',
                              Username = 'Test1' + 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 = '2S6_销售本部窗口&营业助理'];
        //System.assertEquals(p.size(), 1);
        System.runAs(new User(Id = Userinfo.getUserId())) {
            insert user1;
        }
        return user1;
    }
    static User setNewUser(String firstName, String lastName, String aName, String email) {
        User user = new User(Test_staff__c = true);
        user.LastName = ' ' + lastName;
        user.FirstName = firstName;
        user.Alias = aName;
        user.Email = email;
        user.Username = 'Olympus' + email;
        user.CommunityNickname = aName;
        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);
        insert user;

        return user;
    }
    @testSetup static void methodName() {
        User user = getUser();
         Account newAccount = new Account();
        newAccount.recordtypeId = [Select Id FROM RecordType WHERE IsActive = true and SobjectType = 'Account' and DeveloperName = 'HP'].id;
        newAccount.Name = 'test hospital';
        newAccount.Is_Active__c = '有効';
        newAccount.OwnerId = user.Id;

        //WLIG-BS2CJW --执行测试类报异常MIXED_DML_OPERATION ---20200807---update By rentongxiao ---Start
        // insert hospital;
        if (Test.isRunningTest()) {
                System.runAs(new User(Id = UserInfo.getUserId())){
                        insert newAccount;
                }
        }
    }
    
    @isTest static void getInitDataTest(){
        User user = getUser();
        System.runAs(user) {
            RecordType rectCo = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and DeveloperName = 'Hp'];

            Account acc1 = new Account();
            acc1.RecordTypeId = rectCo.Id;
            acc1.Name = 'HP test2';
            acc1.Is_Active__c = '草案中';
            acc1.Is_upload_file__c = true;
            insert acc1;

          User toUser = setNewUser('shenqing', 'shenpi', 'spsqTest', 'shenpi@excemaple.com');
          User user5 = setNewUser('ztest05', 'User005', 'Zhang005', 'test005@excemaple.com');
        // 病院を作る
        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.OwnerId = toUser.Id;

        //WLIG-BS2CJW --执行测试类报异常MIXED_DML_OPERATION ---20200807---update By rentongxiao ---Start
        // insert hospital;
        if (Test.isRunningTest()) {
                System.runAs(new User(Id = UserInfo.getUserId())){
                        insert hospital;
                }
        }
        //WLIG-BS2CJW  ---20200807---update By rentongxiao ---End
        
        //新建 客户变更申请
        Account_Delay_Apply__c ada = new Account_Delay_Apply__c();
        ada.Hospital__c = hospital.Id;
        ada.ChangeReason__c = '地址错误';
        ada.Is_Active__c = '草案中';
        ada.Is_upload_file__c = true;
        ada.OpenWindow__c = user5.Id;
        ada.InstitutionalType__c = '非医疗机构';
        ada.CreatedById = toUser.Id;
        
        if (Test.isRunningTest()) {
            System.runAs(new User(Id = UserInfo.getUserId())){
                insert ada;
            }
        }

            String testlink = '医院新建审批_事业推进部';

            Approval.ProcessSubmitRequest r = new Approval.ProcessSubmitRequest();
            r.setObjectId(acc1.Id);
            Approval.process(r);
            Approval.ProcessSubmitRequest b = new Approval.ProcessSubmitRequest();
            b.setObjectId(ada.Id);
            Approval.process(b);
            // String strid = r.getSubmitterId();
            //'/apex/NewAccountExamine?AccId='+AccId+'&testlink='+testlink+'&'
            // PageReference page = new PageReference('/apex/?AccId='+acc1.Id+'&testlink='+testlink+'&');
            // System.Test.setCurrentPage(page);
            ApprovalHistoryController.getInitData(acc1.id,'Account');
            ApprovalHistoryController.getInitData(ada.id,'Account_Delay_Apply__c');

        }
         
        // System.assertEquals(0,result.size());
    }

      @isTest static void getInitDataTest2(){
        User user = getUser();
        System.runAs(user) {

            SolutionProjectRequirements__c solProject = new SolutionProjectRequirements__c();
            solProject.Name = 'Test';
            solProject.HospitalOwner__c = user.Id;
            solProject.Sales_Assistant__c = user.Id;
            solProject.GIR_Assistant__c = user.Id;
            solProject.Project_Type__c = '新建大楼';
            solProject.Purpose_Proposal__c = '促进销售商谈';
            
            insert solProject;
            Solution_Programme__c solProgramme1 =  new Solution_Programme__c();
            solProgramme1.Name = 'test';
            solProgramme1.SolutionProjectRequirements__c = solProject.Id;
            solProgramme1.Business_Promotion__c = user.id;
            solProgramme1.Scheme_Type__c = '初次';
            insert solProgramme1;
            
        User toUser = setNewUser('shenqing', 'shenpi', 'spsqTest', 'shenpi@excemaple.com');
          User user5 = setNewUser('ztest05', 'User005', 'Zhang005', 'test005@excemaple.com');
        //WLIG-BS2CJW  ---20200807---update By rentongxiao ---End
        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.OwnerId = toUser.Id;

        //WLIG-BS2CJW --执行测试类报异常MIXED_DML_OPERATION ---20200807---update By rentongxiao ---Start
        // insert hospital;
        if (Test.isRunningTest()) {
                System.runAs(new User(Id = UserInfo.getUserId())){
                        insert hospital;
                }
        }
        //新建 客户变更申请
        Account_Delay_Apply__c ada = new Account_Delay_Apply__c();
        ada.Hospital__c = hospital.Id;
        ada.ChangeReason__c = '地址错误';
        ada.Is_Active__c = '草案中';
        ada.Is_upload_file__c = true;
        ada.OpenWindow__c = user5.Id;
        ada.InstitutionalType__c = '非医疗机构';
        ada.CreatedById = toUser.Id;
        
        if (Test.isRunningTest()) {
            System.runAs(new User(Id = UserInfo.getUserId())){
                insert ada;
            }
        }

            String testlink = '医院新建审批_事业推进部';

            Approval.ProcessSubmitRequest c = new Approval.ProcessSubmitRequest();
            c.setObjectId(solProgramme1.Id);
            Approval.process(c);

            ApprovalHistoryController.getInitData(solProgramme1.id,'Solution_Programme__c');

        }
         
        // System.assertEquals(0,result.size());
    }

    @isTest static void searchUsersEmptyListTest(){
        List<LookupSearchResult> result = ApprovalHistoryController.searchUsers(null);
        System.assertEquals(0,result.size());
    }

    @isTest static void searchUsersNotEmptyListTest(){
        List<User> users = [SELECT Id, FirstName, LastName FROM User LIMIT 1];
        Id [] fixedSearchResults= new Id[1];
        fixedSearchResults[0] = users.get(0).Id;
        Test.setFixedSearchResults(fixedSearchResults);
        List<LookupSearchResult> result = ApprovalHistoryController.searchUsers(users.get(0).LastName);
        System.assertNotEquals(0,result.size());
    }

    @isTest static void submitForApprovalNoNextApproverTest(){
        List<Account> accounts = [SELECT Id FROM Account LIMIT 1];
        try{
            ApprovalHistoryController.submitForApproval(accounts.get(0).Id, 'string comments', '');
        }catch(Exception e){
        }
    }

    @isTest static void submitForApprovalWithNextApproverTest(){
        List<User> users = [SELECT Id, FirstName, LastName FROM User LIMIT 1];
        List<Account> accounts = [SELECT Id FROM Account LIMIT 1];
        try{
            ApprovalHistoryController.submitForApproval(accounts.get(0).Id, 'string comments', users.get(0).Id);
        }catch(Exception e){
        }
    }

    @isTest static void getApprovalHistoryTest(){
        List<Account> accounts = [SELECT Id FROM Account LIMIT 1];
        ApprovalHistoryController.getApprovalHistory(accounts.get(0).Id);
    }

    @isTest static void processStepTest(){

        User user = getUser();
        System.runAs(user) {
            RecordType rectCo = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and DeveloperName = 'Hp'];
            Address_Level__c al = new Address_Level__c();
            al.Name = '東京';
            al.Level1_Code__c = 'CN-01';
            al.Level1_Sys_No__c = '999999';
            insert al;
            Account acc1 = new Account();
            acc1.RecordTypeId = rectCo.Id;
            acc1.Name = 'HP test2';
            acc1.Is_Active__c = '草案中';
            acc1.HospitalType__c = '企业集团';
            acc1.Is_upload_file__c = true;
            acc1.State_Master__c = al.id;
            acc1.InstitutionalType__c = '非医疗机构';

            insert acc1;

            


        //   User toUser = setNewUser('shenqing', 'shenpi', 'spsqTest', 'shenpi@excemaple.com');
        //   User user5 = setNewUser('ztest05', 'User005', 'Zhang005', 'test005@excemaple.com');
        // // 病院を作る
        // 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.OwnerId = toUser.Id;
        // // insert hospital;
        // if (Test.isRunningTest()) {
        //         System.runAs(new User(Id = UserInfo.getUserId())){
        //                 insert hospital;
        //         }
        // }

    

            Approval.ProcessSubmitRequest r = new Approval.ProcessSubmitRequest();
            r.setObjectId(acc1.Id);
            Approval.process(r);
        List<User> users = [select Id, FirstName, LastName FROM User LIMIT 1];

        // List<ProcessInstanceWorkitem> workItems = new List<ProcessInstanceWorkitem>();
        // workItems.add(new ProcessInstanceWorkitem(Id = '04i3h000001L5fGAAS'));
        // insert workItems;
         
         // 创建一个Map以表示Account对象
        Map<String, Object> accountMap = new Map<String, Object>();
        accountMap.put('CustomizePageFlg__c', true);
        accountMap.put('InstitutionalType__c', null);
        accountMap.put('RejectionReason__c', '地址错误');
        accountMap.put('HospitalType__c', null);
    
        Map<String, Object> accountMap1 = new Map<String, Object>();
        accountMap1.put('CustomizePageFlg__c', true);
        accountMap1.put('InstitutionalType__c', '非医疗机构');
        accountMap1.put('RejectionReason__c', '地址错误');
        accountMap1.put('HospitalType__c', null);
        

        Map<String, Object> accountMap11 = new Map<String, Object>();
        accountMap11.put('CustomizePageFlg__c', true);
        accountMap11.put('InstitutionalType__c', '医疗机构');
        accountMap11.put('RejectionReason__c', '地址错误');
        accountMap11.put('HospitalType__c', '企业集团');

        Map<String, Object> accountMap111 = new Map<String, Object>();
        accountMap111.put('CustomizePageFlg__c', true);
        accountMap111.put('InstitutionalType__c', '非医疗机构');
        accountMap111.put('RejectionReason__c', null);
        accountMap111.put('HospitalType__c', '企业集团');

        Map<String, Object> accountMap2 = new Map<String, Object>();
        accountMap2.put('CustomizePageFlg__c', true);
        accountMap2.put('InstitutionalType__c', '医疗机构');
        accountMap2.put('RejectionReason__c', null);
        accountMap2.put('HospitalType__c', '企业集团');
        accountMap2.put('AssociatedHospital__c', acc1.id);

        Map<String, Object> accountMap22 = new Map<String, Object>();
        accountMap22.put('CustomizePageFlg__c', true);
        accountMap22.put('InstitutionalType__c', '医疗机构');
        accountMap22.put('RejectionReason__c', '重复');
        accountMap22.put('HospitalType__c', '企业集团');
        accountMap22.put('AssociatedHospital__c', null);
        // 将Map转换为JSON格式的字符串
        String accountJSON = JSON.serialize(accountMap);
        String accountJSON1 = JSON.serialize(accountMap1);
        String accountJSON11 = JSON.serialize(accountMap11);
        String accountJSON111 = JSON.serialize(accountMap111);
        String accountJSON2 = JSON.serialize(accountMap2);
        String accountJSON22 = JSON.serialize(accountMap22);
        ApprovalHistoryController.processStep(acc1.id, 'comments', null, 'approve','Account','批准',accountJSON);
        ApprovalHistoryController.processStep(acc1.id, 'comments', null, 'approve','Account','批准',accountJSON1);
        ApprovalHistoryController.processStep(acc1.id, 'comments', null, 'approve','Account','批准',accountJSON11);
        ApprovalHistoryController.processStep(acc1.id, 'comments', null, 'approve','Account','批准',accountJSON111);
        ApprovalHistoryController.processStep(acc1.id, 'comments', null, 'approve','Account','拒绝',accountJSON2);
        ApprovalHistoryController.processStep(acc1.id, 'comments', null, 'approve','Account','拒绝',accountJSON22);

        } 
    }

     @isTest static void processStepTest4(){

        User user = getUser();
        System.runAs(user) {
            RecordType rectCo = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and DeveloperName = 'Hp'];
            Address_Level__c al = new Address_Level__c();
            al.Name = '東京';
            al.Level1_Code__c = 'CN-01';
            al.Level1_Sys_No__c = '999999';
            insert al;
            Account acc1 = new Account();
            acc1.RecordTypeId = rectCo.Id;
            acc1.Name = 'HP test2';
            acc1.Is_Active__c = '草案中';
            acc1.HospitalType__c = '企业集团';
            acc1.Is_upload_file__c = true;
            acc1.State_Master__c = al.id;
            acc1.InstitutionalType__c = '非医疗机构';
            acc1.CustomizePageFlg__c = true;

            insert acc1;

         // 提交审批请求
        Approval.ProcessSubmitRequest submitReq = new Approval.ProcessSubmitRequest();
        submitReq.setObjectId(acc1.Id);
        Approval.ProcessResult result = Approval.process(submitReq);

        // 验证审批是否成功提交
        System.assert(result.isSuccess(), '审批提交失败');
        // 查询审批工作项
        List<ProcessInstanceWorkitem> workItems = [SELECT Id FROM ProcessInstanceWorkitem WHERE ProcessInstance.TargetObjectId = :acc1.Id ];



        // 执行批准操作
        ProcessInstanceWorkitem workItem = workItems[0];
        Approval.ProcessWorkitemRequest approveReq = new Approval.ProcessWorkitemRequest();
        approveReq.setAction('Approve');
        approveReq.setComments('批准审批流');
        approveReq.setNextApproverIds(new Id[] { UserInfo.getUserId() });
        approveReq.setWorkitemId(workItem.Id);
        Approval.ProcessResult approveResult = Approval.process(approveReq);

         // 查询审批工作项
        List<ProcessInstanceWorkitem> workItems1 = [SELECT Id FROM ProcessInstanceWorkitem WHERE ProcessInstance.TargetObjectId = :acc1.Id ];


        Oly_TriggerHandler.bypass('UpdateContractAimAmountHandler');
        // 执行批准操作
        ProcessInstanceWorkitem workItem1 = workItems1[0];
        Approval.ProcessWorkitemRequest approveReq1 = new Approval.ProcessWorkitemRequest();
        approveReq1.setAction('Approve');
        approveReq1.setComments('批准审批流');
        approveReq1.setNextApproverIds(new Id[] { UserInfo.getUserId() });
        approveReq1.setWorkitemId(workItem1.Id);
        Approval.ProcessResult approveResult1 = Approval.process(approveReq1);

   
        List<User> users = [select Id, FirstName, LastName FROM User LIMIT 1];
         
         // 创建一个Map以表示Account对象
        Map<String, Object> accountMap111 = new Map<String, Object>();
        accountMap111.put('InstitutionalType__c', '非医疗机构');

        accountMap111.put('HospitalType__c', '高等院校');

        Map<String, Object> accountMap2 = new Map<String, Object>();
        accountMap2.put('InstitutionalType__c', '医疗机构');
        accountMap2.put('RejectionReason__c', null);

        String accountJSON111 = JSON.serialize(accountMap111);
        String accountJSON2 = JSON.serialize(accountMap2);


        ApprovalHistoryController.processStep(acc1.id, 'comments', UserInfo.getUserId(), 'approve','Account','批准',accountJSON111);
      
        ApprovalHistoryController.processStep(acc1.id, 'comments', null, 'Rejected','Account','拒绝',accountJSON2);
        
        } 
    }




    @isTest static void processStepTest6(){

        User user = getUser();
        System.runAs(user) {
            RecordType rectCo = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and DeveloperName = 'Hp'];
            Address_Level__c al = new Address_Level__c();
            al.Name = '東京';
            al.Level1_Code__c = 'CN-01';
            al.Level1_Sys_No__c = '999999';
            insert al;
            Account acc1 = new Account();
            acc1.RecordTypeId = rectCo.Id;
            acc1.Name = 'HP test2';
            acc1.Is_Active__c = '草案中';
            acc1.HospitalType__c = '企业集团';
            acc1.Is_upload_file__c = true;
            acc1.State_Master__c = al.id;
            acc1.InstitutionalType__c = '非医疗机构';

            insert acc1;


            

          User toUser = setNewUser('shenqing', 'shenpi', 'spsqTest', 'shenpi@excemaple.com');
          User user5 = setNewUser('ztest05', 'User005', 'Zhang005', 'test005@excemaple.com');
        // 病院を作る
        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.OwnerId = toUser.Id;

        //WLIG-BS2CJW --执行测试类报异常MIXED_DML_OPERATION ---20200807---update By rentongxiao ---Start
        // insert hospital;
        if (Test.isRunningTest()) {
                System.runAs(new User(Id = UserInfo.getUserId())){
                        insert hospital;
                }
        }
        //WLIG-BS2CJW  ---20200807---update By rentongxiao ---End
        
        //新建 客户变更申请
        Account_Delay_Apply__c ada = new Account_Delay_Apply__c();
        ada.Hospital__c = hospital.Id;
        ada.ChangeReason__c = '地址错误';
        ada.Is_Active__c = '草案中';
        ada.Is_upload_file__c = true;
        ada.OpenWindow__c = user5.Id;
        ada.InstitutionalType__c = '非医疗机构';
        ada.HospitalType__c = '企业集团';
        ada.CreatedById = toUser.Id;
        
        if (Test.isRunningTest()) {
            System.runAs(new User(Id = UserInfo.getUserId())){
                insert ada;
            }
        }

           


        List<User> users = [select Id, FirstName, LastName FROM User LIMIT 1];

         // 提交审批请求
        Approval.ProcessSubmitRequest submitReq = new Approval.ProcessSubmitRequest();
        submitReq.setObjectId(ada.Id);
        Approval.ProcessResult result = Approval.process(submitReq);

        // 验证审批是否成功提交
        System.assert(result.isSuccess(), '审批提交失败');
        // 查询审批工作项
        List<ProcessInstanceWorkitem> workItems = [SELECT Id FROM ProcessInstanceWorkitem WHERE ProcessInstance.TargetObjectId = :ada.Id ];


        Oly_TriggerHandler.bypass('AccountDelayApplyHandler');
        // 执行批准操作
        ProcessInstanceWorkitem workItem = workItems[0];
        Approval.ProcessWorkitemRequest approveReq = new Approval.ProcessWorkitemRequest();
        approveReq.setAction('Approve');
        approveReq.setComments('批准审批流');
        approveReq.setNextApproverIds(new Id[] { UserInfo.getUserId() });
        approveReq.setWorkitemId(workItem.Id);
        Approval.ProcessResult approveResult = Approval.process(approveReq);

         // 查询审批工作项
        List<ProcessInstanceWorkitem> workItems1 = [SELECT Id FROM ProcessInstanceWorkitem WHERE ProcessInstance.TargetObjectId = :ada.Id ];


        Oly_TriggerHandler.bypass('AccountDelayApplyHandler');
        // 执行批准操作
        ProcessInstanceWorkitem workItem1 = workItems1[0];
        Approval.ProcessWorkitemRequest approveReq1 = new Approval.ProcessWorkitemRequest();
        approveReq1.setAction('Approve');
        approveReq1.setComments('批准审批流');
        approveReq1.setNextApproverIds(new Id[] { UserInfo.getUserId() });
        approveReq1.setWorkitemId(workItem1.Id);
        Approval.ProcessResult approveResult1 = Approval.process(approveReq1);



         // 创建一个Map以表示Account对象
        Map<String, Object> AccountDelay1 = new Map<String, Object>();
        AccountDelay1.put('InstitutionalType__c', '非医疗机构');

        AccountDelay1.put('HospitalType__c', '高等院校');


        Map<String, Object> AccountDelay2 = new Map<String, Object>();

        AccountDelay2.put('InstitutionalType__c', null);

        // 将Map转换为JSON格式的字符串

        String AccountDelayJson1 = JSON.serialize(AccountDelay1);

        String AccountDelayJson2 = JSON.serialize(AccountDelay2);

        String adaId = ada.id;

        ApprovalHistoryController.processStep(adaId, 'comments', null, 'approve','Account_Delay_Apply__c','批准',AccountDelayJson1);

        ApprovalHistoryController.processStep(adaId, 'comments', null, 'approve','Account_Delay_Apply__c','拒绝',AccountDelayJson2);

        } 
    }

   @isTest static void processStepTest7(){

        User user = getUser();
        System.runAs(user) {
            RecordType rectCo = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and DeveloperName = 'Hp'];
            Address_Level__c al = new Address_Level__c();
            al.Name = '東京';
            al.Level1_Code__c = 'CN-01';
            al.Level1_Sys_No__c = '999999';
            insert al;
            Account acc1 = new Account();
            acc1.RecordTypeId = rectCo.Id;
            acc1.Name = 'HP test2';
            acc1.Is_Active__c = '草案中';
            acc1.HospitalType__c = '企业集团';
            acc1.Is_upload_file__c = true;
            acc1.State_Master__c = al.id;
            acc1.InstitutionalType__c = '非医疗机构';

            insert acc1;


            

          User toUser = setNewUser('shenqing', 'shenpi', 'spsqTest', 'shenpi@excemaple.com');
          User user5 = setNewUser('ztest05', 'User005', 'Zhang005', 'test005@excemaple.com');
        // 病院を作る
        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.OwnerId = toUser.Id;

        //WLIG-BS2CJW --执行测试类报异常MIXED_DML_OPERATION ---20200807---update By rentongxiao ---Start
        // insert hospital;
        if (Test.isRunningTest()) {
                System.runAs(new User(Id = UserInfo.getUserId())){
                        insert hospital;
                }
        }
        //WLIG-BS2CJW  ---20200807---update By rentongxiao ---End
        
        //新建 客户变更申请
        Account_Delay_Apply__c ada = new Account_Delay_Apply__c();
        ada.Hospital__c = hospital.Id;
        ada.ChangeReason__c = '地址错误';
        ada.Is_Active__c = '草案中';
        ada.Is_upload_file__c = true;
        ada.OpenWindow__c = user5.Id;
        ada.InstitutionalType__c = '非医疗机构';
        ada.HospitalType__c = '企业集团';
        ada.CreatedById = toUser.Id;
        
        if (Test.isRunningTest()) {
            System.runAs(new User(Id = UserInfo.getUserId())){
                insert ada;
            }
        }

           


        List<User> users = [select Id, FirstName, LastName FROM User LIMIT 1];

         // 提交审批请求
        Approval.ProcessSubmitRequest submitReq = new Approval.ProcessSubmitRequest();
        submitReq.setObjectId(ada.Id);
        Approval.ProcessResult result = Approval.process(submitReq);

        // 验证审批是否成功提交
        System.assert(result.isSuccess(), '审批提交失败');
        // 查询审批工作项
        List<ProcessInstanceWorkitem> workItems = [SELECT Id FROM ProcessInstanceWorkitem WHERE ProcessInstance.TargetObjectId = :ada.Id ];


        Oly_TriggerHandler.bypass('AccountDelayApplyHandler');
        // 执行批准操作
        ProcessInstanceWorkitem workItem = workItems[0];
        Approval.ProcessWorkitemRequest approveReq = new Approval.ProcessWorkitemRequest();
        approveReq.setAction('Approve');
        approveReq.setComments('批准审批流');
        approveReq.setNextApproverIds(new Id[] { UserInfo.getUserId() });
        approveReq.setWorkitemId(workItem.Id);
        Approval.ProcessResult approveResult = Approval.process(approveReq);

         // 查询审批工作项
        List<ProcessInstanceWorkitem> workItems1 = [SELECT Id FROM ProcessInstanceWorkitem WHERE ProcessInstance.TargetObjectId = :ada.Id ];


        Oly_TriggerHandler.bypass('AccountDelayApplyHandler');
        // 执行批准操作
        ProcessInstanceWorkitem workItem1 = workItems1[0];
        Approval.ProcessWorkitemRequest approveReq1 = new Approval.ProcessWorkitemRequest();
        approveReq1.setAction('Approve');
        approveReq1.setComments('批准审批流');
        approveReq1.setNextApproverIds(new Id[] { UserInfo.getUserId() });
        approveReq1.setWorkitemId(workItem1.Id);
        Approval.ProcessResult approveResult1 = Approval.process(approveReq1);



         // 创建一个Map以表示Account对象
        Map<String, Object> AccountDelay1 = new Map<String, Object>();
        AccountDelay1.put('InstitutionalType__c', '非医疗机构');

        AccountDelay1.put('HospitalType__c', '企业集团');
        AccountDelay1.put('WhetherRiskPassing__c', '否');



        // 将Map转换为JSON格式的字符串

        String AccountDelayJson1 = JSON.serialize(AccountDelay1);


        String adaId = ada.id;

        ApprovalHistoryController.processStep(adaId, 'comments', null, 'approve','Account_Delay_Apply__c','批准',AccountDelayJson1);


        } 
    }



   @isTest static void processStepTest8(){

        User user = getUser();
        System.runAs(user) {
            RecordType rectCo = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and DeveloperName = 'Hp'];
            Address_Level__c al = new Address_Level__c();
            al.Name = '東京';
            al.Level1_Code__c = 'CN-01';
            al.Level1_Sys_No__c = '999999';
            insert al;
            Account acc1 = new Account();
            acc1.RecordTypeId = rectCo.Id;
            acc1.Name = 'HP test2';
            acc1.Is_Active__c = '草案中';
            acc1.HospitalType__c = '企业集团';
            acc1.Is_upload_file__c = true;
            acc1.State_Master__c = al.id;
            acc1.InstitutionalType__c = '非医疗机构';

            insert acc1;


            

          User toUser = setNewUser('shenqing', 'shenpi', 'spsqTest', 'shenpi@excemaple.com');
          User user5 = setNewUser('ztest05', 'User005', 'Zhang005', 'test005@excemaple.com');
        // 病院を作る
        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.OwnerId = toUser.Id;

        //WLIG-BS2CJW --执行测试类报异常MIXED_DML_OPERATION ---20200807---update By rentongxiao ---Start
        // insert hospital;
        if (Test.isRunningTest()) {
                System.runAs(new User(Id = UserInfo.getUserId())){
                        insert hospital;
                }
        }
        //WLIG-BS2CJW  ---20200807---update By rentongxiao ---End
        
        //新建 客户变更申请
        Account_Delay_Apply__c ada = new Account_Delay_Apply__c();
        ada.Hospital__c = hospital.Id;
        ada.ChangeReason__c = '地址错误';
        ada.Is_Active__c = '草案中';
        ada.Is_upload_file__c = true;
        ada.OpenWindow__c = user5.Id;
        ada.InstitutionalType__c = '非医疗机构';
        ada.HospitalType__c = '企业集团';
        ada.CreatedById = toUser.Id;
        
        if (Test.isRunningTest()) {
            System.runAs(new User(Id = UserInfo.getUserId())){
                insert ada;
            }
        }

           


        List<User> users = [select Id, FirstName, LastName FROM User LIMIT 1];

         // 提交审批请求
        Approval.ProcessSubmitRequest submitReq = new Approval.ProcessSubmitRequest();
        submitReq.setObjectId(ada.Id);
        Approval.ProcessResult result = Approval.process(submitReq);

        // 验证审批是否成功提交
        System.assert(result.isSuccess(), '审批提交失败');
        // 查询审批工作项
        List<ProcessInstanceWorkitem> workItems = [SELECT Id FROM ProcessInstanceWorkitem WHERE ProcessInstance.TargetObjectId = :ada.Id ];


        Oly_TriggerHandler.bypass('AccountDelayApplyHandler');
        // 执行批准操作
        ProcessInstanceWorkitem workItem = workItems[0];
        Approval.ProcessWorkitemRequest approveReq = new Approval.ProcessWorkitemRequest();
        approveReq.setAction('Approve');
        approveReq.setComments('批准审批流');
        approveReq.setNextApproverIds(new Id[] { UserInfo.getUserId() });
        approveReq.setWorkitemId(workItem.Id);
        Approval.ProcessResult approveResult = Approval.process(approveReq);

         // 查询审批工作项
        List<ProcessInstanceWorkitem> workItems1 = [SELECT Id FROM ProcessInstanceWorkitem WHERE ProcessInstance.TargetObjectId = :ada.Id ];





         // 创建一个Map以表示Account对象
        Map<String, Object> AccountDelay1 = new Map<String, Object>();
        AccountDelay1.put('InstitutionalType__c', '非医疗机构');

        AccountDelay1.put('HospitalType__c', '高等院校');


        Map<String, Object> AccountDelay2 = new Map<String, Object>();

        AccountDelay2.put('InstitutionalType__c', null);

        // 将Map转换为JSON格式的字符串

        String AccountDelayJson1 = JSON.serialize(AccountDelay1);

        String AccountDelayJson2 = JSON.serialize(AccountDelay2);

        String adaId = ada.id;

        ApprovalHistoryController.processStep(adaId, 'comments', null, 'approve','Account_Delay_Apply__c','批准',AccountDelayJson1);

        ApprovalHistoryController.processStep(adaId, 'comments', null, 'approve','Account_Delay_Apply__c','拒绝',AccountDelayJson2);

        

        } 
    }

















































     @isTest static void processStepTest3(){

        User user = getUser();
        System.runAs(user) {
          
            

            SolutionProjectRequirements__c solProject = new SolutionProjectRequirements__c();
            solProject.Name = 'Test';
            solProject.HospitalOwner__c = user.Id;
            solProject.Sales_Assistant__c = user.Id;
            solProject.GIR_Assistant__c = user.Id;
            solProject.Project_Type__c = '新建大楼';
            solProject.Purpose_Proposal__c = '促进销售商谈';
            
            insert solProject;
            Solution_Programme__c solProgramme1 =  new Solution_Programme__c();
            solProgramme1.Name = 'test';
            solProgramme1.SolutionProjectRequirements__c = solProject.Id;
            solProgramme1.Business_Promotion__c = user.id;
            solProgramme1.Scheme_Type__c = '初次';
            insert solProgramme1;


            Approval.ProcessSubmitRequest c = new Approval.ProcessSubmitRequest();
            c.setObjectId(solProgramme1.Id);
            Approval.process(c);
        List<User> users = [select Id, FirstName, LastName FROM User LIMIT 1];

       
        Map<String, Object> solProgrammemap = new Map<String, Object>();
        solProgrammemap.put('name', null);
        String solProgramme = JSON.serialize(solProgrammemap);
        Map<String, Object> solProgramme11 = new Map<String, Object>();
        solProgramme11.put('Confirmation_Result__c', null);
        String solProgramme2 = JSON.serialize(solProgramme11);
        ApprovalHistoryController.processStep(solProgramme1.id, 'comments', null, 'approve','Solution_Programme__c','拒绝',solProgramme);
        ApprovalHistoryController.processStep(solProgramme1.id, 'comments', null, 'approve','Solution_Programme__c','批准',solProgramme2);

        } 
    }


    @isTest static void processStepTest2(){

        User user = getUser();
        System.runAs(user) {
            RecordType rectCo = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and DeveloperName = 'Hp'];
            Address_Level__c al = new Address_Level__c();
            al.Name = '東京';
            al.Level1_Code__c = 'CN-01';
            al.Level1_Sys_No__c = '999999';
            insert al;
            Account acc1 = new Account();
            acc1.RecordTypeId = rectCo.Id;
            acc1.Name = 'HP test2';
            acc1.Is_Active__c = '草案中';
            acc1.HospitalType__c = '企业集团';
            acc1.Is_upload_file__c = true;
            acc1.State_Master__c = al.id;
            acc1.InstitutionalType__c = '非医疗机构';

            insert acc1;

          User toUser = setNewUser('shenqing', 'shenpi', 'spsqTest', 'shenpi@excemaple.com');
          User user5 = setNewUser('ztest05', 'User005', 'Zhang005', 'test005@excemaple.com');
        // 病院を作る
        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.OwnerId = toUser.Id;

        //WLIG-BS2CJW --执行测试类报异常MIXED_DML_OPERATION ---20200807---update By rentongxiao ---Start
        // insert hospital;
        if (Test.isRunningTest()) {
                System.runAs(new User(Id = UserInfo.getUserId())){
                        insert hospital;
                }
        }
        //WLIG-BS2CJW  ---20200807---update By rentongxiao ---End
        
        //新建 客户变更申请
        Account_Delay_Apply__c ada = new Account_Delay_Apply__c();
        ada.Hospital__c = hospital.Id;
        ada.ChangeReason__c = '地址错误';
        ada.Is_Active__c = '草案中';
        ada.Is_upload_file__c = true;
        ada.OpenWindow__c = user5.Id;
        ada.InstitutionalType__c = '非医疗机构';
        ada.CreatedById = toUser.Id;
        
        if (Test.isRunningTest()) {
            System.runAs(new User(Id = UserInfo.getUserId())){
                insert ada;
            }
        }

           
            Approval.ProcessSubmitRequest b = new Approval.ProcessSubmitRequest();
            b.setObjectId(ada.Id);
            Approval.process(b);

        List<User> users = [select Id, FirstName, LastName FROM User LIMIT 1];

    


         // 创建一个Map以表示Account对象
        Map<String, Object> AccountDelay = new Map<String, Object>();
        AccountDelay.put('InstitutionalType__c', null);

    
        Map<String, Object> AccountDelay1 = new Map<String, Object>();
        AccountDelay1.put('InstitutionalType__c', '非医疗机构');

        AccountDelay1.put('HospitalType__c', null);
        

        Map<String, Object> AccountDelay11 = new Map<String, Object>();
        AccountDelay11.put('InstitutionalType__c', '医疗机构');

        Map<String, Object> AccountDelay2 = new Map<String, Object>();

        AccountDelay2.put('InstitutionalType__c', null);


        Map<String, Object> AccountDelay22 = new Map<String, Object>();
        AccountDelay22.put('InstitutionalType__c', '医疗机构');


         Map<String, Object> AccountDelay222 = new Map<String, Object>();
        AccountDelay222.put('InstitutionalType__c', '非医疗机构');
        // 将Map转换为JSON格式的字符串
        String AccountDelayJson = JSON.serialize(AccountDelay);
        String AccountDelayJson1 = JSON.serialize(AccountDelay1);
        String AccountDelayJson11 = JSON.serialize(AccountDelay11);
        String AccountDelayJson2 = JSON.serialize(AccountDelay2);
        String AccountDelayJson22 = JSON.serialize(AccountDelay22);
        String AccountDelayJson222 = JSON.serialize(AccountDelay222);
        String adaId = ada.id;
        ApprovalHistoryController.processStep(adaId, 'comments', null, 'approve','Account_Delay_Apply__c','批准',AccountDelayJson);
        ApprovalHistoryController.processStep(adaId, 'comments', null, 'approve','Account_Delay_Apply__c','批准',AccountDelayJson1);
        ApprovalHistoryController.processStep(adaId, 'comments', null, 'approve','Account_Delay_Apply__c','批准',AccountDelayJson11);
        // ApprovalHistoryController.processStep(ada.id, 'comments', null, 'approve','Account_Delay_Apply__c','批准',AccountDelay111);
        ApprovalHistoryController.processStep(adaId, 'comments', null, 'approve','Account_Delay_Apply__c','拒绝',AccountDelayJson2);
        ApprovalHistoryController.processStep(adaId, 'comments', null, 'approve','Account_Delay_Apply__c','拒绝',AccountDelayJson22);
        ApprovalHistoryController.processStep(adaId, 'comments', null, 'approve','Account_Delay_Apply__c','拒绝',AccountDelayJson222);
        } 
    }
    @isTest static void reassignStepTest(){
        try{
            List<User> users = [SELECT Id, FirstName, LastName FROM User LIMIT 1];
            List<Account> accounts = [SELECT Id FROM Account LIMIT 1];
            ApprovalHistoryController.reassignStep(accounts.get(0).Id,users.get(0).Id );
        }catch(Exception e){
        }
        ApprovalHistoryController.ApprovalHistoryStep appstep = new ApprovalHistoryController.ApprovalHistoryStep('1', '1',Date.today(),'Submitted','1','1','1');
        ApprovalHistoryController.ApprovalHistoryStep appstep1 = new ApprovalHistoryController.ApprovalHistoryStep('1', '1',Date.today(),'Started','1','1','1');
        ApprovalHistoryController.ApprovalHistoryStep appstep2 = new ApprovalHistoryController.ApprovalHistoryStep('1', '1',Date.today(),'Removed','1','1','1');

        ApprovalHistoryController.ApprovalHistoryStep appstep3 = new ApprovalHistoryController.ApprovalHistoryStep('1', '1',Date.today(),'Submitted','1','1','1','1','1');
        ApprovalHistoryController.ApprovalHistoryStep appstep4 = new ApprovalHistoryController.ApprovalHistoryStep('1', '1',Date.today(),'Started','1','1','1','1','1');
        ApprovalHistoryController.ApprovalHistoryStep appstep5 = new ApprovalHistoryController.ApprovalHistoryStep('1', '1',Date.today(),'Removed','1','1','1','1','1');

    }

}