@isTest
private class RepPAEDecisionRecordControllerTest {
    static testMethod void myUnitTest() {
        List < RecordType > rectCo = [select Id from RecordType where IsActive = true and SobjectType = 'Account'
            and Name = '病院'
        ];
        if (rectCo.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 = rectCo[0].Id;
        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 order by Department_Class_Label__c];
        Account depart1 = new Account();
        depart1.RecordTypeId = rectDpt[0].Id;
        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;
        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 = rectAs[0].Id;
        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.InstallDate = Date.today();
        ast.Manage_type__c = '个体管理';
        ast.FirstApproveDate_old__c = Date.today().addDays(-30);
        insert ast;
        Report__c rep = new Report__c();
        rep.VOC_Informer_Contact__c = '139';
        rep.Comment__c = '测试';
        rep.Manual_Name__c = '测试医院/科室/经销商';
        insert rep;
        System.Test.startTest();
        PageReference page4 = new PageReference('/apex/RepPAEDecisionRecord?ReportId=' + rep.id + '&RecordTypeIds=ASACDecision');
        System.Test.setCurrentPage(page4);
        RepPAEDecisionRecordController controller4 = new RepPAEDecisionRecordController(new ApexPages.StandardController(rep));
        
        controller4.CancelineFlag = 2;
        // 初始化测试
        controller4.init();
        controller4.toIntake();
        controller4.paedRecordDetailView[0].paedrdd.PAED_ManagementCode__c = 'T001';
        controller4.paedRecordDetailView[0].paedrdd.PAE_Judge__c = 'nonPAE';
        controller4.paedRecordDetailView[0].paedrdd.PAED_Status__c = '有效';
        controller4.SaveAndClose();
        PAE_DecisionRecord__c newRCPAED_IdList = [SELECT LastModifiedDate,PAE_Report__c, Id, Name, LastModifiedById, RecordType.DeveloperName FROM PAE_DecisionRecord__c limit 1];
        
        // PAE_DecisionRecord__c newRCPAED_IdList = [SELECT LastModifiedDate, Id, Name, LastModifiedById, RecordType.DeveloperName FROM PAE_DecisionRecord__c where PAE_Report__c = : rep.id And RecordType.DeveloperName = 'ASRCDecision'
        //     Order by LastModifiedDate desc limit 1
        // ];

        PageReference page5 = new PageReference('RepPAEDecisionRecord?RecordTypeIds=ASRCDecision&Id=' + newRCPAED_IdList.id + '&ReportId=' + rep.id);
        System.Test.setCurrentPage(page5);
        RepPAEDecisionRecordController controller5 = new RepPAEDecisionRecordController(new ApexPages.StandardController(rep));
        // 初始化测试
        controller5.Id = newRCPAED_IdList.Id;

        controller5.init();
        controller5.paedRecordDetailView[0].paedrdd.PAED_ManagementCode__c = 'T002';
        controller5.paedRecordDetailView[0].paedrdd.PAE_Judge__c = 'nonPAE';
        controller5.paedRecordDetailView[0].paedrdd.PAED_Status__c = '有效';
        controller5.paedRecordDetailView[0].paedrdd.PAED_Reappear_Result__c = '不再現';
        controller5.InsertRow();
        controller5.paedRecordDetailView[1].paedrdd.PAED_ManagementCode__c = 'T003,T004,T005,T006';
        controller5.paedRecordDetailView[1].paedrdd.PAE_Judge__c = 'Unknown';
        controller5.paedRecordDetailView[1].paedrdd.PAED_Status__c = '有效';
        controller5.paedRecordDetailView[1].paedrdd.PAED_Reappear_Result__c = '不再現';
        controller5.RefreshOriginalInterface('String paedRecordId');
        controller5.InsertMoreRows();
        controller5.SaveAndClose();
        System.Test.stopTest();
    }
    static testMethod void myUnitTest01() {
        List < RecordType > rectCo = [select Id from RecordType where IsActive = true and SobjectType = 'Account'
            and Name = '病院'
        ];
        if (rectCo.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 = rectCo[0].Id;
        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 order by Department_Class_Label__c];
        Account depart1 = new Account();
        depart1.RecordTypeId = rectDpt[0].Id;
        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;
        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 = rectAs[0].Id;
        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.InstallDate = Date.today();
        ast.Manage_type__c = '个体管理';
        ast.FirstApproveDate_old__c = Date.today().addDays(-30);
        insert ast;
        Report__c rep = new Report__c();
        rep.VOC_Informer_Contact__c = '139';
        rep.Comment__c = '测试';
        rep.Manual_Name__c = '测试医院/科室/经销商';
        insert rep;
        PAE_DecisionRecord__c record = new PAE_DecisionRecord__c(PAE_DetermineResults__c = 'nonPAE', // PAE判定结果
            PAE_ConfirmationDate__c = Date.valueOf('2020-05-06'), // OCSM QARA确认日
            PAE_Authenticator__c = '00510000005sEEM', // OCSM QARA确认者
            PAE_reappear_confirm__c = '未确认' // 再现结果确认
            
        );
        record.PAE_Report__c = rep.Id;
        record.RecordTypeId = Schema.SObjectType.PAE_DecisionRecord__c.getRecordTypeInfosByDeveloperName().get('ASACDecision').getRecordTypeId();
        insert record;

        PAE_DecisionRecordDetail__c pae = new PAE_DecisionRecordDetail__c(PAED_RCAC__c = 'Intake', // Intake/Final
            PAED_ManagementCode__c = 'test001', // Code
            PAED_Status__c = '有效', // 状态
            PAED_ConfirmationDate__c = Date.valueOf('2020-05-06'), // 确认日
            PAED_Affirmant__c = '00510000005sEEM', // 确认者
            isInterfaceCreate__c = false, // 是否通过接口新建
            PAE_Judge__c = 'nonPAE', // PAE判定
            PAED_Reappear_Result__c = '不再現', // 再现結果
            InspectionCategory109__c = '0');
        pae.PAE_DecisionRecordD__c = record.Id;
        pae.PAED_Report__c = rep.Id;
        insert pae;

        RSA_master_data__c psa = new RSA_master_data__c(RSS_CODE__C = 'Q001', IS_INTAKE_ESTIMATION__C = 'Intake,Estimation', IS_PAE__c = 'No', IS_INACTIVE__C = 'No');
        insert psa;
        RSA_master_data__c psa2 = new RSA_master_data__c(RSS_CODE__C = 'T002', IS_INTAKE_ESTIMATION__C = 'Intake,Estimation', IS_PAE__c = 'No', IS_INACTIVE__C = 'No');
        insert psa2;
        RSA_master_data__c psa3 = new RSA_master_data__c(RSS_CODE__C = 'T003', IS_INTAKE_ESTIMATION__C = 'Intake,Estimation', IS_PAE__c = 'No', IS_INACTIVE__C = 'No');
        insert psa3;
        RSA_master_data__c psa4 = new RSA_master_data__c(RSS_CODE__C = 'T004', IS_INTAKE_ESTIMATION__C = 'Intake,Estimation', IS_PAE__c = 'No', IS_INACTIVE__C = 'No');
        insert psa4;
        RSA_master_data__c psa5 = new RSA_master_data__c(RSS_CODE__C = 'T005', IS_INTAKE_ESTIMATION__C = 'Intake,Estimation', IS_PAE__c = 'No', IS_INACTIVE__C = 'No');
        insert psa5;
        RSA_master_data__c psa6 = new RSA_master_data__c(RSS_CODE__C = 'T006', IS_INTAKE_ESTIMATION__C = 'Intake,Estimation', IS_PAE__c = 'No', IS_INACTIVE__C = 'No');
        insert psa6;
        System.Test.startTest();
        PAE_DecisionRecord__c newRCPAED_IdList1 = [SELECT LastModifiedDate, Id, Name, LastModifiedById, RecordType.DeveloperName FROM PAE_DecisionRecord__c where PAE_Report__c = : rep.id And RecordType.DeveloperName = 'ASACDecision'
            Order by LastModifiedDate desc limit 1
        ];
        PageReference page4 = new PageReference('RepPAEDecisionRecord?RecordTypeIds=ASRCDecision&Id=' + newRCPAED_IdList1.id + '&ReportId=' + rep.id);
        System.Test.setCurrentPage(page4);
        // PageReference page4 = new PageReference('/apex/RepPAEDecisionRecord?ReportId=' + rep.id + '&RecordTypeIds=ASACDecision');
        // System.Test.setCurrentPage(page4);
        RepPAEDecisionRecordController controller4 = new RepPAEDecisionRecordController(new ApexPages.StandardController(rep));
        controller4.CancelineFlag = 2;

        // 初始化测试
        controller4.init();
        controller4.toIntake();
        controller4.paedRecordDetailView[0].paedrdd.PAED_ManagementCode__c = 'Q001';
        controller4.paedRecordDetailView[0].paedrdd.PAE_Judge__c = 'Unknown';
        controller4.paedRecordDetailView[0].paedrdd.PAED_Status__c = '有效';
        controller4.SaveAndClose();
        PAE_DecisionRecord__c newRCPAED_IdList = [SELECT LastModifiedDate, Id, Name, LastModifiedById, RecordType.DeveloperName FROM PAE_DecisionRecord__c where PAE_Report__c = : rep.id And RecordType.DeveloperName = 'ASACDecision'
            Order by LastModifiedDate desc limit 1
        ];
        PageReference page5 = new PageReference('RepPAEDecisionRecord?RecordTypeIds=ASRCDecision&Id=' + newRCPAED_IdList.id + '&ReportId=' + rep.id);
        System.Test.setCurrentPage(page5);
        RepPAEDecisionRecordController controller5 = new RepPAEDecisionRecordController(new ApexPages.StandardController(rep));
        // 初始化测试
        controller5.CancelineFlag = 3;
        controller5.init();
        controller5.InsertRow();
        controller5.paedRecordDetailView[0].paedrdd.PAED_ManagementCode__c = 'T002';
        controller5.paedRecordDetailView[0].paedrdd.PAE_Judge__c = 'Unknown';
        controller5.paedRecordDetailView[0].paedrdd.PAED_Status__c = '有效';
        controller5.paedRecordDetailView[0].paedrdd.PAED_Reappear_Result__c = '不再現';
        controller5.InsertRow();
        controller5.paedRecordDetailView[1].paedrdd.PAED_ManagementCode__c = 'T003,T004,T005,T006';
        controller5.paedRecordDetailView[1].paedrdd.PAE_Judge__c = 'Unknown';
        controller5.paedRecordDetailView[1].paedrdd.PAED_Status__c = '有效';
        controller5.paedRecordDetailView[1].paedrdd.PAED_Reappear_Result__c = '不再現';
        controller5.RefreshOriginalInterface('String paedRecordId');
        controller5.InsertMoreRows();
        controller5.SaveAndClose();
        System.Test.stopTest();
    }
  
