@isTest
private class ReceivingNoteStockInRequestCtrlTest {
    static ReceivingNoteTestDataFactory factory;
    /**
    @description 安装master数据
    */
    @testSetup static void setupMasterData() {
        factory = new ReceivingNoteTestDataFactory();
        factory.setupMasterData();
    }
    /**
    @param typeStringListString 格式：'备品类型,管理种类;备品类型,管理种类...'
    @description 造出指定类型的清单数据,并插入数据库
    */
    static void setupTestData(String typeStringListString) {
        factory = new ReceivingNoteTestDataFactory();
        if(String.isBlank(typeStringListString)) {
            typeStringListString = '固定资产,个体管理;附属品,个体管理;附属品,数量管理;耗材,数量管理;低值易耗品,数量管理';
        }
        List<String> typeStringList = typeStringListString.split(';');
        factory.setupReceivingNoteData(typeStringList);
    }
    /**
    *@description 测试检索功能
    */
    static testMethod void unitTestSearch() {

        setupTestData('固定资产,个体管理');
        Id rnId = factory.rnMap.get('固定资产,个体管理').Id;

        PageReference ref = new PageReference('/apex/ReceivingNoteStockInRequest?parId=' + rnId);
        Test.setCurrentPage(ref);

        ReceivingNoteStockInRequestController controller = new ReceivingNoteStockInRequestController();

        Test.startTest();
        controller.init();
        controller.getStatusOps();
        controller.getcategoryOps();
        // 不加检索条件则得到全部明细
        controller.search();
        System.assertEquals(1, controller.pageB.rowBList.size());

        // 单条件
        controller.pageB.rn.Status__c = null;
        controller.pageB.rn.Asset_loaner_category__c = '固定资产';
        controller.pageB.fixture_Model_No = null;
        controller.search();
        System.assertEquals(1, controller.pageB.rowBList.size());
        System.assertEquals('SB-MODEL-01', controller.pageB.rowBList[0].rnd.Fixture_Model_No_F__c);

        // 两条件
        controller.pageB.rn.Status__c = '待数据确认';
        controller.pageB.rn.Asset_loaner_category__c = '固定资产';
        controller.pageB.fixture_Model_No = null;
        controller.search();
        System.assertEquals(1, controller.pageB.rowBList.size());
        System.assertEquals('SB-MODEL-01', controller.pageB.rowBList[0].rnd.Fixture_Model_No_F__c);

        // 三条件
        controller.pageB.rn.Status__c = '待数据确认';
        controller.pageB.rn.Asset_loaner_category__c = '固定资产';
        controller.pageB.fixture_Model_No = 'SB-MODEL-01';
        controller.search();
        System.assertEquals(1, controller.pageB.rowBList.size());
        System.assertEquals('SB-MODEL-01', controller.pageB.rowBList[0].rnd.Fixture_Model_No_F__c);

        Test.stopTest();

    }
    /**
    @description 测试保存功能
    */
    static testMethod void unitTestSaveRecord() {
        // 状态调整为已验收
        setupTestData('固定资产,个体管理');
        Id rnId = factory.rnMap.get('固定资产,个体管理').Id;
        // 数据确认
        ReceivingNoteDetail__c rnd = factory.rndMap.get('固定资产,个体管理');
        rnd.unknow_serial_NO_product__c = 'FALSE';
        rnd.Loaner_accsessary__c = 'FALSE';
        rnd.DataConfirmation__c = true;
        upsert rnd Product_Serial_No__c;
        // 收货
        ReceivingNoteSummary__c  rns = factory.rnsMap.get('固定资产,个体管理');
        rns.Received_Confirm_Time__c = System.Now();
        upsert rns Internal_Asset_number__c;
        // 清点
        rnd.AcceptanceTime__c = System.Now();
        rnd.AcceptanceResult__c = 'OK';
        // 验收
        rnd.Inspection_result_after__c = 'OK';
        upsert rnd Product_Serial_No__c;

        PageReference ref = new PageReference('/apex/ReceivingNoteStockInRequest?parId=' + rnId);
        Test.setCurrentPage(ref);

        ReceivingNoteStockInRequestController controller = new ReceivingNoteStockInRequestController();

        Test.startTest();
        // 初始化
        controller.init();
        controller.search();
        // 改为可编辑
        controller.changeFixModel();
        // 取出第一条数据
        ReceivingNoteStockInRequestController.RowBean rowB = controller.pageB.rowBList[0];
        Id rndId = rowB.rnd.Id;
        // 确认修改前各字段值
        System.assertEquals(null, rowB.rnd.Salesdepartment__c);
        System.assertEquals(null, rowB.rnd.SalesProvince__c);
        System.assertEquals(null, rowB.rnd.Product_category__c);
        System.assertEquals(null, rowB.rnd.Equipment_Type__c);
        System.assertEquals('个体管理', rowB.rnd.Manage_type__c);
        System.assertEquals('FALSE', rowB.rnd.unknow_serial_NO_product__c);
        System.assertEquals('FALSE', rowB.rnd.Loaner_accsessary__c);
        System.assertEquals(false, rowB.rnd.DataConfirmationAgain__c);
        // 修改字段值
        rowB.checked = true;
        rowB.rnd.Salesdepartment__c = '0.备品中心';
        rowB.rnd.SalesProvince__c = '大连';
        rowB.rnd.Product_category__c = 'GI';
        rowB.rnd.Equipment_Type__c = '产品试用';
        rowB.rnd.Manage_type__c = '个体管理';
        rowB.rnd.unknow_serial_NO_product__c = 'TRUE';
        rowB.rnd.Loaner_accsessary__c = 'TRUE';
        rowB.rnd.DataConfirmationAgain__c = true;

        controller.saveRecord();
        // 确认修改后结果
        ReceivingNoteDetail__c newRnd = [
                SELECT Salesdepartment__c
                     , SalesProvince__c
                     , Product_category__c
                     , Equipment_Type__c
                     , Manage_type__c
                     , unknow_serial_NO_product__c
                     , Loaner_accsessary__c
                     , DataConfirmationAgain__c
                  FROM ReceivingNoteDetail__c
                 WHERE Id = :rndId
                ];
        System.assertEquals('0.备品中心', newRnd.Salesdepartment__c);
        System.assertEquals('大连', newRnd.SalesProvince__c);
        System.assertEquals('GI', newRnd.Product_category__c);
        System.assertEquals('产品试用', newRnd.Equipment_Type__c);
        System.assertEquals('个体管理', newRnd.Manage_type__c);
        System.assertEquals('TRUE', newRnd.unknow_serial_NO_product__c);
        System.assertEquals('TRUE', newRnd.Loaner_accsessary__c);
        System.assertEquals(true, newRnd.DataConfirmationAgain__c);

        Test.stopTest();
    }
    /**
    @description 测试驳回功能
    */
    static testMethod void unitTestReject() {
        setupTestData('固定资产,个体管理');
        Id rnId = factory.rnMap.get('固定资产,个体管理').Id;
        Id rndId = factory.rndMap.get('固定资产,个体管理').Id;
        ReceivingNoteDetail__c rnd = factory.rndMap.get('固定资产,个体管理');
        rnd.Reject_Time__c = null;
        Datetime now = Datetime.now();
        rnd.After_Inspection_time__c = now;
        rnd.unknow_serial_NO_product__c = 'TRUE';
        rnd.Loaner_accsessary__c = 'TRUE';
        rnd.Inspection_staff_After__c = Userinfo.getUserId();
        rnd.Inspection_Comment__c = 'INSPECTION_COMMENT';
        rnd.Inspection_result_after__c = 'OK';
        rnd.AcceptanceComments__c = 'ACCEPTANCECOMMENTS';
        rnd.AcceptanceResult__c = 'OK';
        rnd.Reject_Reason__c = '驳回理由';
        rnd.Received_Quantity__c = 1;
        rnd.Inspection_Cnt__c = 1;
        upsert rnd Product_Serial_No__c;

        PageReference ref = new PageReference('/apex/ReceivingNoteStockInRequest?parId=' + rnId);
        Test.setCurrentPage(ref);

        ReceivingNoteStockInRequestController controller = new ReceivingNoteStockInRequestController();

        Test.startTest();
        controller.init();
        controller.search();
        // 取出第一条数据
        ReceivingNoteStockInRequestController.RowBean rowB = controller.pageB.rowBList[0];

        // 确认修改前各字段值
        rnd = [SELECT Reject_Time__c
                    , After_Inspection_time__c
                    , Inspection_staff_After__c
                    , Inspection_Comment__c
                    , Inspection_result_after__c
                    , AcceptanceComments__c
                    , AcceptanceResult__c
                    , Received_Quantity__c
                    , Inspection_Cnt__c
                 FROM ReceivingNoteDetail__c
                WHERE Id = :rndId];

        System.assertEquals(null, rnd.Reject_Time__c);
        System.assertEquals(now, rnd.After_Inspection_time__c);
        System.assertEquals( Userinfo.getUserId(), rnd.Inspection_staff_After__c);
        System.assertEquals('INSPECTION_COMMENT', rnd.Inspection_Comment__c);
        System.assertEquals('OK', rnd.Inspection_result_after__c);
        System.assertEquals('ACCEPTANCECOMMENTS', rnd.AcceptanceComments__c);
        System.assertEquals('OK', rnd.AcceptanceResult__c);
        System.assertEquals(1, rnd.Received_Quantity__c);
        System.assertEquals(1, rnd.Inspection_Cnt__c);

        // 执行驳回
        rowB.checked = true;
        controller.reject();

        // 确认修改后结果
        rnd = [
                SELECT Reject_Time__c
                     , After_Inspection_time__c
                     , Inspection_staff_After__c
                     , Inspection_Comment__c
                     , Inspection_result_after__c
                     , AcceptanceComments__c
                     , AcceptanceResult__c
                     , Reject_Reason__c
                     , Received_Quantity__c
                     , Inspection_Cnt__c

                  FROM ReceivingNoteDetail__c
                 WHERE Id=:rndId
                ];

        // 确认修改后各字段值
        System.assertNotEquals(null, rnd.Reject_Time__c);
        System.assertEquals(null, rnd.After_Inspection_time__c);
        System.assertEquals(null, rnd.Inspection_staff_After__c);
        System.assertEquals(null, rnd.Inspection_Comment__c);
        System.assertEquals(null, rnd.Inspection_result_after__c);
        System.assertEquals(0, rnd.Received_Quantity__c);
        System.assertEquals(0, rnd.Inspection_Cnt__c);
        System.assertEquals('驳回理由', rnd.Reject_Reason__c);

        Test.stopTest();

    }
    /**
    @description 测试入库指示
    */
    static testMethod void unitTestInStockRequest() {
        setupTestData('固定资产,个体管理');
        Id rnId = factory.rnMap.get('固定资产,个体管理').Id;
        Id rndId = factory.rndMap.get('固定资产,个体管理').Id;

        ReceivingNoteDetail__c rnd = factory.rndMap.get('固定资产,个体管理');
        rnd.unknow_serial_NO_product__c = 'TRUE';
        rnd.Loaner_accsessary__c = 'TRUE';
        rnd.DataConfirmation__c = true;
        rnd.AcceptanceTime__c = null;
        rnd.Inspection_result_after__c = 'OK';
        rnd.DataConfirmationAgain__c = true;
        upsert rnd Product_Serial_No__c;

        PageReference ref = new PageReference('/apex/ReceivingNoteStockInRequest?parId=' + rnId);
        Test.setCurrentPage(ref);

        ReceivingNoteStockInRequestController controller = new ReceivingNoteStockInRequestController();

        Test.startTest();
        controller.init();
        controller.search();

        // 查看入库指示前
        rnd = [
                SELECT Arrival_wh_Request_time__c
                     , DataConfirmation__c
                     , AcceptanceTime__c
                     , Inspection_result_after__c
                     , DataConfirmationAgain__c
                  FROM ReceivingNoteDetail__c
                 WHERE Id = :rndId
        ];
        System.assertEquals(null, rnd.Arrival_wh_Request_time__c);
        // 取出第一条数据,【再数据确认】字段打勾
        ReceivingNoteStockInRequestController.RowBean rowB = controller.pageB.rowBList[0];
        rowB.checked = true;
        // 入库指示
        controller.inStockRequest();
        // 查看入库指示后
        rnd = [
                SELECT Arrival_wh_Request_time__c
                  FROM ReceivingNoteDetail__c
                 WHERE Id=:rndId
            ];
        System.assertNotEquals(null, rnd.Arrival_wh_Request_time__c.format('MM/dd/yyyy'));

        Test.stopTest();
    }
    /**
    @description 测试取消明细功能
    */
    //static testMethod void unitTestCancelRecord() {
    //    setupTestData('固定资产,个体管理');
    //    Id rnId = factory.rnMap.get('固定资产,个体管理').Id;
    //    Id rnsId = factory.rnsMap.get('固定资产,个体管理').Id;
    //    addDetails(rnsId);

