/**
 * 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 SetProvinceTargetControllerTest {
    
    static private String currentPeriod(Integer i) {
        Date dateNow = Date.today();
        Integer year = dateNow.year();
        Integer month = dateNow.month();
        if (month < 4) {
            year -= 1;
        }
        return String.valueOf(year + i - 1867 + 'P');
    }
    
    static private Date oppCloseDate() {
        Date nowDate = Date.today();
        Integer year = nowDate.year();
        Integer month = nowDate.month();
        if (month < 4) {
            nowDate = nowDate.addYears(-1);
        }
        return nowDate;
    }

    // 主流程
    static testMethod void myUnitTest() {
        
        RecordType[] rt = [select Id from RecordType where SobjectType = 'Opportunity' and IsActive = true and DeveloperName = 'Target'];
        
        //システム管理者
        User u3 = new User();
        u3 = new User();
        u3.LastName = '_サンブリッジ';
        u3.FirstName = 'う';
        u3.Alias = 'う';
        u3.Email = 'olympusTest03@sunbridge.com';
        u3.Username = 'olympusTest03@sunbridge.com';
        u3.CommunityNickname = 'う';
        u3.IsActive = true;
        u3.EmailEncodingKey = 'ISO-2022-JP';
        u3.TimeZoneSidKey = 'Asia/Tokyo';
        u3.LocaleSidKey = 'ja_JP';
        u3.LanguageLocaleKey = 'ja';
        u3.ProfileId = System.Label.ProfileId_SystemAdmin;
        u3.Job_Category__c = '销售推广';
        u3.Province__c = '上海市';
        u3.Use_Start_Date__c = Date.today().addMonths(-6);
        insert u3;
        
        System.runAs(u3) {
            
            User u1 = new User();
            u1.LastName = '_サンブリッジ';
            u1.FirstName = 'あ';
            u1.Alias = 'あ9';
            u1.Email = 'olympusTest01@sunbridge.com';
            u1.Username = 'olympusTest91@sunbridge.com';
            u1.CommunityNickname = 'あ9';
            u1.IsActive = true;
            u1.EmailEncodingKey = 'ISO-2022-JP';
            u1.TimeZoneSidKey = 'Asia/Tokyo';
            u1.LocaleSidKey = 'ja_JP';
            u1.LanguageLocaleKey = 'ja';
            u1.ProfileId = System.Label.ProfileId_SystemAdmin;
            u1.Job_Category__c = '销售推广';
            u1.Province__c = '四川省';
            u1.Use_Start_Date__c = Date.today().addMonths(-6);
            insert u1;
            
            User u2 = new User();
            u2 = new User();
            u2.LastName = '_サンブリッジ';
            u2.FirstName = 'い';
            u2.Alias = 'い';
            u2.Email = 'olympusTest02@sunbridge.com';
            u2.Username = 'olympusTest02@sunbridge.com';
            u2.CommunityNickname = 'い';
            u2.IsActive = true;
            u2.EmailEncodingKey = 'ISO-2022-JP';
            u2.TimeZoneSidKey = 'Asia/Tokyo';
            u2.LocaleSidKey = 'ja_JP';
            u2.LanguageLocaleKey = 'ja';
            u2.ProfileId = System.Label.ProfileId_SystemAdmin;
            u2.Job_Category__c = '销售推广';
            u2.Province__c = '北京市';
            u2.Use_Start_Date__c = Date.today().addMonths(-6);
            insert u2;
            
            SetProvinceTargetController sptc = new SetProvinceTargetController();
            
            // 初期表示
            sptc.init();
            System.assertEquals(false, sptc.saveFlg);
            if (Date.today().month() != 3) {
                System.assertEquals(false, sptc.isPast);
            } else {
                System.assertEquals(true, sptc.isPast);
            }
            System.assertEquals(34, sptc.dataBeans.size());
            //System.assertEquals(currentPeriod(0), sptc.currentPeriod);
            System.assertEquals(true, sptc.previousRendered);
            System.assertEquals(true, sptc.nextRendered);
            
            // 插入数据
           sptc.dataBeans[0].opportunity.Owner_System__c = u1.id;
            sptc.dataBeans[0].amount[0].Amount = 100;
            sptc.dataBeans[0].isChanged = '1';
            sptc.dataBeans[1].opportunity.Owner_System__c = u2.id;
            sptc.dataBeans[1].amount[0].Amount = 200;
            sptc.dataBeans[1].isChanged = '1';
            sptc.saveBtn();
            sptc.UpdateBtn();
            List<Opportunity> opps = [select Id,SAP_Province__c,Amount,Owner_System__c from Opportunity where RecordTypeId = :rt[0].Id order by ownerId];
            System.debug('opps======+++'+opps);
            // System.assertEquals(24, opps.size());
            // System.assertEquals('北京市', opps[0].SAP_Province__c);
            //System.assertEquals(u1.Id, opps[0].Owner_System__c);
            // System.assertEquals(100, opps[0].Amount);
            // System.assertEquals('天津市', opps[12].SAP_Province__c);
           //System.assertEquals(u2.Id, opps[12].Owner_System__c);
            // System.assertEquals(200, opps[12].Amount);
            
            // 上年度
            sptc.previous();
            //System.assertEquals(false, sptc.previousRendered);
            
            // 下年度
            sptc.next();
            //System.assertEquals(null, sptc.dataBeans[0].amount[0].Amount);
            //System.assertEquals(null, sptc.dataBeans[1].amount[0].Amount);
            
            // 上年度
            sptc.previous();
            //System.assertEquals(100, sptc.dataBeans[0].amount[0].Amount);
            //System.assertEquals(200, sptc.dataBeans[1].amount[0].Amount);
            
            // 更新，删除
           sptc.dataBeans[0].opportunity.Owner_System__c = u2.id;
            sptc.dataBeans[0].amount[0].Amount = 1000;
            sptc.dataBeans[0].isChanged = '1';
            sptc.dataBeans[1].amount[0].Amount = 100;
            sptc.dataBeans[1].isChanged = '1';
            Test.startTest();
            sptc.saveBtn();
            
            List<Opportunity> opps2 = [select Id,SAP_Province__c,OwnerId,Amount from Opportunity where RecordTypeId = :rt[0].Id order by ownerId];
            //System.assertEquals(24, opps2.size());
            //System.assertEquals('北京市', opps2[0].SAP_Province__c);
            //System.assertEquals(u2.id, opps2[0].OwnerId);
            //System.assertEquals(1000, opps2[0].Amount);
            
            // 删除担当者
            sptc.dataBeans[0].amount[0].Amount = 100;
            sptc.dataBeans[0].isChanged = '1';
            sptc.saveBtn();
            
            List<Opportunity> opps3 = [select Id,SAP_Province__c,Amount from Opportunity where RecordTypeId = :rt[0].Id order by ownerId];
            //System.assertEquals(24, opps3.size());
            Test.stopTest();
            // 返回
            sptc.backBtn();
        }
    }
    
    // 先保存的上年度，下年度
    static testMethod void myUnitTest2() {
        
        RecordType[] rt = [select Id from RecordType where SobjectType = 'Opportunity' and IsActive = true and DeveloperName = 'Target'];
        
        // システム管理者
        User u3 = new User();
        u3 = new User();
        u3.LastName = '_サンブリッジ';
        u3.FirstName = 'う';
        u3.Alias = 'う';
        u3.Email = 'olympusTest03@sunbridge.com';
        u3.Username = 'olympusTest03@sunbridge.com';
        u3.CommunityNickname = 'う';
        u3.IsActive = true;
        u3.EmailEncodingKey = 'ISO-2022-JP';
        u3.TimeZoneSidKey = 'Asia/Tokyo';
        u3.LocaleSidKey = 'ja_JP';
        u3.LanguageLocaleKey = 'ja';
        u3.ProfileId = System.Label.ProfileId_SystemAdmin;
        u3.Job_Category__c = '销售推广';
        u3.Province__c = '上海市';
        u3.Use_Start_Date__c = Date.today().addMonths(-6);
        insert u3;
        
        System.runAs(u3) {
            
            // 102_销售产品推广
            User u1 = new User();
            u1.LastName = '_サンブリッジ';
            u1.FirstName = 'あ';
            u1.Alias = 'あ9';
            u1.Email = 'olympusTest01@sunbridge.com';
            u1.Username = 'olympusTest91@sunbridge.com';
            u1.CommunityNickname = 'あ9';
            u1.IsActive = true;
            u1.EmailEncodingKey = 'ISO-2022-JP';
            u1.TimeZoneSidKey = 'Asia/Tokyo';
            u1.LocaleSidKey = 'ja_JP';
            u1.LanguageLocaleKey = 'ja';
            u1.ProfileId = System.Label.ProfileId_SystemAdmin;
            u1.Job_Category__c = '销售推广';
            u1.Province__c = '四川省';
            u1.Use_Start_Date__c = Date.today().addMonths(-6);
            insert u1;

            Opportunity[] opportunitys = [select Id from Opportunity where RecordTypeId = :rt[0].Id];
            delete opportunitys;
            
            Opportunity opp1 = new Opportunity();
            opp1.Name = u1.Alias + ' 目标';
            opp1.StageName = '目標';
            opp1.OwnerId = u1.Id;
            opp1.Owner_System__c = u1.Id;
            opp1.Opportunity_Category__c = 'GI';
            opp1.CloseDate = Date.valueOf(oppCloseDate().year() + '-04-01');
            opp1.Amount = 150;
            opp1.Target_category__c = '省目标';
            opp1.SAP_Province__c = '北京市';
            opp1.RecordTypeId = rt[0].Id;
            opp1.OCM_Target_period__c = currentPeriod(0);
            opp1.Target_Source__c = 'SetProvinceTarget';
            insert opp1;
            
            SetProvinceTargetController sptc = new SetProvinceTargetController();
            sptc.getSalesDptOpts();
            // 初期表示
            sptc.init();
            
            // System.assertEquals('北京市', sptc.dataBeans[0].opportunity.SAP_Province__c);
            //System.assertEquals(u1.Id, sptc.dataBeans[0].opportunity.Owner_System__c);
            // System.assertEquals(150, sptc.dataBeans[0].amount[0].Amount);
            
            // 保存,上年度
            sptc.dataBeans[0].amount[0].Amount = 100;
            sptc.dataBeans[0].isChanged = '1';
            sptc.saveFlg = true;
            sptc.previous();
            //System.assertEquals(100, sptc.dataBeans[0].amount[0].Amount);
            
            List<Opportunity> opps = [select Id,SAP_Province__c,Amount from Opportunity where RecordTypeId = :rt[0].Id order by ownerId];
            //System.assertEquals(12, opps.size());
            //System.assertEquals('北京市', opps[0].SAP_Province__c);
            //System.assertEquals(100, opps[0].Amount);
            
            // 保存,下年度
            sptc.dataBeans[0].amount[0].Amount = 160;
            sptc.dataBeans[0].isChanged = '1';
            sptc.saveFlg = true;
            sptc.next();
            //System.assertEquals(null, sptc.dataBeans[0].amount[0].Amount);
            
            List<Opportunity> opps2 = [select Id,SAP_Province__c,Amount from Opportunity where RecordTypeId = :rt[0].Id order by ownerId];
            //System.assertEquals(12, opps.size());
            //System.assertEquals('北京市', opps2[1].SAP_Province__c);
            //System.assertEquals(160, opps2[1].Amount);
        }
    }
    
    // 上年度，下年度
    static testMethod void myUnitTest3() {
        
        RecordType[] rt = [select Id from RecordType where SobjectType = 'Opportunity' and IsActive = true and DeveloperName = 'Target'];
        
        // システム管理者
        User u3 = new User();
        u3 = new User();
        u3.LastName = '_サンブリッジ';
        u3.FirstName = 'う';
        u3.Alias = 'う';
        u3.Email = 'olympusTest03@sunbridge.com';
        u3.Username = 'olympusTest03@sunbridge.com';
        u3.CommunityNickname = 'う';
        u3.IsActive = true;
        u3.EmailEncodingKey = 'ISO-2022-JP';
        u3.TimeZoneSidKey = 'Asia/Tokyo';
        u3.LocaleSidKey = 'ja_JP';
        u3.LanguageLocaleKey = 'ja';
        u3.ProfileId = System.Label.ProfileId_SystemAdmin;
        u3.Job_Category__c = '销售推广';
        u3.Province__c = '上海市';
        u3.Use_Start_Date__c = Date.today().addMonths(-6);
        insert u3;
        
        System.runAs(u3) {
            
            // 102_销售产品推广
            User u1 = new User();
            u1.LastName = '_サンブリッジ';
            u1.FirstName = 'あ';
            u1.Alias = 'あ9';
            u1.Email = 'olympusTest01@sunbridge.com';
            u1.Username = 'olympusTest91@sunbridge.com';
            u1.CommunityNickname = 'あ9';
            u1.IsActive = true;
            u1.EmailEncodingKey = 'ISO-2022-JP';
            u1.TimeZoneSidKey = 'Asia/Tokyo';
            u1.LocaleSidKey = 'ja_JP';
            u1.LanguageLocaleKey = 'ja';
            u1.ProfileId = System.Label.ProfileId_SystemAdmin;
            u1.Job_Category__c = '销售推广';
            u1.Province__c = '四川省';
            u1.Use_Start_Date__c = Date.today().addMonths(-6);
            insert u1;
            
            Opportunity[] opportunitys = [select Id from Opportunity where RecordTypeId = :rt[0].Id];
            delete opportunitys;
            
            Opportunity opp1 = new Opportunity();
            opp1.Name = u1.Alias + ' 目标';
            opp1.StageName = '目標';
            opp1.OwnerId = u1.Id;
            opp1.Owner_System__c = u1.Id;
            opp1.Opportunity_Category__c = 'GI';
            opp1.CloseDate = Date.valueOf(oppCloseDate().year() -1 + '-04-01');
            opp1.Amount = 150;
            opp1.Target_category__c = '省目标';
            opp1.SAP_Province__c = '北京市';
            opp1.RecordTypeId = rt[0].Id;
            opp1.OCM_Target_period__c = currentPeriod(-1);
            opp1.Target_Source__c = 'SetProvinceTarget';
            insert opp1;
            
            Opportunity opp2 = new Opportunity();
            opp2.Name = u1.Alias + ' 目标';
            opp2.StageName = '目標';
            opp2.OwnerId = u1.Id;
            opp2.Owner_System__c = u1.Id;
            opp2.Opportunity_Category__c = 'GI';
            opp2.CloseDate = Date.valueOf(oppCloseDate().year() -2 + '-04-01');
            opp2.Amount = 250;
            opp2.Target_category__c = '省目标';
            opp2.SAP_Province__c = '北京市';
            opp2.RecordTypeId = rt[0].Id;
            opp2.OCM_Target_period__c = currentPeriod(-2);
            opp2.Target_Source__c = 'SetProvinceTarget';
            insert opp2;
            
            SetProvinceTargetController sptc = new SetProvinceTargetController();
            
            // 初期表示
            sptc.init();
            
            //System.assertEquals(currentPeriod(0), sptc.currentPeriod);
            //System.assertEquals(true, sptc.previousRendered);
            //System.assertEquals(true, sptc.nextRendered);
            if (Date.today().month() != 3) {
                System.assertEquals(false, sptc.isPast);
            } else {
                System.assertEquals(true, sptc.isPast);
            }
            System.assertEquals(null, sptc.dataBeans[0].amount[0].Amount);

            // 上年度
            sptc.previous();
            //System.assertEquals(currentPeriod(-1), sptc.currentPeriod);
            //System.assertEquals(true, sptc.previousRendered);
            //System.assertEquals(true, sptc.nextRendered);
            //System.assertEquals(true, sptc.isPast);
            //System.assertEquals(150, sptc.dataBeans[0].amount[0].Amount);
            // 上年度
            sptc.previous();
            //System.assertEquals(currentPeriod(-2), sptc.currentPeriod);
            //System.assertEquals(true, sptc.previousRendered);
            //System.assertEquals(true, sptc.nextRendered);
            //System.assertEquals(true, sptc.isPast);
            //System.assertEquals(250, sptc.dataBeans[0].amount[0].Amount);
            // 上年度
            sptc.previous();
            //System.assertEquals(currentPeriod(-2), sptc.currentPeriod);
            //System.assertEquals(false, sptc.previousRendered);
            //System.assertEquals(true, sptc.nextRendered);
            //System.assertEquals(true, sptc.isPast);
            //System.assertEquals(250, sptc.dataBeans[0].amount[0].Amount);
            // 下年度
            sptc.next();
            //System.assertEquals(currentPeriod(-1), sptc.currentPeriod);
            //System.assertEquals(true, sptc.previousRendered);
            //System.assertEquals(true, sptc.nextRendered);
            //System.assertEquals(true, sptc.isPast);
            //System.assertEquals(150, sptc.dataBeans[0].amount[0].Amount);
            // 下年度
            sptc.next();
            //System.assertEquals(currentPeriod(0), sptc.currentPeriod);
            //System.assertEquals(true, sptc.previousRendered);
            //System.assertEquals(true, sptc.nextRendered);
            //if (Date.today().month() != 3) {
            //    System.assertEquals(false, sptc.isPast);
            //} else {
            //    System.assertEquals(true, sptc.isPast);
            //}
            //System.assertEquals(null, sptc.dataBeans[0].amount[0].Amount);
            //// 下年度
            sptc.next();
            //System.assertEquals(currentPeriod(1), sptc.currentPeriod);
            //System.assertEquals(true, sptc.previousRendered);
            //System.assertEquals(false, sptc.nextRendered);
            //System.assertEquals(false, sptc.isPast);
            //System.assertEquals(null, sptc.dataBeans[0].amount[0].Amount);
        }
    }
    static testMethod void myUnitTest4() {
        
        RecordType[] rt = [select Id from RecordType where SobjectType = 'Opportunity' and IsActive = true and DeveloperName = 'Target'];
        
        // システム管理者
        User u3 = new User();
        u3 = new User();
        u3.LastName = '_サンブリッジ';
        u3.FirstName = 'う';
        u3.Alias = 'う';
        u3.Email = 'olympusTest03@sunbridge.com';
        u3.Username = 'olympusTest03@sunbridge.com';
        u3.CommunityNickname = 'う';
        u3.IsActive = true;
        u3.EmailEncodingKey = 'ISO-2022-JP';
        u3.TimeZoneSidKey = 'Asia/Tokyo';
        u3.LocaleSidKey = 'ja_JP';
        u3.LanguageLocaleKey = 'ja';
        u3.ProfileId = System.Label.ProfileId_SystemAdmin;
        u3.Job_Category__c = '销售推广';
        u3.Province__c = '上海市';
        u3.Use_Start_Date__c = Date.today().addMonths(-6);
        insert u3;
        
        System.runAs(u3) {
            
            // 102_销售产品推广
            User u2 = new User();
            u2.LastName = '四洋';
            u2.FirstName = '张';
            u2.Alias = '张四洋';
            u2.Email = 'olympusTest02@sunbridge.com';
            u2.Username = 'olympusTest02@sunbridge.com';
            u2.CommunityNickname = '张四洋';
            u2.IsActive = true;
            u2.EmailEncodingKey = 'ISO-2022-JP';
            u2.TimeZoneSidKey = 'Asia/Tokyo';
            u2.LocaleSidKey = 'ja_JP';
            u2.LanguageLocaleKey = 'ja';
            u2.ProfileId = System.Label.ProfileId_SystemAdmin;
            u2.Job_Category__c = '销售推广';
            u2.Province__c = '上海市';
            u2.Post__c = '主管';
            u2.Sales_Speciality__c = '医院担当';
            u2.Use_Start_Date__c = Date.today().addYears(-1);
            insert u2;
            
            Opportunity[] opportunitys = [select Id from Opportunity where RecordTypeId = :rt[0].Id];
            delete opportunitys;
            
            Opportunity opp1 = new Opportunity();
            opp1.Name = u2.Alias + ' 目标';
            opp1.StageName = '目標';
            opp1.OwnerId = u2.Id;
            opp1.Owner_System__c = u2.Id;
            opp1.Opportunity_Category__c = 'GI';
            opp1.CloseDate = Date.valueOf(oppCloseDate().year() -1 + '-04-01');
            opp1.Amount = 150;
            opp1.Target_category__c = '省目标';
            opp1.SAP_Province__c = '北京市';
            opp1.RecordTypeId = rt[0].Id;
            opp1.OCM_Target_period__c = currentPeriod(-1);
            opp1.Target_Source__c = 'SetProvinceTarget';
            insert opp1;
            
            Opportunity opp2 = new Opportunity();
            opp2.Name = u2.Alias + ' 目标';
            opp2.StageName = '目標';
            opp2.OwnerId = u2.Id;
            opp2.Owner_System__c = u2.Id;
            opp2.Opportunity_Category__c = 'GI';
            opp2.CloseDate = Date.valueOf(oppCloseDate().year() -2 + '-04-01');
            opp2.Amount = 250;
            opp2.Target_category__c = '省目标';
            opp2.SAP_Province__c = '上海市';
            opp2.RecordTypeId = rt[0].Id;
            opp2.OCM_Target_period__c = currentPeriod(-2);
            opp2.Target_Source__c = 'SetProvinceTarget';
            insert opp2;
            OCM_Management_Province__c aaa = new OCM_Management_Province__c();
            aaa.Province__c='上海市';
            aaa.GI_assistant__c=u2.Id;
            aaa.SP_assistant__c=u2.Id;
            aaa.Name='上海';
            insert aaa;
            SetProvinceTargetController sptc = new SetProvinceTargetController();
            
            // 初期表示
            sptc.init();
            
            if (Date.today().month() != 3) {
                System.assertEquals(false, sptc.isPast);
            } else {
                System.assertEquals(true, sptc.isPast);
            }
            System.assertEquals(null, sptc.dataBeans[0].amount[0].Amount);

            // 上年度
            sptc.previous();
            // 上年度
            sptc.previous();           
            // 上年度
            sptc.previous();            
            // 下年度
            sptc.next();            
            // 下年度
            sptc.next();            
            // 下年度
            sptc.next();
            //查询
            sptc.searchByDpt();
             //导出
            sptc.exportBycsv();
            sptc.csvAsString = '销售本部,省,GDP,GI,ET,BF,GS,URO,GYN,ENT,ENG\n5.华东,上海市,100,2000,2345,123,1200,400,500,40,55';
            //test read csv file
            sptc.importCSVFile();

        }
    }
}