@isTest
private class AssetModifyBelongsChangeTest {

    static Maintenance_Contract__c createMaintenanceContract( Account hospital, Account strategicDep, Account dep) {
        Maintenance_Contract__c contract = new Maintenance_Contract__c();
        contract.Name = 'tect contract';
        contract.status__c ='契約';
        contract.Maintenance_Contract_No__c = 'Kami_Contract_No';
        contract.Contract_Conclusion_Date__c = Date.today();
        contract.Hospital__c = hospital.Id;
        contract.Department_Class__c = strategicDep.Id;
        contract.Department__c = dep.Id;
        contract.Contract_Start_Date__c = Date.today().addDays( -10);   // 10日前
        contract.Contract_End_Date__c = Date.today().addDays( 5);       // 5日後
        contract.SalesOfficeCode_selection__c = '北京RC';
        contract.RecordTypeId = Schema.SObjectType.Maintenance_Contract__c.getRecordTypeInfosByDeveloperName().get('VM_Contract').getRecordTypeId();
        insert contract;
        return contract;
    }
    
    static Maintenance_Contract_Asset__c createMiddleTable( Asset asset, Maintenance_Contract__c contract) {
        // 中間テーブルを設ける
        Maintenance_Contract_Asset__c middleTable = new Maintenance_Contract_Asset__c();
        middleTable.Asset__c = asset.Id;
        middleTable.Maintenance_Contract__c = contract.id;
        middleTable.endDateGurantee_Text__c = Date.today().addDays(1);
        insert middleTable;
        return middleTable;
    }
    
