@isTest
private class RepairPCLControllerTest {

    static testMethod void myUnitTest() {
        
        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;
        
        Repair__c repair1 = new Repair__c();
        repair1.Service_Repair_No__c = 'repair1';
        repair1.Hospital__c            = hp1.Id;
        repair1.Department_Class__c    = dc1s[0].Id;
        repair1.Account__c             = depart1.Id;
        repair1.Delivered_Product__c = ast1.Id;
        repair1.Repair_List_Price__c = 100;
        repair1.Billing_Amount__c = 10;
        repair1.Paid_Amount__c = 1;
        repair1.Failure_Occurrence_Date__c = Date.today().addDays(-1);
        repair1.Repair_Returned_To_HP_Date__c = Date.today().addDays(3);
        repair1.Repair_Shipped_Date__c = Date.today().addDays(1);
        repair1.Incharge_Staff__c = hpOwner.Id;
        
        Repair__c repair2 = new Repair__c();
        repair2.Service_Repair_No__c = 'repair2';
        repair2.Hospital__c            = hp1.Id;
        repair2.Department_Class__c    = dc1s[0].Id;
        repair2.Account__c             = depart1.Id;
        repair2.Delivered_Product__c = ast1.Id;
        repair2.Repair_List_Price__c = 200;
        repair2.Billing_Amount__c = 20;
        repair2.Paid_Amount__c = 2;
        repair2.Failure_Occurrence_Date__c = Date.today().addDays(-1);
        repair2.Repair_Returned_To_HP_Date__c = Date.today().addDays(5);
        repair2.Repair_Shipped_Date__c = Date.today().addDays(3);
        repair2.Incharge_Staff__c = hpOwner.Id;
        
        System.Test.startTest();
        insert new Repair__c[] {repair1,repair2};
        System.Test.stopTest();
        
        PageReference page = new PageReference('/apex/RepairPCL');
        System.Test.setCurrentPage(page);
        RepairPCLController controller = new RepairPCLController();
        // 初始化测试
        controller.init();
        
        System.assertEquals(2, controller.oppRecords.size());
        
        // 排序测试
        controller.sortKey = '1';
        controller.sortTable();
        controller.sortTable();
        
        // 检索测试
        controller.text = 'S:HP_Name__c';
        controller.condition = 'equals';
        controller.value = 'testHp1';
        controller.searchOpp();
        controller.condition = 'contains';
        controller.value = 'test 1';
        controller.searchOpp();
        
        System.assertEquals(2, controller.oppRecords.size());
        
        // 保存测试
        controller.oppRecords[0].changeFlg = '1';
        controller.save();
        
        // 返回测试
        controller.cancel();
    }
    
    static testMethod void myUnitTest2() {
        
        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;
        
        Repair__c repair1 = new Repair__c();
        repair1.Service_Repair_No__c = 'repair1';
        repair1.Hospital__c            = hp1.Id;
        repair1.Department_Class__c    = dc1s[0].Id;
        repair1.Account__c             = depart1.Id;
        repair1.Delivered_Product__c = ast1.Id;
        repair1.Repair_List_Price__c = 100;
        repair1.Billing_Amount__c = 10;
        repair1.Paid_Amount__c = 1;
        repair1.Failure_Occurrence_Date__c = Date.today().addDays(-1);
        repair1.Repair_Returned_To_HP_Date__c = Date.today().addDays(3);
        repair1.Repair_Shipped_Date__c = Date.today().addDays(1);
        repair1.Incharge_Staff__c = hpOwner.Id;
        
        Repair__c repair2 = new Repair__c();
        repair2.Service_Repair_No__c = 'repair2';
        repair2.Hospital__c            = hp1.Id;
        repair2.Department_Class__c    = dc1s[0].Id;
        repair2.Account__c             = depart1.Id;
        repair2.Delivered_Product__c = ast1.Id;
        repair2.Repair_List_Price__c = 200;
        repair2.Billing_Amount__c = 20;
        repair2.Paid_Amount__c = 2;
        repair2.Failure_Occurrence_Date__c = Date.today().addDays(-1);
        repair2.Repair_Returned_To_HP_Date__c = Date.today().addDays(5);
        repair2.Repair_Shipped_Date__c = Date.today().addDays(3);
        repair2.Incharge_Staff__c = hpOwner.Id;
        
        System.Test.startTest();
        insert new Repair__c[] {repair1,repair2};
        System.Test.stopTest();
        
        PageReference page = new PageReference('/apex/RepairPCL?uid=' + UserInfo.getUserId() + '&hpid=' + hp1.Id);
        System.Test.setCurrentPage(page);
        RepairPCLController controller = new RepairPCLController();
        // 初始化测试
        controller.init();
        
        System.assertEquals(2, controller.oppRecords.size());
        
        // 保存测试
        controller.oppRecords[0].changeFlg = '1';
        controller.saveType = '1';
        controller.save();
        
        controller.oppRecords[0].changeFlg = '1';
        controller.saveType = '2';
        controller.save();
        
        // 检索测试
        controller.text = 'S:HP_Name__c';
        controller.condition = 'notequals';
        controller.value = '';
        controller.searchOpp();
        
        controller.dateField = 'Failure_Occurrence_Date__c';
        controller.con1.Birthdate = Date.today().addDays(-2);
        controller.con2.Birthdate = Date.today();
        controller.text = 'S:HP_Name__c';
        controller.condition = 'starts with';
        controller.value = 'test';
        controller.searchOpp();
        
        System.assertEquals(2, controller.oppRecords.size());
    }
}