@isTest
private class ConsumTrialPDFControllerTest {
    @testSetup
    static void setupTestData() {
        // 用户
        User testUser1 = new User(Test_staff__c = true);
        testUser1.LastName = 'TestLastName';
        testUser1.FirstName = 'TestFirstName';
        testUser1.Alias = 'Alias';
        testUser1.Email = 'testuser1@test.com';
        testUser1.Username = 'testuser1@test.com.oly';
        testUser1.CommunityNickname = 'testUser1';
        testUser1.IsActive = true;
        testUser1.EmailEncodingKey = 'ISO-2022-JP';
        testUser1.TimeZoneSidKey = 'Asia/Tokyo';
        testUser1.LocaleSidKey = 'ja_JP';
        testUser1.LanguageLocaleKey = 'ja';
        testUser1.ProfileId = System.Label.ProfileId_SystemAdmin;
        // testUser1.Job_Category__c = '销售推广';
        // testUser1.Province__c = '上海市';
        // testUser1.Dept__c = '医疗华北营业本部';
        // testUser1.Work_Location__c = '北京';
        // testUser1.Use_Start_Date__c = Date.today().addMonths(-6);
        insert testUser1;
        // 省
        Address_Level__c al = new Address_Level__c();
        al.Name = '東京';
        al.Level1_Code__c = 'CN-99';
        al.Level1_Sys_No__c = '999999';
        insert al;
        // 市
        Address_Level2__c al2 = new Address_Level2__c();
        al2.Level1_Code__c = 'CN-99';
        al2.Level1_Sys_No__c = '999999';
        al2.Level1_Name__c = '東京';
        al2.Name = '渋谷区';
        al2.Level2_Code__c = 'CN-9999';
        al2.Level2_Sys_No__c = '9999999';
        al2.Address_Level__c = al.Id;
        insert al2;
        
        // 病院
        Account testHosp = new Account();
        testHosp.recordtypeId = [SELECT Id FROM RecordType WHERE IsActive = true AND SobjectType = 'Account' AND DeveloperName = 'HP'].Id;
        testHosp.Name = 'TestHospName';
        testHosp.Is_Active__c = '有効';
        testHosp.Attribute_Type__c = '卫生部';
        testHosp.Speciality_Type__c = '综合医院';
        testHosp.Grade__c = '一级';
        testHosp.OCM_Category__c = 'SLTV';
        testHosp.Is_Medical__c = '医疗机构';
        testHosp.State_Master__c = al.Id;  // 省
        testHosp.City_Master__c = al2.Id;  // 市
        testHosp.Town__c = '东京';
        insert testHosp;
        // 戦略科室
        Account[] strategicDep = [SELECT Id, Name FROM Account WHERE parentId = :testHosp.Id AND recordType.DeveloperName = 'Department_Class_OTH'];
        // 診療科
        Account dep = new Account();
        dep.recordtypeId = [SELECT Id FROM RecordType WHERE IsActive = true AND SobjectType = 'Account' AND DeveloperName = 'Department_OTH'].Id;
        dep.Name = 'TestDep';
        dep.AgentCode_Ext__c = '9999998';
        dep.ParentId = strategicDep[0].Id;
        dep.Department_Class__c = strategicDep[0].Id;
        dep.Hospital__c = testHosp.Id;
        insert dep;
        // 经销商
        Contact testContact = new Contact();
        testContact.AccountId = dep.Id;
        testContact.FirstName = '責任者';
        testContact.LastName = 'test1经销商';
        insert testContact;
        // 地址
        Shipment_address__c testAddr = new Shipment_address__c();
        testAddr.Name = 'Test办事处地址';
        testAddr.Address__c = '北京市朝阳区酒仙桥';
        testAddr.Post_Code__c = '123321';
        insert testAddr;

        // 产品
        Product2 testProp = new Product2(
                Name = 'TestPropName',
                IsActive=true,Family = 'SP',
                Fixture_Model_No__c = 'No02',
                Serial_Lot_No__c = 'S/N tracing',
                Fixture_Model_No_T__c = 'ModNo02',
                ProductCode_Ext__c = 'PC02',
                Manual_Entry__c = false);
        insert testProp;

        // 保有设备(只有附属品 个体管理)
        Asset testAsset = new Asset(Asset_Owner__c = 'Olympus');
        /*testAsset.RecordTypeId = System.Label.Asset_RecordType;
        testAsset.SerialNumber = 'testAsset';
        testAsset.Name = 'testAsset';
        testAsset.AccountId = dep.Id;
        testAsset.Department_Class__c = strategicDep[0].Id;
        testAsset.Hospital__c = testHosp.Id;
        testAsset.Product2Id = testProp.Id;
        testAsset.Quantity = 1;
        testAsset.Status = '有库存';
        testAsset.Manage_type__c = '个体管理';
        testAsset.Internal_asset_location__c = '北京 备品中心';
        testAsset.Consumable_Guaranteen_end__c = Date.today().addMonths(1);
        testAsset.Loaner_accsessary__c = true;
        testAsset.Delete_Flag__c = false;
        testAsset.Freeze_sign__c = false;
        testAsset.Out_of_wh__c = 0;*/
        
        testAsset.RecordTypeId = System.Label.Asset_RecordType;
        testAsset.SerialNumber = 'testAsset';
        testAsset.Name = 'testAsset';
        testAsset.AccountId = dep.Id;
        testAsset.Department_Class__c = strategicDep[0].Id;
        testAsset.Hospital__c = testHosp.Id;
        testAsset.Product2Id = testProp.Id;
        testAsset.Quantity = 100;
        testAsset.Manage_type__c = '数量管理';
        testAsset.Loaner_accsessary__c = true;
        testAsset.Out_of_wh__c = 0;
        testAsset.Salesdepartment__c = '0.备品中心';
        testAsset.Product_category__c = 'GI';

        testAsset.Asset_loaner_category__c = '耗材';
        testAsset.Delete_Flag__c = false;
        testAsset.AssetManageConfirm__c = true;
        testAsset.Equipment_Type__c = '产品试用';
        testAsset.Fixture_OneToOne_Link__c = null;
        testAsset.Internal_asset_location__c = '上海 备品中心';
        testAsset.RecordTypeId = '01210000000kOPR';
        testAsset.Consumable_Guaranteen_end__c = System.today().addYears(1);
        insert testAsset;








        OlympusCalendar__c oc = new OlympusCalendar__c();
        oc.Date__c = System.Today();
        insert oc;

        // 耗材备品申请
        Consum_Apply__c testConsumApply = new Consum_Apply__c();
        testConsumApply.Name = 'TestConsumApply';  // 耗材备品申请单号
        testConsumApply.Product_category__c = 'ET';  // 产品分类
        //testConsumApply.Demo_purpose1__c = '产品试用';
        testConsumApply.demo_purpose2__c = FixtureUtil.raDemo_purpose2MAP.get('shiyongwuxunjia');  // 出借目的
        testConsumApply.Person_In_Charge__c = testUser1.Id;  // 备品出借担当
        testConsumApply.direct_send__c = '医疗机构';  // 发送方
        testConsumApply.Shipment_address__c = testAddr.Id;  // 办事处地址
        testConsumApply.Loaner_received_staff__c = '测试姓名';  // 收件者姓名 (销售或FSE)
        testConsumApply.Loaner_received_staff_phone__c = '23111111';  // 收件者电话(销售或FSE)
        testConsumApply.direct_shippment_address__c = '北京市';
        testConsumApply.Hospital__c = testHosp.Id;
        testConsumApply.Strategic_dept__c = strategicDep[0].Id;
        testConsumApply.Account__c = dep.Id;
        testConsumApply.Request_shipping_day__c = Date.toDay();  // 希望到货日
        //testConsumApply.Hope_Lonaer_date_Num__c = 1;  // 希望借用天数
        testConsumApply.Phone_number__c = '1234567890';
        testConsumApply.Loaner_medical_Staff__c = testContact.Id;
        insert testConsumApply;

        // 备品配套(只有附属品)
        //Fixture_Set__c testFixSet = new Fixture_Set__c();
        //testFixSet.Name = 'TestFixSet';
        //testFixSet.Fixture_Set_Body_Model_No__c = 'TestModelNo';
        //testFixSet.Loaner_name__c = 'TestLoanerName';
        //insert testFixSet;

        // 耗材备品配套一览
        List<Consum_Apply_Equipment_Set__c> consumEquipSetList = new List<Consum_Apply_Equipment_Set__c>();
        Consum_Apply_Equipment_Set__c consumEquipSet1 = new Consum_Apply_Equipment_Set__c();
        consumEquipSet1.Name = 'TestConsumEquipSet1';
        consumEquipSet1.Consum_Apply__c = testConsumApply.Id;  // 备品耗材申请No
        //consumEquipSet1.Loaner_code_text__c = 'No12345';  // 备品配套型号(耗材时)
        //consumEquipSet1.Fixture_Set__c = testFixSet.Id;
        consumEquipSet1.Cancel_Select__c = false;
        //consumEquipSet1.Shipment_request_time__c = Date.toDay();
        consumEquipSet1.Consum_Start_Date__c = Date.toDay();
        //consumEquipSet1.Consum_End_Date__c = Date.toDay();
        consumEquipSet1.IndexFromUniqueKey__c = 1;
        consumEquipSetList.add(consumEquipSet1);

        insert consumEquipSetList;

        // 耗材备品配套一览明细
        List<Consum_Apply_Equipment_Set_Detail__c> consumEquipSetDetailList = new List<Consum_Apply_Equipment_Set_Detail__c>();
        Consum_Apply_Equipment_Set_Detail__c consumEquipSetDetail1 = new Consum_Apply_Equipment_Set_Detail__c();
        consumEquipSetDetail1.Consum_Apply__c = testConsumApply.Id;  // 备品耗材申请
        consumEquipSetDetail1.Consum_Apply_Equipment_Set__c = consumEquipSet1.Id;  // 耗材备品配套一览
        consumEquipSetDetail1.Fixture_Model_No_text__c = '12345';  // 备品配套明细型号(耗材时)
        consumEquipSetDetail1.Asset__c = testAsset.Id;
        //consumEquipSetDetail1.Is_Body__c = false;
        consumEquipSetDetail1.IndexFromUniqueKey__c = 1;
        consumEquipSetDetail1.Show_demonstration__c = '演示手术';  // 展示/演示
        consumEquipSetDetail1.Consum_Start_Date__c = Date.today();  // 预计使用日
        consumEquipSetDetail1.Case_OR_animal_organ__c = 'TestAnimalCase1';  // 病例/动物脏器
        consumEquipSetDetail1.Trial_User__c = 'testUser1';  // 试用者
        consumEquipSetDetail1.Follower_User__c = testUser1.Id;  // 跟台者
        consumEquipSetDetail1.Spare__c = true;  // 备用
        consumEquipSetDetail1.Comment__c = 'TestComment1';  // 备注
        consumEquipSetDetail1.Salesdepartment_before__c = 'TestDepart';  // 所在地区(本部)
        consumEquipSetDetail1.Internal_asset_location_before__c = 'TestLocation1';  // 备品存放地
        consumEquipSetDetail1.Product_category_text__c = 'GI';  // 产品分类
        consumEquipSetDetail1.Equipment_Type_text__c = 'TestType';  // 备品分类
        consumEquipSetDetail1.Shipment_request_time2__c = Date.today();

        consumEquipSetDetailList.add(consumEquipSetDetail1);

        //Consum_Apply_Equipment_Set_Detail__c consumEquipSetDetail2 = new Consum_Apply_Equipment_Set_Detail__c();
        //consumEquipSetDetail2.Consum_Apply__c = testConsumApply.Id;  // 备品耗材申请
        //consumEquipSetDetail2.Consum_Apply_Equipment_Set__c = consumEquipSet1.Id;  // 耗材备品配套一览
        //consumEquipSetDetail2.Fixture_Model_No_text__c = '54321';  // 备品配套明细型号(耗材时)
        //consumEquipSetDetail2.Asset__c = testAsset.Id;
        //consumEquipSetDetail2.Is_Body__c = false;
        //consumEquipSetDetail2.IndexFromUniqueKey__c = 2;
        //consumEquipSetDetail2.Show_demonstration__c = '陈列展示';  // 展示/演示
        //consumEquipSetDetail1.Consum_Start_Date__c = Date.today();  // 预计使用日
        //consumEquipSetDetail2.Case_OR_animal_organ__c = 'TestAnimalCase2';  // 病例/动物脏器
        //consumEquipSetDetail2.Trial_User__c = 'testUser2';  // 试用者
        //consumEquipSetDetail2.Follower_User__c = testUser1.Id;  // 跟台者
        //consumEquipSetDetail2.Spare__c = false;  // 备用
        //consumEquipSetDetail2.Comment__c = 'TestComment2';  // 备注
        //consumEquipSetDetail2.Salesdepartment_before__c = 'TestDepart';  // 所在地区(本部)
        //consumEquipSetDetail2.Internal_asset_location_before__c = 'TestLocation2';  // 备品存放地
        //consumEquipSetDetail2.Product_category_text__c = 'GI';  // 产品分类
        //consumEquipSetDetail2.Equipment_Type_text__c = 'TestType';  // 备品分类
        //consumEquipSetDetailList.add(consumEquipSetDetail2);
        insert consumEquipSetDetailList;
    }