    //    PageReference ref = new PageReference('/apex/ReceivingNoteStockInRequest?parId=' + rnId);
    //    Test.setCurrentPage(ref);

    //    ReceivingNoteStockInRequestController controller = new ReceivingNoteStockInRequestController();

    //    Test.startTest();
    //    controller.init();
    //    controller.search();
    //    // 取出不带资产的一条明细
    //    List<Id> rndIdList = new List<Id>();
    //    for(ReceivingNoteStockInRequestController.RowBean rowB : controller.pageB.rowBList) {
    //        if(rowB.rnd.RNDAssert__c==null) {
    //            rndIdList.add(rowB.rnd.Id);
    //            rowB.checked = true;
    //            break;
    //        }
    //    }
    //    // 取消前明细的Delete_Flag__c为false
    //    List<ReceivingNoteDetail__c> rndList = [
    //            SELECT Delete_Flag__c
    //              FROM ReceivingNoteDetail__c
    //             WHERE Id in: rndIdList
    //        ];
    //    for(ReceivingNoteDetail__c rnd : rndList) {
    //        System.assertEquals(false, rnd.Delete_Flag__c);
    //    }
    //    // 取消
    //    controller.cancelRecord();
    //    // 取消后该条明细的Delete_Flag__c为true
    //    rndList = [
    //            SELECT Id, Delete_Flag__c
    //              FROM ReceivingNoteDetail__c
    //             WHERE Id in:rndIdList
    //            ];
    //    for(ReceivingNoteDetail__c rnd : rndList) {
    //        System.assertEquals(true, rnd.Delete_Flag__c);
    //    }
    //    Test.stopTest();

