@isTest
private class ReceivingNoteDetailHandlerTest {
    static Id rnId;
    static Date td = Date.today();
    static Time tm = Time.newInstance(8,43,38,0);
    /**
    *@description 造数据
    */
    static void setupTestData() {
        Oly_TriggerHandler.bypass('ContactTriggerHandler');
        ControllerUtil.EscapeNFM001Trigger = true;
        StaticParameter.EscapeNFM001Trigger = true;
        // StaticParameter.EscapeAccountTrigger = true;
        StaticParameter.EscapeNFM001AgencyContractTrigger = true;
        // 省
        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;

        Contact contact2 = new Contact();
        contact2.AccountId = dep.Id;
        contact2.FirstName = '責任者';
        contact2.LastName = 'test1经销商';
        insert contact2;

        // 产品
        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);
        Product2 pro2 = new Product2(Name='name02',IsActive=true,Family='GI',
                Fixture_Model_No__c='n02',Serial_Lot_No__c='Lot tracing',
                Fixture_Model_No_T__c = 'n02', Asset_Model_No__c = 'Pro2',
                ProductCode_Ext__c='pc02',Manual_Entry__c=false);

        insert new Product2[] {pro1, pro2};

        // 保有设备
        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.Posting_Date__c = td;
        asset1.IF_Information_From__c = true;

        Asset asset2 = new Asset(Asset_Owner__c = 'Olympus');
        asset2.RecordTypeId = System.Label.Asset_RecordType;
        asset2.SerialNumber = 'asset2';
        asset2.Name = 'asset2';
        asset2.AccountId = dep.Id;
        asset2.Department_Class__c = strategicDep[0].Id;
        asset2.Hospital__c = hospital.Id;
        asset2.Product2Id = pro2.Id;
        asset2.Quantity = 1;
        asset2.Status = '不明';
        asset2.Manage_type__c = '数量管理';
        asset2.Loaner_accsessary__c = true;
        asset2.Out_of_wh__c = 0;
        asset2.Salesdepartment__c = '1.华北营业本部';
        asset2.Internal_asset_location__c = '北京 备品中心';
        asset2.Product_category__c = 'GI';
        asset2.Equipment_Type__c = '产品试用';
        asset2.SalesProvince__c = '北京';
        asset2.CompanyOfEquipment__c = '北京';
        asset2.Internal_Asset_number__c = '0002';
        asset2.Rental_Count__c = 0;
        asset2.Asset_loaner_category__c = '低值易耗品';
        insert new Asset[] {asset1, asset2};

        // 备品配套
        Fixture_Set__c fsObjA1 = new Fixture_Set__c();
        fsObjA1.Name = 'set1';
        fsObjA1.Fixture_Set_Body_Model_No__c = 'modelNo1';
        fsObjA1.Loaner_name__c = 'name1';
        insert fsObjA1;

        // 备品配套明细
        Fixture_Set_Detail__c fsdObjA1 = new Fixture_Set_Detail__c();
        fsdObjA1.Name = '备品配套明细名1';
        fsdObjA1.Name_CHN_Created__c = '中文名称1';
        fsdObjA1.Product2__c = pro1.Id;
        fsdObjA1.Fixture_Set__c = fsObjA1.Id;
        fsdObjA1.Is_Body__c = true;
        fsdObjA1.Is_Optional__c = false;
        fsdObjA1.Quantity__c = 1;
        fsdObjA1.UniqueKey__c = fsObjA1.Id + ':' + pro1.Id;
        fsdObjA1.SortInt__c = 0;
        // 备品配套明细
        Fixture_Set_Detail__c fsdObjA2 = new Fixture_Set_Detail__c();
        fsdObjA2.Name = '备品配套明细名2';
        fsdObjA2.Name_CHN_Created__c = '中文名称2';
        fsdObjA2.Product2__c = pro2.Id;
        fsdObjA2.Fixture_Set__c = fsObjA1.Id;
        fsdObjA2.Is_Body__c = false;
        fsdObjA2.Is_Optional__c = true;
        fsdObjA2.Quantity__c = 5;
        fsdObjA2.UniqueKey__c = fsObjA1.Id + ':' + pro2.Id;
        fsdObjA2.SortInt__c = 1;

