@isTest
private class AssetMaintainManualBatchTest {
    private static void setupData() {
        // OLY_OCM-643 追加EscapeNFM001Trigger
        ControllerUtil.EscapeNFM001Trigger = 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='n01',Serial_Lot_No__c='S/N tracing',
                Fixture_Model_No_T__c = 'n01', 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);
        Product2 pro3 = new Product2(Name='name03',IsActive=true,Family='GI',
                Fixture_Model_No__c='n03',Serial_Lot_No__c='Lot tracing',
                Fixture_Model_No_T__c = 'n03', Asset_Model_No__c = 'Pro3',
                ProductCode_Ext__c='pc03',Manual_Entry__c=false);
        insert new Product2[] {pro1, pro2, pro3};
        // 保有设备1 (主体) 主体只能是个体管理
        List<Asset> assetTestList = new List<Asset>();
        Asset asset1 = new Asset();
        asset1.RecordTypeId = System.Label.Asset_RecordType;
        asset1.Asset_Owner__c = 'Olympus';
        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.Delete_Flag__c = false;
        asset1.AssetManageConfirm__c = true;
        asset1.Freeze_sign__c = false;
        asset1.Out_of_wh__c = 0;
        asset1.Salesdepartment__c = '9.MA本部';
        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.WH_location__c = '货架号1';
        assetTestList.add(asset1);
        // 保有设备2 (附属品 数量管理)
        Asset asset2 = new Asset();
        asset2.RecordTypeId = System.Label.Asset_RecordType;
        asset2.Asset_Owner__c = 'Olympus';
        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 = 10;
        asset2.Status = '使用中';
        asset2.Manage_type__c = '数量管理';
        asset2.Loaner_accsessary__c = true;
        asset2.Delete_Flag__c = false;
        asset2.AssetManageConfirm__c = true;
        asset2.Freeze_sign__c = false;
        asset2.Out_of_wh__c = 0;
        asset2.Frozen_Quantity__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.WH_location__c = '货架号2';
        assetTestList.add(asset2);

        // 保有设备3 (附属品 个体管理)
        Asset asset3 = new Asset();
        asset3.RecordTypeId = System.Label.Asset_RecordType;
        asset3.Asset_Owner__c = 'Olympus';
        asset3.SerialNumber = 'asset3';
        asset3.Name = 'asset3';
        asset3.AccountId = dep.Id;
        asset3.Department_Class__c = strategicDep[0].Id;
        asset3.Hospital__c = hospital.Id;
        asset3.Product2Id = pro3.Id;
        asset3.Quantity = 1;
        asset3.Status = '使用中';
        asset3.Manage_type__c = '个体管理';
        asset3.Loaner_accsessary__c = true;
        asset3.Delete_Flag__c = false;
        asset3.AssetManageConfirm__c = true;
        asset3.Freeze_sign__c = false;
        asset3.Out_of_wh__c = 0;
        asset3.Frozen_Quantity__c = 0;
        asset3.Salesdepartment__c = '1.华北营业本部';
        asset3.Internal_asset_location__c = '北京 备品中心';
        asset3.Product_category__c = 'GI';
        asset3.Equipment_Type__c = '产品试用';
        asset3.SalesProvince__c = '北京';
        asset3.CompanyOfEquipment__c = '北京';
        asset3.Internal_Asset_number__c = '0003';
        asset3.WH_location__c = '货架号3';
        assetTestList.add(asset3);
        insert assetTestList;
        List<Asset> assetList = [SELECT Id, Asset_Owner__c FROM Asset
                                 WHERE Asset_Owner__c = 'Olympus'
                                 AND Delete_Flag__c = false
                                 AND AssetManageConfirm__c = true
                                 ORDER BY Id asc nulls first
                                ];
        System.assertEquals(3, assetList.size());
        System.assertEquals('Olympus', assetList[0].Asset_Owner__c);

        Fixture_OneToOne_Link__c link1 = new Fixture_OneToOne_Link__c();
        link1.Main_Asset__c = asset1.Id;
        link1.Accessory_Asset__c = asset2.Id;
        link1.Quantity__c = 10;

