@isTest
private class InspectionReportTriggerTest {
    static testMethod void myUnitTest() {
        //维修合同
        Maintenance_Contract__c macnt = new Maintenance_Contract__c();
        Profile p = [select Id from Profile where id =:System.Label.ProfileId_SystemAdmin];
        // ユーザー作成
        User hpOwner = new User(Test_staff__c = true, Job_Category__c='销售服务' ,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;
        // 取引先作成
        RecordType rectHp = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '病院'];
        RecordType rectDpt = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '診療科 消化科'];
        Account hp = new Account(RecordTypeId = rectHp.Id, Name = 'InsReportPDFControllerTestHp1', OwnerId = hpOwner.Id);
        insert hp;
        Account section = [Select Id from Account where RecordType.Name = '戦略科室分類 消化科' and ParentId =: hp.Id];
        Account depart = new Account();
        depart.RecordTypeId = rectDpt.Id;
        depart.Name         = '*';
        depart.Department_Name__c  = 'HistoryTestDepart';
        depart.ParentId            = section.Id;
        depart.Department_Class__c = section.Id;
        depart.Hospital__c         = hp.Id;
        insert depart;

        macnt.Name = '测试服务合同1';
        macnt.Status__c = '合同中';
        //科室
        macnt.Department__c = depart.id;
        //主担当者
        macnt.Service_Contract_Staff__c = hpOwner.Id ;
        insert macnt;

        //点检计划
        Inspectup_Plan__c Inplan = new Inspectup_Plan__c();
        //币种
        Inplan.CurrencyIsoCode = 'CNY';
        //点检计划编码
        Inplan.Name  = 'Check_plan_Test1';
        //实际执行数      
        Inplan.Actual_Execution_Quantity__c = 0;
        //Maintenance_Contract__c：维修合同
        Inplan.Maintenance_Contract__c = macnt.id;
        //Planned_Start_Date__c：计划开始日
        Inplan.Planned_Start_Date__c = Date.today().addDays(-1);
        // Planned_End_Date__c：计划结束日
        Inplan.Planned_End_Date__c = Date.today().addDays(+2);
        //Timeliness_Rate__c：本次点检及时率
        Inplan.Timeliness_Rate__c = 80;
        //Implementation_Rate__c：本次点检实施率
        Inplan.Implementation_Rate__c = 80;
        insert Inplan;

        Asset ass1 = new Asset(
            AccountId = depart.Id,
            Hospital__c = hp.Id,
            SerialNumber = 'lw0001',
            Name = '测试设备0001:aaaaa'
        );
        Asset ass2 = new Asset(
            AccountId = depart.Id,
            Hospital__c = hp.Id,
            SerialNumber = 'lw0002',
            Name = '测试设备0002:aaaaa'
        );
        insert new Asset[] {ass1, ass2};

        Inspection_Report__c ir0 = new Inspection_Report__c(Name = '*',
            Hospital__c = hp.Id,
            Reporter__c = hpOwner.id,
            Approved_date__c = Date.today().addDays(-1)
        );
        insert ir0;

        Inspection_Item__c ii0 = new Inspection_Item__c(
            Inspection_ReportId__c = ir0.Id,
            AssetId__c = ass1.Id
        );
        insert ii0;
        //2021-04-16 mzy  LJPH-BZPB7W SFDC点检报告更新逻辑修改 start 

        //List<Asset> list0 = [select id,Inspection_Report__c from Asset ];
        //System.assertEquals(null,list0);

        //List<Asset> assList1 = [select id, Inspection_Report__c from Asset order by SerialNumber];
        //System.assertEquals(ir0.Id, assList1[0].Inspection_Report__c);
        //System.assertEquals(null, assList1[1].Inspection_Report__c);

        Inspection_Report__c ir = new Inspection_Report__c(Name = '*',
            Hospital__c = hp.Id,
            Reporter__c = hpOwner.id
            //Status__c = '批准'
            //Inspectup_Plan__c
        );//点检报告书
        insert ir;