    static testMethod void testInit() {
        List<User> testUserList = [SELECT Id FROM User WHERE Alias = 'Alias'];
        System.assertEquals(1, testUserList.size());
        System.runAs(testUserList[0]) {
            List<Consum_Apply__c> testAppList = [SELECT Id FROM Consum_Apply__c LIMIT 1];
            System.assertEquals(1, testAppList.size());
            Consum_Apply__c testAppObj = testAppList[0];
            PageReference ref =  new PageReference('/apex/ConsumTrialPDF?Id=' + testAppObj.Id);
            Test.setCurrentPage(ref);
            ConsumTrialPDFController ctp = new ConsumTrialPDFController();

            Test.startTest();
            ctp.init();
            Test.stopTest();

            // 耗材备品申请
            //System.assertEquals('TestConsumApply', ctp.targetConsumApply.Name);
            System.assertEquals('试用（无询价）', ctp.targetConsumApply.demo_purpose2__c);
            // System.assertEquals('医疗华北营业本部', ctp.targetConsumApply.Salesdept__c);
            // System.assertEquals('北京', ctp.targetConsumApply.WorkPlace__c);
            // System.assertEquals('北京市', ctp.targetConsumApply.direct_shippment_address__c);
            System.assertEquals('测试姓名', ctp.targetConsumApply.Loaner_received_staff__c);
            System.assertEquals('23111111', ctp.targetConsumApply.Loaner_received_staff_phone__c);
            // 医疗机构名
            System.assertEquals('TestHospName', ctp.hospitalName);
            // 发货地址
            System.assertEquals('北京市', ctp.shippmentAddress);
            // 所有PDF页集合
            System.assertEquals(1, ctp.pdfPageList.size());
            ConsumTrialPDFController.PdfPageClass testPdfPage = ctp.pdfPageList[0];
            Consum_Apply_Equipment_Set_Detail__c testDetail1 = testPdfPage.equipSetDetailList[0];
            System.assertEquals('No02', testDetail1.Fixture_Model_No_F__c);  // 明细型号
            System.assertEquals('TestPropName', testDetail1.ProductName__c);  // 中文名称
            System.assertNotEquals(null, testDetail1.Consumable_Guaranteen_end_F__c);  // 消耗品有效期
            System.assertEquals('演示手术', testDetail1.Show_demonstration__c);  // 展示/演示
            System.assertNotEquals(null, testDetail1.Consum_Start_Date__c);  // 试用日期
            System.assertEquals('TestAnimalCase1', testDetail1.Case_OR_animal_organ__c);  // 病例/动物脏器
            System.assertEquals('testUser1', testDetail1.Trial_User__c);  // 试用者盖章
            System.assertNotEquals(null, testDetail1.Follower_User__c);  // 跟台者盖章
            System.assertEquals(true, testDetail1.Spare__c);  // 备用
            System.assertEquals('TestComment1', testDetail1.Comment__c);  // 备注
            //Consum_Apply_Equipment_Set_Detail__c testDetail2 = testPdfPage.equipSetDetailList[1];
            //System.assertEquals('No02', testDetail2.Fixture_Model_No_F__c);  // 明细型号
            //System.assertEquals('TestPropName', testDetail2.ProductName__c);  // 中文名称
            //System.assertNotEquals(null, testDetail2.Consumable_Guaranteen_end_F__c);  // 消耗品有效期
            //System.assertEquals('演示手术', testDetail2.Show_demonstration__c);  // 展示/演示
            //System.assertNotEquals(null, testDetail2.Consum_Start_Date__c);  // 试用日期
            //System.assertEquals('TestAnimalCase1', testDetail2.Case_OR_animal_organ__c);  // 病例/动物脏器
            //System.assertEquals('testUser1', testDetail2.Trial_User__c);  // 试用者盖章
            //System.assertNotEquals(null, testDetail2.Follower_User__c);  // 跟台者盖章
            //System.assertEquals(true, testDetail2.Spare__c);  // 备用
            //System.assertEquals('TestComment1', testDetail2.Comment__c);  // 备注
            // 耗材备品总数量
            System.assertEquals(1, ctp.consumApplySetDetailListSize);
            // 错误信息
            System.assertEquals(null, ctp.errorMsg);
        }

    }
}