        Fixture_OneToOne_Link__c link2 = new Fixture_OneToOne_Link__c();
        link2.Main_Asset__c = asset1.Id;
        link2.Accessory_Asset__c = asset3.Id;
        link2.Quantity__c = 1;

        insert new Fixture_OneToOne_Link__c[] {link1,link2};

    }

    @istest
    // 断念取消
    static void testDuanNianQuXiao() {
        setupData();
        Asset ass = [SELECT Id, Abandoned_Inventory__c FROM Asset WHERE Name='asset1' LIMIT 1];
        ass.Abandoned_Inventory__c = 1;
        ass.Internal_Asset_number__c = null;
        update ass;

        AssetMaintainHeader__c amHeader = new AssetMaintainHeader__c();
        amHeader.MaintainType__c = '断念找回(断念取消)';
        amHeader.Date__c = System.today();
        insert amHeader;

        AssetMaintainDetail__c amd = new AssetMaintainDetail__c();
        amd.MaintainType__c = '断念找回(断念取消)';
        amd.AssetMaintainHeader__c = amHeader.Id;
        amd.MaintainCount__c = 1;
        amd.Asset__c = ass.Id;
        amd.OrderNumber__c = 1;
        insert amd;

        Test.startTest();
        List<String> errList = AssetMaintainManualBatch.checkAllDetailAndAsset(amHeader.Id, AssetMaintainManualBatch.Operation.PROCESS).errorList;
        System.assertEquals(0, errList.size());

        ass.MaintainCount_For_Processing__c = 1;
        update ass;

        AssetMaintainManualBatch amBatch = new AssetMaintainManualBatch(amHeader.Id, false);
        Database.executeBatch(amBatch);
        Test.stopTest();

        ass = [SELECT Id, Abandoned_Inventory__c FROM Asset WHERE Name='asset1' LIMIT 1];
        System.assertEquals(0, ass.Abandoned_Inventory__c);
        amd = [SELECT Id, Batch_Status__c FROM AssetMaintainDetail__c LIMIT 1];
        System.assertEquals('完成', amd.Batch_Status__c);
    }

    @istest
    // 断念找回
    static void testChongXinDengLu() {
        setupData();
        Asset ass = [SELECT Id FROM Asset WHERE Name='asset1' LIMIT 1];
        ass.Quantity = 0;
        ass.Internal_Asset_number__c = null;
        ass.ChangeQuantityReason__c = '废弃';
        update ass;

        AssetMaintainHeader__c amHeader = new AssetMaintainHeader__c();
        amHeader.MaintainType__c = '断念找回(重新登录)';
        amHeader.Date__c = System.today();
        insert amHeader;

        AssetMaintainDetail__c amd = new AssetMaintainDetail__c();
        amd.MaintainType__c = '断念找回(重新登录)';
        amd.AssetMaintainHeader__c = amHeader.Id;
        amd.MaintainCount__c = 1;
        amd.Asset__c = ass.Id;
        amd.OrderNumber__c = 1;
        insert amd;

        Test.startTest();
        List<String> errList = AssetMaintainManualBatch.checkAllDetailAndAsset(amHeader.Id, AssetMaintainManualBatch.Operation.PROCESS).errorList;
        System.assertEquals(0, errList.size());

        ass.MaintainCount_For_Processing__c = 1;
        update ass;

        AssetMaintainManualBatch amBatch = new AssetMaintainManualBatch(amHeader.Id, false);
        Database.executeBatch(amBatch);
        Test.stopTest();

        ass = [SELECT Id, Quantity FROM Asset WHERE Name='asset1' LIMIT 1];
        System.assertEquals(1, ass.Quantity);
        amd = [SELECT Id, Batch_Status__c FROM AssetMaintainDetail__c LIMIT 1];
        System.assertEquals('完成', amd.Batch_Status__c);
    }
    @istest
    // 解冻
    static void testJieDong() {
        setupData();
        Asset ass = [SELECT Id FROM Asset WHERE Name='asset1' LIMIT 1];
        ass.Frozen_Quantity__c = 1;
        ass.Freeze_sign__c = true;
        update ass;

        AssetMaintainHeader__c amHeader = new AssetMaintainHeader__c();
        amHeader.MaintainType__c = '解冻';
        amHeader.Date__c = System.today();
        insert amHeader;

        AssetMaintainDetail__c amd = new AssetMaintainDetail__c();
        amd.MaintainType__c = '解冻';
        amd.AssetMaintainHeader__c = amHeader.Id;
        amd.MaintainCount__c = 1;
        amd.Asset__c = ass.Id;
        amd.OrderNumber__c = 1;
        insert amd;

        Test.startTest();
        List<String> errList = AssetMaintainManualBatch.checkAllDetailAndAsset(amHeader.Id, AssetMaintainManualBatch.Operation.FROZEN).errorList;
        System.assertEquals(0, errList.size());

        ass.MaintainCount_For_Processing__c = 1;
        update ass;

        AssetMaintainManualBatch amBatch = new AssetMaintainManualBatch(amHeader.Id, false);
        Database.executeBatch(amBatch);
        Test.stopTest();

        ass = [SELECT Id, Frozen_Quantity__c, Freeze_sign__c FROM Asset WHERE Name='asset1' LIMIT 1];
        System.assertEquals(0, ass.Frozen_Quantity__c);
        System.assertEquals(false, ass.Freeze_sign__c);

        amd = [SELECT Id, Batch_Status__c FROM AssetMaintainDetail__c LIMIT 1];
        System.assertEquals('完成', amd.Batch_Status__c);
    }
    @istest
    // 冻结主体
    static void testDongjieMain() {
        setupData();
        Asset ass = [SELECT Id, Frozen_Quantity__c, Freeze_sign__c FROM Asset WHERE Name='asset1' LIMIT 1];
        System.assertEquals(null, ass.Frozen_Quantity__c);
        System.assertEquals(false, ass.Freeze_sign__c);
        update ass;

        AssetMaintainHeader__c amHeader = new AssetMaintainHeader__c();
        amHeader.MaintainType__c = '冻结';
        amHeader.Date__c = System.today();
        insert amHeader;

        AssetMaintainDetail__c amd = new AssetMaintainDetail__c();
        amd.MaintainType__c = '冻结';
        amd.AssetMaintainHeader__c = amHeader.Id;
        amd.MaintainCount__c = 1;
        amd.Asset__c = ass.Id;
        amd.OrderNumber__c = 1;
        insert amd;

        Test.startTest();
        List<String> errList = AssetMaintainManualBatch.checkAllDetailAndAsset(amHeader.Id, AssetMaintainManualBatch.Operation.PROCESS).errorList;
        System.assertEquals(0, errList.size());

        ass.MaintainCount_For_Processing__c = 1;
        update ass;

        AssetMaintainManualBatch amBatch = new AssetMaintainManualBatch(amHeader.Id, false);
        Database.executeBatch(amBatch);
        Test.stopTest();

        ass = [SELECT Id, Frozen_Quantity__c, Freeze_sign__c FROM Asset WHERE Name='asset1' LIMIT 1];
        System.assertEquals(1, ass.Frozen_Quantity__c);
        System.assertEquals(true, ass.Freeze_sign__c);

        amd = [SELECT Id, Batch_Status__c FROM AssetMaintainDetail__c LIMIT 1];
        System.assertEquals('完成', amd.Batch_Status__c);

        List<Fixture_OneToOne_Link__c> links = [SELECT Id FROM Fixture_OneToOne_Link__c];
        System.assertEquals(0, links.size());
    }
    @istest
    // 冻结一对一数量附属品
    static void testDongjieAccessory() {
        setupData();
        Asset ass2 = [SELECT Id, Frozen_Quantity__c, Freeze_sign__c FROM Asset WHERE Name='asset2' LIMIT 1];
        System.assertEquals(0, intValueOf(ass2.Frozen_Quantity__c));
        System.assertEquals(false, ass2.Freeze_sign__c);
        update ass2;

        AssetMaintainHeader__c amHeader = new AssetMaintainHeader__c();
        amHeader.MaintainType__c = '冻结';
        amHeader.Date__c = System.today();
        insert amHeader;

        AssetMaintainDetail__c amd = new AssetMaintainDetail__c();
        amd.MaintainType__c = '冻结';
        amd.AssetMaintainHeader__c = amHeader.Id;
        amd.MaintainCount__c = 10;
        amd.Asset__c = ass2.Id;
        amd.OrderNumber__c = 1;
        amd.OnetoOne_Main__c = [SELECT Id FROM Asset WHERE Name='asset1' LIMIT 1].Id;
        amd.DisconnectCount__c = 10;
        insert amd;

        Test.startTest();
        List<String> errList = AssetMaintainManualBatch.checkAllDetailAndAsset(amHeader.Id, AssetMaintainManualBatch.Operation.PROCESS).errorList;
        System.assertEquals(0, errList.size());

        ass2.MaintainCount_For_Processing__c = 10;
        update ass2;

        AssetMaintainManualBatch amBatch = new AssetMaintainManualBatch(amHeader.Id, false);
        Database.executeBatch(amBatch);
        Test.stopTest();

        ass2 = [SELECT Id, Frozen_Quantity__c, Freeze_sign__c FROM Asset WHERE Name='asset2' LIMIT 1];
        System.assertEquals(10, ass2.Frozen_Quantity__c);
        System.assertEquals(true, ass2.Freeze_sign__c);

        amd = [SELECT Id, Batch_Status__c FROM AssetMaintainDetail__c LIMIT 1];
        System.assertEquals('完成', amd.Batch_Status__c);

        List<Fixture_OneToOne_Link__c> links = [SELECT Id FROM Fixture_OneToOne_Link__c WHERE Accessory_Asset__c =: ass2.Id];
        System.assertEquals(0, links.size());
    }
    @istest
    // 实物报废 提交时检查数量
    static void testBaoFeiDongJie() {
        setupData();
        Asset ass = [SELECT Id, MaintainCount_For_Processing__c FROM Asset WHERE Name='asset1' LIMIT 1];
        System.assertEquals(0, intValueOf(ass.MaintainCount_For_Processing__c));

        AssetMaintainHeader__c amHeader = new AssetMaintainHeader__c();
        amHeader.MaintainType__c = '实物报废';
        amHeader.Date__c = System.today();
        insert amHeader;

        AssetMaintainDetail__c amd = new AssetMaintainDetail__c();
        amd.MaintainType__c = '实物报废';
        amd.AssetMaintainHeader__c = amHeader.Id;
        amd.MaintainCount__c = 1;
        amd.Asset__c = ass.Id;
        amd.OrderNumber__c = 1;
        amd.Batch_Status__c = '未处理';
        insert amd;

        Test.startTest();
        List<String> errList = AssetMaintainManualBatch.checkAllDetailAndAsset(amHeader.Id, AssetMaintainManualBatch.Operation.FROZEN).errorList;
        Test.stopTest();

        System.assertEquals(0, errList.size());

    }
    @istest
    // 实物报废 取消时解冻
    static void testBaoFeiJieDong() {
        setupData();
        Asset ass = [SELECT Id, MaintainCount_For_Processing__c FROM Asset WHERE Name='asset1' LIMIT 1];
        ass.MaintainCount_For_Processing__c = 1;
        update ass;

        AssetMaintainHeader__c amHeader = new AssetMaintainHeader__c();
        amHeader.MaintainType__c = '实物报废';
        amHeader.Date__c = System.today();
        amHeader.Batch_Processing__c = true;
        insert amHeader;

        AssetMaintainDetail__c amd = new AssetMaintainDetail__c();
        amd.MaintainType__c = '实物报废';
        amd.AssetMaintainHeader__c = amHeader.Id;
        amd.MaintainCount__c = 1;
        amd.Asset__c = ass.Id;
        amd.OrderNumber__c = 1;
        amd.Batch_Status__c = '未处理';
        insert amd;

        Test.startTest();
        AssetMaintainManualBatch amBatch = new AssetMaintainManualBatch(amHeader.Id, true, AssetMaintainManualBatch.Operation.UNFROZEN);
        Database.executeBatch(amBatch);
        Test.stopTest();

        ass = [SELECT Id, MaintainCount_For_Processing__c FROM Asset WHERE Name='asset1' LIMIT 1];
        System.assertEquals(0, intValueOf(ass.MaintainCount_For_Processing__c));

        amHeader = [SELECT Id, Batch_Processing__c FROM AssetMaintainHeader__c LIMIT 1];
        System.assertEquals(false, amHeader.Batch_Processing__c);
    }
    @istest
    // 实物报废 批准
    static void testBaoFeiPiZhun() {
        setupData();
        Asset ass = [SELECT Id, MaintainCount_For_Processing__c FROM Asset WHERE Name='asset1' LIMIT 1];
        ass.MaintainCount_For_Processing__c = 1;
        update ass;

        AssetMaintainHeader__c amHeader = new AssetMaintainHeader__c();
        amHeader.MaintainType__c = '实物报废';
        amHeader.Date__c = System.today();
        amHeader.Batch_Processing__c = true;
        insert amHeader;

        AssetMaintainDetail__c amd = new AssetMaintainDetail__c();
        amd.MaintainType__c = '实物报废';
        amd.AssetMaintainHeader__c = amHeader.Id;
        amd.MaintainCount__c = 1;
        amd.Asset__c = ass.Id;
        amd.OrderNumber__c = 1;
        amd.Batch_Status__c = '未处理';
        insert amd;

        Test.startTest();
        AssetMaintainManualBatch amBatch = new AssetMaintainManualBatch(amHeader.Id, true);
        Database.executeBatch(amBatch);
        Test.stopTest();

        ass = [SELECT Id, MaintainCount_For_Processing__c, Abandoned_RealThing__c, Status FROM Asset WHERE Name='asset1' LIMIT 1];
        System.assertEquals(0, intValueOf(ass.MaintainCount_For_Processing__c));
        System.assertEquals(1, intValueOf(ass.Abandoned_RealThing__c));
        System.assertEquals('待报废', ass.Status);

        List<Fixture_OneToOne_Link__c> links = [SELECT Id FROM Fixture_OneToOne_Link__c];
        System.assertEquals(0, links.size());

        amHeader = [SELECT Id, Batch_Processing__c FROM AssetMaintainHeader__c LIMIT 1];
        System.assertEquals(false, amHeader.Batch_Processing__c);
    }
    @istest
    // 实物报废 废弃
    static void testBaoFeiFeiQi() {
        setupData();
        Asset ass = [SELECT Id, Abandoned_RealThing__c FROM Asset WHERE Name='asset1' LIMIT 1];
        ass.Abandoned_RealThing__c = 1;
        update ass;

        AssetMaintainHeader__c amHeader = new AssetMaintainHeader__c();
        amHeader.MaintainType__c = '实物报废';
        amHeader.Date__c = System.today();
        amHeader.Batch_Processing__c = true;
        insert amHeader;

        AssetMaintainDetail__c amd = new AssetMaintainDetail__c();
        amd.MaintainType__c = '实物报废';
        amd.AssetMaintainHeader__c = amHeader.Id;
        amd.MaintainCount__c = 1;
        amd.AbandonCount__c = 1;
        amd.Asset__c = ass.Id;
        amd.OrderNumber__c = 1;
        amd.Batch_Status__c = '处理中';
        insert amd;

        Test.startTest();
        AssetMaintainManualBatch amBatch = new AssetMaintainManualBatch(amHeader.Id, true, AssetMaintainManualBatch.Operation.ABANDON);
        amBatch.errorList.add('error');
        Database.executeBatch(amBatch);
        Test.stopTest();

        ass = [SELECT Id, Abandoned_RealThing__c, Status FROM Asset WHERE Name='asset1' LIMIT 1];
        System.assertEquals(0, intValueOf(ass.Abandoned_RealThing__c));
        System.assertEquals(FixtureUtil.assetStatusMap.get(FixtureUtil.AssetStatus.Fei_Qi.ordinal()), ass.Status);

        amd = [SELECT Id, Batch_Status__c FROM AssetMaintainDetail__c LIMIT 1];
        System.assertEquals('完成', amd.Batch_Status__c);

        amHeader = [SELECT Id, Status__c, Batch_Processing__c FROM AssetMaintainHeader__c LIMIT 1];
        System.assertEquals('已完成', amHeader.Status__c);
        System.assertEquals(false, amHeader.Batch_Processing__c);
    }
    private static Integer intValueOf(Decimal d) {
        if(d == null || d < 0) {
            return 0;
        }
        return Integer.valueOf(d);
    }
}