        Inspection_Report__c ir2 = new Inspection_Report__c(Name = '*',
            Hospital__c = hp.Id,
            Reporter__c = hpOwner.id,
            Status__c = '草案中',
            Inspectup_Plan__c = Inplan.Id,
            Contract__c = macnt.id,
            Inspection_Date__c = Date.today()
        );//点检报告书
        insert ir2;


        Inspection_Item__c ii1 = new Inspection_Item__c(//点检报告书明细
            Inspection_ReportId__c = ir.Id,
            AssetId__c = ass1.Id
        );
        Inspection_Item__c ii2 = new Inspection_Item__c(
            Inspection_ReportId__c = ir2.Id,
            // Inspection_ReportId__r.Inspectup_Plan__c = Inplan.Id,
            AssetId__c = ass2.Id
        );
        insert new Inspection_Item__c[] {ii1, ii2};

        ir.Status__c ='批准';
        ir.Approved_date__c = Date.today();
        update ir;
        ir2.Status__c = '申请中';
        update ir2;
        ir2.Status__c = '批准';
        update ir2;


        //List<Asset> assList2 = [select id, Inspection_Report__c from Asset order by SerialNumber];
        //System.assertEquals(ir.Id, assList2[0].Inspection_Report__c);
        //System.assertEquals(ir.Id, assList2[1].Inspection_Report__c);

        delete ir;

        //List<Asset> assList3 = [select id, Inspection_Report__c from Asset order by SerialNumber];
        //System.assertEquals(ir0.Id, assList3[0].Inspection_Report__c);
        //System.assertEquals(null, assList3[1].Inspection_Report__c);

        //2021-04-16 mzy  LJPH-BZPB7W SFDC点检报告更新逻辑修改 end


        
    }


    static testMethod void testupdateVM(){
        //维修合同
        Maintenance_Contract__c macnt = new Maintenance_Contract__c();
        Profile p = [select Id from Profile where id =:System.Label.ProfileId_SystemAdmin];
        // ユーザー作成
        User hpOwner = new User(Test_staff__c = true, Job_Category__c='销售服务' ,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;
        // 取引先作成
        RecordType rectHp = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '病院'];
        RecordType rectDpt = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '診療科 消化科'];
        Account hp = new Account(RecordTypeId = rectHp.Id, Name = 'InsReportPDFControllerTestHp1', OwnerId = hpOwner.Id);
        insert hp;
        Account section = [Select Id from Account where RecordType.Name = '戦略科室分類 消化科' and ParentId =: hp.Id];
        Account depart = new Account();
        depart.RecordTypeId = rectDpt.Id;
        depart.Name         = '*';
        depart.Department_Name__c  = 'HistoryTestDepart';
        depart.ParentId            = section.Id;
        depart.Department_Class__c = section.Id;
        depart.Hospital__c         = hp.Id;
        insert depart;

        macnt.Name = '测试服务合同1';
        macnt.Status__c = '合同中';
        //科室
        macnt.Department__c = depart.id;
        //主担当者
        macnt.Service_Contract_Staff__c = hpOwner.Id ;
        insert macnt;


        //创建一个点检报告书
        Inspection_Report__c ir = new Inspection_Report__c(Name = '*',
            Hospital__c = hp.Id,
            Reporter__c = hpOwner.id,
            Status__c = '草案中',
            Contract__c = macnt.id,
            Inspection_Date__c = Date.today(),
            Used_ET__c = '12',
            UsedMachine__c = 'UsedMachine__c',
            Disinfectant__c  = 'Disinfectant__c',
            CleaningFluid__c  = 'CleaningFluid__c ',
            Remarks__c = 'Remarks__c ',
            Mode__c = 'Mode__c '
        );//点检报告书
        insert ir;

        ir.Status__c= '批准';
        update ir;


    }
}