        insert new Fixture_Set_Detail__c[] {fsdObjA1, fsdObjA2};

        ////收货清单
        ReceivingNote__c rn = new ReceivingNote__c();
        rn.Name = 'testra';
        rn.Status__c = '已数据确认';
        rn.DateOfDelivery__c = td;
        insert rn;
        //收货清单一览
        ReceivingNoteSummary__c rns = new ReceivingNoteSummary__c();
        rns.ReceivingNote__c = rn.Id;
        rns.Fixture_Set__c = fsObjA1.Id;
        insert rns;

        // 收货一览明细
        ReceivingNoteDetail__c rnd1 = new ReceivingNoteDetail__c();
        rnd1.ReceivingNoteSummary__c = rns.Id;
        rnd1.Is_Body__c = true;

        insert new ReceivingNoteDetail__c[] {rnd1};

        rnId = rn.Id;
    }
    /**
    @description 测试清单一览中第一条明细是否被赋值
    */
    static testMethod void testSetRNSFirst() {
        setupTestData();

        Test.startTest();

        List<ReceivingNoteDetail__c> rndList = [
                SELECT Id
                     , RNDAssert__c
                  FROM ReceivingNoteDetail__c
                   FOR UPDATE
                ];
        System.assertEquals(1, rndList.size());
        ReceivingNoteDetail__c rnd = rndList[0];
        // 一览明细的资产为空
        System.assertEquals(null, rnd.RNDAssert__c);

        List<Asset> assetList = [SELECT Id FROM Asset];
        System.assertEquals(2, assetList.size());
        Asset asset = assetList[0];

        List<ReceivingNoteSummary__c> rnsList  = [SELECT Id
                                                       , First_RND__c
                                                    FROM ReceivingNoteSummary__c];
        System.assertEquals(1, rnsList.size());
        ReceivingNoteSummary__c rnsObj = rnsList[0];
        // 一览中没有first明细
        System.assertEquals(null, rnsObj.First_RND__c);

        // 给资产赋值并更新
        rnd.RNDAssert__c = asset.Id;
        update rnd;

        List<ReceivingNoteSummary__c> newRnsList  = [Select Id
                                                          , First_RND__c
                                                       FROM ReceivingNoteSummary__c];
        System.assertEquals(1, RnsList.size());
        ReceivingNoteSummary__c newRns = newRnsList[0];
        // 一览中存在first明细
        System.assertEquals(rnd.Id, newRns.First_RND__c);

        Test.stopTest();

    }
    /**
    @description 测试清单一览明细在插入时FSD_Id__c，UniqueKey__c，ReceivingNote__c的赋值情况
    */
    static testMethod void testBeforeSetValue() {
        setupTestData();
        Test.startTest();
        List<ReceivingNoteDetail__c> rndList = [select Id,FSD_Id__c,UniqueKey__c,ReceivingNote__c from ReceivingNoteDetail__c for update];
        System.assertEquals(1, rndList.size());
        ReceivingNoteDetail__c rnd = rndList[0];
        // 对应的配套明和UniqueKey__c为空
        System.assertEquals(null, rnd.FSD_Id__c);
        System.assertEquals(null, rnd.UniqueKey__c);
        // setupTestData()造数据时没有清单Id，此时有值
        System.assertEquals(rnId, rnd.ReceivingNote__c);


        List<Fixture_Set_Detail__c> fsdList = [select Id from Fixture_Set_Detail__c];
        System.assertEquals(2, fsdList.size());
        Fixture_Set_Detail__c fsd = fsdList[0];
        rnd.Fixture_Set_Detail__c = fsd.Id;
        rnd.DataMigration_Flag__c = true;
        update rnd;
        rndList = [
                SELECT Id
                     , FSD_Id__c
                     , UniqueKey__c
                     , Delete_Flag__c
                     , passNo3__c
                  FROM ReceivingNoteDetail__c
                   FOR UPDATE
                ];
        rnd = rndList[0];
        // 给Fixture_Set_Detail__c赋值，导致FSD_Id__c有值
        System.assertEquals(fsd.Id, rnd.FSD_Id__c);
        // DataMigration_Flag__c为true时，UniqueKey__c仍是null
        System.assertEquals(null, rnd.UniqueKey__c);

        rnd.Delete_Flag__c = false;
        rnd.DataMigration_Flag__c = false;
        rnd.passNo3__c = '1';
        update rnd;
        rndList = [
                SELECT Id
                     , ReceivingNote__c
                     , ReceivingNoteSummary__c
                     , FSD_Id__c
                     , passNo3__c
                     , Delete_Flag__c
                     , UniqueKey__c
                  FROM ReceivingNoteDetail__c
                   FOR UPDATE
                ];
        rnd = rndList[0];
        System.assertEquals(rnd.ReceivingNote__c
                            + ':' + rnd.ReceivingNoteSummary__c
                            + ':'+ rnd.FSD_Id__c
                            + ':' + rnd.passNo3__c,
                            rnd.UniqueKey__c);


        rnd.Delete_Flag__c = true;
        update rnd;
        rndList = [
                SELECT Id
                     , ReceivingNote__c
                     , ReceivingNoteSummary__c
                     , FSD_Id__c
                     , passNo3__c
                     , Delete_Flag__c
                     , UniqueKey__c
                  FROM ReceivingNoteDetail__c
                ];
        rnd = rndList[0];
        System.assertEquals(rnd.ReceivingNote__c
                            + ':' + rnd.ReceivingNoteSummary__c
                            + ':' + rnd.FSD_Id__c
                            + ':' + rnd.passNo3__c
                            + ':' + rnd.Id,
                            rnd.UniqueKey__c);

        Test.stopTest();
    }

    static void testCalculateBase(Map<Integer, Date> cldMap, Map<Integer, Boolean> isWdMap, Map<String, Datetime> dm, Map<String,Decimal> ast) {
        setupTestData();
        // 日历
        List<OlympusCalendar__c> ocList = new List<OlympusCalendar__c>();
        for (Integer i = 0; i < 8; i++) {
            OlympusCalendar__c oc = new OlympusCalendar__c();
            oc.Date__c = cldMap.get(i);
            if (true == isWdMap.get(i)) {
                oc.ChangeToWorkday__c = true;
            } else {
                oc.ChangeToHoliday__c = true;
            }
            ocList.add(oc);
        }
        insert ocList;
        //收货清单
        ReceivingNote__c rn = new ReceivingNote__c();
        rn.Name = 'testra2';
        rn.Status__c = '已数据确认';
        rn.DateOfDelivery__c = td;
        insert rn;
        //收货清单一览
        ReceivingNoteSummary__c rns = new ReceivingNoteSummary__c();
        rns.ReceivingNote__c = rn.Id;
        rns.Fixture_Set__c = [SELECT Id FROM Fixture_Set__c][0].Id;
        rns.Received_Confirm_Time__c = dm.get('Received_Confirm_Time');
        insert rns;
        // 收货一览明细
        ReceivingNoteDetail__c rnd1 = new ReceivingNoteDetail__c();
        rnd1.ReceivingNoteSummary__c = rns.Id;
        rnd1.Is_Body__c = true;
        rnd1.RNDAssert__c = [SELECT Id FROM Asset WHERE Internal_Asset_number__c = '0001'][0].Id;
        rnd1.DataConfirmation__c = true;
        rnd1.DataMigration_Flag__c = true;
        insert new ReceivingNoteDetail__c[] {rnd1};

        rnd1.DataConfirmationTime__c = dm.get('DataConfirmationTime');
        rnd1.AcceptanceTime__c = dm.get('AcceptanceTime');
        rnd1.After_Inspection_time__c = dm.get('After_Inspection_time');
        rnd1.DataConfirmationAgainTime__c = dm.get('DataConfirmationAgainTime');
        rnd1.Arrival_wh_Request_time__c = dm.get('Arrival_wh_Request_time');
        rnd1.Arrival_wh_time__c = dm.get('Arrival_wh_time');
        update rnd1;

        Test.startTest();
        List<ReceivingNoteDetail__c> rndList = [
            select Id
                 , ProductArrivalTime_Day__c
                 , AccepDataConfirmTime_Day__c
                 , AccepDataConfirmTime_Hour__c
                 , WarehouseReceiptTime_Day__c
                 , WarehouseAcceptanceTime_Hour__c
                 , WarehouseAcceptanceTime_Day__c
                 , AcceptDataConfirmAgainTime_Hour__c
                 , AcceptDataConfirmAgainTime_Day__c
                 , AcceptDataArrivalRequestAgainTime_Hour__c
                 , AcceptDataArrivalRequestAgainTime_Day__c
                 , WarehouseArrivalTime_Hour__c
                 , WarehouseArrivalTime_Day__c
                 , ProductReceiptTotalTime_Day__c
                 , InspectionAcceptanceTime_Day__c
                 , InspectionAcceptanceTime_Hour__c
            from ReceivingNoteDetail__c
            where DataConfirmationTime__c <> null
        ];

        ReceivingNoteDetail__c rndObj = rndList[0];
        System.assertEquals(ast.get('ProductArrivalTime_Day'), rndObj.ProductArrivalTime_Day__c);
        //System.assertEquals(ast.get('AccepDataConfirmTime_Day'), rndObj.AccepDataConfirmTime_Day__c);
        //System.assertEquals(ast.get('AccepDataConfirmTime_Hour'), rndObj.AccepDataConfirmTime_Hour__c);
        System.assertEquals(ast.get('WarehouseReceiptTime_Day'), rndObj.WarehouseReceiptTime_Day__c);
        System.assertEquals(ast.get('WarehouseAcceptanceTime_Hour'), rndObj.WarehouseAcceptanceTime_Hour__c);
        System.assertEquals(ast.get('WarehouseAcceptanceTime_Day'), rndObj.WarehouseAcceptanceTime_Day__c);
        System.assertEquals(ast.get('InspectionAcceptanceTime_Day'), rndObj.InspectionAcceptanceTime_Day__c);
        System.assertEquals(ast.get('InspectionAcceptanceTime_Hour'), rndObj.InspectionAcceptanceTime_Hour__c);
        System.assertEquals(ast.get('AcceptDataConfirmAgainTime_Hour'), rndObj.AcceptDataConfirmAgainTime_Hour__c);
        System.assertEquals(ast.get('AcceptDataConfirmAgainTime_Day'), rndObj.AcceptDataConfirmAgainTime_Day__c);
        System.assertEquals(ast.get('AcceptDataArrivalRequestAgainTime_Hour'), rndObj.AcceptDataArrivalRequestAgainTime_Hour__c);
        System.assertEquals(ast.get('AcceptDataArrivalRequestAgainTime_Day'), rndObj.AcceptDataArrivalRequestAgainTime_Day__c);
        System.assertEquals(ast.get('WarehouseArrivalTime_Hour'), rndObj.WarehouseArrivalTime_Hour__c);
        System.assertEquals(ast.get('WarehouseArrivalTime_Day'), rndObj.WarehouseArrivalTime_Day__c);
        System.assertEquals(ast.get('ProductReceiptTotalTime_Day'), rndObj.ProductReceiptTotalTime_Day__c);
    }

    static testMethod void testCalculate1() {
        Map<Integer, Date> calenderMap = new Map<Integer, Date>();
        calenderMap.put(0, td);
        calenderMap.put(1, td.addDays(1));
        calenderMap.put(2, td.addDays(2));
        calenderMap.put(3, td.addDays(3));
        calenderMap.put(4, td.addDays(4));
        calenderMap.put(5, td.addDays(5));
        calenderMap.put(6, td.addDays(6));
        calenderMap.put(7, td.addDays(7));

        Map<Integer, Boolean> isWorkDayMap = new Map<Integer, Boolean>();
        isWorkDayMap.put(0, true);
        isWorkDayMap.put(1, true);
        isWorkDayMap.put(2, true);
        isWorkDayMap.put(3, true);
        isWorkDayMap.put(4, true);
        isWorkDayMap.put(5, true);
        isWorkDayMap.put(6, true);
        isWorkDayMap.put(7, true);

        Map<String, Datetime> dateTimeMap = new Map<String, Datetime>();
        dateTimeMap.put('Received_Confirm_Time', Datetime.newInstance(td.addDays(2), tm.addHours(2)));
        dateTimeMap.put('DataConfirmationTime', Datetime.newInstance(td.addDays(1), tm.addHours(1)));
        dateTimeMap.put('AcceptanceTime', Datetime.newInstance(td.addDays(3), tm.addHours(3)));
        dateTimeMap.put('After_Inspection_time', Datetime.newInstance(td.addDays(4), tm.addHours(4)));
        dateTimeMap.put('DataConfirmationAgainTime', Datetime.newInstance(td.addDays(5), tm.addHours(5)));
        dateTimeMap.put('Arrival_wh_Request_time', Datetime.newInstance(td.addDays(6), tm.addHours(6)));
        dateTimeMap.put('Arrival_wh_time', Datetime.newInstance(td.addDays(7), tm.addHours(7)));

        Map<String, Decimal> resultMap = new Map<String, Decimal>();
        resultMap.put('ProductArrivalTime_Day', 3.00);
        resultMap.put('AccepDataConfirmTime_Day', 0.00);
        resultMap.put('AccepDataConfirmTime_Hour', 0.00);
        resultMap.put('WarehouseReceiptTime_Day', 1.12);
        resultMap.put('WarehouseAcceptanceTime_Day', 1.12);
        resultMap.put('WarehouseAcceptanceTime_Hour', 9.5);
        resultMap.put('InspectionAcceptanceTime_Day', 1.12);
        resultMap.put('InspectionAcceptanceTime_Hour', 9.5);
        resultMap.put('AcceptDataConfirmAgainTime_Day', 1.12);
        resultMap.put('AcceptDataConfirmAgainTime_Hour', 9.5);
        resultMap.put('AcceptDataArrivalRequestAgainTime_Day', 2.24);
        resultMap.put('AcceptDataArrivalRequestAgainTime_Hour', 19.00);
        resultMap.put('WarehouseArrivalTime_Day', 1.12);
        resultMap.put('WarehouseArrivalTime_Hour', 9.5);
        resultMap.put('ProductReceiptTotalTime_Day', 5.59);

        testCalculateBase(calenderMap, isWorkDayMap, dateTimeMap, resultMap);
    }

    static testMethod void testCalculate2() {
        Map<Integer, Date> calenderMap = new Map<Integer, Date>();
        calenderMap.put(0, td);
        calenderMap.put(1, td.addDays(1));
        calenderMap.put(2, td.addDays(2));
        calenderMap.put(3, td.addDays(3));
        calenderMap.put(4, td.addDays(4));
        calenderMap.put(5, td.addDays(5));
        calenderMap.put(6, td.addDays(6));
        calenderMap.put(7, td.addDays(7));

        Map<Integer, Boolean> isWorkDayMap = new Map<Integer, Boolean>();
        isWorkDayMap.put(0, false);
        isWorkDayMap.put(1, false);
        isWorkDayMap.put(2, false);
        isWorkDayMap.put(3, false);
        isWorkDayMap.put(4, false);
        isWorkDayMap.put(5, false);
        isWorkDayMap.put(6, false);
        isWorkDayMap.put(7, false);

        Map<String, Datetime> dateTimeMap = new Map<String, Datetime>();
        dateTimeMap.put('Received_Confirm_Time', Datetime.newInstance(td.addDays(2), tm.addHours(2)));
        dateTimeMap.put('DataConfirmationTime', Datetime.newInstance(td.addDays(1), tm.addHours(1)));
        dateTimeMap.put('AcceptanceTime', Datetime.newInstance(td.addDays(3), tm.addHours(3)));
        dateTimeMap.put('After_Inspection_time', Datetime.newInstance(td.addDays(4), tm.addHours(4)));
        dateTimeMap.put('DataConfirmationAgainTime', Datetime.newInstance(td.addDays(5), tm.addHours(5)));
        dateTimeMap.put('Arrival_wh_Request_time', Datetime.newInstance(td.addDays(6), tm.addHours(6)));
        dateTimeMap.put('Arrival_wh_time', Datetime.newInstance(td.addDays(7), tm.addHours(7)));

        Map<String, Decimal> resultMap = new Map<String, Decimal>();
        resultMap.put('ProductArrivalTime_Day', 0.00);
        resultMap.put('AccepDataConfirmTime_Day', 0.00);
        resultMap.put('AccepDataConfirmTime_Hour', 0.00);
        resultMap.put('WarehouseReceiptTime_Day', 0.00);
        resultMap.put('WarehouseAcceptanceTime_Day', 0.00);
        resultMap.put('WarehouseAcceptanceTime_Hour', 0.00);
        resultMap.put('InspectionAcceptanceTime_Day', 0.00);
        resultMap.put('InspectionAcceptanceTime_Hour', 0.00);
        resultMap.put('AcceptDataConfirmAgainTime_Day', 0.00);
        resultMap.put('AcceptDataConfirmAgainTime_Hour', 0.00);
        resultMap.put('AcceptDataArrivalRequestAgainTime_Day', 0.00);
        resultMap.put('AcceptDataArrivalRequestAgainTime_Hour', 0.00);
        resultMap.put('WarehouseArrivalTime_Day', 0.00);
        resultMap.put('WarehouseArrivalTime_Hour', 0.00);
        resultMap.put('ProductReceiptTotalTime_Day', 0.00);

        testCalculateBase(calenderMap, isWorkDayMap, dateTimeMap, resultMap);
    }

    static testMethod void testCalculate3() {
        Map<Integer, Date> calenderMap = new Map<Integer, Date>();
        calenderMap.put(0, td);
        calenderMap.put(1, td.addDays(1));
        calenderMap.put(2, td.addDays(2));
        calenderMap.put(3, td.addDays(3));
        calenderMap.put(4, td.addDays(4));
        calenderMap.put(5, td.addDays(5));
        calenderMap.put(6, td.addDays(6));
        calenderMap.put(7, td.addDays(7));

        Map<Integer, Boolean> isWorkDayMap = new Map<Integer, Boolean>();
        isWorkDayMap.put(0, true);
        isWorkDayMap.put(1, false);
        isWorkDayMap.put(2, true);
        isWorkDayMap.put(3, false);
        isWorkDayMap.put(4, true);
        isWorkDayMap.put(5, false);
        isWorkDayMap.put(6, true);
        isWorkDayMap.put(7, false);

        Map<String, Datetime> dateTimeMap = new Map<String, Datetime>();
        dateTimeMap.put('Received_Confirm_Time', Datetime.newInstance(td.addDays(2), tm.addHours(2)));
        dateTimeMap.put('DataConfirmationTime', Datetime.newInstance(td.addDays(1), tm.addHours(1)));
        dateTimeMap.put('AcceptanceTime', Datetime.newInstance(td.addDays(3), tm.addHours(3)));
        dateTimeMap.put('After_Inspection_time', Datetime.newInstance(td.addDays(4), tm.addHours(4)));
        dateTimeMap.put('DataConfirmationAgainTime', Datetime.newInstance(td.addDays(5), tm.addHours(5)));
        dateTimeMap.put('Arrival_wh_Request_time', Datetime.newInstance(td.addDays(6), tm.addHours(6)));
        dateTimeMap.put('Arrival_wh_time', Datetime.newInstance(td.addDays(7), tm.addHours(7)));

        Map<String, Decimal> resultMap = new Map<String, Decimal>();
        resultMap.put('ProductArrivalTime_Day', 2.00);
        resultMap.put('AccepDataConfirmTime_Day', 0.00);
        resultMap.put('AccepDataConfirmTime_Hour', 0.00);
        resultMap.put('WarehouseReceiptTime_Day', 0.2);
        resultMap.put('WarehouseAcceptanceTime_Day', 0.8);
        resultMap.put('WarehouseAcceptanceTime_Hour', 6.78);
        resultMap.put('InspectionAcceptanceTime_Day', 0.44);
        resultMap.put('InspectionAcceptanceTime_Hour', 3.72);
        resultMap.put('AcceptDataConfirmAgainTime_Day', 0.56);
        resultMap.put('AcceptDataConfirmAgainTime_Hour', 4.78);
        resultMap.put('AcceptDataArrivalRequestAgainTime_Day', 1.24);
        resultMap.put('AcceptDataArrivalRequestAgainTime_Hour', 10.5);
        resultMap.put('WarehouseArrivalTime_Day', 0.33);
        resultMap.put('WarehouseArrivalTime_Hour', 2.78);
        resultMap.put('ProductReceiptTotalTime_Day', 2.8);

        testCalculateBase(calenderMap, isWorkDayMap, dateTimeMap, resultMap);
    }

    static testMethod void testCalculate4() {
        Map<Integer, Date> calenderMap = new Map<Integer, Date>();
        calenderMap.put(0, td);
        calenderMap.put(1, td.addDays(-1));
        calenderMap.put(2, td.addDays(-2));
        calenderMap.put(3, td.addDays(-3));
        calenderMap.put(4, td.addDays(-4));
        calenderMap.put(5, td.addDays(-5));
        calenderMap.put(6, td.addDays(-6));
        calenderMap.put(7, td.addDays(-7));

        Map<Integer, Boolean> isWorkDayMap = new Map<Integer, Boolean>();
        isWorkDayMap.put(0, false);
        isWorkDayMap.put(1, true);
        isWorkDayMap.put(2, false);
        isWorkDayMap.put(3, true);
        isWorkDayMap.put(4, false);
        isWorkDayMap.put(5, true);
        isWorkDayMap.put(6, false);
        isWorkDayMap.put(7, true);

        Map<String, Datetime> dateTimeMap = new Map<String, Datetime>();
        dateTimeMap.put('Received_Confirm_Time', Datetime.newInstance(td.addDays(-2), tm.addHours(2)));
        dateTimeMap.put('DataConfirmationTime', Datetime.newInstance(td.addDays(-1), tm.addHours(1)));
        dateTimeMap.put('AcceptanceTime', Datetime.newInstance(td.addDays(-3), tm.addHours(3)));
        dateTimeMap.put('After_Inspection_time', Datetime.newInstance(td.addDays(-4), tm.addHours(4)));
        dateTimeMap.put('DataConfirmationAgainTime', Datetime.newInstance(td.addDays(-5), tm.addHours(5)));
        dateTimeMap.put('Arrival_wh_Request_time', Datetime.newInstance(td.addDays(-6), tm.addHours(6)));
        dateTimeMap.put('Arrival_wh_time', Datetime.newInstance(td.addDays(-7), tm.addHours(7)));

        Map<String, Decimal> resultMap = new Map<String, Decimal>();
        resultMap.put('ProductArrivalTime_Day', null);
        resultMap.put('AccepDataConfirmTime_Day', null);
        resultMap.put('AccepDataConfirmTime_Hour', null);
        resultMap.put('WarehouseReceiptTime_Day', null);
        resultMap.put('WarehouseAcceptanceTime_Day', null);
        resultMap.put('WarehouseAcceptanceTime_Hour', null);
        resultMap.put('InspectionAcceptanceTime_Day', null);
        resultMap.put('InspectionAcceptanceTime_Hour', null);
        resultMap.put('AcceptDataConfirmAgainTime_Day', null);
        resultMap.put('AcceptDataConfirmAgainTime_Hour', null);
        resultMap.put('AcceptDataArrivalRequestAgainTime_Day', null);
        resultMap.put('AcceptDataArrivalRequestAgainTime_Hour', null);
        resultMap.put('WarehouseArrivalTime_Day', null);
        resultMap.put('WarehouseArrivalTime_Hour', null);
        resultMap.put('ProductReceiptTotalTime_Day', null);

        testCalculateBase(calenderMap, isWorkDayMap, dateTimeMap, resultMap);
    }
}