    //}
    /**
    @param rnsId 一览Id
    @description 给一个一览分配明细
    */
    private static void addDetails(Id rnsId) {

        PageReference ref = new PageReference('/apex/ReceivingNoteDetailSelect?sids=' + rnsId);
        Test.setCurrentPage(ref);
        ReceivingNoteDetailSelectController controller = new ReceivingNoteDetailSelectController();
        CreateRelationListPagingCmpCtrl cmp = new CreateRelationListPagingCmpCtrl();
        cmp.pageController = controller;
        controller.myComponentController.init();
        List<ReceivingNoteDetail__c> rndList = [
                SELECT Id,Fixture_Set_Detail__c
                  FROM ReceivingNoteDetail__c
                 WHERE ReceivingNoteSummary__c =: rnsId
            ];
        // 本来有一条一览明细
        System.assertEquals(1,rndList.size());

        controller.init();
        controller.searchOpp();
        // 本来有两条配套明细
        System.assertEquals(2, controller.viewList.size());
        ReceivingNoteDetail__c rnd = (ReceivingNoteDetail__c) controller.viewList[1].sobj;

        // 第二条配套明细数量调为3
        rnd.Rental_Num__c = 3;
        controller.viewList[0].check = true;
        controller.viewList[1].check = true;
        controller.save();

        rndList = [
                SELECT Id,Fixture_Set_Detail__c
                  FROM ReceivingNoteDetail__c
                 WHERE ReceivingNoteSummary__c =: rnsId
            ];
        // 原有的1条加上后加的3条，一共有4条一览明细
        System.assertEquals(4,rndList.size());
    }