    @isTest 
    static void test_method_one() {
        // Implement test code
        List<RecordType> rectHp = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and DeveloperName = 'HP'];
        if (rectHp.size() == 0) {
            return;
        }
        List<RecordType> rectDpt = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and DeveloperName = 'Department_GI'];
        if (rectDpt.size() == 0) {
            return;
        }
        Profile p = [select Id from Profile where id =:System.Label.ProfileId_SystemAdmin];

        User hpOwner = new User(Test_staff__c = true, LastName = 'hp', FirstName = 'owner', Alias = 'hp', Work_Location__c = '北京', CommunityNickname = 'hpOwner', Email = 'olympus_hpowner@sunbridge.com', Username = 'olympus_hpowner@sunbridge.com', IsActive = true, EmailEncodingKey = 'ISO-2022-JP', TimeZoneSidKey = 'Asia/Tokyo', LocaleSidKey = 'ja_JP', LanguageLocaleKey = 'ja', ProfileId = p.id);
        insert hpOwner;
        User hpOwner2 = new User(Test_staff__c = true, LastName = 'hp2', FirstName = 'owner', Alias = 'hp2', Work_Location__c = '重庆', CommunityNickname = 'hpOwner2', Email = 'olympus_hpowner@sunbridge.com', Username = 'olympus_hpowner2@sunbridge.com', IsActive = true, EmailEncodingKey = 'ISO-2022-JP', TimeZoneSidKey = 'Asia/Tokyo', LocaleSidKey = 'ja_JP', LanguageLocaleKey = 'ja', ProfileId = p.id);
        insert hpOwner2;
        // 病院作成
        Account hp = new Account(RecordTypeId = rectHp[0].Id, Name = 'hp', OwnerId = hpOwner.Id);
        hp.FSE_GI_Main_Leader__c = hpOwner.Id;
        hp.FSE_SP_Main_Leader__c = hpOwner2.Id;
        insert hp;

        Account dc = [select Id, Name, RecordType_DeveloperName__c, Account2__c from Account where ParentId = :hp.Id and RecordType_DeveloperName__c = 'Department_Class_GI'];
        
        // 診療科を作る
        Account dpt = new Account(RecordTypeId = rectDpt[0].Id);
        dpt.Name         = '*';
        dpt.Department_Name__c  = 'TestDepart';
        dpt.ParentId            = dc.Id;
        dpt.Department_Class__c = dc.Id;
        dpt.Hospital__c         = hp.Id;
        insert dpt;

        Account dpt1 = new Account(RecordTypeId = rectDpt[0].Id);
        dpt1.Name         = '*';
        dpt1.Department_Name__c  = 'TestDepart1';
        dpt1.ParentId            = dc.Id;
        dpt1.Department_Class__c = dc.Id;
        dpt1.Hospital__c         = hp.Id;
        insert dpt1;


        Asset asset = new Asset();
        asset.Name = 'テスト機器';
        asset.AccountId = dpt.Id;
        asset.Department_Class__c = dc.Id;
        asset.Hospital__c = hp.Id;
        asset.SerialNumber = 'testserial';
        insert asset;


        AssetModifyBelongsChange__c ambc = new AssetModifyBelongsChange__c();
        ambc.rawAccount__c = dpt.Id;
        ambc.AccountField__c = dpt1.Id;
        ambc.Change_status__c = '已提交';
        insert ambc;

        AssetModifyBelongsChangeDetail__c ambcDetail  = new AssetModifyBelongsChangeDetail__c();
        ambcDetail.ChangeAndChangeDetail__c = ambc.Id;
        ambcDetail.Asset__c = asset.Id;

        insert ambcDetail;

        ambc.Change_status__c = '批准';
        
        update ambc;
    }
    
    @isTest 
    private static void assetOwnerChange() {
        //Schema.SObjectType.Asset.getRecordTypeInfosByDeveloperName().get('HPAsset').getRecordTypeId();
        // List<RecordType> rectCo = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '病院'];
        // if (rectCo.size() == 0) {
        //     return;
        // }
        // List<RecordType> rectSct = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '戦略科室分類 呼吸科'];
        // if (rectSct.size() == 0) {
        //     return;
        // }
        // List<RecordType> rectDpt = [select Id, Name from RecordType where IsActive = true and SobjectType = 'Account' and Name IN ('診療科 消化科', '診療科 呼吸科') order by Name desc];
        // if (rectDpt.size() == 0) {
        //     return;
        // }
        // List<RecordType> rectAs = [select Id, Name from RecordType where IsActive = true and SobjectType = 'Asset' and Name = '备品'];
        // if (rectAs.size() == 0) {
        //     return;
        // }

        Account company1 = new Account();
        company1.RecordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByDeveloperName().get('HP').getRecordTypeId();
        company1.Name = '病院テスト1';
        List<Account> hps = new Account[]{company1};
        insert hps;
          
        List<Account> dc1s = [Select Id, Name, Department_Class_Label__c, Sys_Dept_Name_Change_Chk__c from Account where Parent.Id = :company1.Id and RecordType_DeveloperName__c = 'Department_Class_BF'];
        StaticParameter.EscapeAccountTrigger = true;
        Account depart1 = new Account();
        depart1.RecordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByDeveloperName().get('Department_BF').getRecordTypeId();
        depart1.Name         = '*';
        depart1.Department_Name__c  = '診療科1';
        depart1.ParentId            = dc1s[0].Id;
        depart1.Department_Class__c = dc1s[0].Id;
        depart1.Hospital__c         = company1.Id;
        insert depart1;
        Account depart2 = new Account();
        depart2.RecordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByDeveloperName().get('Department_BF').getRecordTypeId();
        depart2.Name         = '*';
        depart2.Department_Name__c  = '消化科1';
        depart2.ParentId            = dc1s[0].Id;
        depart2.Department_Class__c = dc1s[0].Id;
        depart2.Hospital__c         = company1.Id;
        insert depart2;
        StaticParameter.EscapeSyncProduct2Trigger = true;
        Product2 prd1 = new Product2();
        prd1.ProductCode_Ext__c     = 'HistoryPrd1';
        prd1.ProductCode            = 'HistoryPrd1';
        prd1.Repair_Product_Code__c = 'HistoryPrd1_RP';
        prd1.Name                   = 'HistoryPrd1';
        prd1.Manual_Entry__c        = false;
        prd1.Extend_new_product_gurantee__c = true;
        prd1.Extend_Gurantee_Start__c = Date.today().addDays(-60);
        prd1.Extend_Gurantee_End__c = Date.today().addDays(60);
        insert prd1;

        Oly_TriggerHandler.bypass('AssetHandler');

        Asset ast = new Asset();
        ast.RecordTypeId = Schema.SObjectType.Asset.getRecordTypeInfosByDeveloperName().get('HPAsset').getRecordTypeId();
        ast.SerialNumber = 'abcdefg';
        ast.Name = '测试资产1';
        ast.AccountId              = depart1.Id;
        ast.Department_Class__c    = dc1s[0].Id;
        ast.Hospital__c            = company1.Id;
        ast.Product2Id             = prd1.Id;
        ast.Guarantee_period_for_products__c = Date.today();
        ast.Posting_Date__c = Date.today().addDays(30);
        ast.InstallDate                      = Date.today().addDays(30);
        ast.Manage_type__c = '个体管理';
        ast.FirstApproveDate_old__c = Date.today().addDays(-30);
        ast.Tracing_Information_From__c = true;
        insert ast;

        Maintenance_Contract__c mContract = createMaintenanceContract(company1, dc1s[0], depart1);
        Maintenance_Contract_Asset__c mca = createMiddleTable( ast, mContract);
        system.debug('mca===='+mca);
        List<Maintenance_Contract_Asset__c> lcaTemp = [select Id,endDateGurantee_changeAcc__c,Asset_Status__c,Maintenance_Contract__r.RecordType.DeveloperName,endDateGurantee_Text__c from Maintenance_Contract_Asset__c ];
        System.assertEquals(Date.today().addDays(30), lcaTemp[0].endDateGurantee_Text__c);
        // System.assertEquals(true, lcaTemp[0].Asset_Status__c);
        // System.assertEquals('VM_Contract', lcaTemp[0].Maintenance_Contract__r.RecordType.DeveloperName);





        AssetModifyBelongsChange__c ambc = new AssetModifyBelongsChange__c();
        ambc.rawAccount__c = depart1.Id;
        ambc.AccountField__c = depart2.Id;
        ambc.Change_status__c = '已提交';
        insert ambc;

        AssetModifyBelongsChangeDetail__c ambcDetail  = new AssetModifyBelongsChangeDetail__c();
        ambcDetail.ChangeAndChangeDetail__c = ambc.Id;
        ambcDetail.Asset__c = ast.Id;

        insert ambcDetail;

        ambc.Change_status__c = '批准';
        update ambc;
    }
    
}