@isTest
private class ConsumInventoryStartAssetSnapBatchTest {
    // 创建Asset数据
    static void setupTestData() {
        ControllerUtil.EscapeNFM001Trigger = true;

        // 病院を作る
        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='n01',Serial_Lot_No__c='Lot tracing',
                ProductCode_Ext__c='pc01',Manual_Entry__c=false);
        insert pro1;

        // 耗材类保有设备
        Asset assetC1 = new Asset(Asset_Owner__c = 'Olympus');
        assetC1.RecordTypeId = System.Label.Asset_RecordType;
        assetC1.SerialNumber = 'ass01';
        assetC1.Name = 'ass01';
        assetC1.AccountId = dep.Id;
        assetC1.Department_Class__c = strategicDep[0].Id;
        assetC1.Hospital__c = hospital.Id;
        assetC1.Product2Id = pro1.Id;
        assetC1.Quantity = 10;
        assetC1.Consumed_Count__c = 1;
        assetC1.Status = '有库存';
        assetC1.Manage_type__c = '数量管理';
        assetC1.Loaner_accsessary__c = true;
        assetC1.Product_category__c = 'GI';
        assetC1.Delete_Flag__c = false;
        assetC1.Freeze_sign__c = false;
        assetC1.Out_of_wh__c = 3;
        assetC1.Asset_loaner_category__c = '耗材';
        assetC1.AssetMark__c = '耗材';
        assetC1.Frozen_Quantity__c = 2;
        assetC1.Salesdepartment__c = '0.备品中心';
        assetC1.Internal_asset_location__c = '上海 备品中心';
        assetC1.Product_category__c = 'GI';
        assetC1.Equipment_Type__c = '产品试用';
        assetC1.SalesProvince__c = '上海';
        assetC1.WH_location__c = '货架号1';
        assetC1.AssetManageConfirm__c = true;
        insert assetC1;

        Consum_Apply__c ca = new Consum_Apply__c();
        ca.DataMigration_Flag__c = true;
        insert ca;
        Consum_Apply_Equipment_Set__c caes = new Consum_Apply_Equipment_Set__c();
        caes.Consum_Apply__c = ca.Id;
        caes.Consum_Start_Date__c = Date.today().addDays(20);
        caes.DataMigration_Flag__c = true;
        insert caes;
        Consum_Apply_Equipment_Set_Detail__c caesd = new Consum_Apply_Equipment_Set_Detail__c();
        caesd.DataMigration_Flag__c = true;
        caesd.Consum_Apply__c = ca.Id;
        caesd.Consum_Apply_Equipment_Set__c = caes.Id;
        caesd.Asset__c = assetC1.Id;
        caesd.Consum_Start_Date__c = caes.Consum_Start_Date__c;
        caesd.Trial_User__c = '我';
        caesd.Follower_User__c = UserInfo.getUserId();
        insert caesd; // 草案中
        ca.Request_demo_time__c = System.Now();
        ca.Request_approval_time__c = System.Now();
        ca.Status__c = '已批准';
        update ca; // 暂定分配

        caesd.Select_Time__c = System.now();
        //update caesd; // 已分配

        FixtureDeliverySlip__c slip = new FixtureDeliverySlip__c();
        slip.Name = '运输单号';
        slip.DeliveryType__c = '发货';
        slip.Distributor_method__c = '陆运';
        slip.DeliveryCompany__c = 'Fedex';
        slip.Wh_Staff__c = Userinfo.getUserId();
        slip.Combine_Pack__c = '123';
        insert slip;

        caesd.Shippment_loaner_time__c = System.Now();
        caesd.DeliverySlip__c = slip.Id;
        //update caesd; // 已出库

        caesd.Received_Confirm__c = 'OK';
        update caesd; // 申请者已收货

        Consum_Apply_Equipment_Set_Detail__c caesd1 = caesd.clone();
        caesd1.Received_Confirm__c = 'NG';
        caesd1.Asset_Center_Confirm__c = 'OK';
        //caesd1.Check_lost_Item_Final__c = '消耗';
        //caesd1.Lost_item_check_time_Final__c = System.today();
        insert caesd1; // 已消耗

    }

    static testMethod void testMethod2() {
        setupTestData();
        //盘点表头
        String cunFangDi = '上海 备品中心';
        Inventory_Header__c header = new Inventory_Header__c();
        header.Inventory_Start_Date__c = Date.toDay();
        header.UniqueKey__c = 'IH-0000';
        header.Inventory_Status__c = '处理中';
        header.Internal_asset_location__c = cunFangDi;
        insert header;

        List<Inventory_Header__c> headerList = [select UniqueKey__c from Inventory_Header__c];
        System.assertEquals(1, headerList.size());

        System.Test.StartTest();
        Database.executeBatch(new ConsumInventoryStartAssetSnapshotBatch(cunFangDi,header));
        System.Test.StopTest();

        List<Inventory_Header__c> consumHeaderList = [SELECT Id FROM Inventory_Header__c WHERE Fixture_Header__c=:header.Id];
        System.assertEquals(1, consumHeaderList.size());

        List<Consum_Inventory_Detail__c> cinvdList = [SELECT Id FROM Consum_Inventory_Detail__c];
        System.assertEquals(5, cinvdList.size());
    }

    static testMethod void testMethod3() {
        setupTestData();

        String cunFangDi = '上海 备品中心';
        Inventory_Header__c header = new Inventory_Header__c();
        header.Inventory_Start_Date__c = Date.toDay();
        header.UniqueKey__c = 'IH-0000';
        header.Inventory_Status__c = '处理中';
        header.Internal_asset_location__c = cunFangDi;
        insert header;
        //盘点表头
        Inventory_Header__c cheader = new Inventory_Header__c();
        cheader.Inventory_Start_Date__c = Date.toDay();
        cheader.UniqueKey__c = 'IH-0001';
        cheader.Internal_asset_location__c = cunFangDi;
        cheader.Fixture_Header__c = header.Id;
        insert cheader;

        System.Test.StartTest();
        Database.executeBatch(new ConsumInventoryStartAssetSnapshotBatch(cunFangDi, header));
        System.Test.StopTest();

        List<Inventory_Header__c> consumHeaderList = [SELECT Id FROM Inventory_Header__c WHERE Fixture_Header__c=:header.Id];
        System.assertEquals(1, consumHeaderList.size());

        List<Consum_Inventory_Detail__c> cinvdList = [SELECT Id FROM Consum_Inventory_Detail__c];
        System.assertEquals(5, cinvdList.size());
    }
}