@isTest
private class PersonalEvaluationControllerTest {
    private static UserRole ur1{get;set;}
    private static UserRole ur2{get;set;}
    private static User u1{get;set;}
    private static User u2{get;set;}
    private static User u3{get;set;}

    private static void init() {
        Profile p = [select id from Profile where id =:System.Label.ProfileId_SystemAdmin];
        ur1 = [Select Id from UserRole where DeveloperName = 'Quality_Laws_Department'];
        ur2 = [Select Id from UserRole where DeveloperName = 'Quality_Laws_Section'];
        // まず user を作成
        u1 = new User();
        u1.LastName = '_サンブリッジ';
        u1.FirstName = 'あ';
        u1.Alias = 'あ';
        u1.Email = 'olympusTest01@sunbridge.com';
        u1.Username = 'olympusTest11@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 = p.id;
        u1.Job_Category__c = '销售服务';
        u1.Province__c = '北京市';
        u1.Post__c = '一般';
        u1.Province_select__c = '北京市;河南省';
        u1.UserRoleId = ur1.id;
        u2 = new User();
        u2.LastName = '_サンブリッジ';
        u2.FirstName = 'い';
        u2.Alias = 'い';
        u2.Email = 'olympusTest02@sunbridge.com';
        u2.Username = 'olympusTest12@sunbridge.com';
        u2.CommunityNickname = 'い2';
        u2.IsActive = true;
        u2.EmailEncodingKey = 'ISO-2022-JP';
        u2.TimeZoneSidKey = 'Asia/Tokyo';
        u2.LocaleSidKey = 'ja_JP';
        u2.LanguageLocaleKey = 'ja';
        u2.ProfileId = p.id;
        u2.Job_Category__c = '销售推广';
        u2.Province__c = '北京市';
        u2.Post__c = '高级';
        u2.Province_select__c = '北京市;河南省';
        u2.UserRoleId = ur2.id;
        u3 = new User();
        u3.LastName = '_サンブリッジ';
        u3.FirstName = 'う';
        u3.Alias = 'う';
        u3.Email = 'olympusTest03@sunbridge.com';
        u3.Username = 'olympusTest13@sunbridge.com';
        u3.CommunityNickname = 'う3';
        u3.IsActive = true;
        u3.EmailEncodingKey = 'ISO-2022-JP';
        u3.TimeZoneSidKey = 'Asia/Tokyo';
        u3.LocaleSidKey = 'ja_JP';
        u3.LanguageLocaleKey = 'ja';
        u3.ProfileId = p.id;
        u3.Job_Category__c = '销售服务';
        u3.Province__c = '河南省';
        u3.Post__c = '主管';
        u3.Province_select__c = '北京市;河南省';
        u3.UserRoleId = ur2.id;
        insert new List<User>{u1, u2, u3};

        // その後、UserRoleを変更
        // http://blog.livedoor.jp/volvic_beer/archives/52285724.html
//        System.runAs(u1) {
//            update new List<User>{u1, u2, u3};
//        }
    }
    