    /**
    @description 显示错误信息
    */
    static testMethod void unitTestShowApplyMsg() {
        setupTestData('固定资产,个体管理');
        Id rnId = factory.rnMap.get('固定资产,个体管理').Id;

        PageReference ref = new PageReference('/apex/ReceivingNoteStockInRequest?parId=' + rnId);
        ReceivingNoteStockInRequestController controller = new ReceivingNoteStockInRequestController();
        Test.setCurrentPage(ref);
        Test.startTest();

        // 设定错误信息
        ref.getParameters().put('applyMsg', 'TEST MESSAGE');
        ref.getParameters().put('applyMsgType', 'Fail');

        // 执行之前，页面上没有报错误信息
        List<Apexpages.Message> msgs = ApexPages.getMessages();
        Boolean b = false;
        for(Apexpages.Message msg:msgs) {
            if (msg.getDetail().contains('TEST MESSAGE')) {
                b = true;
            }
        }
        System.assertEquals(false, b);

        // 执行
        controller.showApplyMsg();

        // 错误信息被报出
        msgs = ApexPages.getMessages();
        b = false;
        for(Apexpages.Message msg:msgs) {
            if (msg.getDetail().contains('TEST MESSAGE')) {
                b = true;
            }
        }
        System.assertEquals(true, b);
        Test.stopTest();
    }
}