@isTest
private class ReceivingNotePDFControllerTest {
    static Id rnId;
    static Id rnsId;    
    /**
    *@description 造数据，一个清单，二个一览，每个一览下7条明细
    */
    static void setupTestData() {
        // 省
        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 hospital = new Account();
        hospital.recordtypeId = [Select Id FROM RecordType WHERE IsActive = true and SobjectType = 'Account' and DeveloperName = 'HP'].id;
        hospital.Name = 'test hospital';
        hospital.Is_Active__c = '有効';
        hospital.Attribute_Type__c = '卫生部';
        hospital.Speciality_Type__c = '综合医院';
        hospital.Grade__c = '一级';
        hospital.OCM_Category__c = 'SLTV';
        hospital.Is_Medical__c = '医疗机构';
        hospital.State_Master__c = al.id;
        hospital.City_Master__c = al2.id;
        hospital.Town__c = '东京';
        insert hospital;

        // 戦略科室を得る
        Account[] strategicDep = [SELECT ID, Name FROM Account WHERE parentId = :hospital.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 = 'test dep';
        dep.AgentCode_Ext__c = '9999998';
        dep.ParentId = strategicDep[0].Id;
        dep.Department_Class__c = strategicDep[0].Id;
        dep.Hospital__c = hospital.Id;
        insert dep;

        // 产品
        Product2 pro1 = new Product2(Name='name01',IsActive=true,Family='GI',
                Fixture_Model_No__c='TD-TB400',Serial_Lot_No__c='S/N tracing',
                Fixture_Model_No_T__c = 'TD-TB400', Asset_Model_No__c = 'Pro1',
                ProductCode_Ext__c='pc01',Manual_Entry__c=false,ProductCode = 'PRODUCTCODE',
                Packing_list_manual__c = 1);
        insert pro1;

        // 一个FS
        Fixture_Set__c fs = new Fixture_Set__c();
        fs.Name = 'SB-FS-01';
        fs.Fixture_Set_Body_Model_No__c = 'SB-FS-MODEL-01';
        fs.Loaner_name__c = 'SB-FS-1';
        insert fs;

        // 一个FSD
        Fixture_Set_Detail__c fsd = new Fixture_Set_Detail__c();
        fsd.Name = 'SB-FSD-1';
        fsd.Name_CHN_Created__c = 'SB-中文FSD-1';
        fsd.Product2__c = pro1.Id;
        fsd.Fixture_Set__c = fs.Id;
        fsd.Is_Body__c = true;
        fsd.Quantity__c = 1;
        fsd.UniqueKey__c = fs.Id + ':' + pro1.Id;
        fsd.SortInt__c = 1;
        upsert fsd;

        // 保有设备
        Asset asset1 = new Asset(Asset_Owner__c = 'Olympus');
        asset1.RecordTypeId = System.Label.Asset_RecordType;
        asset1.SerialNumber = 'asset1';
        asset1.Name = 'asset1';
        asset1.AccountId = dep.Id;
        asset1.Department_Class__c = strategicDep[0].Id;
        asset1.Hospital__c = hospital.Id;
        asset1.Product2Id = pro1.Id;
        asset1.Quantity = 1;
        asset1.Status = '不明';
        asset1.Manage_type__c = '个体管理';
        asset1.Loaner_accsessary__c = false;
        asset1.Out_of_wh__c = 0;
        asset1.Salesdepartment__c = '1.华北营业本部';
        asset1.Internal_asset_location__c = '北京 备品中心';
        asset1.Product_category__c = 'GI';
        asset1.Equipment_Type__c = '产品试用';
        asset1.SalesProvince__c = '大连';
        asset1.CompanyOfEquipment__c = '北京';
        asset1.Internal_Asset_number__c = '0001';
        asset1.Rental_Count__c = 0;
        asset1.Asset_loaner_category__c = '固定资产';
        asset1.Consumable_Guaranteen_end__c = System.Today() + 1;
        // asset1.OT_CODE__c = F:Product2.ProductCode = 'PRODUCTCODE'
        // asset1.Fixture_Model_No_F__c = F:Product2.Fixture_Model_No_T__c = 'TD-TB400'
        // asset1.CompanyCodeOfEquipment__c = auto CompanyOfEquipment__c '北京'-> "5111"
        // asset1.Internal_Asset_number_key__c = F: CompanyCodeOfEquipment__c & Internal_Asset_number__c or "" = '51110001'
        insert asset1;

        ////收货清单
        ReceivingNote__c rn = new ReceivingNote__c();
        rn.Sales_order_number__c = 'SALES_ORDER_NUMBER';      //合同编码字段
        rn.OrderCode__c = 'ORDERCODE';                    //订单编码
        rn.ApprovalNumber__c = 'APPROVALNUMBER';          //采购申请决裁号
        rn.DateOfDelivery__c = System.Today();             //发货日
        rn.Internal_asset_location__c = 'INTERNAL_ASSET_LOCATION'; //备品存放地
        rn.ManagementCenter__c = 'MGCT';
        rn.Asset_loaner_category__c = '固定资产';
        // rn.OCM_period__c = auto
        // rn.passNo7__c = auto 
        // rn.ReceivingNoteNo__c = ManagementCenter__c & ":" & OCM_period__c & ":" & passNo7__c   //清单编号
        rn.QRId__c = 'QRID';                     //二维码
        insert rn;

        //收货清单一览A
        ReceivingNoteSummary__c rnsA = new ReceivingNoteSummary__c();
        rnsA.ReceivingNote__c = rn.Id;
        rnsA.Fixture_Model_No__c = 'FIXTURE_MODEL_NO_A';
        rnsA.Fixture_Set__c = fs.Id;
        rnsA.passTxtNo3__c = '001';
        // rnsA.ReceivingNoteSummaryNo__c = ReceivingNote__r.passNo7__c & ":" & Fixture_Model_No__c & ":" & passTxtNo3__c = 7位数字:TD-TB400:001
        insert rnsA;

        //一览A下的7条明细
        List<ReceivingNoteDetail__c> rndListA = new List<ReceivingNoteDetail__c> ();
        for(Integer i = 0;i<7;i++){
            ReceivingNoteDetail__c rnd = new ReceivingNoteDetail__c();
            rnd.ReceivingNoteSummary__c = rnsA.Id;
            rnd.passNo2__c = '01';
            rnd.passNo3__c = ''+(i+1);
            rnd.Name = 'NAME_A'+i;
            rndListA.add(rnd);
        }

        //对一览A下的第一条明细中的需要展示的字段赋值
        // rndListA[0].ReceivingNoteDetailNo__c = F:ReceivingNoteSummary__r.ReceivingNoteSummaryNo__c & ":" & passNo2__c & ":" & passNo3__c = '7位数字:FIXTURE_MODEL_NO_A:3位数字:01:1'
        rndListA[0].RNDAssert__c = asset1.Id;
        // rndListA[0].OT_CODE__c = F:RNDAssert__r.OT_CODE__c = 'PRODUCTCODE'
        // rndListA[0].Fixture_Model_No_F__c = F: RNDAssert__r.Fixture_Model_No_F__c or Fixture_Set_Detail__r.Fixture_Model_No_F__c or Fixture_Model_No_text__c ='TD-TB400'
        // rndListA[0].Internal_Asset_number_key__c = F: RNDAssert__r.Internal_Asset_number_key__c = '51110001'
        // rndListA[0].PeriodOfUse__c = F: RNDAssert__r.Consumable_Guaranteen_end__c = System.Today + 1;
        // rndListA[0].Packing_list_Fixture_F__c = F: TEXT( RNDAssert__r.Product2.Packing_list_Fixture_F__c ) = '1'
        rndListA[0].Fixture_Set_Detail__c = fsd.Id;
        rndListA[0].SerialNumber__c = 'SERIAL_NUMBER_A0';
        rndListA[0].EquipmentSet_Managment_Code__c = 'EQUIPMENTSET_MANAGMENT_CODE_A0';
        rndListA[0].Salesdepartment__c = '1.华北营业本部';
        rndListA[0].SalesProvince__c = '大连';
        rndListA[0].Product_category__c = 'GI';
        rndListA[0].Equipment_Type__c = '产品试用';
        rndListA[0].Manage_type__c = '个体管理';
        rndListA[0].unknow_serial_NO_product__c = 'FALSE';
        rndListA[0].Loaner_accsessary__c = 'TRUE';
        insert rndListA;

        //收货清单一览B
        ReceivingNoteSummary__c rnsB = new ReceivingNoteSummary__c();
        rnsB.ReceivingNote__c = rn.Id;
        insert rnsB;

        // 一览B下的7条明细
        List<ReceivingNoteDetail__c> rndListB = new List<ReceivingNoteDetail__c> ();
        for(Integer i = 0;i<7;i++){
            ReceivingNoteDetail__c rnd = new ReceivingNoteDetail__c();
            rnd.passNo2__c = '02';
            rnd.passNo3__c = ''+(i+1);
            rnd.ReceivingNoteSummary__c = rnsB.Id;
            rnd.Name = 'NAME_B'+i;
            rndListB.add(rnd);
        }
        insert rndListB;

        rnId = rn.Id;
    }
    /**
    @description 检查导出的PDF
    */
    static testMethod void unitTestReceivingNotePDF(){
        setupTestData();
        PageReference ref = new PageReference('/apex/ReceivingNotePDF?rnid=' + rnId);
        Test.setCurrentPage(ref);
        ReceivingNotePDFController controller = new ReceivingNotePDFController();

        Test.startTest();
        controller.init();
        // 共2页
        System.assertEquals(2, controller.pageCnt); 
        // 共2页
        System.assertEquals(2, controller.pages.size()); 

        // 检查清单信息
        System.assertEquals('SALES_ORDER_NUMBER',controller.receivingNoteHeadShow.Sales_order_number__c);
        System.assertEquals('ORDERCODE',controller.receivingNoteHeadShow.OrderCode__c);
        System.assertEquals('APPROVALNUMBER',controller.receivingNoteHeadShow.ApprovalNumber__c);
        System.assertEquals(System.Today(),controller.receivingNoteHeadShow.DateOfDelivery__c);
        System.assertEquals('INTERNAL_ASSET_LOCATION',controller.receivingNoteHeadShow.Internal_asset_location__c);
       // System.assertEquals('MGCT',controller.receivingNoteHeadShow.ReceivingNoteNo__c.substring(0,4));
        System.assertEquals('QRID',controller.receivingNoteHeadShow.QRId__c);

        // 检查明细信息
        ReceivingNoteDetail__c rnd = new ReceivingNoteDetail__c();

        // 检查明细行数
        for(Integer i=0;i<2;i++){
            for(Integer j=0;j<13;j++){
                rnd = controller.pages[i].lineList[j];
                if( i==0 || j ==0 ){
                    //第0页第0行~第1页第0行有内容
                    Integer k = 1;
                    String ab = '';
                    if (i == 0 && j < 7){
                        // 第0页 0~6行是rnsA的0~6明细
                        ab = '01';
                        k = j+1;
                    }
                    else if (i == 0 && j>=7){
                        // 第0页 7~12行是rnsB的0~5明细
                        ab = '02';
                        k = j - 7 + 1;
                    }
                    else{
                        // 第1页 0行是rnsB的6明细
                        ab = '02';
                        k = 6 + 1;
                    }
                    System.assertEquals( ab + ':' + String.valueOf(k).leftPad(3,'0'), rnd.ReceivingNoteDetailNo__c.right(6));
                }
                else{
                    //第1页第1行~第1页第12行为空行
                    System.assertEquals(null, rnd.ReceivingNoteDetailNo__c);
                }
            }
        }

        // 针对第0页第0行的内容，逐个字段检查
        rnd = controller.pages[0].lineList[0];
        System.debug(rnd.ReceivingNoteDetailNo__c);
        System.assert(Pattern.matches('[0-9]{7}:TD-TB400:001:01:001',rnd.ReceivingNoteDetailNo__c));
        System.assertEquals('PRODUCTCODE',rnd.OT_CODE__c);
        System.assertEquals('SB-FSD-1', rnd.Fixture_Model_Name_F__c);
        System.assertEquals('TD-TB400',rnd.Fixture_Model_No_F__c);
        System.assertEquals('51110001',rnd.Internal_Asset_number_key__c);
        System.assertEquals(System.Today() + 1,rnd.PeriodOfUse__c);
        System.assertEquals('1',rnd.Packing_list_Fixture_F__c);
        System.assertEquals('固定资产',rnd.Asset_loaner_category_F__c);
        System.assertEquals('SERIAL_NUMBER_A0',rnd.SerialNumber_F__c);
        System.assertEquals('EQUIPMENTSET_MANAGMENT_CODE_A0',rnd.EquipmentSet_Managment_Code__c);
        System.assertEquals('1.华北营业本部',rnd.Salesdepartment__c);
        System.assertEquals('大连',rnd.SalesProvince__c);
        System.assertEquals('GI',rnd.Product_category__c);
        System.assertEquals('产品试用',rnd.Equipment_Type__c);
        System.assertEquals('个体管理',rnd.Manage_type__c);
        System.assertEquals('FALSE',rnd.unknow_serial_NO_product__c);
        System.assertEquals('TRUE',rnd.Loaner_accsessary__c);

        Test.stopTest();

    }

}