@isTest
private class ConsumableTargetManageBatchTest2 {

    public static Account account1 = new Account();
    public static Account account2 = new Account();
    public static Contact contact1 = new Contact();
    public static Contact contact2 = new Contact();
    public static User user = new User();
    public static Agency_Hospital_Link__c agency_hospital_link = new Agency_Hospital_Link__c();
    public static Agency_Contact__c agency_contact = new Agency_Contact__c();
    public static ProductTypes__c product_types = new ProductTypes__c();
    // 经销商活动周报导入 update by vivek start
    public static ProductTypes__c product_types1 = new ProductTypes__c();
    public static ProductTypes__c product_types2 = new ProductTypes__c();
    public static ProductTypes__c product_types3 = new ProductTypes__c();
    // 经销商活动周报导入 update by vivek end
    public static Agency_Opportunity__c agency_opportunity = new Agency_Opportunity__c();
    public static OlympusCalendar__c olympus_calendar = new OlympusCalendar__c();
    
    public static String report_id;
       public static String report_header_id;
    
    @testSetup
    static void setUp(){
        TestDataUtility.CreatePIPolicyConfigurations( new string[]{'Agency_Contact__c'});
        TestDataUtility.CreatePIPolicyConfigurations( new string[]{'Contact'});
    }
    // @isTest
    // static void TestgetAwsurl(){
    //     WeeklyReportCmp.getAwsurl('Agency_Contact__c');
    // }
    
    /* 病院。コンタクト、ユーザー、カレンダーなどの初期データ作る
    */
    private static void testInit() {
        // 取引先
        RecordType id1=[SELECT  id from RecordType  where name='病院' limit 1];
        account1.Name = 'test1医院';
        account1.RecordTypeId = id1.id;
        account1.ET_owner__c=UserInfo.getUserId();
        insert account1;
        
        RecordType id2=[SELECT  id from RecordType  where name='販売店' limit 1];
        account2.Name = 'test1经销商';
        account2.RecordTypeId = id2.id;
        insert account2;
        
        // 取引先責任者
        contact1.AccountId = account1.Id;
        //contact1.FirstName = '責任者';
        contact1.LastName = 'test1医院';
        insert contact1;
        
        contact2.AccountId = account2.Id;
        //contact2.FirstName = '責任者';
        contact2.LastName = 'test1经销商';
        insert contact2;

        Profile p = [select Id from Profile where Name = '901_经销商活动系统'];
        //Profile p = [select Id from Profile where id =:System.Label.ProfileId_SystemAdmin];
        user.ProfileId = p.Id;
        user.ContactId = contact2.Id;
        //user.FirstName = 'ユーザー';
        user.LastName = 'テスト';
        user.Email = 'test_user@example.com';
        user.emailencodingkey='UTF-8';
        user.languagelocalekey='zh_CN';
        user.localesidkey='ja_JP';
        user.timezonesidkey='Asia/Shanghai';
        user.Username = 'test_user2@example.com';
        user.Alias = 'テユ';
        user.CommunityNickname = 'テストユーザー';
        insert user;
        
        // 代理店医院
        agency_hospital_link.Name = 'test1代理店医院';
        agency_hospital_link.Hospital__c = account1.Id;
        agency_hospital_link.Agency__c = account2.Id;
        agency_hospital_link.OwnerId = user.Id;
        agency_hospital_link.Agency_Campaign_Obj__c = true;
        insert agency_hospital_link;
        
        // 先生
        agency_contact.Name = 'test1医院先生';
        agency_contact.Doctor_Division1__c = '院长';
        agency_contact.Type__c = '医生';
        agency_contact.Agency_Hospital__c = agency_hospital_link.Id;
        agency_contact.OwnerId = user.Id;
        // 经销商活动周报导入 update by vivek start
        // agency_contact.Hospital_ID18__c = agency_hospital_link.Id;
        // 经销商活动周报导入 update by vivek end
        insert agency_contact;
        
        // 製品区分
        product_types.Name = '製品区分1';
        product_types.OwnerId = user.Id;
        product_types.Department_Cateogy__c = 'GI;BF;ET;GYN;OTH;URO;GS;ENT';
        insert product_types;
        // 经销商活动周报导入 update by vivek end

        // 引合
        agency_opportunity.RecordTypeId = [select Id,DeveloperName from RecordType where IsActive = true and SobjectType = 'Agency_Opportunity__c' and DeveloperName = 'Opportunity'].Id;
        agency_opportunity.Name = '引合1';
        agency_opportunity.Hospital_Target__c = account1.Id;
        agency_opportunity.Agency__c = account2.Id;
        agency_opportunity.Agency_Hospital__c = agency_hospital_link.Id;
        agency_opportunity.StageName__c = '还没申请预算';
        agency_opportunity.OwnerId = user.Id;
        agency_opportunity.NewOpportunity_Agency_Apply_Status__c = '批准';
        insert agency_opportunity;
        
        // オリンパスカレンダー
        olympus_calendar.Date__c = Date.valueOf('2023-05-01');
        olympus_calendar.OwnerId = user.Id;
        insert olympus_calendar;
        // ProductTypes__c producttyoe = WeeklyReportCmp.getProduct(product_types.Id);
    }

