@isTest
private class ReceivingNoteListControllerTest {
    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 testInit() {
        setupTestData('固定资产,个体管理');
        ReceivingNote__c rn = factory.rnMap.get('固定资产,个体管理');
        ReceivingNoteSummary__c rns = factory.rnsMap.get('固定资产,个体管理');
        rn = [
                SELECT ReceivingNoteNo__c,ReceivingNoteNo_New__c
                  FROM ReceivingNote__c
                 WHERE Id=:rn.Id
            ];


        PageReference ref = new PageReference('/apex/ReceivingNoteList?id=' + rn.ReceivingNoteNo_New__c);
        Test.setCurrentPage(ref);
        ReceivingNoteListController controller = new ReceivingNoteListController();
        Test.startTest();
        controller.init();
        //System.assertEquals(1, controller.summaryInfoList.size());
        System.assertEquals(rns.Id, controller.summaryInfoList[0].rns.Id);
        Test.stopTest();
    }

    /**
    @description 记忆上一次操作
    */
    static testMethod void testSetType() {
        setupTestData('固定资产,个体管理');
        ReceivingNote__c rn = factory.rnMap.get('固定资产,个体管理');
        rn = [
                SELECT ReceivingNoteNo__c,ReceivingNoteNo_New__c
                  FROM ReceivingNote__c
                 WHERE Id=:rn.Id
            ];

        PageReference ref = new PageReference('/apex/ReceivingNoteList?id=' + rn.ReceivingNoteNo_New__c);
        Test.setCurrentPage(ref);
        ReceivingNoteListController controller = new ReceivingNoteListController();
        Test.startTest();
        controller.init();
        // 读出现有的操作
        String userId = Userinfo.getUserId();
        System_UserSetting__c config = System_UserSetting__c.getInstance(userId);
        System.assertEquals(null, config.ReceivingNote_Action_Type__c);
        // 修改操作类型
        controller.selectedType = '验收';
        controller.setType();
        // 再次查看修改后的操作类型
        config = System_UserSetting__c.getInstance(userId);
        System.assertEquals('验收', config.ReceivingNote_Action_Type__c);

        Test.stopTest();
    }

    /**
    @description 收货确认 与 取消收货确认
    */
    static testMethod void testReceivedConfirm() {
        // 收货状态调整为‘已数据确认’
        setupTestData('固定资产,个体管理');
        ReceivingNoteDetail__c rnd = factory.rndMap.get('固定资产,个体管理');
        rnd.DataConfirmation__c = true;
        rnd.unknow_serial_NO_product__c = 'TRUE';
        rnd.Loaner_accsessary__c = 'TRUE';
        upsert rnd Product_Serial_No__c;

        ReceivingNote__c rn = factory.rnMap.get('固定资产,个体管理');
        rn = [
                SELECT ReceivingNoteNo__c,ReceivingNoteNo_New__c
                  FROM ReceivingNote__c
                 WHERE Id=:rn.Id
            ];

        PageReference ref = new PageReference('/apex/ReceivingNoteList?id=' + rn.ReceivingNoteNo_New__c);
        Test.setCurrentPage(ref);
        ReceivingNoteListController controller = new ReceivingNoteListController();
        Test.startTest();
        controller.init();

        // 读出初始收货状态
        ReceivingNoteSummary__c rns = [
                SELECT Received_Confirm_Time__c
                     , Received_Confirm_Staff__c
                     , RNS_Status__c
                     , Package_Status_Memo__c
                     , Package_Status__c
                  FROM ReceivingNoteSummary__c
            ];
        // 当前状态为‘已收货确认’
        System.assertEquals(FixtureUtil.RNSStatusMap.get(FixtureUtil.RNSStatus.Yi_Shu_Ju_Que_Ren.ordinal()), rns.RNS_Status__c);
        System.assertEquals(true, String.isBlank(rns.Package_Status_Memo__c));
        System.assertEquals(true, String.isBlank(rns.Package_Status__c));
        System.assertEquals(null, rns.Received_Confirm_Time__c);
        System.assertEquals(null, rns.Received_Confirm_Staff__c);

        // 改为确认收货状态
        controller.selectedType = '收货确认';
        controller.summaryInfoList[0].isChecked=true;
        controller.summaryInfoList[0].rns.Package_Status_Memo__c = 'MEMO';
        controller.summaryInfoList[0].rns.Package_Status__c = 'OK';

        controller.receivedConfirm();

        // 再次查看收货状态
        rns = [
                SELECT Received_Confirm_Time__c
                     , Received_Confirm_Staff__c
                     , RNS_Status__c
                     , Package_Status_Memo__c
                     , Package_Status__c
                  FROM ReceivingNoteSummary__c
            ];
        // 当前状态为‘已收货确认’
        System.assertEquals(FixtureUtil.RNSStatusMap.get(FixtureUtil.RNSStatus.Yi_Shou_Huo_Que_Ren.ordinal()),rns.RNS_Status__c);
        System.assertEquals('MEMO', rns.Package_Status_Memo__c);
        System.assertEquals('OK', rns.Package_Status__c);
        System.assertNotEquals(null, rns.Received_Confirm_Time__c);
        System.assertNotEquals(null, rns.Received_Confirm_Staff__c);

        // 取消确认收货状态
        controller.selectedType = '取消收货确认';
        controller.summaryInfoList[0].isChecked=true;
        controller.receivedConfirm();

        // 再次查看收货状态
        rns = [
                SELECT Received_Confirm_Time__c
                     , Received_Confirm_Staff__c
                     , RNS_Status__c
                  FROM ReceivingNoteSummary__c];
        System.assertEquals(FixtureUtil.RNSStatusMap.get(FixtureUtil.RNSStatus.Yi_Shu_Ju_Que_Ren.ordinal()),rns.RNS_Status__c);
        System.assertEquals(null, rns.Received_Confirm_Time__c);
        System.assertEquals(null, rns.Received_Confirm_Staff__c);

        Test.stopTest();
    }
}