/**
 * 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 organization are executed whenever Apex code is deployed
 * to a production organization 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 organization. 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 organization size limit for all Apex scripts.
 *
 * See the Apex Language Reference for more information about Testing and Code Coverage.
 */
@isTest
private class ProvinceEvaluationControllerTest {
    private static User u1{get;set;}
    
    private static void init() {
        Profile p = [select id from Profile where id =:System.Label.ProfileId_SystemAdmin];
        // まず user を作成
        u1 = new User();
        u1.LastName = '_サンブリッジ';
        u1.FirstName = 'あ';
        u1.Alias = 'あ';
        u1.Email = 'olympusTest01@sunbridge.com';
        u1.Username = 'olympusTest01@sunbridge.com';
        u1.CommunityNickname = 'あ';
        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 = '河北省;河南省';
        
        insert new List<User>{u1};
    }
    
    static testMethod void myUnitTest() {
        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 Province__c IN:Plist';
            pe1.Report2_2__c = 'select sum(Fiscal_Workdays__c) wt, sum(OPD_target_number_year__c) num from User where Province__c IN:Plist';
            pe1.Report2_3__c = 'select sum(Fiscal_Workdays__c) wt, sum(OPD_target_number_year__c) num from User where Province__c IN:Plist';
            pe1.Report3_1__c = 'select sum(Fiscal_Workdays__c) wt, sum(OPD_target_number_year__c) num from User where Province__c IN:Plist';
            pe1.Report3_2__c = 'select sum(Fiscal_Workdays__c) wt, sum(OPD_target_number_year__c) num from User where Province__c IN:Plist';
            pe1.Report3_3__c = 'select sum(Fiscal_Workdays__c) wt, sum(OPD_target_number_year__c) num from User where Province__c IN:Plist';
            pe1.Report4_1__c = 'select sum(Fiscal_Workdays__c) wt, sum(OPD_target_number_year__c) num from User where Province__c IN:Plist';
            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 Opportunity__r.State_Owner__c IN:Plist';
            pe1.Report5_1__c = 'Select Sales_forecast_day_BA__c, Sales_forecast_YM_BA__c, Id from Opportunity where State_Owner__c IN:Plist 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.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 Opportunity__r.State_Owner__c IN:Plist';
            pe1.Report7_3_Sql__c = 'Select Sales_forecast_day_BA__c, Sales_forecast_YM_BA__c, Id from Opportunity where State_Owner__c IN:Plist 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 State_Owner__c IN:Plist';
            pe1.Report8_2_Sql__c = 'Select Sum(Amount_Without_Tax__c) wt, Sum(Number_of_PCL__c) num from Opportunity where State_Owner__c IN:Plist';
            pe1.Report8_3_Sql__c = 'Select Sum(Amount_Without_Tax__c) wt, Sum(Number_of_PCL__c) num from Opportunity where State_Owner__c IN:Plist';
            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 = 50;
            pe1.IsProvince__c = true;
            insert pe1;
        }
        System.runAs(u1) {
            PageReference page = new PageReference('/apex/ProvinceEvaluation');
            System.Test.setCurrentPage(page);
            ProvinceEvaluationController controller = new ProvinceEvaluationController();
            
            controller.init();
            System.assertEquals(2, controller.provinceOpts.size());
            System.assertEquals(0, controller.provinceSelectedOpts.size());
            
            page = new PageReference('/apex/ProvinceEvaluation?tab=0&province=' + EncodingUtil.urlEncode('河北省', 'UTF-8'));
            System.Test.setCurrentPage(page);
            controller = new ProvinceEvaluationController();
            controller.init2();
            
            controller.getProvinceUrl();
            controller.getProvince();
            
            ProvinceEvaluationController.getPeSettings('3.业绩管理');
            ProvinceEvaluationController.getJsonSAP('', '河北省','151P');
            ProvinceEvaluationController.getJsonOppSum('', '河北省');
            ProvinceEvaluationController.getJsonMBSA('', '河北省');
            ProvinceEvaluationController.getJsonForcast('', '河北省');
            ProvinceEvaluationController.getPersonalEvaluationSettingJson();
            ProvinceEvaluationController.getFirstTabIdx();
            
            ProvinceEvaluationController.saveComment(u1.Id, '01210000000RDuV', '反馈', '测试', '', '', '报表反馈', '', '河北省;河南省');
            ProvinceEvaluationController.saveComment(u1.Id, '01210000000RDua', '反馈2', '测试2', '', 'Tab反馈', '', '', '河北省;河南省');
        }
    }
}