    // @isTest public static void getProductListTest() {
    //     //testInit();
        
    //     User thisUser = [SELECT Id FROM User WHERE Id = :UserInfo.getUserId()];
    //     system.runAs(thisUser){
    //         testInit();
    //         // 病院リスト取得
    //         List<Map<String,String>> test = WeeklyReportCmp.getProductList('%GI%', '');
    //         System.assertNotEquals(test.size(), 1);
    //     }
    // }
    
    // @isTest public static void getalldataTest() {
    //     //testInit();
        
    //     User thisUser = [SELECT Id FROM User WHERE Id = :UserInfo.getUserId()];
    //     system.runAs(thisUser){
    //         testInit();
    //         // 病院リスト取得
    //         WeeklyReportCmp test = WeeklyReportCmp.getalldata();
    //         System.assertNotEquals(test, null);
    //     }
    // }
    
    // @isTest
    // public static void createReportHeaderTest() {
    //     //testInit();
        
    //     User thisUser = [SELECT Id FROM User WHERE Id = :UserInfo.getUserId()];
    //     system.runAs(thisUser){
    //         testInit();
    //         // 週報ヘッダー作成
    //         String name = 'テスト ユーザー';
    //         String s_date = '2017-04-10';
    //         String s_agency = contact2.Id;
    //         String head_key = contact2.Id + ':20170410';
    //         report_header_id = WeeklyReportCmp.createReportHeader(name, s_date, s_agency, head_key);
            