    // 各种条件画面初始化测试
    static testMethod void test1() {
        init();
        User thisUser = [ select Id from User where Id = :UserInfo.getUserId() ];
        System.runAs(thisUser) {
            Personal_Evaluation__c pe1 = new Personal_Evaluation__c(Name = '3.业绩管理', Api_Name__c = '3.业绩管理', IsActive__c = true);
            pe1.Report1_1__c = 'select GBIC__c GBIC, sum(Sales_amount_without_tax__c) wt, sum(Qty__c) num from ship__c';
            pe1.Report1_2__c = 'select GBIC__c GBIC, sum(Sales_amount_without_tax__c) wt, sum(Qty__c) num from ship__c';
            pe1.Report1_3__c = 'select GBIC__c GBIC, sum(Sales_amount_without_tax__c) wt, sum(Qty__c) num from ship__c';
            pe1.Report2_1__c = 'select sum(Fiscal_Workdays__c) wt, sum(OPD_target_number_year__c) num from User where Id = :userId';
            pe1.Report2_2__c = 'select sum(Fiscal_Workdays__c) wt, sum(OPD_target_number_year__c) num from User where Id = :userId';
            pe1.Report2_3__c = 'select sum(Fiscal_Workdays__c) wt, sum(OPD_target_number_year__c) num from User where Id = :userId';
            pe1.Report3_1__c = 'select sum(Fiscal_Workdays__c) wt, sum(OPD_target_number_year__c) num from User where Id = :userId';
            pe1.Report3_2__c = 'select sum(Fiscal_Workdays__c) wt, sum(OPD_target_number_year__c) num from User where Id = :userId';
            pe1.Report3_3__c = 'select sum(Fiscal_Workdays__c) wt, sum(OPD_target_number_year__c) num from User where Id = :userId';
            pe1.Report4_1__c = 'select sum(Fiscal_Workdays__c) wt, sum(OPD_target_number_year__c) num from User where Id = :userId';
            pe1.Report4_3__c = 'Select sum(order_number__c) order, sum(Not_collect_money__c) NClct, sum(Not_received_endo_user_contract__c) NCtrc from Statu_Achievements__c where Owner_ID__c = :userId';
            pe1.Report5_1__c = 'Select Sales_forecast_day_BA__c, Sales_forecast_YM_BA__c, Id from Opportunity where OwnerId = :userId and Sales_forecast_day_BA__c < :maxForcastDay';
            pe1.Report1_1_Column__c = 'where GBIC__c=\'GI\' group by GBIC__c';
            pe1.Report1_2_Column__c = 'where GBIC__c=\'BF\' group by GBIC__c';
            pe1.Report1_3_Column__c = 'where GBIC__c=\'BF\' group by GBIC__c';
            pe1.Report5_1_Column__c = 'Order By Sales_forecast_day_BA__c';
            pe1.Tab_Index__c = 0;
            insert pe1;
            
            Personal_Evaluation__c pe2 = new Personal_Evaluation__c(Name = '99', Api_Name__c = '99', IsActive__c = true);
            pe2.Report1_1__c = 'Select Name, Report_Date__c From SMARM__c  limit 1000';
            pe2.Report1_2__c = 'Select Training_date__c, RecordType.Name from Training_test_Result__c  limit 1000';
            pe2.Report1_3__c = 'Select Received_call_day__c, LINK__c, prod__c from case where recordtypeId = \'01210000000QsYp\' limit 1000';
            pe2.Tab_Index__c = 99;
            insert pe2;
        }
        System.runAs(u1) {
            PageReference page = new PageReference('/apex/PersonalEvaluation');
            System.Test.setCurrentPage(page);
            PersonalEvaluationController controller = new PersonalEvaluationController();
            //初始化
            controller.init();
            System.assertEquals(2, controller.userOpts.size());
            System.assertEquals(u1.Id, controller.selectUser);      // ログインユーザー
            page = new PageReference('/apex/PersonalEvaluation?tabIdx=0&user=' + UserInfo.getUserId());
            System.Test.setCurrentPage(page);
            controller = new PersonalEvaluationController();
            controller.init2();

            // 条件追加されたため、件数が減ります
            page = new PageReference('/apex/PersonalEvaluation?province=' + EncodingUtil.urlEncode('河南省', 'UTF-8') + '&tab=0');
            System.Test.setCurrentPage(page);
            PersonalEvaluationController controller2 = new PersonalEvaluationController();
            //初始化
            controller2.init();
            System.assertEquals(1, controller2.userOpts.size());
            System.assertEquals(u3.Id, controller2.selectUser);
            
            page = new PageReference('/apex/PersonalEvaluation?salesDpt=' + EncodingUtil.urlEncode('3.西北', 'UTF-8') + '&ocm=0');
            System.Test.setCurrentPage(page);
            PersonalEvaluationController controller3 = new PersonalEvaluationController();
            //初始化
            controller3.init();
            System.assertEquals(1, controller3.userOpts.size());
            System.assertEquals(u3.Id, controller3.selectUser);
            /*
            page = new PageReference('/apex/PersonalEvaluation?text=S:Job_Category__c&cond=notequals&val=销售服务,支援');
            System.Test.setCurrentPage(page);
            PersonalEvaluationController controller4 = new PersonalEvaluationController();
            //初始化
            controller4.init();
            System.assertEquals(1, controller4.userOpts.size());
            System.assertEquals(u2.Id, controller4.selectUser);
            */
            page = new PageReference('/apex/PersonalEvaluation?user=' + u2.Id);
            System.Test.setCurrentPage(page);
            PersonalEvaluationController controller5 = new PersonalEvaluationController();
            //初始化
            controller5.init();
            System.assertEquals(2, controller5.userOpts.size());
            System.assertEquals(u2.Id, controller5.selectUser);
            
            controller5.getProvinceUrl();
            controller5.getSearchJsUrl();
            controller5.getUserUrl();
        }
    }
    