     static testMethod void myUnitTest5() {
        Profile p = [select Id from Profile where id =:System.Label.ProfileId_SystemAdmin];
        // ユーザー作成
        User hpOwner = new User(Job_Category__c = '销售服务', Test_staff__c = true, LastName = 'hp', FirstName = 'owner', Alias = 'hp', 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;
        // 取引先作成
        List<RecordType> rectHp = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '病院'];
        if (rectHp.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;
        }
        Account hp1 = new Account(RecordTypeId = rectHp[0].Id, Name = 'testHp1', OwnerId = hpOwner.Id);
        insert hp1;

        List<Account> dc1s = [Select Id, Name, Department_Class_Label__c from Account where Parent.Id = :hp1.Id and Department_Class_Label__c in ('消化科', '呼吸科') order by Department_Class_Label__c];
        
        Account depart1 = new Account();
        depart1.RecordTypeId = rectDpt[1].Id;
        depart1.Name         = '*';
        depart1.Department_Name__c  = '診療科1';
        depart1.ParentId            = dc1s[0].Id;
        depart1.Department_Class__c = dc1s[0].Id;
        depart1.Hospital__c         = hp1.Id;
        
        insert depart1;
        
        Product2 prd1 = new Product2();
        prd1.ProductCode_Ext__c     = 'Prd1';
        prd1.ProductCode            = 'Prd1';
        prd1.Repair_Product_Code__c = 'Prd1_RP';
        prd1.Name                   = 'Prd1';
        prd1.Manual_Entry__c        = false;
        prd1.Category3__c = '';
        prd1.Category4__c = 'CV';
        
        insert prd1;
        
        Asset ast1 = new Asset();
        ast1.Name                   = '保有設備1';
        ast1.Hospital__c            = hp1.Id;
        ast1.Department_Class__c    = dc1s[0].Id;
        ast1.AccountId              = depart1.Id;
        ast1.Product2Id             = prd1.Id;
        ast1.SerialNumber           = 'SerialNumber1';
        ast1.Guarantee_period_for_products__c = Date.today();
        ast1.InstallDate                      = Date.today();
        
        insert ast1;

         Report__c rep = new Report__c();
        rep.VOC_Informer_Contact__c = '139';
        rep.Comment__c = '测试';
        rep.Manual_Name__c = '测试医院/科室/经销商';
        insert rep;
        

        System.Test.startTest();
        PageReference page = new PageReference('/apex/PAEDecisionRecord?RepairId='+rep.id+'&RecordTypeIds=ASRCDecision');
        System.Test.setCurrentPage(page);
        RepPAEDecisionRecordController controller = new RepPAEDecisionRecordController(new ApexPages.StandardController(rep));
        controller.CancelineFlag = 2;
        controller.intakePAEReappearConfirm = 'nonull';
        controller.editFlag = false;
        controller.RecordTypeName='';
        controller.isOSH = true;
        // 初始化测试
        controller.init();
        controller.paedRecordDetailView[0].paedrdd.PAED_ManagementCode__c = '';
        controller.paedRecordDetailView[0].paedrdd.PAE_Judge__c = 'nonPAE';
        controller.paedRecordDetailView[0].paedrdd.PAED_Status__c = '有效';
        controller.paedRecordDetailView[0].paedrdd.PAED_Reappear_Result__c = '再現';
        controller.InsertMoreRows();
        controller.SaveAndClose();
        //-------------------
        
        RepPAEDecisionRecordController controller2 = new RepPAEDecisionRecordController(new ApexPages.StandardController(rep));
        // 初始化测试
        controller2.init();
        
        controller2.paedRecordDetailView[0].paedrdd.PAED_ManagementCode__c = 'T002';
        controller2.paedRecordDetailView[0].paedrdd.PAE_Judge__c = 'nonPAE';
        controller2.paedRecordDetailView[0].paedrdd.PAED_Status__c = '有效';
        controller2.paedRecordDetailView[0].paedrdd.PAED_Reappear_Result__c = '不再現';

        controller2.InsertRow();
        controller2.paedRecordDetailView[1].paedrdd.PAED_ManagementCode__c = 'T003,T004';
        controller2.paedRecordDetailView[1].paedrdd.PAE_Judge__c = 'Unknown';
        controller2.paedRecordDetailView[1].paedrdd.PAED_Status__c = '有效';
        controller2.paedRecordDetailView[1].paedrdd.PAED_Reappear_Result__c = '不再現';
        controller2.InsertMoreRows();
        controller2.SaveAndClose();
    
    }
    
    
}