    //         Agency_Report_Header__c data = [select Id, Name, Week__c, Agency_Person2__c, HeaderInputKey__c, OlympusDate__r.Date__c from Agency_Report_Header__c where HeaderInputKey__c = :head_key];
    //         System.assertEquals(name + ' (' + s_date + ')', data.Name);
    //         System.assertEquals(Date.valueOf(s_date), data.Week__c);
    //         System.assertEquals(s_agency, data.Agency_Person2__c);
    //         System.assertEquals(head_key, data.HeaderInputKey__c);
    //         System.assertEquals(Date.valueOf(s_date), data.OlympusDate__r.Date__c);
    //     }
    // }
    
    
    @isTest static void saveAgencyReportTest() {
        //testInit();
        
        system.runAs(new User(Id = UserInfo.getUserId())){
            Oly_TriggerHandler.bypass(AgencyReportHandler.Class.getName());
            Oly_TriggerHandler.bypass(ConsumableTargetManageHandler.Class.getName());
            Oly_TriggerHandler.bypass(AgencyOpportunityHandler.Class.getName());

            testInit();
            String name = 'テスト ユーザー';
            String s_date = '2023-05-01';
            String s_agency = contact2.Id;
            String head_key = contact2.Id + ':20170410';
            report_header_id = WeeklyReportCmp.createReportHeader(name, s_date, s_agency, head_key);
            
            // 週報作成
            String Department_Cateogy = '';
            String Purpose_Type = '';
            String Agency_Report_Header = '';
            String Agency_Hospital = '';
            String Person_In_Charge2 = '';
            String doctor = '';
            String Submit_date = '';
            String Product_Category = '';
            String Result = '';
            String Opportunity = '';
            //fy 20220505 
            String SupportNeedsc ='';
            
            Department_Cateogy = 'GI';
            Purpose_Type = '询价挖掘-会议对应';
            Agency_Report_Header = report_header_id;
            Agency_Hospital = agency_hospital_link.Id;
            Person_In_Charge2 = contact2.Id;
            doctor = agency_contact.Id;
            Submit_date = '2023-05-01';
            Product_Category = product_types.Id;
            Result = '一般';
            Opportunity = agency_opportunity.Id;
            //fy 20220505 
            SupportNeedsc = '维修备品';                                                    //fy 20220505 SupportNeedsc
            report_id = WeeklyReportCmp.saveAgencyReport(Department_Cateogy, Purpose_Type,SupportNeedsc, Agency_Report_Header, Agency_Hospital, Person_In_Charge2, doctor, Submit_date, Product_Category,'','', Result, Opportunity,'还没申请预算','15','16','2017-04-10',Submit_date);
            
            Agency_Report__c data = [select Id, Department_Cateogy__c, Purpose_Type__c, Agency_Report_Header__c, Agency_Hospital__c,Agency_Hospital__r.Hospital__c,
                                     Person_In_Charge2__c, doctor2__c, Submit_date__c, Product_Category__c, Result__c, Opportunity__c from Agency_Report__c where Id=:report_id];
            data.ProductClassification__c='ESD';
            data.WarlockClassification__c='高频治疗钳';
            data.WorkRecord__c='临床信息(使用量)收集';
            data.Report_Date__c=Date.ValueOf('2023-05-01');
            data.Submit_date__c = Date.ValueOf('2023-05-01');
            update data;

            ConsumableTargetManage__c ctm=new ConsumableTargetManage__c();
            ctm.OCM_Year__c = 'FY2024';
            ctm.productType__c='ET耗材';
            ctm.Hospital__c = data.Agency_Hospital__r.Hospital__c;
            ctm.Category3__c = 'ESD';
            ctm.Category4__c = '高频治疗钳';
            ctm.ConsumableTargetManageKey__c= data.Agency_Hospital__r.Hospital__c+'-ESD-高频治疗钳-FY2024';
            insert ctm;  

            Database.executeBatch(new ConsumableTargetManageBatch4(Date.ValueOf('2023-05-01'),Date.ValueOf('2024-03-31')),1);
     // WarlockClassification__c!=null AND WorkRecord__c != null AND Agency_Hospital__c!=null AND Agency_Hospital__r.Hospital__c!=null AND ProductClassification__c!=null


        }
    }
    static testMethod void testMethod8() {
        
        system.runAs(new User(Id = UserInfo.getUserId())){
            Oly_TriggerHandler.bypass(AgencyReportHandler.Class.getName());
            Oly_TriggerHandler.bypass(ConsumableTargetManageHandler.Class.getName());
            Oly_TriggerHandler.bypass(AgencyOpportunityHandler.Class.getName());

                   RecordType id2=[SELECT  id from RecordType  where name='販売店' limit 1];
        account2.Name = 'test1经销商';
        account2.RecordTypeId = id2.id;
        insert account2;
        List<RecordType> rectCo = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '病院'];
        if (rectCo.size() == 0) {
            return;
        }
        List<RecordType> rectSct = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '戦略科室分類 呼吸科'];
        if (rectSct.size() == 0) {
            return;
        }
        List<RecordType> rectDpt = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '診療科 消化科'];
        if (rectDpt.size() == 0) {
            return;
        } 
        Profile p = [select Id from Profile where id =:System.Label.ProfileId_SystemAdmin];

        Account hospital = new Account();
        hospital.RecordTypeId = rectCo[0].Id;
        hospital.Name         = 'NFM007Testhospital';
        hospital.ET_owner__c=UserInfo.getUserId();
        insert hospital;

        // 戦略科室を得る
        List<Account> strategicDep = [SELECT ID, Name FROM Account WHERE parentId = :hospital.Id AND recordType.DeveloperName = 'Department_Class_GI'];
        Account section = new Account();
        section.RecordTypeId = rectSct[0].Id;
        section.Name         = '*';
        section.Department_Class_Label__c = '消化科';
        section.ParentId                  = hospital.Id;
        section.Hospital_Department_Class__c = hospital.Id;
        section.AgentCode_Ext__c = '9999900';
        insert section;



        Product2 prd1 = new Product2();
        prd1.ProductCode_Ext__c     = 'Prd1';
        prd1.ProductCode            = 'Prd1';
        prd1.Repair_Product_Code__c = 'Prd1_RP';
        prd1.Name                   = 'Prd1';
        prd1.Manual_Entry__c        = false;
        // prd1.Category2__c = '本体';
        prd1.Category3__c = '呼吸科ET';
        prd1.Category4__c = '活检套装';
        // prd1.Category5__c = '260SL系列';
        prd1.Asset_Model_No__c = '1001';
        prd1.Is_DangerousChemicals__c=false;
        prd1.Family= 'ET';
        prd1.Category2__c = '耗材';
        prd1.Dealer_special_Object__c=false;
        prd1.Intra_Trade_List_RMB_Date1__c  = Date.ValueOf('2023-12-01');
        prd1.Intra_Trade_List_RMB_Date2__c  = Date.ValueOf('2023-12-01');
        prd1.Intra_Trade_List_RMB_End_Date1__c  = Date.ValueOf('2023-12-01');
        prd1.Intra_Trade_List_RMB_End_Date2__c  = Date.ValueOf('2023-12-01');

        prd1.Intra_Trade_List_RMB_1__c  = 2000;
        prd1.Intra_Trade_List_RMB_2__c  = 2060;
        prd1.Important_product__c = true;
        prd1.Important_Rroduct_1GI__c = true;
        insert prd1;

     


        Product2__c pro1 = new Product2__c(Name='Pro001',OT_CODE_Text__c='Test001',Product2__c = prd1.Id);
        // Product2__c pro2 = new Product2__c(Name='Pro002',OT_CODE_Text__c='Test002',Product2__c = prd2.Id);
        // Product2__c pro3 = new Product2__c(Name='Pro003',OT_CODE_Text__c='Test003',Product2__c = prd3.Id);
        pro1.Product2__c=prd1.Id;
        insert new Product2__c[] {pro1};

        
        Consumable_order__c Order1 = new Consumable_order__c();
        Order1.Name = 'OCM_01_001';
        Order1.Order_status__c = '批准';
        Order1.Order_type__c = '订单';
        Order1.Dealer_Info__c = account2.id;
        Order1.RecordTypeid = System.Label.RT_ConOrder_Delivery;
        Order1.Order_ProType__c = 'ET';
        Order1.SummonsForDirction__c = '直接销售给医院';
        Order1.Order_ForHospital__c = hospital.Id;
        insert Order1;

        Consumable_order_details2__c saledet1 = new Consumable_order_details2__c();
        saledet1.Name = 'OCM_01_001001';
        saledet1.Consumable_order_minor__c = Order1.Id;
        saledet1.RecordTypeId = System.Label.RT_ConOrderDetail2_Delivery;
        saledet1.Bar_Code__c = '0114953170032070111804001082K250BBBBB';
        saledet1.Consumable_Product__c = pro1.Id;
        saledet1.Used_account__c = section.Id;
        saledet1.Delivery_List_RMB__c = 11;
        saledet1.Box_Piece__c = '盒';
        saledet1.TracingCode__c = 'BBBBB';
        saledet1.SerialLotNo__c = '82K';
        saledet1.Guarantee_period_for_products__c = Date.ValueOf('2023-12-01');
        saledet1.Sterilization_limit__c = Date.ValueOf('2023-12-01');
        saledet1.Send_Date__c = null;
        //saledet1.Dealer_Shipment__c = false;
        saledet1.Arrive_date__c = null;
        //saledet1.Dealer_Arrive__c = false;
        //saledet1.Dealer_Saled__c = false;
        saledet1.Deliver_date__c = Date.ValueOf('2023-12-01');
        saledet1.Used_date__c =Date.ValueOf('2023-10-01');
        saledet1.Return_date__c=null;
        saledet1.Consumable_Sale_order__c = Order1.Id;
        saledet1.Send_Date__c = Date.ValueOf('2023-12-01');
        saledet1.Lose_Flag__c=false;
        insert saledet1;
        Database.executeBatch(new DealerConsumableManageBatch3(Date.ValueOf('2023-05-01'),Date.ValueOf('2024-03-31'),false),1);
		saledet1=[SELECT OCM_man_province__c from Consumable_order_details2__c limit1];
            Dealer_ConsumableManage__c ctm=new Dealer_ConsumableManage__c();
            ctm.Dealer_Name__c = account2.id;

                ctm.ET_ENG__c='ENG';
            ctm.FY__c = 'FY2024';
            ctm.TMS_Key__c = ctm.Dealer_Name__c+'-'+ctm.ET_ENG__c+'-'+ctm.FY__c+'-'+saledet1.OCM_man_province__c;
            insert ctm;
        Database.executeBatch(new DealerConsumableManageBatch3(Date.ValueOf('2023-05-01'),Date.ValueOf('2024-03-31'),false),1);
            
        }
    }
}