    // RemoteAction方法测试
    static testMethod void test2() {
        init();
        User thisUser = [ select Id from User where Id = :UserInfo.getUserId() ];
        System.runAs(thisUser) {
            Personal_Evaluation__c pe1 = new Personal_Evaluation__c(Name = '3.业绩管理', Api_Name__c = '3.业绩管理', IsActive__c = true);
            pe1.Report6_1_Sql__c = 'select GBIC__c GBIC, sum(Sales_amount_without_tax__c) wt, sum(Qty__c) num from ship__c';
            pe1.Report6_2_Sql__c = 'select GBIC__c GBIC, sum(Sales_amount_without_tax__c) wt, sum(Qty__c) num from ship__c';
            pe1.Report6_3_Sql__c = 'select GBIC__c GBIC, sum(Sales_amount_without_tax__c) wt, sum(Qty__c) num from ship__c';
            pe1.Report7_1_Sql__c = 'Select sum(order_number__c) order, sum(Not_collect_money__c) NClct, sum(Not_received_endo_user_contract__c) NCtrc from Statu_Achievements__c where Owner_ID__c = :userId';
            pe1.Report7_3_Sql__c = 'Select Sales_forecast_day_BA__c, Sales_forecast_YM_BA__c, Id from Opportunity where OwnerId = :userId and Sales_forecast_day_BA__c < :maxForcastDay';
            pe1.Report8_1_Sql__c = 'Select Sum(Amount_Without_Tax__c) wt, Sum(Number_of_PCL__c) num from Opportunity where OwnerId = :userId';
            pe1.Report8_2_Sql__c = 'Select Sum(Amount_Without_Tax__c) wt, Sum(Number_of_PCL__c) num from Opportunity where OwnerId = :userId';
            pe1.Report8_3_Sql__c = 'Select Sum(Amount_Without_Tax__c) wt, Sum(Number_of_PCL__c) num from Opportunity where OwnerId = :userId';
            pe1.Report6_1_Sql2__c = 'where GBIC__c=\'GI\' group by GBIC__c';
            pe1.Report6_2_Sql2__c = 'where GBIC__c=\'BF\' group by GBIC__c';
            pe1.Report6_3_Sql2__c = 'where GBIC__c=\'BF\' group by GBIC__c';
            pe1.Report7_3_Sql2__c = 'Order By Sales_forecast_day_BA__c';
            pe1.Tab_Index__c = 0;
            insert pe1;
            
            Personal_Evaluation__c pe2 = new Personal_Evaluation__c(Name = '99', Api_Name__c = '99', IsActive__c = true);
            pe2.Report1_1__c = 'Select Name, Report_Date__c From SMARM__c  limit 1000';
            pe2.Report1_2__c = 'Select Training_date__c, RecordType.Name from Training_test_Result__c  limit 1000';
            pe2.Report1_3__c = 'Select Received_call_day__c, LINK__c, prod__c from case where recordtypeId = \'01210000000QsYp\' limit 1000';
            pe2.Tab_Index__c = 99;
            insert pe2;
        }
        
        System.runAs(u1) {
            PageReference page = new PageReference('/apex/PersonalEvaluation');
            System.Test.setCurrentPage(page);
            PersonalEvaluationController controller = new PersonalEvaluationController();
            //初始化
            controller.init();
            
            PersonalEvaluationController.saveComment(u1.Id, '01210000000RDuV', 'subject', 'comment', null, '', 'rptName', 'rptNum');
            PersonalEvaluationController.saveComment(u1.Id, '01210000000RDua', 'subject', 'comment', null, 'tabName', '', '');
            PersonalEvaluationController.getPeSettings('3.业绩管理', u1.Id);
            PersonalEvaluationController.getJsonSAP('', u1.Id,'151P');
            PersonalEvaluationController.getJsonOppSum('', u1.Id);
            PersonalEvaluationController.getJsonMBSA('', u1.Id);
            PersonalEvaluationController.getJsonForcast('', u1.Id);
            PersonalEvaluationController.getPersonalEvaluationSettingJson();
            PersonalEvaluationController.getFirstTabIdx();
            controller.getR1InfoList();
            controller.getR2InfoList();
            controller.getR3InfoList();
            controller.getR1InfoColumnList();
            controller.getR2InfoColumnList();
            controller.getR3InfoColumnList();
        }
    }

    static testMethod void testPersonalEvaluationCommentListController() {
        PageReference page = new PageReference('/apex/PersonalEvaluationCommentList?user=A&rpt=rpt');
        System.Test.setCurrentPage(page);
        PersonalEvaluationCommentListController controller = new PersonalEvaluationCommentListController();
        controller.getCommentList();
        page = new PageReference('/apex/PersonalEvaluationCommentList?user=A&tab=tab');
        System.Test.setCurrentPage(page);
        controller = new PersonalEvaluationCommentListController();
        controller.getCommentList();
    }

