@isTest
private class ReceivingNoteStockInControllerTest {
    static ReceivingNoteTestDataFactory factory;
    /**
    @description 安装master数据
    */
    @testSetup 
    static void setupMasterData(){
        StaticParameter.EscapeNFM001AgencyContractTrigger = true;
        StaticParameter.EscapeNFM001Trigger = true;
        Oly_TriggerHandler.bypass(Product2Handler.Class.getName());
        Oly_TriggerHandler.bypass(ContactTriggerHandler.Class.getName());
        Oly_TriggerHandler.bypass(AssetHandler.Class.getName());
        Oly_TriggerHandler.bypass(FixtureSetHandler.Class.getName());
        Oly_TriggerHandler.bypass(RentalApplyTriggerHandler.Class.getName());
        Oly_TriggerHandler.bypass(RentalApplyEquipmentSetHandler.Class.getName());
        Oly_TriggerHandler.bypass(RentalApplyEquipmentSetDetailHandler.Class.getName());
        // システム管理者
        User user = new User(Test_staff__c = true);
        user.LastName = '_サンブリッジ';
        user.FirstName = 'う';
        user.Alias = 'う';
        user.Email = 'olympusTest03@sunbridge.com';
        user.Username = 'olympusTest03@sunbridge.com';
        user.CommunityNickname = 'う';
        user.IsActive = true;
        user.EmailEncodingKey = 'ISO-2022-JP';
        user.TimeZoneSidKey = 'Asia/Tokyo';
        user.LocaleSidKey = 'ja_JP';
        user.LanguageLocaleKey = 'ja';
        user.ProfileId = System.Label.ProfileId_SystemAdmin;
        user.Job_Category__c = '销售推广';
        user.Province__c = '上海市';
        user.Dept__c='医疗华北营业本部';
        user.Use_Start_Date__c = Date.today().addMonths(-6);
        user.SalesManager__c = UserInfo.getUserId();
        user.BuchangApprovalManagerSales__c = UserInfo.getUserId();
        user.JingliApprovalManager__c = UserInfo.getUserId();
        user.BuchangApprovalManager__c = UserInfo.getUserId();
        user.ZongjianApprovalManager__c = UserInfo.getUserId();
        System.runAs(new User(Id = Userinfo.getUserId())) {
            List<Fixture_Set__c> fsList = [Select Id FROM Fixture_Set__c WHERE Name = 'SB-FS-01'];
            if(fsList.isEmpty()){
                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';
                fsList.add(fs);
                upsert fsList Loaner_name__c;
            }
        }

        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 unitTestInit() {
        setupTestData('固定资产,个体管理');
        ReceivingNoteSummary__c rns = factory.rnsMap.get('固定资产,个体管理');

        PageReference ref = new PageReference('/apex/ReceivingNoteStockIn?id=' + rns.Id);
        Test.setCurrentPage(ref);

        ReceivingNoteStockInController controller = new ReceivingNoteStockInController();

        Test.startTest();
        controller.init();
        // System.assertEquals(1, controller.esdList.size());
        Test.stopTest();
    }

    /**
    @description 测试保存功能
    */
    static testMethod void unitTestSave() {
        // 状态调整为已入库指示
        setupTestData('固定资产,个体管理');
        // 数据确认
        ReceivingNoteDetail__c rnd = factory.rndMap.get('固定资产,个体管理');
        rnd.unknow_serial_NO_product__c = 'FALSE';
        rnd.Loaner_accsessary__c = 'TRUE';
        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';
        // 再确认
        rnd.DataConfirmationAgain__c = true;
        rnd.Arrival_wh_Request_time__c = System.Now();

        upsert rnd Product_Serial_No__c;

        PageReference ref = new PageReference('/apex/ReceivingNoteStockIn?id=' + rns.Id);
        Test.setCurrentPage(ref);

        ReceivingNoteStockInController controller = new ReceivingNoteStockInController();

        controller.init();
        // System.assertEquals(1, controller.esdList.size());
        // 保存前
        ReceivingNoteStockInController.EsdInfo esdInfo = controller.esdList[0];
        // System.assertEquals(false,esdInfo.rec.Arrival_in_wh__c);
        // System.assertEquals(null,esdInfo.rec.WH_location__c);
        // System.assertEquals(true, esdInfo.editable);
        // 修改
        Test.startTest();
        esdInfo.rec.Arrival_in_wh__c = true;
        esdInfo.rec.WH_location__c = '123';
        controller.save();
        controller.save();
        Test.stopTest();
        // 检查保存结果
        List<ReceivingNoteDetail__c> rndList = [
                SELECT Arrival_in_wh__c
                     , WH_location__c
                  FROM ReceivingNoteDetail__c
              ORDER BY SerialNumber__c
            ];
        // System.assertEquals(true,rndList[0].Arrival_in_wh__c);
        // System.assertEquals('123',rndList[0].WH_location__c);
        // System.assertEquals(false, controller.esdList[0].editable);
    }
    /**
    @description 一对一入库
    */
    static testMethod void unitTestSaveOneToOne() {
        setupTestData('固定资产,个体管理');
        // 修改成一对一配套
        Fixture_Set_Detail__c fsd0 = factory.fsdList[0];
        Fixture_Set_Detail__c fsd1 = factory.fsdList[1];
        fsd0.Is_Body__c = true;
        fsd1.Is_Body__c = false;
        fsd1.Is_OneToOne__c = true;

        update new Fixture_Set_Detail__c[] {fsd0, fsd1};

        // 状态调整为已入库指示
        ReceivingNoteSummary__c  rns = factory.rnsMap.get('固定资产,个体管理');

        ReceivingNoteDetail__c rnd = factory.rndMap.get('固定资产,个体管理');
        rnd.unknow_serial_NO_product__c = 'TRUE';
        rnd.Loaner_accsessary__c = 'TRUE';
        rnd.DataConfirmation__c = true;

        ReceivingNoteDetail__c rnd1 = new ReceivingNoteDetail__c();
        rnd1.DataMigration_Flag__c = true;
        rnd1.ReceivingNoteSummary__c = rns.Id;
        rnd1.FSD_Id__c = factory.fsdList[1].Id;
        rnd1.unknow_serial_NO_product__c = 'TRUE';
        rnd1.Loaner_accsessary__c = 'TRUE';
        rnd1.Fixture_Set_Detail__c = factory.fsdList[1].Id;
        rnd1.Product_Serial_No__c = 'SB-PRO-SERIAL-'+2;
        rnd1.passNo2__c = '02';
        rnd1.passNo3__c = '2';
        rnd1.SerialNumber__c = 'SB-ASSET-SERIAL-6';
        rnd1.DataConfirmation__c = true;
        upsert new ReceivingNoteDetail__c[] {rnd, rnd1} Product_Serial_No__c;

        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';
        rnd.DataConfirmationAgain__c = true;
        rnd.Arrival_wh_Request_time__c = System.Now();

        rnd1.AcceptanceTime__c = System.Now();
        rnd1.AcceptanceResult__c = 'OK';
        rnd1.Inspection_result_after__c = 'OK';
        rnd1.DataConfirmationAgain__c = true;
        rnd1.Arrival_wh_Request_time__c = System.Now();
        rnd1.Salesdepartment__c = '1.华北营业本部';
        rnd1.Product_category__c = 'GI';
        rnd1.Equipment_Type__c = '产品试用';

        update new ReceivingNoteDetail__c[] {rnd, rnd1};

        PageReference ref = new PageReference('/apex/ReceivingNoteStockIn?id=' + rns.Id);
        Test.setCurrentPage(ref);

        ReceivingNoteStockInController controller = new ReceivingNoteStockInController();
        Test.startTest();
        controller.init();
        // System.assertEquals(2, controller.esdList.size());
        // 保存前
        ReceivingNoteStockInController.EsdInfo esdInfo = controller.esdList[0];
        // System.assertEquals(false,esdInfo.rec.Arrival_in_wh__c);
        // System.assertEquals(null,esdInfo.rec.WH_location__c);
        // System.assertEquals(true, esdInfo.editable);
        ReceivingNoteStockInController.EsdInfo esdInfo1 = controller.esdList[1];
        // System.assertEquals(false,esdInfo1.rec.Arrival_in_wh__c);
        // System.assertEquals(null,esdInfo1.rec.WH_location__c);
        // System.assertEquals(true, esdInfo1.editable);
        // 修改
        esdInfo.rec.Arrival_in_wh__c = true;
        esdInfo.rec.WH_location__c = '123';
        esdInfo1.rec.Arrival_in_wh__c = true;
        esdInfo1.rec.WH_location__c = '321';
        controller.save();
        controller.save();
        Test.stopTest();
        // 检查保存结果
        List<ReceivingNoteDetail__c> rndList = [
                SELECT Arrival_in_wh__c
                     , WH_location__c
                  FROM ReceivingNoteDetail__c
              ORDER BY Product_Serial_No__c
            ];
        // System.assertEquals(true,rndList[0].Arrival_in_wh__c);
        // System.assertEquals('123',rndList[0].WH_location__c);
        // System.assertEquals(false, controller.esdList[0].editable);

        // System.assertEquals(true,rndList[1].Arrival_in_wh__c);
        // System.assertEquals('321',rndList[1].WH_location__c);
        // System.assertEquals(false, controller.esdList[1].editable);
    }
    /**
    @description 取照片
    */
    static testMethod void unitTestFetchPhotoStatus() {
        setupTestData('固定资产,个体管理');
        ReceivingNoteSummary__c rns = factory.rnsMap.get('固定资产,个体管理');
        ReceivingNoteDetail__c rnd = factory.rndMap.get('固定资产,个体管理');
        rnd.Arrival_wh_Request_time__c = Datetime.now();
        update rnd;

        PageReference ref = new PageReference('/apex/ReceivingNoteStockIn?id=' + rns.Id);
        Test.setCurrentPage(ref);

        ReceivingNoteStockInController controller = new ReceivingNoteStockInController();

        Test.startTest();
        controller.init();
        controller.fetchPhotoStatus();
        Test.stopTest();

        // System.assertEquals(1, controller.esdList.size());
        // System.assertEquals(true, controller.esdList[0].editable);

    }
    /**
    @description 耗材入库
    */
    static testMethod void unitTestSaveConsum() {
        // 状态调整为已入库指示
        setupTestData('耗材,数量管理');
        // 数据确认
        ReceivingNoteDetail__c rnd = factory.rndMap.get('耗材,数量管理');
        rnd.unknow_serial_NO_product__c = 'FALSE';
        rnd.Loaner_accsessary__c = 'TRUE';
        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';
        // 再确认
        rnd.DataConfirmationAgain__c = true;
        rnd.Arrival_wh_Request_time__c = System.Now();

        upsert rnd Product_Serial_No__c;

        PageReference ref = new PageReference('/apex/ReceivingNoteStockIn?id=' + rns.Id);
        Test.setCurrentPage(ref);

        ReceivingNoteStockInController controller = new ReceivingNoteStockInController();

        controller.init();
        // System.assertEquals(1, controller.esdList.size());
        // 保存前
        ReceivingNoteStockInController.EsdInfo esdInfo = controller.esdList[0];
        // System.assertEquals(false,esdInfo.rec.Arrival_in_wh__c);
        // System.assertEquals(null,esdInfo.rec.WH_location__c);
        // System.assertEquals(true, esdInfo.editable);
        // 修改
        Test.startTest();
        esdInfo.rec.Arrival_in_wh__c = true;
        esdInfo.rec.WH_location__c = '123';
        controller.save();
        controller.save();
        Test.stopTest();
        // 检查保存结果
        List<ReceivingNoteDetail__c> rndList = [
                SELECT Arrival_in_wh__c
                     , WH_location__c
                  FROM ReceivingNoteDetail__c
              ORDER BY SerialNumber__c
            ];
        // System.assertEquals(true,rndList[0].Arrival_in_wh__c);
        // System.assertEquals('123',rndList[0].WH_location__c);
        // System.assertEquals(false, controller.esdList[0].editable);
    }
    /**
    @description 个体变体入库，直接对接口Asset修改
    */
    static testMethod void unitTestBiantiSave() {
        // 状态调整为已入库指示
        setupTestData('固定资产,个体管理');
        // 数据确认
        ReceivingNoteDetail__c rnd = factory.rndMap.get('固定资产,个体管理');
        rnd.unknow_serial_NO_product__c = 'FALSE';
        rnd.Loaner_accsessary__c = 'TRUE';
        rnd.DataConfirmation__c = true;
        rnd.Fixture_Arrival_Process__c = '变体';
        rnd.Fixture_Arrival_Product__c = factory.proList[2].Id;
        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';
        // 再确认
        rnd.DataConfirmationAgain__c = true;
        rnd.Arrival_wh_Request_time__c = System.Now();

        upsert rnd Product_Serial_No__c;

        PageReference ref = new PageReference('/apex/ReceivingNoteStockIn?id=' + rns.Id);
        Test.setCurrentPage(ref);

        ReceivingNoteStockInController controller = new ReceivingNoteStockInController();

        controller.init();
        System.assertEquals(1, controller.esdList.size());
        // 保存前
        ReceivingNoteStockInController.EsdInfo esdInfo = controller.esdList[0];
        // System.assertEquals(false,esdInfo.rec.Arrival_in_wh__c);
        // System.assertEquals(null,esdInfo.rec.WH_location__c);
        // System.assertEquals(true, esdInfo.editable);
        // 入库前的Asset
        List<Asset> assList = [SELECT Id,Fixture_Model_No_F__c,AccountId,Product2Id,Product_Old__c FROM Asset WHERE Id=:esdInfo.rec.RNDAssert__c];
        // System.assertEquals(factory.accountMap.get('备品(待收货)').Id, assList[0].AccountId) ;
        // System.assertEquals(null, assList[0].Product_Old__c) ;
        // System.assertEquals(factory.proList[0].Id, assList[0].Product2Id) ;

        // 修改
        Test.startTest();
        esdInfo.rec.Arrival_in_wh__c = true;
        esdInfo.rec.WH_location__c = '123';
        controller.save();
        controller.save();
        Test.stopTest();
        // 检查保存结果
        List<ReceivingNoteDetail__c> rndList = [
                SELECT Arrival_in_wh__c
                     , WH_location__c
                  FROM ReceivingNoteDetail__c
              ORDER BY SerialNumber__c
            ];
        // System.assertEquals(true,rndList[0].Arrival_in_wh__c);
        // System.assertEquals('123',rndList[0].WH_location__c);
        // System.assertEquals(false, controller.esdList[0].editable);
        // 入库后Asset
        assList = [SELECT Id,Fixture_Model_No_F__c,AccountId,Product2Id,Product_Old__c FROM Asset WHERE Id=:esdInfo.rec.RNDAssert__c];
        // System.assertEquals(factory.accountMap.get('备品').Id, assList[0].AccountId) ;
        // System.assertEquals(factory.proList[0].Id, assList[0].Product_Old__c) ;
        // System.assertEquals(factory.proList[2].Id, assList[0].Product2Id) ;

    }

    /**
    @description 合并入库
    */
    static testMethod void unitTestHebingSave() {
        // 状态调整为已入库指示
        setupTestData('固定资产,个体管理');
        // 数据确认
        ReceivingNoteDetail__c rnd = factory.rndMap.get('固定资产,个体管理');
        rnd.unknow_serial_NO_product__c = 'FALSE';
        rnd.Loaner_accsessary__c = 'TRUE';
        rnd.DataConfirmation__c = true;
        rnd.Fixture_Arrival_Process__c = '合并';
        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';
        // 再确认
        rnd.DataConfirmationAgain__c = true;
        rnd.Arrival_wh_Request_time__c = System.Now();

        upsert rnd Product_Serial_No__c;

        PageReference ref = new PageReference('/apex/ReceivingNoteStockIn?id=' + rns.Id);
        Test.setCurrentPage(ref);

        ReceivingNoteStockInController controller = new ReceivingNoteStockInController();

        controller.init();
        // System.assertEquals(1, controller.esdList.size());
        // 保存前
        ReceivingNoteStockInController.EsdInfo esdInfo = controller.esdList[0];
        // System.assertEquals(false,esdInfo.rec.Arrival_in_wh__c);
        // System.assertEquals(null,esdInfo.rec.WH_location__c);
        // System.assertEquals(true, esdInfo.editable);
        // 变体前的Asset
        List<Asset> assList = [SELECT Id,Fixture_Model_No_F__c,AccountId FROM Asset WHERE Id=:esdInfo.rec.RNDAssert__c];
        // System.assertEquals(factory.accountMap.get('备品(待收货)').Id, assList[0].AccountId) ;

        // 修改
        Test.startTest();
        esdInfo.rec.Arrival_in_wh__c = true;
        esdInfo.rec.WH_location__c = '123';
        controller.save();
        // controller.save();
        Test.stopTest();
        // 检查保存结果
        List<ReceivingNoteDetail__c> rndList = [
                SELECT Arrival_in_wh__c
                     , WH_location__c
                  FROM ReceivingNoteDetail__c
              ORDER BY SerialNumber__c
            ];
        // System.assertEquals(true,rndList[0].Arrival_in_wh__c);
        // System.assertEquals('123',rndList[0].WH_location__c);
        // System.assertEquals(false, controller.esdList[0].editable);
        // 变体前的Asset
        assList = [SELECT Id,Fixture_Model_No_F__c,AccountId FROM Asset WHERE Id=:esdInfo.rec.RNDAssert__c];
        // System.assertEquals(factory.accountMap.get('备品(已收货)').Id, assList[0].AccountId) ;

    }
}