@isTest
public class SetProvinceTargetBatchTest {
    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 testMethod1() {
        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;
            
            Provincial_Goal_Setting_Input__c bbb = new Provincial_Goal_Setting_Input__c();
            bbb.key__c='黑龙江省_URO';
            bbb.Target_Source__c='SetProvinceTarget';
            bbb.SAP_Province__c='黑龙江省';
            bbb.iYear__c=2021;
            bbb.Owner_System__c=u2.Id;
            bbb.Department__c='2.东北';
            bbb.Amount__c=3000;
            bbb.Is_Processing__c = true;
            insert bbb;
            Database.executeBatch(new SetProvinceTargetBatch(), 1);

        }
    }
}