    // RemoteAction方法测试  upsertSmarm
    static testMethod void test3() {
        init();
        User thisUser = [ select Id from User where Id = :UserInfo.getUserId() ];
        System.runAs(thisUser) {
            Personal_Evaluation__c pe1 = new Personal_Evaluation__c(Name = '3.业绩管理', Api_Name__c = '3.业绩管理', IsActive__c = true);
            pe1.Report6_1_Sql__c = 'select GBIC__c GBIC, sum(Sales_amount_without_tax__c) wt, sum(Qty__c) num from ship__c';
            pe1.Report6_2_Sql__c = 'select GBIC__c GBIC, sum(Sales_amount_without_tax__c) wt, sum(Qty__c) num from ship__c';
            pe1.Report6_3_Sql__c = 'select GBIC__c GBIC, sum(Sales_amount_without_tax__c) wt, sum(Qty__c) num from ship__c';
            pe1.Report7_1_Sql__c = 'Select sum(order_number__c) order, sum(Not_collect_money__c) NClct, sum(Not_received_endo_user_contract__c) NCtrc from Statu_Achievements__c where Owner_ID__c = :userId';
            pe1.Report7_3_Sql__c = 'Select Sales_forecast_day_BA__c, Sales_forecast_YM_BA__c, Id from Opportunity__c where OwnerId = :userId and Sales_forecast_day_BA__c < :maxForcastDay';
            pe1.Report8_1_Sql__c = 'Select Sum(Amount_Without_Tax__c) wt, Sum(Number_of_PCL__c) num from Opportunity where OwnerId = :userId';
            pe1.Report8_2_Sql__c = 'Select Sum(Amount_Without_Tax__c) wt, Sum(Number_of_PCL__c) num from Opportunity where OwnerId = :userId';
            pe1.Report8_3_Sql__c = 'Select Sum(Amount_Without_Tax__c) wt, Sum(Number_of_PCL__c) num from Opportunity where OwnerId = :userId';
            pe1.Report6_1_Sql2__c = 'where GBIC__c=\'GI\' group by GBIC__c';
            pe1.Report6_2_Sql2__c = 'where GBIC__c=\'BF\' group by GBIC__c';
            pe1.Report6_3_Sql2__c = 'where GBIC__c=\'BF\' group by GBIC__c';
            pe1.Report7_3_Sql2__c = 'Order By Sales_forecast_day_BA__c';
            pe1.Tab_Index__c = 0;
            insert pe1;
            
            Personal_Evaluation__c pe2 = new Personal_Evaluation__c(Name = '99', Api_Name__c = '99', IsActive__c = true);
            pe2.Report1_1__c = 'Select Name, Report_Date__c From SMARM__c  limit 1000';
            pe2.Report1_2__c = 'Select Training_date__c, RecordType.Name from Training_test_Result__c  limit 1000';
            pe2.Report1_3__c = 'Select Received_call_day__c, LINK__c, prod__c from case where recordtypeId = \'01210000000QsYp\' limit 1000';
            pe2.Tab_Index__c = 99;
            insert pe2;
            SMARM__c s = new SMARM__c(
                OwnerId = '00510000000fSYI',
                Status__c = 'Delete',
                Review_Practitioner__c = UserInfo.getUserId(),
                Review_Target__c = u1.id,
                
                Image1__c = 'img1',
                ReportName1__c = 'report1',
                Comment1__c = 'comment1',
                
                Image3__c = 'img3',
                ReportName3__c = 'report3',
                Comment3__c = 'comment3',
                
                Image4__c = 'img4',
                ReportName4__c = 'report4',
                Comment4__c = 'comment4'
            );
            insert s;
        }
        
        System.runAs(u1) {
            PageReference page = new PageReference('/apex/PersonalEvaluation');
            System.Test.setCurrentPage(page);
            PersonalEvaluationController controller = new PersonalEvaluationController();
            //初始化
            controller.init();
            PersonalEvaluationController.upsertSmarm(u1.Id,'ReportName1__c','11111',true);
            //PersonalEvaluationController.saveComment(u1.Id, '01210000000RDuV', 'subject', 'comment', null, '', 'rptName', 'rptNum');
            //PersonalEvaluationController.saveComment(u1.Id, '01210000000RDua', 'subject', 'comment', null, 'tabName', '', '');
            //PersonalEvaluationController.getPeSettings('3.业绩管理', u1.Id);
            //PersonalEvaluationController.getJsonSAP('', u1.Id);
            //PersonalEvaluationController.getJsonSAPNameJ('', u1.Id,'151P');
            //PersonalEvaluationController.getJsonOppSum('', u1.Id);
            //PersonalEvaluationController.getJsonMBSA('', u1.Id);
            //PersonalEvaluationController.getJsonForcast('', u1.Id);
            //PersonalEvaluationController.getPersonalEvaluationSettingJson();
            //PersonalEvaluationController.getFirstTabIdx();
            //controller.getR1InfoList();
            //controller.getR2InfoList();
            //controller.getR3InfoList();
            //controller.getR1InfoColumnList();
            //controller.getR2InfoColumnList();
            //controller.getR3InfoColumnList();
        }
    }
}